Panda3D
Loading...
Searching...
No Matches
blend_test.cxx
1
2#include <iostream>
3#include <fstream>
4#include <stdlib.h>
5#include <string>
6
7#ifndef _BOOL
8#define _BOOL 1
9#endif
10
11#define REQUIRE_IOSTREAM
12
13#include <maya/MGlobal.h>
14#include <maya/MFileIO.h>
15#include <maya/MLibrary.h>
16#include <maya/MStatus.h>
17#include <maya/MItDag.h>
18#include <maya/MDagPath.h>
19#include <maya/MFnDagNode.h>
20#include <maya/MFnBlendShapeDeformer.h>
21#include <maya/MFnMesh.h>
22#include <maya/MPointArray.h>
23
24using std::cerr;
25
26void
27scan_nodes() {
28 MStatus status;
29
30 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
31 if (!status) {
32 status.perror("MItDag constructor");
33 exit(1);
34 }
35
36 while (!dag_iterator.isDone()) {
37 MDagPath dag_path;
38 status = dag_iterator.getPath(dag_path);
39 if (!status) {
40 status.perror("MItDag::getPath");
41 exit(1);
42 }
43
44 MFnDagNode dag_node(dag_path, &status);
45 if (!status) {
46 status.perror("MFnDagNode constructor");
47 exit(1);
48 }
49
50 cerr << dag_node.name() << "\n";
51
52 dag_iterator.next();
53 }
54}
55
56MFnBlendShapeDeformer *
57get_slider(MString slider_name) {
58 MStatus status;
59
60 status = MGlobal::selectByName(slider_name, MGlobal::kReplaceList);
61 if (!status) {
62 status.perror(slider_name);
63 exit(1);
64 }
65 MSelectionList list;
66 status = MGlobal::getActiveSelectionList(list);
67 if (!status) {
68 status.perror("MGLobal::getActiveSelectionList");
69 exit(1);
70 }
71
72 unsigned int num_objects = list.length();
73 if (num_objects != 1) {
74 cerr << "Warning: multiple objects match " << slider_name << "\n";
75 }
76
77 for (unsigned int i = 0; i < num_objects; i++) {
78 MObject obj;
79 status = list.getDependNode(i, obj);
80 if (!status) {
81 cerr << "selected element is not a dependency node.\n";
82 status.perror("getDependNode");
83 } else {
84 if (obj.hasFn(MFn::kBlendShape)) {
85 MFnBlendShapeDeformer *slider = new MFnBlendShapeDeformer(obj, &status);
86 if (!status) {
87 status.perror("MFnBlendShapeDeformer constructor");
88 } else {
89 cerr << "got slider " << slider->name() << "\n";
90 return slider;
91 }
92 }
93
94 cerr << "selected element is not a blend shape\n";
95 }
96 }
97
98 cerr << "Couldn't find slider " << slider_name << "\n";
99 exit(1);
100}
101
102MFnMesh *
103get_mesh(MString mesh_name) {
104 MStatus status;
105
106 status = MGlobal::selectByName(mesh_name, MGlobal::kReplaceList);
107 if (!status) {
108 status.perror(mesh_name);
109 exit(1);
110 }
111 MSelectionList list;
112 status = MGlobal::getActiveSelectionList(list);
113 if (!status) {
114 status.perror("MGLobal::getActiveSelectionList");
115 exit(1);
116 }
117
118 unsigned int num_objects = list.length();
119 if (num_objects != 1) {
120 cerr << "Warning: multiple objects match " << mesh_name << "\n";
121 }
122
123 for (unsigned int i = 0; i < num_objects; i++) {
124 MObject obj;
125 status = list.getDependNode(i, obj);
126 if (!status) {
127 cerr << "selected element is not a dependency node.\n";
128 status.perror("getDependNode");
129
130 } else {
131 if (obj.hasFn(MFn::kMesh)) {
132 // Maybe it's a mesh object itself.
133 MFnMesh *mesh = new MFnMesh(obj, &status);
134 if (!status) {
135 status.perror("MFnMesh constructor");
136 } else {
137 cerr << "got mesh " << mesh->name() << "\n";
138 return mesh;
139 }
140
141 } else if (obj.hasFn(MFn::kDagNode)) {
142 // Maybe it's a dag node.
143 MDagPath path;
144 status = MDagPath::getAPathTo(obj, path);
145 if (!status) {
146 status.perror("MDagPath::getAPathTo");
147 exit(1);
148 }
149 if (path.hasFn(MFn::kMesh)) {
150 MFnMesh *mesh = new MFnMesh(path, &status);
151 if (!status) {
152 status.perror("MFnMesh constructor");
153 } else {
154 cerr << "got mesh " << mesh->name() << "\n";
155 return mesh;
156 }
157 }
158 }
159
160 cerr << "selected element is not a mesh\n";
161 }
162 }
163
164 cerr << "Couldn't find mesh " << mesh_name << "\n";
165 exit(1);
166}
167
168void
169output_vertices(const char *filename, MFnMesh &mesh) {
170 MStatus status;
171
172 MPointArray verts;
173 // status = mesh.getPoints(verts, MSpace::kObject);
174 status = mesh.getPoints(verts, MSpace::kWorld);
175 if (!status) {
176 status.perror("mesh.getPoints");
177 exit(1);
178 }
179
180 std::ofstream file(filename, std::ios::out | std::ios::trunc);
181 if (!file) {
182 cerr << "Couldn't open " << filename << " for output.\n";
183 exit(1);
184 }
185
186 unsigned int num_verts = verts.length();
187 cerr << "writing " << num_verts << " vertices to " << filename << "\n";
188 for (unsigned int i = 0; i < num_verts; i++) {
189 file << i << ". " << verts[i] << "\n";
190 }
191}
192
193int
194main(int argc, char *argv[]) {
195 cerr << "Initializing Maya\n";
196 MStatus status;
197 status = MLibrary::initialize(argv[0]);
198 if (!status) {
199 status.perror("Could not initialize");
200 exit(1);
201 }
202
203 cerr << "Using Maya library version " << MGlobal::mayaVersion() << "\n";
204
205 if (argc < 4) {
206 cerr << "\nUsage:\n\nblend_test.cxx file.mb slider_name mesh_name\n\n";
207 exit(1);
208 }
209
210 MString filename = argv[1];
211 MString slider_name = argv[2];
212 MString mesh_name = argv[3];
213
214 MFileIO::newFile(true);
215
216 cerr << "Reading " << filename << "\n";
217 status = MFileIO::open(filename);
218 if (!status) {
219 status.perror(filename);
220 exit(1);
221 }
222
223 MFnBlendShapeDeformer *slider = get_slider(slider_name);
224 MFnMesh *mesh = get_mesh(mesh_name);
225
226 cerr << "\nOriginal slider value is " << slider->weight(0) << "\n";
227
228 output_vertices("before.txt", *mesh);
229
230 cerr << "Setting slider to 1\n";
231 status = slider->setWeight(0, 1.0);
232 if (!status) {
233 status.perror("MFnBlendShapeDeformer::setWeight");
234 exit(1);
235 }
236
237 output_vertices("after.txt", *mesh);
238
239 return 0;
240}