Creating attributes using attribute patterns

 
 
 

You can create dynamic or extension attributes using attribute patterns. An attribute pattern is a description of the dynamic or extension attributes that can be added to any specific node, or node type. This feature removes the need to create each attribute using individual addAttr or addExtension commands.

This feature is supported by the pyJsonAttrPatternFactory.py plug-in.

Attribute pattern files

To use this feature, first create a pattern file. The pattern file lists the attributes that are to be added to a node or node type, and can be in any format that is defined by your plug-in. The Maya plug-in pyJsonAttrPatternFactory.py supports the .json format. Optionally, you can also include a schema file that describes the syntax of the pattern file.

Patterns are simply descriptions of attributes to be copied onto nodes or node types via the applyAttrPattern command.

Some pattern types are defined by a plug-in and are not deleted unless the plug-in is unloaded.

A pattern file can contain more than one pattern and each pattern can contain more than one attribute. When you apply a pattern, all of its attributes are applied and not just a subset. See attrPatternSchema.json for a sample schema file and sampleAttrPatterns.json for a sample pattern file in the <maya installation directory>/devkit/plug-ins/scripted directory.

Attribute pattern commands

Use the createAttrPatterns command to create the set of patterns listed in the pattern file. Then, use the applyAttrPattern command to attach the attributes listed in any one pattern in the pattern file to a node or a node type. The applyAttrPattern command allows you to determine whether to create dynamic or extension attributes. If you list a node name after the command, dynamic attributes are created. If you use the -nt flag to list a node type, extension attributes are created.

Use the listAttrPatterns command to list the available pattern instances and pattern types.

After applying attributes to a node using a pattern, the attributes are no longer associated with the pattern and can only be removed using the deleteAttr or deleteExtension commands and not the deleteAttrPattern command.

Workflow outline for using attribute patterns to create dynamic or extension attributes

  1. Create a pattern file in json format that lists the attributes to be added to your node or node type.
    TipOnly set the value of flags that do not take on the default value. Any flag that is not explicitly set assumes its default value.
  2. Add the location <maya directory>/devkit/plug-ins/scripted/ to your PYTHONPATH environment variable.
  3. Select Window > Settings/Preferences > Plug-in Manager and load the pyJsonAttrPatternFactory.py plug-in.
    NoteAlternatively, you can also use the loadPlugin pyJsonAttrPatternFactory.py; command.
  4. Use the createAttrPatterns command to create the set of patterns in your pattern file.
    NoteFor simpler attributes that do not require a pattern file, you can use the createAttrPatterns command with its -patternDefinition flag and a hardcoded string containing the pattern description to create the pattern.
  5. Use the applyAttrPattern command to attach the attributes outlined in the pattern to a specific node (dynamic attributes) or node type (extension attributes).
NoteYou can also use the MPxAttributePatternFactory class to define new attribute pattern types. The factory creates MAttributePattern that contains the description of the new attributes.

Example

The following is a simple sample application of this feature:

  1. Create a pattern file as follows and save it as your .json file:
    [
               {
                  “name” : “samplePattern”,
                  “attribute” : [
                        {
                                  “name” : “sampleAttr”,
                                  “defaultValue” : 0.0,
                                  “attributeType”: “float”
                        }
                                ]
               }
            ]
    
  2. Load the pyJsonAttrPatternFactory.py plug-in via the Plug-in Manager or by using loadPlugin in the command line:
    loadPlugin pyJsonAttrPatternFactory.py;
  3. Create samplePattern as follows:
    createAttrPatterns –pt “json” –pf “yourfile.json”;
    NoteNow if you enter the listAttrPatterns; command, samplePattern is listed.
  4. Create a node and list its attributes:
    createNode addMatrix;
    // Result: addMatrix1 //
    listAttrs;
    // Result: [the addMatrix attributes] //
    
  5. Attach the pattern (in other words, its attributes) from the pattern file to the node using the applyAttrPattern command:
    applyAttrPattern –pn “samplePattern” “addMatrix1”;

    The dynamic attribute sampleAttr is added.

  6. Verify that the dynamic attribute is added by using the listAttrs command:
    listAttrs;
    // Result: [the addMatrix attributes] sampleAttr //

Creative Commons License Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License