Extension attributes are really helpful when plugin developers want to add more attributes to a plugin without modifying the existing plugin code. In Maya, there are many ways to add/remove the extension attributes like MEL way, Python way and C++ API way as well.
MEL Way to add/remove extension attributes:
- To add extension attribute, use addExtension command
- To remove extension attribute, use deleteExtension command
- To list only extension attribute of an object use "listAttr -ex object"
Example: // Create unknown Node createNode "unknown"; // Add extension attribute to node type "unknown" addExtension -nt "unknown" -shortName "te" -longName "testExtAttr" -at double; // List all the attributes of unknown1 object; listAttr unknown1; // List only extension attribute of "unknown" node listAttr -ex unknown1; // Delete the extension attribute of node type "unknown" deleteExtension -nt "unknown" -at "testExtAttr" -forceDelete on; // List all the attributes of unknown1 object listAttr unknown1;
The MEL way to add/delete extension is straight-forward and users can easily watch you have done in the MEL script. However, if you want to hide all the extension attribute implementation, you can create your own plugin using C++ API.
C++ API Way to add/remove extension attributes:
To add extension attribute, you can use addExtensionAttribute() API. If you want to remove the extension attribute you can use removeExtensionAttribute() API. If you closely look at the MEL way to add/delete extension attribute you have to use particular nodeType each time while you add/remove extension attributes. In C++ API, there is a way to add an extension attribute to your plugin which may contain different nodeTypes. For example a plugin can have nodeType1, nodeType2,.. You can add extension attribute to a plugin regardless of nodeTypes. It means that the extension attribute is common for all nodeTypes within plugin. You can do this using linkExtensionAttributeToPlugin() API.
MDGModifier fCmd; MNodeClass* fNodeType; MFnPlugin plugin( obj ); ... ... MFnCompoundAttribute compAttr; MFnNumericAttribute numAttr1; MFnNumericAttribute numAttr2; MObject extensionChild1 = numAttr1.create( kChild1NameLong, kChild1NameShort, MFnNumericData::kFloat, 0, &status ); CHECK_MSTATUS(status); MObject extensionChild2 = numAttr2.create( kChild2NameLong, kChild2NameShort, MFnNumericData::kFloat, 0, &status ); CHECK_MSTATUS(status); MObject extensionParent = compAttr.create( kParentNameLong, kParentNameShort, &status ); CHECK_MSTATUS(status); compAttr.addChild( extensionChild1 ); compAttr.addChild( extensionChild2 ); // Add parent extension attribute to node status = fCmd.addExtensionAttribute( *fNodeType, extensionParent ); CHECK_MSTATUS(status); // Link the extension attribute to plugin regardless of nodeTypes status = fCmd.linkExtensionAttributeToPlugin(plugin,extensionParent); CHECK_MSTATUS(status);
Users can delete the extension attribute using removeExtensionAttribute() API. Moreover, you can delete the extension attributes regardless of nodeTypes as well for that you have to use unlinkExtensionAttributeToPlugin() API.
MObject parentAttr = fNodeType->attribute( kParentNameLong, &status ); CHECK_MSTATUS(status); // Remove the extension attribute for a particular node type status = fCmd.removeExtensionAttribute( *fNodeType, parentAttr ); CHECK_MSTATUS(status); // Unlink the extension attribute from plugin regardless of nodeTypes status = fCmd.unlinkExtensionAttributeFromPlugin(plugin, parentAttr); CHECK_MSTATUS(status);
Remember Maya never remove or unlink the attribute from node and from plugin automatically. User has to do the above remove and unlink steps manually.
BTW, if you need to list all the extension attributes of a node, here is the way using C++ API:
MObjectArray allAttrs; status = fNodeType->getAttributes(allAttrs); CHECK_MSTATUS(status); int attrCount = fNodeType->attributeCount(); for (int i=0; i < attrCount; i++) { MFnAttribute attr; attr.setObject(allAttrs[i]); if ( attr.isExtension() ) cout << "Attribute is extension, name is <" << attr.name() << ">"<< endl; }
In both MEL and C++ way, you have to add the extension attributes one by one i.e, you have to call addExtension MEL command or addExtensionAttribute() API for each attribute. Let's say I want to add bunch of extension attributes but I don't want to add them one be one, yes there is a way in Maya 2017. You can create a attribute pattern file where you can add all the extension attributes with default values. The attribute pattern file is basically a JSON file which is the simple and easiest way to add extension attributes to Maya. Please check the below link for reference. http://help.autodesk.com/view/MAYAUL/2017/ENU/?guid=GUID-B103DD61-8ED8-440E-AB55-64F70E09F7D3
Comments
You can follow this conversation by subscribing to the comment feed for this post.