Python´s xml.dom.minidom and Maya

Python´s xml.dom.minidom and Maya

5inch
Contributor Contributor
1,041 Views
1 Reply
Message 1 of 2

Python´s xml.dom.minidom and Maya

5inch
Contributor
Contributor
Hi Folks,

has anyone of you experience with
Python´s xml.dom.minidom and Maya.

I want to export some Maya Attributes (Int,String,Float)
via Python in a XML-File.
I didn´t find any good howTo/Tutorial on the Web which explains me all the syntax in a
Dummie-fashion-way.

Thanks for any Links/help,

Cheers, 5inch
0 Likes
1,042 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
I am not in front of Maya so there may be some minor syntax errors, but the code below should give you some guidance...

hope it helps

import xml.dom.minidom as xml
import pymel.core as pm

## Create the xml document node which gives
## us access to everything
xml_doc = xml.Document()

## Create the first ( root ) xml node. The argument is
## the node name. We then need to make the node a
## child of the document
xml_root = xml_doc.createElement( "Object" )
xml_doc.appendChild( xml_root )


## Cycle through all the attributes of the selected
## object
for obj in pm.selected() :

## Create a node for each selected object
xml_obj = xml_doc.createElement( "Object")
xml_obj.setAttribute( 'name', obj.name() )

## Now cycle the attributes for this object
for attr in obj.listAttr() :

## Create an xml nde to represent the attribute
xml_attr = xml_doc.createNode( 'Attribute' )
xml_attr.setAttribute( 'name', attr.name() )
xml_attr.setAttribute( 'type', attr.type() )

## we'll store the value as a text node
text_xml = xml_doc.createTextNode( str(attr.get()))

## set the node parenting
xml_attr.appendChild( text_xml )
obj.appendChild( xml_attr )

## set the object node as a child of the root
xml_root.appendChild( xml_obj )
f = open( r"c:\test.xml" )
f.write( xml_doc.toprettyxml() )
f.close()
0 Likes