1

我正在尝试创建一个脚本来选择一堆 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))
4

2 回答 2

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列表以在其他地方处理。

于 2014-02-02T22:33:12.773 回答
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)
于 2014-03-26T18:47:08.803 回答