选择最长和最短的曲线

时间:2014-02-02 10:03:05

标签: python maya curves nurbs

我试图创建一个会选择一堆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))

2 个答案:

答案 0 :(得分:3)

你可以通过列表理解来完成循环。将所有形状及其长度收集到(长度,形状)对列表中并对其进行排序 - 这样可以得到最短的曲线:

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列表以便在其他位置进行处理。

答案 1 :(得分:0)

我建议开始使用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)