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/MFnBlendShapeDeformer.h> 21 #include <maya/MFnMesh.h> 22 #include <maya/MPointArray.h> 30 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
32 status.perror(
"MItDag constructor");
36 while (!dag_iterator.isDone()) {
38 status = dag_iterator.getPath(dag_path);
40 status.perror(
"MItDag::getPath");
44 MFnDagNode dag_node(dag_path, &status);
46 status.perror(
"MFnDagNode constructor");
50 cerr << dag_node.name() <<
"\n";
56 MFnBlendShapeDeformer *
57 get_slider(MString slider_name) {
60 status = MGlobal::selectByName(slider_name, MGlobal::kReplaceList);
62 status.perror(slider_name);
66 status = MGlobal::getActiveSelectionList(list);
68 status.perror(
"MGLobal::getActiveSelectionList");
72 unsigned int num_objects = list.length();
73 if (num_objects != 1) {
74 cerr <<
"Warning: multiple objects match " << slider_name <<
"\n";
77 for (
unsigned int i = 0; i < num_objects; i++) {
79 status = list.getDependNode(i, obj);
81 cerr <<
"selected element is not a dependency node.\n";
82 status.perror(
"getDependNode");
84 if (obj.hasFn(MFn::kBlendShape)) {
85 MFnBlendShapeDeformer *slider =
new MFnBlendShapeDeformer(obj, &status);
87 status.perror(
"MFnBlendShapeDeformer constructor");
89 cerr <<
"got slider " << slider->name() <<
"\n";
94 cerr <<
"selected element is not a blend shape\n";
98 cerr <<
"Couldn't find slider " << slider_name <<
"\n";
103 get_mesh(MString mesh_name) {
106 status = MGlobal::selectByName(mesh_name, MGlobal::kReplaceList);
108 status.perror(mesh_name);
112 status = MGlobal::getActiveSelectionList(list);
114 status.perror(
"MGLobal::getActiveSelectionList");
118 unsigned int num_objects = list.length();
119 if (num_objects != 1) {
120 cerr <<
"Warning: multiple objects match " << mesh_name <<
"\n";
123 for (
unsigned int i = 0; i < num_objects; i++) {
125 status = list.getDependNode(i, obj);
127 cerr <<
"selected element is not a dependency node.\n";
128 status.perror(
"getDependNode");
131 if (obj.hasFn(MFn::kMesh)) {
133 MFnMesh *mesh =
new MFnMesh(obj, &status);
135 status.perror(
"MFnMesh constructor");
137 cerr <<
"got mesh " << mesh->name() <<
"\n";
141 }
else if (obj.hasFn(MFn::kDagNode)) {
144 status = MDagPath::getAPathTo(obj, path);
146 status.perror(
"MDagPath::getAPathTo");
149 if (path.hasFn(MFn::kMesh)) {
150 MFnMesh *mesh =
new MFnMesh(path, &status);
152 status.perror(
"MFnMesh constructor");
154 cerr <<
"got mesh " << mesh->name() <<
"\n";
160 cerr <<
"selected element is not a mesh\n";
164 cerr <<
"Couldn't find mesh " << mesh_name <<
"\n";
169 output_vertices(
const char *filename, MFnMesh &mesh) {
174 status = mesh.getPoints(verts, MSpace::kWorld);
176 status.perror(
"mesh.getPoints");
180 std::ofstream file(filename, std::ios::out | std::ios::trunc);
182 cerr <<
"Couldn't open " << filename <<
" for output.\n";
186 unsigned int num_verts = verts.length();
187 cerr <<
"writing " << num_verts <<
" vertices to " << filename <<
"\n";
188 for (
unsigned int i = 0; i < num_verts; i++) {
189 file << i <<
". " << verts[i] <<
"\n";
194 main(
int argc,
char *argv[]) {
195 cerr <<
"Initializing Maya\n";
197 status = MLibrary::initialize(argv[0]);
199 status.perror(
"Could not initialize");
203 cerr <<
"Using Maya library version " << MGlobal::mayaVersion() <<
"\n";
206 cerr <<
"\nUsage:\n\nblend_test.cxx file.mb slider_name mesh_name\n\n";
210 MString filename = argv[1];
211 MString slider_name = argv[2];
212 MString mesh_name = argv[3];
214 MFileIO::newFile(
true);
216 cerr <<
"Reading " << filename <<
"\n";
217 status = MFileIO::open(filename);
219 status.perror(filename);
223 MFnBlendShapeDeformer *slider = get_slider(slider_name);
224 MFnMesh *mesh = get_mesh(mesh_name);
226 cerr <<
"\nOriginal slider value is " << slider->weight(0) <<
"\n";
228 output_vertices(
"before.txt", *mesh);
230 cerr <<
"Setting slider to 1\n";
231 status = slider->setWeight(0, 1.0);
233 status.perror(
"MFnBlendShapeDeformer::setWeight");
237 output_vertices(
"after.txt", *mesh);