MakeCenterLine for Maya

Ever wanted to quickly draw a line in the center of a 3D object in Maya? Say you need to place a curve there or joints, or both. This short Python script will let you create a curve or joints down the volumetric center of a mesh by way of an edge loop selection.

 The script has three functions, depending on what you select.
  • If you select one edge and run the script, it will generate a curve down the center of the object using all edge loops.
  • Select two edges, and it will generate a curve BETWEEN those two edge loops.
  • Select three or more edges, and it will generate a curve only using those selected edge loops (Good for heavy, redundant geo)
There are two flags, make centerline(0,1) creates a joint chain, (1,0) makes a NURBS curve and (1,1) makes both.

import maya.cmds as cmd
import maya.mel
import re

def makeCenterLine(c=1, j=1):
    #track selection order must be ON for this to work
    points = 'curve -d 3 '
    sel = cmd.ls(os=True, fl = True)
    ob = cmd.ls(o=True, sl=True)
    num = []
    out = []
    edges = []
    joints = []
    for one in sel:
        strip = re.search(r"\[([0-9]+)\]", one)
        num.append(strip.group(1))
    size = len(num)
    if (size):
        if size == 1:      
            edges = cmd.polySelect(edgeRing=(int(num[0])), ns = True)
        if size == 2:      
            edges = cmd.polySelect(edgeRingPath=(int(num[0]), int(num[1])), ns = True)
        if size > 2:
            edges = num
        for one in edges:
            cmd.select(ob)
            cmd.polySelect(elb=int(one)) 
            clust = cmd.cluster(n = 'poo#')
            cmd.select(clear=True)
            posi = cmd.getAttr(clust[0]+'HandleShape.origin')
            if c:
                points = points + ('-p %s %s %s ' %(posi[0][0], posi[0][1], posi[0][2]))
            if j:
                joints.append(cmd.joint(p = (posi[0][0], posi[0][1], posi[0][2])))
            cmd.delete (clust[0])
        if c:
            out.append(mel.eval(points))
        if j:
            for i in reversed(range(1, len(edges))):
                cmd.parent(joints[i], joints[i-1])
            cmd.joint(joints[0], e=True, oj = 'xyz', sao= 'zup', ch=True, zso=True)
            cmd.joint(joints[-1],e=True, o =(0,0,0))
            out.append(joints[0])
        cmd.select(out)
    else:
        print "Nothing is selected"
       
makeCenterLine(0, 1)
      
                        

Comments

  1. just copy-paste the script? does not work for me. maya 2017.

    ReplyDelete
    Replies
    1. makeCenterLine(1, 1)
      # Error: invalid syntax #

      Delete
    2. Are you pasting into a python tab or a mel tab?

      Delete
    3. It could be the formatting. Try downloading the script from here: https://www.highend3d.com/maya/script/makecenterline-for-maya

      Delete
  2. Hi! I am having trouble making this work as well. I get this error: # Error: invalid syntax #

    I downloaded it from that site, and am using Maya 2022. Running it as a python script. Do you know if it works with Maya 2022? Extremely new to python and scripts in Maya... but would really appreciate any help!!

    ReplyDelete
    Replies
    1. Just figured it out- it needed parentheses around "Nothing is detected"
      ie: print ("Nothing is selected")

      Delete

Post a Comment