Maya | Python | Create node and connect attribute
- Max Liu
- Dec 9, 2024
- 1 min read
It is possible in Maya to create node graph through python.
import maya.cmds as cmds
gear_list = cmds.ls('C_gear_*_GEO')
for i, gear in enumerate(gear_list):
if i > 0:
radius = cmds.getAttr(gear + '.radius')
multiDoubleLinear = cmds.createNode('multDoubleLinear', name = gear.replace('GEO','MDL'))
if i % 2 != 0:
mult_factor = float(1)/radius * -1
else:
mult_factor = float(1)/radius
cmds.connectAttr(gear_list[0] + '.rotateZ', multiDoubleLinear + '.input1')
cmds.setAttr(multiDoubleLinear + '.input2', mult_factor)
cmds.connectAttr(multiDoubleLinear + '.output', gear + '.rotateZ')

To test this example code, you need to add attribute to objects that you want to connect and apply attribute.

Final result: When you rotate a object, other object will rotate with the object that you select. It is useful when your project requires you to create node graph for the team.

Commentaires