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> 33 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
35 status.perror(
"MItDag constructor");
39 while (!dag_iterator.isDone()) {
41 status = dag_iterator.getPath(dag_path);
43 status.perror(
"MItDag::getPath");
47 MFnDagNode dag_node(dag_path, &status);
49 status.perror(
"MFnDagNode constructor");
53 cerr << dag_node.name() <<
"\n";
59 MFnBlendShapeDeformer *
60 get_slider(MString slider_name) {
63 status = MGlobal::selectByName(slider_name, MGlobal::kReplaceList);
65 status.perror(slider_name);
69 status = MGlobal::getActiveSelectionList(list);
71 status.perror(
"MGLobal::getActiveSelectionList");
75 unsigned int num_objects = list.length();
76 if (num_objects != 1) {
77 cerr <<
"Warning: multiple objects match " << slider_name <<
"\n";
80 for (
unsigned int i = 0; i < num_objects; i++) {
82 status = list.getDependNode(i, obj);
84 cerr <<
"selected element is not a dependency node.\n";
85 status.perror(
"getDependNode");
87 if (obj.hasFn(MFn::kBlendShape)) {
88 MFnBlendShapeDeformer *slider =
new MFnBlendShapeDeformer(obj, &status);
90 status.perror(
"MFnBlendShapeDeformer constructor");
92 cerr <<
"got slider " << slider->name() <<
"\n";
97 cerr <<
"selected element is not a blend shape\n";
101 cerr <<
"Couldn't find slider " << slider_name <<
"\n";
106 get_mesh(MString mesh_name) {
109 status = MGlobal::selectByName(mesh_name, MGlobal::kReplaceList);
111 status.perror(mesh_name);
115 status = MGlobal::getActiveSelectionList(list);
117 status.perror(
"MGLobal::getActiveSelectionList");
121 unsigned int num_objects = list.length();
122 if (num_objects != 1) {
123 cerr <<
"Warning: multiple objects match " << mesh_name <<
"\n";
126 for (
unsigned int i = 0; i < num_objects; i++) {
128 status = list.getDependNode(i, obj);
130 cerr <<
"selected element is not a dependency node.\n";
131 status.perror(
"getDependNode");
134 if (obj.hasFn(MFn::kMesh)) {
136 MFnMesh *mesh =
new MFnMesh(obj, &status);
138 status.perror(
"MFnMesh constructor");
140 cerr <<
"got mesh " << mesh->name() <<
"\n";
144 }
else if (obj.hasFn(MFn::kDagNode)) {
147 status = MDagPath::getAPathTo(obj, path);
149 status.perror(
"MDagPath::getAPathTo");
152 if (path.hasFn(MFn::kMesh)) {
153 MFnMesh *mesh =
new MFnMesh(path, &status);
155 status.perror(
"MFnMesh constructor");
157 cerr <<
"got mesh " << mesh->name() <<
"\n";
163 cerr <<
"selected element is not a mesh\n";
167 cerr <<
"Couldn't find mesh " << mesh_name <<
"\n";
172 output_vertices(
const char *filename, MFnMesh &mesh) {
177 status = mesh.getPoints(verts, MSpace::kWorld);
179 status.perror(
"mesh.getPoints");
183 std::ofstream file(filename, std::ios::out | std::ios::trunc);
185 cerr <<
"Couldn't open " << filename <<
" for output.\n";
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";
199 MDagPath dagPath, *_dag_path;
202 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
204 status.perror(
"MItDag constructor");
208 while (!dag_iterator.isDone()) {
209 status = dag_iterator.getPath(dagPath);
211 status.perror(
"MItDag::getPath");
215 _dag_path =
new MDagPath(dagPath);
217 MFnDagNode dag_node(*_dag_path, &status);
219 status.perror(
"MFnDagNode constructor");
223 cerr << dag_node.name() <<
"\n";
225 if (_dag_path->hasFn(MFn::kMesh)) {
226 unsigned int numShapes;
227 status = _dag_path->numberOfShapesDirectlyBelow(numShapes);
228 cerr <<
"----numShapes: " << numShapes <<
"\n";
236 MItMeshPolygon faceIter(*_dag_path, component, &status);
237 if( !status ) cerr <<
"Error at MItMeshPolygon" << endl;
239 MFnMesh meshFn(*_dag_path);
242 for (; !faceIter.isDone();faceIter.next()) {
243 int nVerts = faceIter.polygonVertexCount();
246 for (
int i = 0;i<nVerts;i++) {
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;
259 main(
int argc,
char *argv[]) {
260 cerr <<
"Initializing Maya\n";
262 status = MLibrary::initialize(argv[0]);
264 status.perror(
"Could not initialize");
268 cerr <<
"Using Maya library version " << MGlobal::mayaVersion() <<
"\n";
271 cerr <<
"\nUsage:\n\nnormal_test.cxx file.mb mesh_name\n\n";
275 MString filename = argv[1];
277 MFileIO::newFile(
true);
279 cerr <<
"Reading " << filename <<
"\n";
280 status = MFileIO::open(filename);
282 status.perror(filename);