Panda3D
mayaToEgg.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file mayaToEgg.cxx
10  * @author drose
11  * @date 2000-02-15
12  *
13  * Additional Maintenance by the PandaSE team
14  * Carnegie Mellon Entertainment Technology Center
15  * Spring '10
16  * Team Members:
17  * Deepak Chandraskeran - producer / programmer
18  * Andrew Gartner - programmer/technical artist
19  * Federico Perazzi - programmer
20  * Shuying Feng - programmer
21  * Wei-Feng Huang - programmer
22  * (Egger additions by Andrew Gartner and Wei-Feng Huang)
23  * The egger can now support vertex color in a variety
24  * of combinations with flat color and file color textures
25  * (see set_vertex_color). Also, there are two new
26  * command line options "legacy-shaders" and "texture-copy".
27  * The first treats any Maya material/shader as if it were
28  * a legacy shader. Passing it through the legacy codepath.
29  * This feature was originally intended to fix a bug where
30  * flat-color was being ignored in the modern (Phong) codepath
31  * However, with the new vertex and flat color functions it
32  * may not be necessary. Still, until the newer color functions
33  * have been tried and tested more, the feature has been left in
34  * to anticipate any problems that may arise. The texture copy
35  * feature was added to provide a way to resolve build path issues
36  * and can support both relative and absolute paths. The feature
37  * will copy any file maps/textures to the specified directory
38  * and update the egg file accordingly.
39  */
40 
41 #include "mayaToEgg.h"
42 #include "mayaToEggConverter.h"
43 #include "config_mayaegg.h"
44 #include "config_maya.h" // for maya_cat
45 #include "globPattern.h"
46 
47 /**
48  *
49  */
50 MayaToEgg::
51 MayaToEgg() :
52  SomethingToEgg("Maya", ".mb")
53 {
54  add_path_replace_options();
55  add_path_store_options();
56  add_animation_options();
57  add_units_options();
58  add_normals_options();
59  add_transform_options();
60 
61  set_program_brief("convert Maya model files to .egg");
62  set_program_description
63  ("This program converts Maya model files to egg. Static and animatable "
64  "models can be converted, with polygon or NURBS output. Animation tables "
65  "can also be generated to apply to an animatable model.");
66 
67  add_option
68  ("p", "", 0,
69  "Generate polygon output only. Tesselate all NURBS surfaces to "
70  "polygons via the built-in Maya tesselator. The tesselation will "
71  "be based on the tolerance factor given by -ptol.",
72  &MayaToEgg::dispatch_none, &_polygon_output);
73 
74  add_option
75  ("ptol", "tolerance", 0,
76  "Specify the fit tolerance for Maya polygon tesselation. The smaller "
77  "the number, the more polygons will be generated. The default is "
78  "0.01.",
79  &MayaToEgg::dispatch_double, nullptr, &_polygon_tolerance);
80 
81  add_option
82  ("bface", "", 0,
83  "Respect the Maya \"double sided\" rendering flag to indicate whether "
84  "polygons should be double-sided or single-sided. Since this flag "
85  "is set to double-sided by default in Maya, it is often better to "
86  "ignore this flag (unless your modelers are diligent in turning it "
87  "off where it is not desired). If this flag is not specified, the "
88  "default is to treat all polygons as single-sided, unless an "
89  "egg object type of \"double-sided\" is set.",
90  &MayaToEgg::dispatch_none, &_respect_maya_double_sided);
91 
92  add_option
93  ("suppress-vcolor", "", 0,
94  "Ignore vertex color for geometry that has a texture applied. "
95  "(This is the way Maya normally renders internally.) The egg flag "
96  "'vertex-color' may be applied to a particular model to override "
97  "this setting locally.",
98  &MayaToEgg::dispatch_none, &_suppress_vertex_color);
99 
100  add_option
101  ("convert-cameras", "", 0,
102  "Convert all camera nodes to locators. Will preserve position and rotation.",
103  &MayaToEgg::dispatch_none, &_convert_cameras);
104 
105  add_option
106  ("convert-lights", "", 0,
107  "Convert all light nodes to locators. Will preserve position and rotation only.",
108  &MayaToEgg::dispatch_none, &_convert_lights);
109 
110  add_option
111  ("keep-uvs", "", 0,
112  "Convert all UV sets on all vertices, even those that do not appear "
113  "to be referenced by any textures.",
114  &MayaToEgg::dispatch_none, &_keep_all_uvsets);
115 
116  add_option
117  ("round-uvs", "", 0,
118  "round up uv coordinates to the nearest 1/100th. i.e. -0.001 becomes"
119  "0.0; 0.444 becomes 0.44; 0.778 becomes 0.78.",
120  &MayaToEgg::dispatch_none, &_round_uvs);
121 
122  add_option
123  ("copytex", "dir", 41,
124  "Legacy option. Same as -pc.",
125  &MayaToEgg::dispatch_filename, &_legacy_copytex, &_legacy_copytex_dir);
126 
127  add_option
128  ("trans", "type", 0,
129  "Specifies which transforms in the Maya file should be converted to "
130  "transforms in the egg file. The option may be one of all, model, "
131  "dcs, or none. The default is model, which means only transforms on "
132  "nodes that have the model flag or the dcs flag are preserved.",
133  &MayaToEgg::dispatch_transform_type, nullptr, &_transform_type);
134 
135  add_option
136  ("subroot", "name", 0,
137  "Specifies that only a subroot of the geometry in the Maya file should "
138  "be converted; specifically, the geometry under the node or nodes whose "
139  "name matches the parameter (which may include globbing characters "
140  "like * or ?). This parameter may be repeated multiple times to name "
141  "multiple roots. If it is omitted altogether, the entire file is "
142  "converted.",
143  &MayaToEgg::dispatch_vector_string, nullptr, &_subroots);
144 
145  add_option
146  ("subset", "name", 0,
147  "Specifies that only a subset of the geometry in the Maya file should "
148  "be converted; specifically, the geometry under the node or nodes whose "
149  "name matches the parameter (which may include globbing characters "
150  "like * or ?). This parameter may be repeated multiple times to name "
151  "multiple roots. If it is omitted altogether, the entire file is "
152  "converted.",
153  &MayaToEgg::dispatch_vector_string, nullptr, &_subsets);
154 
155  add_option
156  ("exclude", "name", 0,
157  "Specifies that a subset of the geometry in the Maya file should "
158  "not be converted; specifically, the geometry under the node or nodes whose "
159  "name matches the parameter (which may include globbing characters "
160  "like * or ?). This parameter may be repeated multiple times to name "
161  "multiple roots.",
162  &MayaToEgg::dispatch_vector_string, nullptr, &_excludes);
163 
164  add_option
165  ("ignore-slider", "name", 0,
166  "Specifies the name of a slider (blend shape deformer) that maya2egg "
167  "should not process. The slider will not be touched during conversion "
168  "and it will not become a part of the animation. This "
169  "parameter may including globbing characters, and it may be repeated "
170  "as needed.",
171  &MayaToEgg::dispatch_vector_string, nullptr, &_ignore_sliders);
172 
173  add_option
174  ("force-joint", "name", 0,
175  "Specifies the name of a DAG node that maya2egg "
176  "should treat as a joint, even if it does not appear to be a Maya joint "
177  "and does not appear to be animated.",
178  &MayaToEgg::dispatch_vector_string, nullptr, &_force_joints);
179 
180  add_option
181  ("v", "", 0,
182  "Increase verbosity. More v's means more verbose.",
183  &MayaToEgg::dispatch_count, nullptr, &_verbose);
184 
185  add_option
186  ("legacy-shaders", "", 0,
187  "Use this flag to turn off modern (Phong) shader generation"
188  "and treat all shaders as if they were Lamberts (legacy).",
189  &MayaToEgg::dispatch_none, &_legacy_shader);
190 
191  // Unfortunately, the Maya API doesn't allow us to differentiate between
192  // relative and absolute pathnames--everything comes out as an absolute
193  // pathname, even if it is stored in the Maya file as a relative path. So
194  // we can't support -noabs.
195  remove_option("noabs");
196 
197  _verbose = 0;
198  _polygon_tolerance = 0.01;
199  _transform_type = MayaToEggConverter::TT_model;
200  _got_tbnauto = true;
201 }
202 
203 /**
204  *
205  */
206 void MayaToEgg::
207 run() {
208  // Set the verbose level by using Notify.
209  if (_verbose >= 3) {
210  maya_cat->set_severity(NS_spam);
211  mayaegg_cat->set_severity(NS_spam);
212  } else if (_verbose >= 2) {
213  maya_cat->set_severity(NS_debug);
214  mayaegg_cat->set_severity(NS_debug);
215  } else if (_verbose >= 1) {
216  maya_cat->set_severity(NS_info);
217  mayaegg_cat->set_severity(NS_info);
218  }
219 
220  if (_legacy_copytex && !_path_replace->_copy_files) {
221  _path_replace->_copy_files = true;
222  _path_replace->_copy_into_directory = _legacy_copytex_dir;
223  }
224 
225  // Let's convert the output file to a full path before we initialize Maya,
226  // since Maya now has a nasty habit of changing the current directory.
227  if (_got_output_filename) {
228  _output_filename.make_absolute();
229  _path_replace->_path_directory.make_absolute();
230  }
231 
232  nout << "Initializing Maya.\n";
233  MayaToEggConverter converter(_program_name);
234  // reverting directories is really not needed for maya2egg. It's more
235  // needed for mayaeggloader and such
236  if (!converter.open_api(false)) {
237  nout << "Unable to initialize Maya.\n";
238  exit(1);
239  }
240 
241  // Copy in the command-line parameters.
242  converter._polygon_output = _polygon_output;
243  converter._polygon_tolerance = _polygon_tolerance;
244  converter._respect_maya_double_sided = _respect_maya_double_sided;
245  converter._always_show_vertex_color = !_suppress_vertex_color;
246  converter._keep_all_uvsets = _keep_all_uvsets;
247  converter._convert_cameras = _convert_cameras;
248  converter._convert_lights = _convert_lights;
249  converter._round_uvs = _round_uvs;
250  converter._transform_type = _transform_type;
251  converter._legacy_shader = _legacy_shader;
252 
253  vector_string::const_iterator si;
254  if (!_subroots.empty()) {
255  converter.clear_subroots();
256  for (si = _subroots.begin(); si != _subroots.end(); ++si) {
257  converter.add_subroot(GlobPattern(*si));
258  }
259  }
260 
261  if (!_subsets.empty()) {
262  converter.clear_subsets();
263  for (si = _subsets.begin(); si != _subsets.end(); ++si) {
264  converter.add_subset(GlobPattern(*si));
265  }
266  }
267 
268  if (!_excludes.empty()) {
269  converter.clear_excludes();
270  for (si = _excludes.begin(); si != _excludes.end(); ++si) {
271  converter.add_exclude(GlobPattern(*si));
272  }
273  }
274 
275  if (!_ignore_sliders.empty()) {
276  converter.clear_ignore_sliders();
277  for (si = _ignore_sliders.begin(); si != _ignore_sliders.end(); ++si) {
278  converter.add_ignore_slider(GlobPattern(*si));
279  }
280  }
281 
282  if (!_force_joints.empty()) {
283  converter.clear_force_joints();
284  for (si = _force_joints.begin(); si != _force_joints.end(); ++si) {
285  converter.add_force_joint(GlobPattern(*si));
286  }
287  }
288 
289  // Copy in the path and animation parameters.
290  apply_parameters(converter);
291 
292  // Set the coordinate system to match Maya's.
293  if (!_got_coordinate_system) {
294  _coordinate_system = converter._maya->get_coordinate_system();
295  }
296  _data->set_coordinate_system(_coordinate_system);
297 
298  converter.set_egg_data(_data);
299 
300  if (!converter.convert_file(_input_filename)) {
301  nout << "Errors in conversion.\n";
302  exit(1);
303  }
304 
305  // Use the standard Maya units, if the user didn't specify otherwise. This
306  // always returns centimeters, which is the way all Maya files are stored
307  // internally (and is the units returned by all of the API functions called
308  // here).
309  if (_input_units == DU_invalid) {
310  _input_units = converter.get_input_units();
311  }
312 
313  write_egg_file();
314  nout << "\n";
315 }
316 
317 /**
318  * Dispatches a parameter that expects a MayaToEggConverter::TransformType
319  * option.
320  */
321 bool MayaToEgg::
322 dispatch_transform_type(const std::string &opt, const std::string &arg, void *var) {
323  MayaToEggConverter::TransformType *ip = (MayaToEggConverter::TransformType *)var;
325 
326  if ((*ip) == MayaToEggConverter::TT_invalid) {
327  nout << "Invalid type for -" << opt << ": " << arg << "\n"
328  << "Valid types are all, model, dcs, and none.\n";
329  return false;
330  }
331 
332  return true;
333 }
334 
335 int main(int argc, char *argv[]) {
336  MayaToEgg prog;
337  prog.parse_command_line(argc, argv);
338  prog.run();
339  return 0;
340 }
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_egg_file()
Writes out the egg file as the normal result of the program.
Definition: eggWriter.cxx:177
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class supervises the construction of an EggData structure from a single Maya file,...
void make_absolute()
Converts the filename to a fully-qualified pathname from the root (if it is a relative pathname),...
Definition: filename.cxx:968
This is the general base class for a file-converter program that reads some model file format and gen...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static TransformType string_transform_type(const std::string &arg)
Returns the TransformType value corresponding to the indicated string, or TT_invalid.
This class can be used to test for string matches against standard Unix- shell filename globbing conv...
Definition: globPattern.h:32