/* xml_lib.mel J. Adrian Herbez This script is meant to be used as a library, rather than a standalone script. To use the script, place the following line at the top of your MEL script: source xml_lib.mel; That will give you access to all of the functions defined herein. The script will parse XML data and store it into a scene in the form of empty transform nodes, parented to preserve the hierarchy in the file. */ global string $currParent; global proc string getData(string $node) { string $output; if (catch($output = `getAttr ($node+".data")`)) { warning("Specified node has no data - try requesting attributes, or examining the children nodes"); } return $output; } // this procedure returns an array of all the attributes of the given node // at a minimum, there should always be a type attribute holding the type of node // each attribute is listed as attName=value // break it apart with tokenize, or just use MEL's getAttr directly global proc string[] getAtts(string $node) { string $atts[] = `listAttr -ud $node`; string $list[]; string $temp; for ($i in $atts) { $temp = $i; $temp += "="; $temp += `getAttr ($node+"."+$i)`; $list[size($list)] = $temp; } return $list; } // returns a list of all nodes of a given type in the specified XML file global proc string[] getByType(string $topNode, string $type) { string $list[]; string $all[] = `listRelatives -ad $topNode`; string $temp; for ($i in $all) { $temp = `getAttr ($i+".type")`; if ($temp == $type) { $list[size($list)] = $i; } } return $list; } // parses the specified XML file global proc string loadXML(string $fileName) { global string $currParent; int $fileID = `fopen $fileName "r"`; string $currTag[]; int $tagI = 0; string $line = `fgetline $fileID`; string $char; string $tag; string $close; int $start = 1; string $content = ""; string $topNode = makeNode("XML file",1); $currParent = $topNode; string $newNode; while ($line != "") { for ($i=1;$i"` != 0) { $tag += $char; $i++; $char = `substring $line $i $i`; } if ($start) { // make a new node makeNode($tag,0); } else { // jump up a level closeTag(); } } else { // not in a tag- must be actual data $content += $char; $i++; $char = `substring $line $i $i`; while ((`strcmp $char "<"` != 0) && ($i < size($line))) { $content += $char; $i++; $char = `substring $line $i $i`; } $i--; } } $line = `fgetline $fileID`; } fclose $fileID; select -r $topNode; print ("\n"+$content+"\n"); return $topNode; } ///////////////////////////////////////////////////////////////////////////////////////// /* Below are a number of helper procedures used by the above functions. The procedures below aren't meant to be called directly. */ // creates a node, sets its attributes, and parents it to the appropriate node global proc string makeNode(string $type,int $root) { $type = substituteAllString($type,"?",""); global string $currParent; string $buffer[]; tokenize $type " " $buffer; if (size($buffer) > 1) { $type = $buffer[0]; } int $addOn = 1; string $tempName = $type + $addOn; while (`objExists $tempName`) { $addOn++; $tempName = $type + $addOn; } $name = `group -em `; if (!$root) { string $temp[] = `parent $name $currParent`; $name = $temp[0]; } $name = `rename $name $tempName`; $currParent = $name; addAttr -ln type -dt "string" $name; setAttr -type "string" ($name+".type") $type; for ($i=1;$i