import maya.cmds as cmds
# In the "RenderManProceduralShape->Scripts->Pre Shape Python Script" copy the following text:
# import rfm2.api.strings as apistr;import pp_place_spheres;pp_place_spheres.setDataStr(apistr.expand_string("<shape>"))
def setDataStr(shapeName):
tnode = cmds.getAttr(shapeName + '.target')
# If the name of the transform node of the target shape has
# not been specified we assume the RenderManProgram has been
# parented to the polymesh that will "receive" the spheres.
if len(tnode) == 0:
tnode = cmds.listRelatives(shapeName, parent=True)[0]
tnode = cmds.listRelatives(tnode, parent=True)[0]
rad = str(cmds.getAttr(shapeName + '.radius'))
use_local_space = cmds.getAttr(shapeName + '.use_local_space')
coords = get_coordinates(tnode, use_local_space)
rounded = []
# Reduce precision to 3 decimal places - less text to pass.
for coord in coords:
rounded.append(round(coord,3))
coords_str = " ".join(map(str, rounded))
text = rad + ' ' + str(len(coords)) + ' ' + coords_str
cmds.setAttr(shapeName + '.data', text, type='string')
# Returns a flat list of coords ie. [x0,y0,z0,x1,y1,z1....]
def get_coordinates(tnode, useLocalSpace=True):
verts = []
shape = cmds.listRelatives(tnode, shapes=True)[0]
num_verts = cmds.polyEvaluate(tnode, vertex=True)
for n in range(num_verts):
vert_str = shape + '.vtx[%d]' % n;
vert_pos = cmds.pointPosition(vert_str, local=useLocalSpace)
verts.extend(vert_pos)
return verts