Some example uses:
- Construct our game level on maya and import it to the game engine with the xml file.
- Copying animation poses.
- Copying light settings.
- ...
Write the XML file:
from xml.dom.minidom import Document
import maya.cmds as cmds
doc = Document()
root_node = doc.createElement("scene")
doc.appendChild(root_node)
# Selection:
# grab all selected objects
#selection = cmds.ls(sl=True)
# grab all visible objects, which type is transform
selection = cmds.ls(type="transform", v=True )
for object in selection:
# create object element
object_node = doc.createElement("object")
root_node.appendChild(object_node)
# BEWARE: after freeze transformations the translate is a bit tricky
#object_translation = cmds.getAttr(object + ".translate")[0]
object_translation = cmds.xform(object, query = True, worldSpace = True, rotatePivot = True)
# set attributes
object_node.setAttribute("name", str(object))
object_node.setAttribute("translateX", str(object_translation[0]))
object_node.setAttribute("translateY", str(object_translation[1]))
object_node.setAttribute("translateZ", str(object_translation[2]))
xml_file = open("C:/Temp/test.xml", "w")
xml_file.write(doc.toprettyxml())
xml_file.close()
print
print doc.toprettyxml()
Reading the XML file:
from xml.dom.minidom import parse
dom = parse("C:/Temp/test.xml")
# visit every object node
for node in dom.getElementsByTagName('object'):
# method 1: using keys
attrs = node.attributes.keys()
for a in attrs:
pair = node.attributes[a]
print (str(pair.name) + " = " + str(pair.value))
print
#method 2: by attribute name
"""
print str(node.getAttribute("name"))
print str(node.getAttribute("translateX"))
print str(node.getAttribute("translateY"))
print str(node.getAttribute("translateZ"))
print
"""
Based and inspired from Luiz Kruel's script