1 |
|
---|
2 | #include "OgreExporter.h"
|
---|
3 |
|
---|
4 | namespace OgreMayaExporter
|
---|
5 | {
|
---|
6 | void OgreExporter::exit()
|
---|
7 | {
|
---|
8 | if (m_pMesh)
|
---|
9 | delete m_pMesh;
|
---|
10 | if (m_pMaterialSet)
|
---|
11 | delete m_pMaterialSet;
|
---|
12 | m_params.closeFiles();
|
---|
13 | }
|
---|
14 |
|
---|
15 | MStatus OgreExporter::doIt(const MArgList& args)
|
---|
16 | {
|
---|
17 | // Parse the arguments.
|
---|
18 | m_params.parseArgs(args);
|
---|
19 |
|
---|
20 | // Create output files
|
---|
21 | m_params.openFiles();
|
---|
22 |
|
---|
23 | // Create a new empty mesh
|
---|
24 | m_pMesh = new Mesh();
|
---|
25 |
|
---|
26 | // Create a new empty material set
|
---|
27 | m_pMaterialSet = new MaterialSet();
|
---|
28 |
|
---|
29 | /**************************** LOAD DATA **********************************/
|
---|
30 | if (m_params.exportAll)
|
---|
31 | { // we are exporting the whole scene
|
---|
32 | std::cout << "Export the whole scene\n";
|
---|
33 | MItDag dagIter;
|
---|
34 | MFnDagNode worldDag (dagIter.root());
|
---|
35 | MDagPath worldPath;
|
---|
36 | worldDag.getPath(worldPath);
|
---|
37 | stat = translateNode(worldPath);
|
---|
38 | }
|
---|
39 | else
|
---|
40 | { // we are translating a selection
|
---|
41 | std::cout << "Export selected objects\n";
|
---|
42 | // get the selection list
|
---|
43 | MSelectionList activeList;
|
---|
44 | stat = MGlobal::getActiveSelectionList(activeList);
|
---|
45 | if (MS::kSuccess != stat)
|
---|
46 | {
|
---|
47 | std::cout << "Error retrieving selection list\n";
|
---|
48 | exit();
|
---|
49 | return MS::kFailure;
|
---|
50 | }
|
---|
51 | MItSelectionList iter(activeList);
|
---|
52 |
|
---|
53 | for ( ; !iter.isDone(); iter.next())
|
---|
54 | {
|
---|
55 | MDagPath dagPath;
|
---|
56 | stat = iter.getDagPath(dagPath);
|
---|
57 | stat = translateNode(dagPath);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | // load skeleton animation (do it now, so we have loaded all needed joints)
|
---|
62 | if (m_pMesh->getSkeleton() && m_params.exportAnims)
|
---|
63 | {
|
---|
64 | m_pMesh->getSkeleton()->loadAnims(m_params);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**************************** WRITE DATA **********************************/
|
---|
68 | // write mesh data
|
---|
69 | if (m_params.exportMesh)
|
---|
70 | {
|
---|
71 | std::cout << "Writing mesh data to xml file...\n";
|
---|
72 | stat = m_pMesh->writeXML(m_params);
|
---|
73 | if (stat == MS::kSuccess)
|
---|
74 | std::cout << "OK\n";
|
---|
75 | else
|
---|
76 | {
|
---|
77 | std::cout << "Error writing mesh to XML\n";
|
---|
78 | exit();
|
---|
79 | return MS::kFailure;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | // write skeleton data
|
---|
83 | if (m_params.exportSkeleton)
|
---|
84 | {
|
---|
85 | std::cout << "Writing skeleton data to xml file...\n";
|
---|
86 | if (m_pMesh->getSkeleton())
|
---|
87 | {
|
---|
88 | stat = m_pMesh->getSkeleton()->writeXML(m_params);
|
---|
89 | if (stat == MS::kSuccess)
|
---|
90 | std::cout << "OK\n";
|
---|
91 | else
|
---|
92 | {
|
---|
93 | std::cout << "Error writing skeleton to XML\n";
|
---|
94 | exit();
|
---|
95 | return MS::kFailure;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | std::cout << "Mesh has no linked skeleton, creating an empty skeleton file\n";
|
---|
101 | }
|
---|
102 | }
|
---|
103 | // write materials data
|
---|
104 | if (m_params.exportMaterial)
|
---|
105 | {
|
---|
106 | std::cout << "Writing materials data...\n";
|
---|
107 | stat = m_pMaterialSet->writeXML(m_params);
|
---|
108 | if (stat == MS::kSuccess)
|
---|
109 | std::cout << "OK\n";
|
---|
110 | else
|
---|
111 | {
|
---|
112 | std::cout << "Error writing materials file\n";
|
---|
113 | exit();
|
---|
114 | return MS::kFailure;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | exit();
|
---|
119 |
|
---|
120 | return MS::kSuccess;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | // Method for iterating over nodes in a dependency graph from top to bottom, translating only meshes
|
---|
126 | MStatus OgreExporter::translateNode(MDagPath& dagPath)
|
---|
127 | {
|
---|
128 | if (m_params.exportAnimCurves)
|
---|
129 | {
|
---|
130 | MItDependencyGraph animIter( dagPath.node(),
|
---|
131 | MFn::kAnimCurve,
|
---|
132 | MItDependencyGraph::kUpstream,
|
---|
133 | MItDependencyGraph::kDepthFirst,
|
---|
134 | MItDependencyGraph::kNodeLevel,
|
---|
135 | &stat );
|
---|
136 |
|
---|
137 | if (stat)
|
---|
138 | {
|
---|
139 | for (; !animIter.isDone(); animIter.next())
|
---|
140 | {
|
---|
141 | MObject anim = animIter.thisNode(&stat);
|
---|
142 | MFnAnimCurve animFn(anim,&stat);
|
---|
143 | std::cout << "Found animation curve: " << animFn.name().asChar() << "\n";
|
---|
144 | std::cout << "Translating animation curve: " << animFn.name().asChar() << "...\n";
|
---|
145 | stat = writeAnim(animFn);
|
---|
146 | if (MS::kSuccess == stat)
|
---|
147 | std::cout << "OK\n";
|
---|
148 | else
|
---|
149 | {
|
---|
150 | std::cout << "Error, Aborting operation\n";
|
---|
151 | return MS::kFailure;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if (dagPath.hasFn(MFn::kMesh)&&(m_params.exportMesh||m_params.exportMaterial||m_params.exportSkeleton)
|
---|
157 | && (dagPath.childCount() == 0))
|
---|
158 | { // we have found a mesh shape node, it can't have any children, and it contains
|
---|
159 | // all the mesh geometry data
|
---|
160 | MDagPath meshDag = dagPath;
|
---|
161 | MFnMesh meshFn(meshDag);
|
---|
162 | if (!meshFn.isIntermediateObject())
|
---|
163 | {
|
---|
164 | std::cout << "Found mesh node: " << meshDag.fullPathName().asChar() << "\n";
|
---|
165 | std::cout << "Loading mesh node " << meshDag.fullPathName().asChar() << "...\n";
|
---|
166 | stat = m_pMesh->load(meshDag,m_params);
|
---|
167 | if (MS::kSuccess == stat)
|
---|
168 | std::cout << "OK\n";
|
---|
169 | else
|
---|
170 | {
|
---|
171 | std::cout << "Error, mesh skipped\n";
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 | else if (dagPath.hasFn(MFn::kCamera)&&(m_params.exportCameras) && (!dagPath.hasFn(MFn::kShape)))
|
---|
176 | { // we have found a camera shape node, it can't have any children, and it contains
|
---|
177 | // all information about the camera
|
---|
178 | MFnCamera cameraFn(dagPath);
|
---|
179 | if (!cameraFn.isIntermediateObject())
|
---|
180 | {
|
---|
181 | std::cout << "Found camera node: "<< dagPath.fullPathName().asChar() << "\n";
|
---|
182 | std::cout << "Translating camera node: "<< dagPath.fullPathName().asChar() << "...\n";
|
---|
183 | stat = writeCamera(cameraFn);
|
---|
184 | if (MS::kSuccess == stat)
|
---|
185 | std::cout << "OK\n";
|
---|
186 | else
|
---|
187 | {
|
---|
188 | std::cout << "Error, Aborting operation\n";
|
---|
189 | return MS::kFailure;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | else if ( ( dagPath.apiType() == MFn::kParticle ) && m_params.exportParticles )
|
---|
194 | { // we have found a set of particles
|
---|
195 | MFnDagNode fnNode(dagPath);
|
---|
196 | if (!fnNode.isIntermediateObject())
|
---|
197 | {
|
---|
198 | std::cout << "Found particles node: "<< dagPath.fullPathName().asChar() << "\n";
|
---|
199 | std::cout << "Translating particles node: "<< dagPath.fullPathName().asChar() << "...\n";
|
---|
200 | Particles particles;
|
---|
201 | particles.load(dagPath,m_params);
|
---|
202 | stat = particles.writeToXML(m_params);
|
---|
203 | if (MS::kSuccess == stat)
|
---|
204 | std::cout << "OK\n";
|
---|
205 | else
|
---|
206 | {
|
---|
207 | std::cout << "Error, Aborting operation\n";
|
---|
208 | return MS::kFailure;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | // look for meshes and cameras within the node's children
|
---|
213 | for (unsigned int i=0; i<dagPath.childCount(); i++)
|
---|
214 | {
|
---|
215 | MObject child = dagPath.child(i);
|
---|
216 | MDagPath childPath;
|
---|
217 | stat = MDagPath::getAPathTo(child,childPath);
|
---|
218 | if (MS::kSuccess != stat)
|
---|
219 | {
|
---|
220 | std::cout << "Error retrieving path to child " << i << " of: " << dagPath.fullPathName().asChar();
|
---|
221 | return MS::kFailure;
|
---|
222 | }
|
---|
223 | stat = translateNode(childPath);
|
---|
224 | if (MS::kSuccess != stat)
|
---|
225 | return MS::kFailure;
|
---|
226 | }
|
---|
227 | return MS::kSuccess;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 |
|
---|
232 | /********************************************************************************************************
|
---|
233 | * Method to translate a single animation curve *
|
---|
234 | ********************************************************************************************************/
|
---|
235 | MStatus OgreExporter::writeAnim(MFnAnimCurve& anim)
|
---|
236 | {
|
---|
237 | m_params.outAnim << "anim " << anim.name().asChar() << "\n";
|
---|
238 | m_params.outAnim <<"{\n";
|
---|
239 | m_params.outAnim << "\t//Time / Value\n";
|
---|
240 |
|
---|
241 | for (unsigned int i=0; i<anim.numKeys(); i++)
|
---|
242 | m_params.outAnim << "\t" << anim.time(i).as(MTime::kSeconds) << "\t" << anim.value(i) << "\n";
|
---|
243 |
|
---|
244 | m_params.outAnim << "}\n\n";
|
---|
245 | return MS::kSuccess;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 |
|
---|
250 | /********************************************************************************************************
|
---|
251 | * Method to translate a single camera *
|
---|
252 | ********************************************************************************************************/
|
---|
253 | MStatus OgreExporter::writeCamera(MFnCamera& camera)
|
---|
254 | {
|
---|
255 | MPlug plug;
|
---|
256 | MPlugArray srcplugarray;
|
---|
257 | double dist;
|
---|
258 | MAngle angle;
|
---|
259 | MFnTransform* cameraTransform = NULL;
|
---|
260 | MFnAnimCurve* animCurve = NULL;
|
---|
261 | // get camera transform
|
---|
262 | for (unsigned int i=0; i<camera.parentCount(); i++)
|
---|
263 | {
|
---|
264 | if (camera.parent(i).hasFn(MFn::kTransform))
|
---|
265 | {
|
---|
266 | cameraTransform = new MFnTransform(camera.parent(i));
|
---|
267 | continue;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | // start camera description
|
---|
271 | m_params.outCameras << "camera " << cameraTransform->partialPathName().asChar() << "\n";
|
---|
272 | m_params.outCameras << "{\n";
|
---|
273 |
|
---|
274 | //write camera type
|
---|
275 | m_params.outCameras << "\ttype ";
|
---|
276 | if (camera.isOrtho())
|
---|
277 | m_params.outCameras << "ortho\n";
|
---|
278 | else
|
---|
279 | m_params.outCameras << "persp\n";
|
---|
280 |
|
---|
281 | // write translation data
|
---|
282 | m_params.outCameras << "\ttranslation\n";
|
---|
283 | m_params.outCameras << "\t{\n";
|
---|
284 | m_params.outCameras << "\t\tx ";
|
---|
285 | plug = cameraTransform->findPlug("translateX");
|
---|
286 | if (plug.isConnected() && m_params.exportCamerasAnim)
|
---|
287 | {
|
---|
288 | plug.connectedTo(srcplugarray,true,false,&stat);
|
---|
289 | for (i=0; i < srcplugarray.length(); i++)
|
---|
290 | {
|
---|
291 | if (srcplugarray[i].node().hasFn(MFn::kAnimCurve))
|
---|
292 | {
|
---|
293 | if (animCurve)
|
---|
294 | delete animCurve;
|
---|
295 | animCurve = new MFnAnimCurve(srcplugarray[i].node());
|
---|
296 | continue;
|
---|
297 | }
|
---|
298 | else if (i == srcplugarray.length()-1)
|
---|
299 | {
|
---|
300 | std::cout << "Invalid link to translateX attribute\n";
|
---|
301 | return MS::kFailure;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | m_params.outCameras << "anim " << animCurve->name().asChar() << "\n";
|
---|
305 | }
|
---|
306 | else
|
---|
307 | {
|
---|
308 | plug.getValue(dist);
|
---|
309 | m_params.outCameras << "= " << dist << "\n";
|
---|
310 | }
|
---|
311 | m_params.outCameras << "\t\ty ";
|
---|
312 | plug = cameraTransform->findPlug("translateY");
|
---|
313 | if (plug.isConnected() && m_params.exportCamerasAnim)
|
---|
314 | {
|
---|
315 | plug.connectedTo(srcplugarray,true,false,&stat);
|
---|
316 | for (i=0; i< srcplugarray.length(); i++)
|
---|
317 | {
|
---|
318 | if (srcplugarray[i].node().hasFn(MFn::kAnimCurve))
|
---|
319 | {
|
---|
320 | if (animCurve)
|
---|
321 | delete animCurve;
|
---|
322 | animCurve = new MFnAnimCurve(srcplugarray[i].node());
|
---|
323 | continue;
|
---|
324 | }
|
---|
325 | else if (i == srcplugarray.length()-1)
|
---|
326 | {
|
---|
327 | std::cout << "Invalid link to translateY attribute\n";
|
---|
328 | return MS::kFailure;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | m_params.outCameras << "anim " << animCurve->name().asChar() << "\n";
|
---|
332 | }
|
---|
333 | else
|
---|
334 | {
|
---|
335 | plug.getValue(dist);
|
---|
336 | m_params.outCameras << "= " << dist << "\n";
|
---|
337 | }
|
---|
338 | m_params.outCameras << "\t\tz ";
|
---|
339 | plug = cameraTransform->findPlug("translateZ");
|
---|
340 | if (plug.isConnected() && m_params.exportCamerasAnim)
|
---|
341 | {
|
---|
342 | plug.connectedTo(srcplugarray,true,false,&stat);
|
---|
343 | for (i=0; i< srcplugarray.length(); i++)
|
---|
344 | {
|
---|
345 | if (srcplugarray[i].node().hasFn(MFn::kAnimCurve))
|
---|
346 | {
|
---|
347 | if (animCurve)
|
---|
348 | delete animCurve;
|
---|
349 | animCurve = new MFnAnimCurve(srcplugarray[i].node());
|
---|
350 | continue;
|
---|
351 | }
|
---|
352 | else if (i == srcplugarray.length()-1)
|
---|
353 | {
|
---|
354 | std::cout << "Invalid link to translateZ attribute\n";
|
---|
355 | return MS::kFailure;
|
---|
356 | }
|
---|
357 | }
|
---|
358 | m_params.outCameras << "anim " << animCurve->name().asChar() << "\n";
|
---|
359 | }
|
---|
360 | else
|
---|
361 | {
|
---|
362 | plug.getValue(dist);
|
---|
363 | m_params.outCameras << "= " << dist << "\n";
|
---|
364 | }
|
---|
365 | m_params.outCameras << "\t}\n";
|
---|
366 |
|
---|
367 | // write rotation data
|
---|
368 | m_params.outCameras << "\trotation\n";
|
---|
369 | m_params.outCameras << "\t{\n";
|
---|
370 | m_params.outCameras << "\t\tx ";
|
---|
371 | plug = cameraTransform->findPlug("rotateX");
|
---|
372 | if (plug.isConnected() && m_params.exportCamerasAnim)
|
---|
373 | {
|
---|
374 | plug.connectedTo(srcplugarray,true,false,&stat);
|
---|
375 | for (i=0; i< srcplugarray.length(); i++)
|
---|
376 | {
|
---|
377 | if (srcplugarray[i].node().hasFn(MFn::kAnimCurve))
|
---|
378 | {
|
---|
379 | if (animCurve)
|
---|
380 | delete animCurve;
|
---|
381 | animCurve = new MFnAnimCurve(srcplugarray[i].node());
|
---|
382 | continue;
|
---|
383 | }
|
---|
384 | else if (i == srcplugarray.length()-1)
|
---|
385 | {
|
---|
386 | std::cout << "Invalid link to rotateX attribute\n";
|
---|
387 | return MS::kFailure;
|
---|
388 | }
|
---|
389 | }
|
---|
390 | m_params.outCameras << "anim " << animCurve->name().asChar() << "\n";
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | plug.getValue(angle);
|
---|
395 | m_params.outCameras << "= " << angle.asDegrees() << "\n";
|
---|
396 | }
|
---|
397 | m_params.outCameras << "\t\ty ";
|
---|
398 | plug = cameraTransform->findPlug("rotateY");
|
---|
399 | if (plug.isConnected() && m_params.exportCamerasAnim)
|
---|
400 | {
|
---|
401 | plug.connectedTo(srcplugarray,true,false,&stat);
|
---|
402 | for (i=0; i< srcplugarray.length(); i++)
|
---|
403 | {
|
---|
404 | if (srcplugarray[i].node().hasFn(MFn::kAnimCurve))
|
---|
405 | {
|
---|
406 | if (animCurve)
|
---|
407 | delete animCurve;
|
---|
408 | animCurve = new MFnAnimCurve(srcplugarray[i].node());
|
---|
409 | continue;
|
---|
410 | }
|
---|
411 | else if (i == srcplugarray.length()-1)
|
---|
412 | {
|
---|
413 | std::cout << "Invalid link to rotateY attribute\n";
|
---|
414 | return MS::kFailure;
|
---|
415 | }
|
---|
416 | }
|
---|
417 | m_params.outCameras << "anim " << animCurve->name().asChar() << "\n";
|
---|
418 | }
|
---|
419 | else
|
---|
420 | {
|
---|
421 | plug.getValue(angle);
|
---|
422 | m_params.outCameras << "= " << angle.asDegrees() << "\n";
|
---|
423 | }
|
---|
424 | m_params.outCameras << "\t\tz ";
|
---|
425 | plug = cameraTransform->findPlug("rotateZ");
|
---|
426 | if (plug.isConnected() && m_params.exportCamerasAnim)
|
---|
427 | {
|
---|
428 | plug.connectedTo(srcplugarray,true,false,&stat);
|
---|
429 | for (i=0; i< srcplugarray.length(); i++)
|
---|
430 | {
|
---|
431 | if (srcplugarray[i].node().hasFn(MFn::kAnimCurve))
|
---|
432 | {
|
---|
433 | if (animCurve)
|
---|
434 | delete animCurve;
|
---|
435 | animCurve = new MFnAnimCurve(srcplugarray[i].node());
|
---|
436 | continue;
|
---|
437 | }
|
---|
438 | else if (i == srcplugarray.length()-1)
|
---|
439 | {
|
---|
440 | std::cout << "Invalid link to rotateZ attribute\n";
|
---|
441 | return MS::kFailure;
|
---|
442 | }
|
---|
443 | }
|
---|
444 | m_params.outCameras << "anim " << animCurve->name().asChar() << "\n";
|
---|
445 | }
|
---|
446 | else
|
---|
447 | {
|
---|
448 | plug.getValue(angle);
|
---|
449 | m_params.outCameras << "= " << angle.asDegrees() << "\n";
|
---|
450 | }
|
---|
451 | m_params.outCameras << "\t}\n";
|
---|
452 |
|
---|
453 | // end camera description
|
---|
454 | m_params.outCameras << "}\n\n";
|
---|
455 | if (cameraTransform != NULL)
|
---|
456 | delete cameraTransform;
|
---|
457 | if (animCurve != NULL)
|
---|
458 | delete animCurve;
|
---|
459 | return MS::kSuccess;
|
---|
460 | }
|
---|
461 |
|
---|
462 | } // end namespace
|
---|