11 #define REQUIRE_IOSTREAM 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> 32 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
34 status.perror(
"MItDag constructor");
38 while (!dag_iterator.isDone()) {
40 status = dag_iterator.getPath(dag_path);
42 status.perror(
"MItDag::getPath");
46 MFnDagNode dag_node(dag_path, &status);
48 status.perror(
"MFnDagNode constructor");
52 cerr << dag_node.name() <<
"\n";
58 MFnBlendShapeDeformer *
59 get_slider(MString slider_name) {
62 status = MGlobal::selectByName(slider_name, MGlobal::kReplaceList);
64 status.perror(slider_name);
68 status = MGlobal::getActiveSelectionList(list);
70 status.perror(
"MGLobal::getActiveSelectionList");
74 unsigned int num_objects = list.length();
75 if (num_objects != 1) {
76 cerr <<
"Warning: multiple objects match " << slider_name <<
"\n";
79 for (
unsigned int i = 0; i < num_objects; i++) {
81 status = list.getDependNode(i, obj);
83 cerr <<
"selected element is not a dependency node.\n";
84 status.perror(
"getDependNode");
86 if (obj.hasFn(MFn::kBlendShape)) {
87 MFnBlendShapeDeformer *slider =
new MFnBlendShapeDeformer(obj, &status);
89 status.perror(
"MFnBlendShapeDeformer constructor");
91 cerr <<
"got slider " << slider->name() <<
"\n";
96 cerr <<
"selected element is not a blend shape\n";
100 cerr <<
"Couldn't find slider " << slider_name <<
"\n";
105 get_mesh(MString mesh_name) {
108 status = MGlobal::selectByName(mesh_name, MGlobal::kReplaceList);
110 status.perror(mesh_name);
114 status = MGlobal::getActiveSelectionList(list);
116 status.perror(
"MGLobal::getActiveSelectionList");
120 unsigned int num_objects = list.length();
121 if (num_objects != 1) {
122 cerr <<
"Warning: multiple objects match " << mesh_name <<
"\n";
125 for (
unsigned int i = 0; i < num_objects; i++) {
127 status = list.getDependNode(i, obj);
129 cerr <<
"selected element is not a dependency node.\n";
130 status.perror(
"getDependNode");
133 if (obj.hasFn(MFn::kMesh)) {
135 MFnMesh *mesh =
new MFnMesh(obj, &status);
137 status.perror(
"MFnMesh constructor");
139 cerr <<
"got mesh " << mesh->name() <<
"\n";
143 }
else if (obj.hasFn(MFn::kDagNode)) {
146 status = MDagPath::getAPathTo(obj, path);
148 status.perror(
"MDagPath::getAPathTo");
151 if (path.hasFn(MFn::kMesh)) {
152 MFnMesh *mesh =
new MFnMesh(path, &status);
154 status.perror(
"MFnMesh constructor");
156 cerr <<
"got mesh " << mesh->name() <<
"\n";
162 cerr <<
"selected element is not a mesh\n";
166 cerr <<
"Couldn't find mesh " << mesh_name <<
"\n";
171 output_vertices(
const char *filename, MFnMesh &mesh) {
176 status = mesh.getPoints(verts, MSpace::kWorld);
178 status.perror(
"mesh.getPoints");
182 ofstream file(filename, ios::out | ios::trunc);
184 cerr <<
"Couldn't open " << filename <<
" for output.\n";
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";
198 MDagPath dagPath, *_dag_path;
201 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
203 status.perror(
"MItDag constructor");
207 while (!dag_iterator.isDone()) {
208 status = dag_iterator.getPath(dagPath);
210 status.perror(
"MItDag::getPath");
214 _dag_path =
new MDagPath(dagPath);
216 MFnDagNode dag_node(*_dag_path, &status);
218 status.perror(
"MFnDagNode constructor");
222 cerr << dag_node.name() <<
"\n";
224 if (_dag_path->hasFn(MFn::kMesh)) {
225 unsigned int numShapes;
226 status = _dag_path->numberOfShapesDirectlyBelow(numShapes);
227 cerr <<
"----numShapes: " << numShapes <<
"\n";
235 MItMeshPolygon faceIter(*_dag_path, component, &status);
236 if( !status ) cerr <<
"Error at MItMeshPolygon" << endl;
238 MFnMesh meshFn(*_dag_path);
241 for (; !faceIter.isDone();faceIter.next()) {
242 int nVerts = faceIter.polygonVertexCount();
245 for (
int i = 0;i<nVerts;i++) {
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;
258 main(
int argc,
char *argv[]) {
259 cerr <<
"Initializing Maya\n";
261 status = MLibrary::initialize(argv[0]);
263 status.perror(
"Could not initialize");
267 cerr <<
"Using Maya library version " << MGlobal::mayaVersion() <<
"\n";
270 cerr <<
"\nUsage:\n\nnormal_test.cxx file.mb mesh_name\n\n";
274 MString filename = argv[1];
276 MFileIO::newFile(
true);
278 cerr <<
"Reading " << filename <<
"\n";
279 status = MFileIO::open(filename);
281 status.perror(filename);