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/MFnBlendShapeDeformer.h> 00021 #include <maya/MFnMesh.h> 00022 #include <maya/MPointArray.h> 00023 00024 using namespace std; 00025 00026 void 00027 scan_nodes() { 00028 MStatus status; 00029 00030 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status); 00031 if (!status) { 00032 status.perror("MItDag constructor"); 00033 exit(1); 00034 } 00035 00036 while (!dag_iterator.isDone()) { 00037 MDagPath dag_path; 00038 status = dag_iterator.getPath(dag_path); 00039 if (!status) { 00040 status.perror("MItDag::getPath"); 00041 exit(1); 00042 } 00043 00044 MFnDagNode dag_node(dag_path, &status); 00045 if (!status) { 00046 status.perror("MFnDagNode constructor"); 00047 exit(1); 00048 } 00049 00050 cerr << dag_node.name() << "\n"; 00051 00052 dag_iterator.next(); 00053 } 00054 } 00055 00056 MFnBlendShapeDeformer * 00057 get_slider(MString slider_name) { 00058 MStatus status; 00059 00060 status = MGlobal::selectByName(slider_name, MGlobal::kReplaceList); 00061 if (!status) { 00062 status.perror(slider_name); 00063 exit(1); 00064 } 00065 MSelectionList list; 00066 status = MGlobal::getActiveSelectionList(list); 00067 if (!status) { 00068 status.perror("MGLobal::getActiveSelectionList"); 00069 exit(1); 00070 } 00071 00072 unsigned int num_objects = list.length(); 00073 if (num_objects != 1) { 00074 cerr << "Warning: multiple objects match " << slider_name << "\n"; 00075 } 00076 00077 for (unsigned int i = 0; i < num_objects; i++) { 00078 MObject obj; 00079 status = list.getDependNode(i, obj); 00080 if (!status) { 00081 cerr << "selected element is not a dependency node.\n"; 00082 status.perror("getDependNode"); 00083 } else { 00084 if (obj.hasFn(MFn::kBlendShape)) { 00085 MFnBlendShapeDeformer *slider = new MFnBlendShapeDeformer(obj, &status); 00086 if (!status) { 00087 status.perror("MFnBlendShapeDeformer constructor"); 00088 } else { 00089 cerr << "got slider " << slider->name() << "\n"; 00090 return slider; 00091 } 00092 } 00093 00094 cerr << "selected element is not a blend shape\n"; 00095 } 00096 } 00097 00098 cerr << "Couldn't find slider " << slider_name << "\n"; 00099 exit(1); 00100 } 00101 00102 MFnMesh * 00103 get_mesh(MString mesh_name) { 00104 MStatus status; 00105 00106 status = MGlobal::selectByName(mesh_name, MGlobal::kReplaceList); 00107 if (!status) { 00108 status.perror(mesh_name); 00109 exit(1); 00110 } 00111 MSelectionList list; 00112 status = MGlobal::getActiveSelectionList(list); 00113 if (!status) { 00114 status.perror("MGLobal::getActiveSelectionList"); 00115 exit(1); 00116 } 00117 00118 unsigned int num_objects = list.length(); 00119 if (num_objects != 1) { 00120 cerr << "Warning: multiple objects match " << mesh_name << "\n"; 00121 } 00122 00123 for (unsigned int i = 0; i < num_objects; i++) { 00124 MObject obj; 00125 status = list.getDependNode(i, obj); 00126 if (!status) { 00127 cerr << "selected element is not a dependency node.\n"; 00128 status.perror("getDependNode"); 00129 00130 } else { 00131 if (obj.hasFn(MFn::kMesh)) { 00132 // Maybe it's a mesh object itself. 00133 MFnMesh *mesh = new MFnMesh(obj, &status); 00134 if (!status) { 00135 status.perror("MFnMesh constructor"); 00136 } else { 00137 cerr << "got mesh " << mesh->name() << "\n"; 00138 return mesh; 00139 } 00140 00141 } else if (obj.hasFn(MFn::kDagNode)) { 00142 // Maybe it's a dag node. 00143 MDagPath path; 00144 status = MDagPath::getAPathTo(obj, path); 00145 if (!status) { 00146 status.perror("MDagPath::getAPathTo"); 00147 exit(1); 00148 } 00149 if (path.hasFn(MFn::kMesh)) { 00150 MFnMesh *mesh = new MFnMesh(path, &status); 00151 if (!status) { 00152 status.perror("MFnMesh constructor"); 00153 } else { 00154 cerr << "got mesh " << mesh->name() << "\n"; 00155 return mesh; 00156 } 00157 } 00158 } 00159 00160 cerr << "selected element is not a mesh\n"; 00161 } 00162 } 00163 00164 cerr << "Couldn't find mesh " << mesh_name << "\n"; 00165 exit(1); 00166 } 00167 00168 void 00169 output_vertices(const char *filename, MFnMesh &mesh) { 00170 MStatus status; 00171 00172 MPointArray verts; 00173 // status = mesh.getPoints(verts, MSpace::kObject); 00174 status = mesh.getPoints(verts, MSpace::kWorld); 00175 if (!status) { 00176 status.perror("mesh.getPoints"); 00177 exit(1); 00178 } 00179 00180 ofstream file(filename, ios::out | ios::trunc); 00181 if (!file) { 00182 cerr << "Couldn't open " << filename << " for output.\n"; 00183 exit(1); 00184 } 00185 00186 unsigned int num_verts = verts.length(); 00187 cerr << "writing " << num_verts << " vertices to " << filename << "\n"; 00188 for (unsigned int i = 0; i < num_verts; i++) { 00189 file << i << ". " << verts[i] << "\n"; 00190 } 00191 } 00192 00193 int 00194 main(int argc, char *argv[]) { 00195 cerr << "Initializing Maya\n"; 00196 MStatus status; 00197 status = MLibrary::initialize(argv[0]); 00198 if (!status) { 00199 status.perror("Could not initialize"); 00200 exit(1); 00201 } 00202 00203 cerr << "Using Maya library version " << MGlobal::mayaVersion() << "\n"; 00204 00205 if (argc < 4) { 00206 cerr << "\nUsage:\n\nblend_test.cxx file.mb slider_name mesh_name\n\n"; 00207 exit(1); 00208 } 00209 00210 MString filename = argv[1]; 00211 MString slider_name = argv[2]; 00212 MString mesh_name = argv[3]; 00213 00214 MFileIO::newFile(true); 00215 00216 cerr << "Reading " << filename << "\n"; 00217 status = MFileIO::open(filename); 00218 if (!status) { 00219 status.perror(filename); 00220 exit(1); 00221 } 00222 00223 MFnBlendShapeDeformer *slider = get_slider(slider_name); 00224 MFnMesh *mesh = get_mesh(mesh_name); 00225 00226 cerr << "\nOriginal slider value is " << slider->weight(0) << "\n"; 00227 00228 output_vertices("before.txt", *mesh); 00229 00230 cerr << "Setting slider to 1\n"; 00231 status = slider->setWeight(0, 1.0); 00232 if (!status) { 00233 status.perror("MFnBlendShapeDeformer::setWeight"); 00234 exit(1); 00235 } 00236 00237 output_vertices("after.txt", *mesh); 00238 00239 return 0; 00240 }