This module contains the MAReader class which can be used as a base class for reading Maya ASCII (*.ma) files. The class reads the file and invokes callback methods with the corresponding data in the file. Derived classes have to implement those callback methods and process the data as appropriate.
The module also contains a couple of helper classes that may be used by a derived reader class to facilitate the processing of the data.
Split a Maya DAG path into its components. The path is given as a string that may have the form <namespace>:<path> where <path> is a sequence of strings separated by ‘|’. The return value is a 2-tuple (namespace, names) where namespace is None if the path did not contain a ‘:’ character. names is a list of individual path names.
Remove surrounding quotes if there are any. The function returns the string s without surrounding quotes (i.e. "foo" -> foo). If there are no quotes the string is returned unchanged.
The MAReader class reads Maya ASCII files and calls appropriate methods which have to be implemented in a derived class. The content of the file is actually a subset of the Maya Embedded Language (MEL) which is the scripting language implemented inside Maya. The MAReader parses the file, breaks down the content of the file in commands and their arguments and options (expressions are not evaluated). Each MEL command will then trigger a callback method that has to execute the command. These callback methods have to be implemented in a derived class.
There are 13 MEL commands that can appear in a Maya ASCII file :
Each command has a number of arguments and can also take options. The callback methods receive the arguments as regular arguments to the method and the options as an additional argument opts which is a dictionary containing the options that were specified in the file. The key is the long name of the option (without leading dash) and the value is a list of strings containing the option values. The number of values and how they have to be interpreted depend on the actual option.
Creates an instance of the reader.
The file name (if it could be obtained). This may be used for generating warning or error messages.
The current line number. This may be used for generating warning or error messages.
Aborts reading the current file. This method can be called in handler methods to abort reading the file.
Read the content of a file. f is either a file like object that can be used to read the content of the file or the name of a file.
Callback method that is called before the file is read.
Callback method that is called after the file was read.
Reference an external file.
Specify a requirement that is needed to load the file properly. product is a string containing the required software component and version is a string containing the required version of that component.
Specifies information about the file. keyword and value are both strings.
Specify the units (linear, angular, time) used in the file.
Create a new node. nodetype is a string specifying the type of node that is to be created. The new node will automatically be selected (i.e. subsequent setAttr commands refer to this node).
Set a node attribute. attr is a string containing the attribute to be set. vals is a list of values. The number of elements and the type of each element depends on the attribute.
Add a new attribute to the node.
Connect two attributes. srcattr is a string specifiying the attribute that serves as a source and dstattr is the name of the attribute that will receive the value.
Break the attribute connection between two attributes.
Set the parent of one or more nodes. objects is a list of node names and parent the name of the parent.
Select a node from a referenced file. objects is a list of strings containing the node names.
Lock/unlock a node. objects is a list of strings containing the node names (the list may be empty).
args is a list of strings containing the command line arguments. opts is the options dict.
This class is derived from the MAReader class and implements the callback methods which build the graph in memory using the helper classes (Node, etc.). You may derive from this class and only implement the end() callback to process the graph. All nodes of the graph can be found in the nodelist attribute.
Creates an instance of the reader.
This list will contain all Node objects that have been created. The order is the same as they have been encountered in the file.
This class does not implement the actual functionality of a particular Maya node, it just tracks attribute changes and connections which can later be retrieved once the entire file was read. So this class can be used for all Maya nodes in a file.
This is a string containing the type of the node (this is the value that was passed to the constructor).
This is the option dictionary that was passed to the constructor (i.e. that is used to create the node).
Return the name of the node. If no node name was specified during the creation of the object, the dummy name ‘MayaNode’ is returned.
Return the name of the parent node or None if no parent was specified.
parent is either None to remove the node from its parent or it is another Node object that will be the new parent.
attr, vals and opts are the arguments of the onSetAttr() callback of the MAReader class. The Python value of an attribute can be obtained by calling getAttrValue(). The final Python value can be retrieved with the getAttrValue() method.
Get the Python value of an attribute. lname is the long name, sname the short name. type is the required type and n the required number of elements (see the Attribute.getValue() method for more information on the type). type and n may be None. The return value is either a normal Python type (int, float, sequence) or a MultiAttrStorage object in cases where the setAttr command contained the index operator. When no attribute with the given long or short name could be found the provided default value is returned.
Specify an incoming connection. nodename is the name of a node and attrname the full attribute name.
Specify an outgoing connection. node is a Node object, nodename the name of the node and attrname the full attribute name.
Return the node and attribute that serves as input for the local attribute with long name localattr_long and short name localattr_short. The return value is a 2-tuple (nodename, attrname) that specifies the input connection for the local attribute. (None, None) is returned if there is no connection.
Return the nodes and attributes that the specified local attribute connects to. localattr_long is the long name of the local attribute and localattr_short its short name. The return value is a list of 3-tuples (node, nodename, attrname) that specify the output connections for the local attribute. The tuple contains the values that were previously added with addOutConnection().
Check if a local attribute is connected to a particular type of node. Returns a tuple (node, attrname) where node is the Node object of the destination node and attrname the name of the destination attribute. If there is no connection with a node of type dstnodetype, the method returns (None, None).
If the attribute is connected to more than one node with the given type or to several attributes of the same node then only the first connection encountered is returned.
The Attribute class can be used to convert the value of an attribute (as specified by the setAttr MEL command) into an appropriate Python value.
An Attribute object is initialized with the arguments that were passed to the onSetAttr() callback of the reader class. The value can be retrieved using the getValue() method. Whenever you have prior knowledge of the node you are currently processing you should pass the expected type of the attribute to the getValue() method to prevent the method from having to guess the type in case it is not specified in the setAttr call.
attr, vals and opts are the arguments of the onSetAttr() callback of the MAReader class.
Return the base name of the attribute. This is the first part of the attribute name (and may actually refer to another attribute).
".t" -> "t" ".ed[0:11]" -> "ed" ".uvst.uvsn" -> "uvst"
Return the full attribute specifier.
Return the value of the attribute as an appropriate Python value. type is a string containing the required type of the value. If None is passed, the method tries to retrieve the value from the attribute itself. If it fails, an exception is thrown. The following table lists the valid type strings and their corresponding Python type:
|"short3"||(int, int, int)|
|"long3"||(int, int, int)|
|"float3"||(float, float, float)|
|"double3"||(float, float, float)|
|"polyFaces"||PolyFace (see PolyFace class)|
|"nurbsSurface"||NurbsSurface (see NurbsSurface class)|
|"nurbsCurve"||NurbsCurve (see NurbsCurve class)|
The argument n specifies how many values are expected. An exception is thrown if the number of values that were set by the setAttr call doesn’t match the required number. If None is passed, an arbitrary number of values is allowed. The value of n also influences the return type. If the value is 1 the method will return one of the types in the above table, otherwise it will return a list of the above types.
This class stores the data of a polygonal face. PolyFace objects are returned when the value of a polyFaces attribute is requested.
The class has the following data members:
This is a list of integers containing the edge indices of the edges making up the face. If an index is negative the edge has to be reversed (the edge index then is -i-1).
This is a list of holes. Each hole is described by a list of integers containing the edge indices of the edges making up the hole in the face. If an index is negative the edge has to be reversed (the edge index then is -i-1).
This is a list of texture coordinate ids of the face. This data type is obsolete as of Maya version 3.0. It is replaced by “mu”.
This is a list of texture coordinate ids of the hole. This data type is obsolete as of Maya version 3.0. It is replaced by “mu”.
For each loop (i.e. outer loop or hole) this list contains a list of 2-tuples (uvset, ids) where uvset is the index of the UV set and ids the indices of the texture coordinates.
For each loop (outer loop or hole) this list contains a list of color index values.
This class stores the data of a NURBS surface. NurbsSurface objects are returned when the value of a nurbsSurface attribute is requested.
The class has the following data members:
Degree in u direction.
Degree in v direction.
Form attribute for the u direction. The attribute can have one of the following values:
Form attribute for the v direction (see above).
This attribute is True if the surface contains a rational component. In this case, the control vertices are given as 4-tuples, otherwise as 3-tuples.
This is a list of floats containing the knot values for the u direction.
This is a list of floats containing the knot values for the v direction.
A list of control vertices. Each vertex is given either as a 3-tuple or 4-tuple of floats.
This class stores the data of a NURBS curve. NurbsCurve objects are returned when the value of a nurbsCurve attribute is requested.
The class has the following data members:
The degree of the curve.
The number of spans.
Form attribute. The attribute can have one of the following values:
This attribute is True if the curve contains a rational component. In this case, the control vertices have one additional value.
The dimension of the curve (2 or 3).
This is a list of floats containing the knot values.
A list of control vertices. Each vertex is a tuple of 2, 3 or 4 floats (the actual number depends on the dimension and isrational settings).
|||Actually, there could appear any MEL command, but at least Maya will only export files containing the above commands.|