【问题标题】:longest and shortest curves selected选择最长和最短的曲线
【发布时间】:2014-02-02 10:03:05
【问题描述】:

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

【问题讨论】:

    标签: python maya curves nurbs


    【解决方案1】:

    您可以通过列表推导摆脱循环。将所有形状及其长度收集到一个(长度,形状)对列表中并对其进行排序 - 这会为您提供最短的曲线:

    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 列表以便在别处处理。

    【讨论】:

    • 谢谢theodox,我会试试看的。
    【解决方案2】:

    出于简单易用的明显原因,我建议开始使用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多