Panda3D
normal_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/MItMeshPolygon.h>
21 #include <maya/MItSelectionList.h>
22 #include <maya/MFnBlendShapeDeformer.h>
23 #include <maya/MFnMesh.h>
24 #include <maya/MPointArray.h>
25 
26 using std::cerr;
27 using std::endl;
28 
29 void
30 scan_nodes() {
31  MStatus status;
32 
33  MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
34  if (!status) {
35  status.perror("MItDag constructor");
36  exit(1);
37  }
38 
39  while (!dag_iterator.isDone()) {
40  MDagPath dag_path;
41  status = dag_iterator.getPath(dag_path);
42  if (!status) {
43  status.perror("MItDag::getPath");
44  exit(1);
45  }
46 
47  MFnDagNode dag_node(dag_path, &status);
48  if (!status) {
49  status.perror("MFnDagNode constructor");
50  exit(1);
51  }
52 
53  cerr << dag_node.name() << "\n";
54 
55  dag_iterator.next();
56  }
57 }
58 
59 MFnBlendShapeDeformer *
60 get_slider(MString slider_name) {
61  MStatus status;
62 
63  status = MGlobal::selectByName(slider_name, MGlobal::kReplaceList);
64  if (!status) {
65  status.perror(slider_name);
66  exit(1);
67  }
68  MSelectionList list;
69  status = MGlobal::getActiveSelectionList(list);
70  if (!status) {
71  status.perror("MGLobal::getActiveSelectionList");
72  exit(1);
73  }
74 
75  unsigned int num_objects = list.length();
76  if (num_objects != 1) {
77  cerr << "Warning: multiple objects match " << slider_name << "\n";
78  }
79 
80  for (unsigned int i = 0; i < num_objects; i++) {
81  MObject obj;
82  status = list.getDependNode(i, obj);
83  if (!status) {
84  cerr << "selected element is not a dependency node.\n";
85  status.perror("getDependNode");
86  } else {
87  if (obj.hasFn(MFn::kBlendShape)) {
88  MFnBlendShapeDeformer *slider = new MFnBlendShapeDeformer(obj, &status);
89  if (!status) {
90  status.perror("MFnBlendShapeDeformer constructor");
91  } else {
92  cerr << "got slider " << slider->name() << "\n";
93  return slider;
94  }
95  }
96 
97  cerr << "selected element is not a blend shape\n";
98  }
99  }
100 
101  cerr << "Couldn't find slider " << slider_name << "\n";
102  exit(1);
103 }
104 
105 MFnMesh *
106 get_mesh(MString mesh_name) {
107  MStatus status;
108 
109  status = MGlobal::selectByName(mesh_name, MGlobal::kReplaceList);
110  if (!status) {
111  status.perror(mesh_name);
112  exit(1);
113  }
114  MSelectionList list;
115  status = MGlobal::getActiveSelectionList(list);
116  if (!status) {
117  status.perror("MGLobal::getActiveSelectionList");
118  exit(1);
119  }
120 
121  unsigned int num_objects = list.length();
122  if (num_objects != 1) {
123  cerr << "Warning: multiple objects match " << mesh_name << "\n";
124  }
125 
126  for (unsigned int i = 0; i < num_objects; i++) {
127  MObject obj;
128  status = list.getDependNode(i, obj);
129  if (!status) {
130  cerr << "selected element is not a dependency node.\n";
131  status.perror("getDependNode");
132 
133  } else {
134  if (obj.hasFn(MFn::kMesh)) {
135  // Maybe it's a mesh object itself.
136  MFnMesh *mesh = new MFnMesh(obj, &status);
137  if (!status) {
138  status.perror("MFnMesh constructor");
139  } else {
140  cerr << "got mesh " << mesh->name() << "\n";
141  return mesh;
142  }
143 
144  } else if (obj.hasFn(MFn::kDagNode)) {
145  // Maybe it's a dag node.
146  MDagPath path;
147  status = MDagPath::getAPathTo(obj, path);
148  if (!status) {
149  status.perror("MDagPath::getAPathTo");
150  exit(1);
151  }
152  if (path.hasFn(MFn::kMesh)) {
153  MFnMesh *mesh = new MFnMesh(path, &status);
154  if (!status) {
155  status.perror("MFnMesh constructor");
156  } else {
157  cerr << "got mesh " << mesh->name() << "\n";
158  return mesh;
159  }
160  }
161  }
162 
163  cerr << "selected element is not a mesh\n";
164  }
165  }
166 
167  cerr << "Couldn't find mesh " << mesh_name << "\n";
168  exit(1);
169 }
170 
171 void
172 output_vertices(const char *filename, MFnMesh &mesh) {
173  MStatus status;
174 
175  MPointArray verts;
176  // status = mesh.getPoints(verts, MSpace::kObject);
177  status = mesh.getPoints(verts, MSpace::kWorld);
178  if (!status) {
179  status.perror("mesh.getPoints");
180  exit(1);
181  }
182 
183  std::ofstream file(filename, std::ios::out | std::ios::trunc);
184  if (!file) {
185  cerr << "Couldn't open " << filename << " for output.\n";
186  exit(1);
187  }
188 
189  unsigned int num_verts = verts.length();
190  cerr << "writing " << num_verts << " vertices to " << filename << "\n";
191  for (unsigned int i = 0; i < num_verts; i++) {
192  file << i << ". " << verts[i] << "\n";
193  }
194 }
195 
196 void
197 output_normals() {
198  MStatus status;
199  MDagPath dagPath, *_dag_path;
200  MObject component;
201 
202  MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
203  if (!status) {
204  status.perror("MItDag constructor");
205  exit(1);
206  }
207 
208  while (!dag_iterator.isDone()) {
209  status = dag_iterator.getPath(dagPath);
210  if (!status) {
211  status.perror("MItDag::getPath");
212  exit(1);
213  }
214 
215  _dag_path = new MDagPath(dagPath);
216 
217  MFnDagNode dag_node(*_dag_path, &status);
218  if (!status) {
219  status.perror("MFnDagNode constructor");
220  exit(1);
221  }
222 
223  cerr << dag_node.name() << "\n";
224 
225  if (_dag_path->hasFn(MFn::kMesh)) {
226  unsigned int numShapes;
227  status = _dag_path->numberOfShapesDirectlyBelow(numShapes);
228  cerr << "----numShapes: " << numShapes << "\n";
229  /*
230  if (numShapes == 1) {
231  _dag_path->extendToShape();
232  }
233  */
234  }
235 
236  MItMeshPolygon faceIter(*_dag_path, component, &status);
237  if( !status ) cerr << "Error at MItMeshPolygon" << endl;
238 
239  MFnMesh meshFn(*_dag_path);
240 
241  // Traverse the polygonal face
242  for (; !faceIter.isDone();faceIter.next()) {
243  int nVerts = faceIter.polygonVertexCount();
244 
245  // Traverse the vertices to get their indexes and print out the normals
246  for (int i = 0;i<nVerts;i++) {
247  MVector normal;
248  faceIter.getNormal( i, normal, MSpace::kWorld );
249  cerr << "MItMeshPolygon::getNormal for the vertex [" << faceIter.vertexIndex(i)
250  << "] for face [" << faceIter.index() << "] is " << normal << endl;
251  }
252  }
253 
254  dag_iterator.next();
255  }
256 }
257 
258 int
259 main(int argc, char *argv[]) {
260  cerr << "Initializing Maya\n";
261  MStatus status;
262  status = MLibrary::initialize(argv[0]);
263  if (!status) {
264  status.perror("Could not initialize");
265  exit(1);
266  }
267 
268  cerr << "Using Maya library version " << MGlobal::mayaVersion() << "\n";
269 
270  if (argc < 2) {
271  cerr << "\nUsage:\n\nnormal_test.cxx file.mb mesh_name\n\n";
272  exit(1);
273  }
274 
275  MString filename = argv[1];
276 
277  MFileIO::newFile(true);
278 
279  cerr << "Reading " << filename << "\n";
280  status = MFileIO::open(filename);
281  if (!status) {
282  status.perror(filename);
283  exit(1);
284  }
285 
286  output_normals();
287 
288  return 0;
289 }