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