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