我正在尝试创建一个将选择一堆NURBS曲线的脚本,并测量这些曲线的长度。
因此,理想情况下,我可以选择最短的曲线,而较长的曲线则未选择(或相反)。到目前为止,我都有:
import maya
import string
for i in maya.cmds.ls(selection=True):
shapeNodes = maya.cmds.listRelatives(i,shapes=True)
for shape in shapeNodes:
if maya.cmds.nodeType(shape) == "nurbsCurve":
print "Curve: %s is %s units long" % (shape, maya.cmds.arclen(shape,))
cvs = mc.getAttr(string.join(shapeNodes) + '.spans')+1
print "The: %s has %s cvs" % (shape,cvs)
else:
print "Wrong: %s is a %s" % (shape, maya.cmds.nodeType(shape))
您可以在列表理解的情况下摆脱循环。将所有形状及其长度收集到(长度,形状)对的列表中,然后排序 - 这给您最短的曲线:
import maya.cmds as cmds
sel = cmds.ls(sl=True)
shapeNodes = cmds.listRelatives(sel,shapes=True)
shapeNodes = cmds.ls(shapeNodes, type= 'nurbsCurve', l=True) # long paths to avoid confusion
selectable = [ ( cmds.arclen(item), item) for item in shapeNodes]
if selectable:
selectable.sort()
cmds.select( selectable[0][-1])
else:
cmds.select(cl = True)
您也可以将其放入函数中,然后返回selectable
列表以在其他地方处理。
我建议出于简单和轻松的明显原因开始使用pymel
import pymel.core as pm
curveInfo = pm.createNode('curveInfo')
for thisCurve in pm.ls(sl=True):
#get the shape node
thisShape = thisCurve.getShape()
#connect the world space to the curve info
thisShape.worldSpace >> curveInfo.inputCurve
#this is how you get the value
print curveInfo.arcLength.get()
#from here you can put the value in whatever you need
#delete the curve info
pm.delete(curveInfo)