Create an attribute control using Python

To create a control for an attribute using a callback defined in a Python module, complete the following steps:

Create the attribute control

In the following example a floatSlider control is created by the Python procedure AEaddFloatSliderModule, and uses the change command AEaddFloatSliderModuleCB. The callback AEFloatSliderModule is defined in the module AEaddFloatSlider.py.

import maya.cmds as cmds


def AEaddFloatSliderModule(	plug, sliderLabel, annot ):

	cmds.rowLayout( nc=2 )
	val = cmds.getAttr( plug )
	cmds.text( label=sliderLabel )
	slider = cmds.floatSlider( annotation=annot, v=val )
	def AEaddFloatSliderModuleCB( *args ):
		val = cmds.floatSlider( slider, q=1, v=1 )
		cmds.setAttr( plug, val )
	cmds.floatSlider( slider, e=1, cc=AEaddFloatSliderModuleCB )
	cmds.setParent( u=1 )

The following parameters are used by the Python procedure:

Specify the location of the Python module

This step provides Maya with the path to the directory containing the Python module. Set the PYTHONPATH environment variable to the path and location of your module.

Define the callback to the Python procedure

When you are creating the template file, add a <description language="cb"> tag to an attribute declaration or view definition to specify the callback to the Python procedure.

To specify the Python callback AEaddFloatSliderModule in the Python module AEaddFloatSlider.py, the declaration should be py.AEaddFloatSlider.AEaddFloadSliderModule. The declaration uses the following format: py.<ModuleName>.<ProcedureName>.

To add the custom slider control defined in the Python module above to the Incandescence Blue attribute, use the following:

<attribute name='incandescenceB' type='maya.float'>
    <label>Incandescence Blue</label>
      <description language="cb">py.AEaddFloatSlider.AEaddFloatSliderModule</description>
</attribute>
When you access the attribute view in the Attribute Editor, the Incandescence Blue attribute uses the customized slider.

Related topics