1

I'm trying to create a script that will select a bunch of Nurbs curves, and measure the length of those curves.
So ideally I could select the shortest curve leaving longer curves unselected (or the opposite). So far i have this:

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))
Honest Abe
  • 8,430
  • 4
  • 49
  • 64
pj1138
  • 9
  • 3

2 Answers2

3

You can get out of doing the loops with a list comprehension. Collect all of the shapes and their lengths into a list of (length, shape) pairs and sort that - that gives you the shortest curve:

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)

You could also just make this into a function and return the selectable list for processing elsewhere.

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
theodox
  • 12,028
  • 3
  • 23
  • 36
0

i recommend to start using pymel for the obvious reasons of simplicity and ease

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)
cronicryo
  • 437
  • 1
  • 4
  • 15