Apr 16 2013

XML Parssing in maxscript

A friend at work asked me for a quick example of how to parse an xml file in maxscript…
to my knowledge the only simple way to do this is by tapping into .net
.net has a nice, easy to use xml parser to explore the contents of an xml file..

his a snippet to get you started..

in this example we are looking at a maya point cache file which is loaded via an xml file..
we are looking to pull the cache type (single file, or file per frame )
we also want to get the number of frames..
 xmlFile = @"C:\_cache\temp\XML_Test\Sphere001.xml" --your xml file

 dotnet.LoadAssembly "system.xml.dll" --source the xml parser into .net
 xmlDoc = dotNetObject "System.Xml.XmlDocument" --create your xml parser object

 xmlDoc.Load xmlFile --load the xml onto the parser object

 header =  xmlDoc.item["Autodesk_Cache_File"] --this is how you pull an element section by name

--elments can have children elements.. in this case we are quarying the value of the attribute "Type" inside the "cacheType" element which is of the "Autodesk_Cache_File"..
cacheType = (header.item["cacheType"].GetAttribute("Type"))
timePerFrame = (header.item["cacheTimePerFrame"].GetAttribute("TimePerFrame"))

print cacheType
print timePerFrame

look into using the show , and showmethods function in maxscript to exploer other functions and attributes available to the element objects.
this will allow you to do more complex parsing..
until next time!

Leave a Reply