xml_lib.mel v1.0

J. Adrian Herbez


Contents:

What it is

xml_lib.mel is very different from most MEL scripts, in that it is meant to be used as a libary rather than being called directly. By including xml_lib in your own scripts, you will be able to load XML data into a Maya scene and utilize it in any way you choose.

Installing xml_lib.mel

Installing xml_lib is no different that installing any other MEL script- the differences lie with how you use it.

To install xml_lib.mel, first download the script from
here.

Once you've done that, unzip the file and place xml_lib.mel in the scripts directory, on Windows machines, that will mean placing it in:

 My Documents/maya/version number/scripts


After having done that, be sure to use the rehash command to make Maya take notice

Using xml_lib in your scripts

In order to use the procedures defined in xml_lib in your own scripts, you'll need to make sure that they have been added to the global namespace. That is easy to accomplish by placing the following at the start of your script:


 source xml_lib.mel


Once you've done that, you'll have access to all of the procedures defined in the library.

How it works

The xml_lib script loads data into a scene by creating a number of transform nodes. The data (both attributes within tags and raw text) contained in the XML file is stored in various custom attributes residing on the various nodes. The transform nodes are basically being used as a way of getting MEL to work in a more object-orientated fashion. For a more thourough description of that approach, have a look at my article on faking object-orientated programming in MEL, available
here

The nodes are arranged in a hierarchy to represent the nesting order of tags in the document. Here's an example of a simple XML file and the resulting heirarchy in Maya:



All of the created nodes have a "type" attribute added to them. In this case, there is one with a type of XMLfile, two of type "object", and two of type" subname. "Foo" and "Hello", since they are attributes within the object tag, are stored in attribute of type name (the same as the name of the attribute listed in the tag). "Bar" and "World", on the other hand, are both raw text within a subname tag. As a result, they are stored into the hierarchy in a "data" attribute on the two subname nodes.

Procedure Reference

string loadXML(string $filename)

This procedure takes the name of an XML file to open and loads it into your scene by creating a hierarchy of transform nodes with various attributes added to hold data. It returns the name fo the top-level node in the generated heirarchy.

string getData(string $nodeName)

This will return the contents of the data attribute on the specified node. Note that a node will only have a data attribute if it was wrapped around raw text. If you try to use this function with a node that doesn't have a data attribute, a warning message will be displayed.

string[] getAtts(string $nodeName)

This will return a string array of all the attributes on the specified object.

string[] getByType(string $topNode, string $type)

This function takes both the name of a top-level XML file node, and a particular type (such as either "object" or "subname" in the above example. It then returns an array of all decendants of the top node that are of the specified type.

Other functions

Since the information is stored into a heirarchy of transform nodes, and the attributes and data are stored as custom attributes, there are also a number of standard MEL commands you're likely to find useful when working with the generated heirarchy:

listRelatives

Offers a number of options for grabbing nodes related to a given node. You can get the parent, the children, or use the -ad flag to get all descendants.

listAttr

The list attributes function will allow you to find out what attributes are available on a given node. If used with the -ud (user defined) flag, you'll get only custom attributes.

getAttr

Will get the value of a specified attribute. You might want to wrap it in a catch statement, in case the specified object doesn't contain the specified attribute.

Examples

Loading an XML file with xml_lib

Here's a small example of using xml_lib to read in an XML file

source xml_lib.mel;

global proc testGUI()
{
	string $winName = `window -title "XML GUI test" -rtf 1`;
	columnLayout;

	button -label "Grab File" -command "grabXML()";

	showWindow $winName;
}

global proc grabXML()
{
	string $temp = `fileDialog -dm "*.xml"`;

	string $theFile = loadXML($temp);
	print ($temp + " loaded, stored into " + $theFile + "\n");

}