A node's attributes define the data used by the node. A node should not use any data that is not defined by its attributes.
Attribute names must be unique across the entire node hierarchy. This means that they must be unique across all of a node's derived and parent classes. However, the same attribute name can be reused between unrelated nodes.
When you define an attribute, you must define the type and name of the attribute. Several function sets are available for different types of attributes. These all inherit from MFnAttribute. If no class exists for the type of attribute you need, you can use the MFnTypedAttribute or MFnGenericAttribute.
Attributes are created, configured, and added to a node in the node's initialize() function.
You will need to declare an attribute function set for each type of attribute your node uses. Always use the attribute function set that corresponds to the type of data the attribute will be handling.
For example:
MFnNumericAttribute numericAttr; MFnTypedAttribute typedAttr;
Each attribute function set can be used to create multiple attributes. This means that you do not have to declare an attribute function set for each attribute. You only need to declare an attribute function set for each attribute type.
All of an attribute's characteristics must be set before the next attribute can be created.
output = nAttr.create("output", "out", MFnNumericData::kFloat, 0.0); nAttr.setWritable(false); nAttr.setReadable(true); nAttr.setStorable(false); input1 = nAttr.create("input1", "in1", MFnNumericData::kFloat, 0.0); nAttr.setStorable(false); nAttr.setWritable(true); nAttr.setReadable(false); input2 = nAttr.create("input2", "in2", MFnNumericData::kInt, 2); nAttr.setStorable(false); nAttr.setWritable(true); nAttr.setReadable(false);
The attribute's characteristics specify how it will be used. setWritable(), setReadable(), and setStorable() are used to set characterists that determine whether the attribute is an input attribute, an output attribute, or if the attribute's value is retained between runs of the graph.
setReadable() | When set to true, the attribute is an output attribute. Its value can read by nodes connected to it. Attributes are readable by default. Input nodes should have setReadable(false). |
setWritable() | When set to true, the attribute is an input attribute. A node connected to this attribute can write a value to it. Attributes are writeable by default. Output nodes should have setWriteable(false). |
setStorable() | When set to false, the attribute's value will not be retained between runs of the graph. Attributes are storable by default. |
For a complete list of attribute characteristics, see the documentation for MFnAttribute.
Use attributeAffects() to set dependency relationships between attributes. This information is used to create Evaluation Graphs.
attributeAffects(input1, output); attributeAffects(input2, output);
Finally, add your attributes to your node using the addAttribute() function.
addAttribute(input1); addAttribute(input2); addAttribute(output);