Panda3D
|
00001 00002 #include <iostream> 00003 #include <fstream> 00004 #include <stdlib.h> 00005 #include <string> 00006 00007 #ifndef _BOOL 00008 #define _BOOL 1 00009 #endif 00010 00011 #define REQUIRE_IOSTREAM 00012 00013 #include <maya/MGlobal.h> 00014 #include <maya/MFileIO.h> 00015 #include <maya/MLibrary.h> 00016 #include <maya/MStatus.h> 00017 #include <maya/MItDag.h> 00018 #include <maya/MDagPath.h> 00019 #include <maya/MFnDagNode.h> 00020 #include <maya/MItMeshPolygon.h> 00021 #include <maya/MItSelectionList.h> 00022 #include <maya/MFnBlendShapeDeformer.h> 00023 #include <maya/MFnMesh.h> 00024 #include <maya/MPointArray.h> 00025 00026 using namespace std; 00027 00028 void 00029 scan_nodes() { 00030 MStatus status; 00031 00032 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status); 00033 if (!status) { 00034 status.perror("MItDag constructor"); 00035 exit(1); 00036 } 00037 00038 while (!dag_iterator.isDone()) { 00039 MDagPath dag_path; 00040 status = dag_iterator.getPath(dag_path); 00041 if (!status) { 00042 status.perror("MItDag::getPath"); 00043 exit(1); 00044 } 00045 00046 MFnDagNode dag_node(dag_path, &status); 00047 if (!status) { 00048 status.perror("MFnDagNode constructor"); 00049 exit(1); 00050 } 00051 00052 cerr << dag_node.name() << "\n"; 00053 00054 dag_iterator.next(); 00055 } 00056 } 00057 00058 MFnBlendShapeDeformer * 00059 get_slider(MString slider_name) { 00060 MStatus status; 00061 00062 status = MGlobal::selectByName(slider_name, MGlobal::kReplaceList); 00063 if (!status) { 00064 status.perror(slider_name); 00065 exit(1); 00066 } 00067 MSelectionList list; 00068 status = MGlobal::getActiveSelectionList(list); 00069 if (!status) { 00070 status.perror("MGLobal::getActiveSelectionList"); 00071 exit(1); 00072 } 00073 00074 unsigned int num_objects = list.length(); 00075 if (num_objects != 1) { 00076 cerr << "Warning: multiple objects match " << slider_name << "\n"; 00077 } 00078 00079 for (unsigned int i = 0; i < num_objects; i++) { 00080 MObject obj; 00081 status = list.getDependNode(i, obj); 00082 if (!status) { 00083 cerr << "selected element is not a dependency node.\n"; 00084 status.perror("getDependNode"); 00085 } else { 00086 if (obj.hasFn(MFn::kBlendShape)) { 00087 MFnBlendShapeDeformer *slider = new MFnBlendShapeDeformer(obj, &status); 00088 if (!status) { 00089 status.perror("MFnBlendShapeDeformer constructor"); 00090 } else { 00091 cerr << "got slider " << slider->name() << "\n"; 00092 return slider; 00093 } 00094 } 00095 00096 cerr << "selected element is not a blend shape\n"; 00097 } 00098 } 00099 00100 cerr << "Couldn't find slider " << slider_name << "\n"; 00101 exit(1); 00102 } 00103 00104 MFnMesh * 00105 get_mesh(MString mesh_name) { 00106 MStatus status; 00107 00108 status = MGlobal::selectByName(mesh_name, MGlobal::kReplaceList); 00109 if (!status) { 00110 status.perror(mesh_name); 00111 exit(1); 00112 } 00113 MSelectionList list; 00114 status = MGlobal::getActiveSelectionList(list); 00115 if (!status) { 00116 status.perror("MGLobal::getActiveSelectionList"); 00117 exit(1); 00118 } 00119 00120 unsigned int num_objects = list.length(); 00121 if (num_objects != 1) { 00122 cerr << "Warning: multiple objects match " << mesh_name << "\n"; 00123 } 00124 00125 for (unsigned int i = 0; i < num_objects; i++) { 00126 MObject obj; 00127 status = list.getDependNode(i, obj); 00128 if (!status) { 00129 cerr << "selected element is not a dependency node.\n"; 00130 status.perror("getDependNode"); 00131 00132 } else { 00133 if (obj.hasFn(MFn::kMesh)) { 00134 // Maybe it's a mesh object itself. 00135 MFnMesh *mesh = new MFnMesh(obj, &status); 00136 if (!status) { 00137 status.perror("MFnMesh constructor"); 00138 } else { 00139 cerr << "got mesh " << mesh->name() << "\n"; 00140 return mesh; 00141 } 00142 00143 } else if (obj.hasFn(MFn::kDagNode)) { 00144 // Maybe it's a dag node. 00145 MDagPath path; 00146 status = MDagPath::getAPathTo(obj, path); 00147 if (!status) { 00148 status.perror("MDagPath::getAPathTo"); 00149 exit(1); 00150 } 00151 if (path.hasFn(MFn::kMesh)) { 00152 MFnMesh *mesh = new MFnMesh(path, &status); 00153 if (!status) { 00154 status.perror("MFnMesh constructor"); 00155 } else { 00156 cerr << "got mesh " << mesh->name() << "\n"; 00157 return mesh; 00158 } 00159 } 00160 } 00161 00162 cerr << "selected element is not a mesh\n"; 00163 } 00164 } 00165 00166 cerr << "Couldn't find mesh " << mesh_name << "\n"; 00167 exit(1); 00168 } 00169 00170 void 00171 output_vertices(const char *filename, MFnMesh &mesh) { 00172 MStatus status; 00173 00174 MPointArray verts; 00175 // status = mesh.getPoints(verts, MSpace::kObject); 00176 status = mesh.getPoints(verts, MSpace::kWorld); 00177 if (!status) { 00178 status.perror("mesh.getPoints"); 00179 exit(1); 00180 } 00181 00182 ofstream file(filename, ios::out | ios::trunc); 00183 if (!file) { 00184 cerr << "Couldn't open " << filename << " for output.\n"; 00185 exit(1); 00186 } 00187 00188 unsigned int num_verts = verts.length(); 00189 cerr << "writing " << num_verts << " vertices to " << filename << "\n"; 00190 for (unsigned int i = 0; i < num_verts; i++) { 00191 file << i << ". " << verts[i] << "\n"; 00192 } 00193 } 00194 00195 void 00196 output_normals() { 00197 MStatus status; 00198 MDagPath dagPath, *_dag_path; 00199 MObject component; 00200 00201 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status); 00202 if (!status) { 00203 status.perror("MItDag constructor"); 00204 exit(1); 00205 } 00206 00207 while (!dag_iterator.isDone()) { 00208 status = dag_iterator.getPath(dagPath); 00209 if (!status) { 00210 status.perror("MItDag::getPath"); 00211 exit(1); 00212 } 00213 00214 _dag_path = new MDagPath(dagPath); 00215 00216 MFnDagNode dag_node(*_dag_path, &status); 00217 if (!status) { 00218 status.perror("MFnDagNode constructor"); 00219 exit(1); 00220 } 00221 00222 cerr << dag_node.name() << "\n"; 00223 00224 if (_dag_path->hasFn(MFn::kMesh)) { 00225 unsigned int numShapes; 00226 status = _dag_path->numberOfShapesDirectlyBelow(numShapes); 00227 cerr << "----numShapes: " << numShapes << "\n"; 00228 /* 00229 if (numShapes == 1) { 00230 _dag_path->extendToShape(); 00231 } 00232 */ 00233 } 00234 00235 MItMeshPolygon faceIter(*_dag_path, component, &status); 00236 if( !status ) cerr << "Error at MItMeshPolygon" << endl; 00237 00238 MFnMesh meshFn(*_dag_path); 00239 00240 // Traverse the polygonal face 00241 for (; !faceIter.isDone();faceIter.next()) { 00242 int nVerts = faceIter.polygonVertexCount(); 00243 00244 // Traverse the vertices to get their indexes and print out the normals 00245 for (int i = 0;i<nVerts;i++) { 00246 MVector normal; 00247 faceIter.getNormal( i, normal, MSpace::kWorld ); 00248 cerr << "MItMeshPolygon::getNormal for the vertex [" << faceIter.vertexIndex(i) 00249 << "] for face [" << faceIter.index() << "] is " << normal << endl; 00250 } 00251 } 00252 00253 dag_iterator.next(); 00254 } 00255 } 00256 00257 int 00258 main(int argc, char *argv[]) { 00259 cerr << "Initializing Maya\n"; 00260 MStatus status; 00261 status = MLibrary::initialize(argv[0]); 00262 if (!status) { 00263 status.perror("Could not initialize"); 00264 exit(1); 00265 } 00266 00267 cerr << "Using Maya library version " << MGlobal::mayaVersion() << "\n"; 00268 00269 if (argc < 2) { 00270 cerr << "\nUsage:\n\nnormal_test.cxx file.mb mesh_name\n\n"; 00271 exit(1); 00272 } 00273 00274 MString filename = argv[1]; 00275 00276 MFileIO::newFile(true); 00277 00278 cerr << "Reading " << filename << "\n"; 00279 status = MFileIO::open(filename); 00280 if (!status) { 00281 status.perror(filename); 00282 exit(1); 00283 } 00284 00285 output_normals(); 00286 00287 return 0; 00288 }