import maya.cmds as cmds
from random import uniform, choice
def add_string(attrname, paths):
# we take care of using the special prefix
attrname = 'mtoa_constant_' + attrname
# selections will be either a number of shapes (possibly only one shape),
# or one or more groups that contains shapes.
shapes = []
selections = cmds.ls(sl=True)
for sel in selections:
shapes = cmds.listRelatives(sel, shapes=True)
# sel is a group...
if shapes == None:
transforms = cmds.listRelatives(sel, children=True)
shapes = []
for tran in transforms:
shapes.extend(cmds.listRelatives(tran, shapes=True))
if len(shapes) == 0 or len(shapes[0]) == 0:
print('Cannot find a shape to add attribute')
continue
for shape in shapes:
if cmds.attributeQuery(attrname, node=shape, exists=True) == False:
cmds.addAttr(shape, ln=attrname, sn=attrname, nn=attrname, dt="string")
# get a random value between min_max_value[0] and min_max_value[1]
# and use it to set the value
path = choice(paths)
cmds.setAttr(shape + '.' + attrname, path, type="string")
#==============================================================================
def add_random_float(attrname, min_max_value, min_max=[0, 10.0]):
# we take care of using the special prefix
attrname = 'mtoa_constant_' + attrname
# selections will be either a number of shapes (possibly only one shape),
# or one or more groups that contains shapes.
shapes = []
selections = cmds.ls(sl=True)
for sel in selections:
shapes = cmds.listRelatives(sel, shapes=True)
# sel is a group...
if shapes == None:
transforms = cmds.listRelatives(sel, children=True)
shapes = []
for tran in transforms:
shapes.extend(cmds.listRelatives(tran, shapes=True))
if len(shapes) == 0 or len(shapes[0]) == 0:
print('Cannot find a shape to add attribute')
continue
for shape in shapes:
if cmds.attributeQuery(attrname, node=shape, exists=True) == False:
cmds.addAttr(shape, ln=attrname, sn=attrname, nn=attrname,
k=True, at='double',
min=min_max[0], max=min_max[1], dv=0)
# get a random value between min_max_value[0] and min_max_value[1]
# and use it to set the value
value = uniform(min_max_value[0], min_max_value[1])
cmds.setAttr(shape + '.' + attrname, value)
#==============================================================================
def add_random_int(attrname, min_max_value, min_max=[0, 10.0]):
# we take care of using the special prefix
attrname = 'mtoa_constant_' + attrname
# selections will be either a number of shapes (possibly only one shape),
# or one or more groups that contains shapes.
shapes = []
selections = cmds.ls(sl=True)
for sel in selections:
shapes = cmds.listRelatives(sel, shapes=True)
# sel is a group...
if shapes == None:
transforms = cmds.listRelatives(sel, children=True)
shapes = []
for tran in transforms:
shapes.extend(cmds.listRelatives(tran, shapes=True))
if len(shapes) == 0 or len(shapes[0]) == 0:
print('Cannot find a shape to add attribute')
continue
for shape in shapes:
if cmds.attributeQuery(attrname, node=shape, exists=True) == False:
cmds.addAttr(shape, ln=attrname, sn=attrname, nn=attrname, at='long',
min=min_max[0], max=min_max[1], dv=0)
# get a random value between min_max_value[0] and min_max_value[1]
# and use it to set the value
value = uniform(min_max_value[0], min_max_value[1])
cmds.setAttr(shape + '.' + attrname, value)
#==============================================================================
# For example, add_arnold_attrs.add_random_color('tint', [0,0,0], [1,1,1])
def add_random_color(attrname, min_rgb, max_rgb):
# we take care of using the special prefix
attrname = 'mtoa_constant_' + attrname
# selections will be either a number of shapes (possibly only one shape),
# or one or more groups that contains shapes.
shapes = []
selections = cmds.ls(sl=True)
for sel in selections:
shapes = cmds.listRelatives(sel, shapes=True)
# sel is a group...
if shapes == None:
transforms = cmds.listRelatives(sel, children=True)
shapes = []
for tran in transforms:
shapes.extend(cmds.listRelatives(tran, shapes=True))
if len(shapes) == 0 or len(shapes[0]) == 0:
print('Cannot find a shape to add attribute')
continue
for shape in shapes:
if cmds.attributeQuery(attrname, node=shape, exists=True) == False:
# addAttr -ln $name -sn $name -at "float3" -usedAsColor -k 1 $shape;
cmds.addAttr(shape, ln=attrname, sn=attrname, nn=attrname, at='float3',
usedAsColor=True, k=True)
red = attrname + "_r";
green = attrname + "_g";
blue = attrname + "_b";
cmds.addAttr(shape, ln=red, at="float", k=True, parent=attrname)
cmds.addAttr(shape, ln=green, at="float", k=True, parent=attrname)
cmds.addAttr(shape, ln=blue, at="float", k=True, parent=attrname)
# get a random value between min_max_value[0] and min_max_value[1]
# and use it to set the value
r = uniform(min_rgb[0], max_rgb[0])
g = uniform(min_rgb[1], max_rgb[1])
b = uniform(min_rgb[2], max_rgb[2])
cmds.setAttr(shape + '.' + attrname,r,g,b, type="float3")