Search code examples
pythonattributesmaya

Maya Python updating attributes


I have 3 attributes, 2 of which will be used together to determine the value of the 3rd.

Trig Function - The user selects which trig function they want to use. Right now it's just sin and cos

Radians - User enters the radians value for the trig function.

output - This value will be connected to an attribute on a noise texture.

My problem is how do I set this up so that when I change the values of "Trig Function" & "Radians" the output value gets updated?

Example code:

import maya.cmds as cmds
import math

cmds.window(title="Simple UI in Maya", width=300 )
theMasterLayout = cmds.columnLayout()
groupName = "testGrp"
cmds.group(empty=True, name=groupName)
cmds.addAttr(ln="WaveType", at='enum', en="sin:cos")
cmds.addAttr(ln="radians", at='double', min=0, max=10, dv=0.2, k=True)
cmds.addAttr(ln='WaveValue', at='double', dv=0)
if cmds.getAttr(groupName + ".WaveType") == "sin":
    wave = math.sin(cmds.getAttr(groupName + ".radians"))
else:
    wave = math.cos(cmds.getAttr(groupName + ".radians"))
cmds.setAttr(groupName + ".WaveValue", wave)

# Display the window
cmds.showWindow()

Solution

  • I was able to find a solution to this issue. Basically it's just using expressions. But if anyone knows of an alternative method I would still love to hear it.

    import maya.cmds as cmds
    
    groupName = "testGrp"
    cmds.group(empty=True, name=groupName)
    cmds.addAttr(ln="WaveType", at='enum', en="sin:cos")
    cmds.addAttr(ln="radians", at='double', min=0, max=10, dv=0.2, k=True)
    cmds.addAttr(ln='WaveValue', at='double', dv=0)
    expString = 'if ('+ groupName + '.WaveType == 0){\n'
    expString += groupName + '.WaveValue = sin(' + groupName + '.radians);\n}'
    expString += '\nelse {\n' + groupName + '.WaveValue = cos(' + groupName + '.radians);\n}'
    cmds.expression( s=expString,
                     o=groupName,
                     n="WaveResult",
                     ae=1,
                     uc=all )