22 #include <maya/MStringArray.h>
23 #include <maya/MGlobal.h>
24 #include <maya/MFileIO.h>
25 #include <maya/MItDag.h>
26 #include <maya/MFnDagNode.h>
27 #include <maya/MFnNurbsSurface.h>
28 #include <maya/MFnMesh.h>
29 #include <maya/MObject.h>
30 #include <maya/MDagPath.h>
31 #include <maya/MIntArray.h>
44 set_program_brief(
"copy Maya .mb files into a CVS source hierarchy");
45 set_program_description
46 (
"mayacopy copies one or more Maya .mb files into a "
47 "CVS source hierarchy. "
48 "Rather than copying the named files immediately into the current "
49 "directory, it first scans the entire source hierarchy, identifying all "
50 "the already-existing files. If the named file to copy matches the "
51 "name of an already-existing file in the current directory or elsewhere "
52 "in the hierarchy, that file is overwritten. Other .mb files, as "
53 "well as texture files, that are externally referenced by the "
54 "named .mb file(s) are similarly copied.");
57 add_runline(
"[opts] file.mb [file.mb ... ]");
61 "Don't attempt to strip the Maya version number from the tail of the "
62 "source filename before it is copied into the tree.",
63 &CVSCopy::dispatch_none, &_keep_ver);
75 "Character animation files do not need to copy the texures. "
76 "This option omits the textures of the models to be re-mayacopied",
77 &CVSCopy::dispatch_none, &_omit_tex);
81 "Character animation files do not need to copy internal file references. "
82 "This option omits the references of the models to be re-mayacopied",
83 &CVSCopy::dispatch_none, &_omit_ref);
87 "Write a .ma file instead of a .mb file (regardless of input type)",
88 &CVSCopy::dispatch_none, &_maya_ascii);
90 add_path_replace_options();
98 _maya = MayaApi::open_api(_program_name);
99 if (!_maya->is_valid()) {
100 nout <<
"Unable to initialize Maya.\n";
104 SourceFiles::iterator fi;
105 for (fi = _source_files.begin(); fi != _source_files.end(); ++fi) {
112 nout <<
"\nUnable to copy, aborting!\n\n";
126 ExtraData *ed = (ExtraData *)extra_data;
129 return copy_maya_file(source, dest, dir);
135 return copy_texture(source, dest, dir);
138 nout <<
"Internal error: invalid type " << (int)ed->_type <<
"\n";
149 filter_filename(
const string &source) {
154 size_t dot = source.rfind(
'.');
155 size_t underscore = source.rfind(
"_v", dot);
156 if ((underscore != string::npos) && !isdigit(source.at(underscore+2)))
157 underscore = string::npos;
159 string extension = source.substr(dot);
160 if (extension ==
".ma" || extension ==
".mb") {
171 if (underscore == string::npos) {
173 return source.substr(0, dot) + extension;
175 return source.substr(0, underscore) + extension;
185 if (!_maya->read(source)) {
187 <<
"Unable to read " << source <<
"\n";
193 MStatus status = MFileIO::getReferences(refs);
194 if (status != MStatus::kSuccess) {
195 status.perror(
"MItDag constructor");
200 unsigned int num_refs = refs.length();
202 unsigned int ref_index;
203 maya_cat.info() <<
"num_refs = " << num_refs << endl;
204 for (ref_index = 0; ref_index < num_refs; ref_index++) {
205 maya_cat.info() <<
"curr_idx " << _curr_idx << endl;
206 string lookup = refs[ref_index].asChar();
207 string blah =
"file -q -referenceNode \"" + lookup +
"\";";
208 maya_cat.info() << blah << endl;
210 status = MGlobal::executeCommand(MString(blah.c_str()), result);
211 maya_cat.info() <<
"result = " << result.asChar() << endl;
215 size_t dup = lookup.find(
'{');
216 if (dup != string::npos){
223 string refNode = result.asChar();
224 string refCheckCmd =
"file -rfn " + refNode +
" -q -dr;";
226 status = MGlobal::executeCommand(MString(refCheckCmd.c_str()), deferredRef);
227 maya_cat.info() <<
"deferredRef = " << deferredRef << endl;
228 if (deferredRef == 1) {
236 _tree.choose_directory(filename.
get_basename(), dir, _force, _interactive);
239 if (maya_cat.is_spam()) {
245 if (maya_cat.is_debug()) {
246 string cmdStr =
"pwd";
248 status = MGlobal::executeCommand(MString(cmdStr.c_str()), result3);
249 maya_cat.debug() <<
"result = " << result3.asChar() <<
"\n";
251 _exec_string.push_back(
"file -loadReference \"" +
string(result.asChar()) +
"\" -type \"mayaBinary\" -options \"v=0\" \"" + new_filename.
to_os_generic() +
"\";");
253 maya_cat.info() <<
"executing command: " << _exec_string[_curr_idx] <<
"\n";
254 status = MGlobal::executeCommand(MString(_exec_string[_curr_idx].c_str()));
255 if (status != MStatus::kSuccess) {
256 status.perror(
"loadReference failed");
267 for (
int i = 0; i < num_shaders; i++) {
269 for (
size_t j = 0; j < shader->_all_maps.size(); j++) {
270 if (!extract_texture(*shader->_all_maps[j], dir)) {
278 if (!_maya->write(dest)) {
280 <<
"Cannot write " << dest <<
"\n";
284 for (ref_index = 0; ref_index < num_refs; ref_index++) {
289 string lookup = refs[ref_index].asChar();
292 size_t dup = lookup.find(
'{');
293 if (dup != string::npos){
301 <<
"External ref: " << filename <<
"\n";
324 _path_replace->convert_path(color_def._texture_filename);
325 if (!texture_filename.
exists()) {
326 nout <<
"*** Error: texture " << texture_filename
327 <<
" does not exist.\n";
330 nout <<
"*** Error: texture " << texture_filename
331 <<
" is not a regular file.\n";
335 ed._type = FT_texture;
338 import(texture_filename, &ed, _map_dir);
359 if (!copy_binary_file(source, dest)) {
374 MItDag dag_iterator(MItDag::kDepthFirst, MFn::kTransform, &status);
375 if (status != MStatus::kSuccess) {
376 status.perror(
"MItDag constructor");
384 while (!dag_iterator.isDone()) {
386 status = dag_iterator.getPath(dag_path);
387 if (status != MStatus::kSuccess) {
388 status.perror(
"MItDag::getPath");
390 if (!collect_shader_for_node(dag_path)) {
399 nout <<
"Errors encountered in traversal.\n";
410 collect_shader_for_node(
const MDagPath &dag_path) {
412 MFnDagNode dag_node(dag_path, &status);
413 if (status != MStatus::kSuccess) {
414 status.perror(
"MFnDagNode constructor");
418 if (dag_path.hasFn(MFn::kNurbsSurface)) {
419 MFnNurbsSurface surface(dag_path, &status);
424 }
else if (dag_path.hasFn(MFn::kMesh)) {
425 MFnMesh mesh(dag_path, &status);
428 MObjectArray shaders;
429 MIntArray poly_shader_indices;
431 status = mesh.getConnectedShaders(dag_path.instanceNumber(),
432 shaders, poly_shader_indices);
434 unsigned int num_shaders = shaders.length();
435 for (
unsigned int shader_index = 0;
436 shader_index < num_shaders;
438 MObject engine = shaders[shader_index];
452 int main(
int argc,
char *argv[]) {