Development / Tips March 13, 2011 @ 16:07

Script: Connect Maya Nurbs Curves


This simple python script does not actually attach two curves.
Instead, it matches the first and last two control vertices to ensure continuity in translation and velocity.
Which ends will be connected is determined by selection order and curve direction.

EDIT: Ignorant me, I just discovered that Maya already has a built-in command for this: alignCurve.

import maya.OpenMaya as OpenMaya
import maya.cmds as cmds

def init():
selection = cmds.ls(sl=True)

if not (len(selection) == 2):
OpenMaya.MGlobal.displayError("Select exactly 2 curves")
return
alignCurves(selection[0], selection[1])
cmds.select(selection)

def alignCurves(curve1, curve2):
spans1 = cmds.getAttr(curve1 + '.spans')
degree1 = cmds.getAttr(curve1 + '.degree')
cvs1 = spans1 + degree1

p1 = cmds.xform('%s.cv[%i]' % (curve1, cvs1-2), q=True, ws=True, t=True)
p2 = cmds.xform('%s.cv[%i]' % (curve1, cvs1-1), q=True, ws=True, t=True)
p3 = cmds.xform('%s.cv[%i]' % (curve2, 0), q=True, ws=True, t=True)
p4 = cmds.xform('%s.cv[%i]' % (curve2, 1), q=True, ws=True, t=True)

# Align endpoints
pM = interpolate(p2, p3, 0.5)
cmds.xform('%s.cv[%i]' % (curve1, cvs1-1), a=True, ws=True, t=pM)
cmds.xform('%s.cv[%i]' % (curve2, 0), a=True, ws=True, t=pM)

# Align tangents
v1 = sub(pM, sub(p4, p3))
v2 = sub(pM, sub(p1, p2))
t1 = interpolate(v1, p1, 0.5)
t2 = interpolate(v2, p4, 0.5)
cmds.xform('%s.cv[%i]' % (curve1, cvs1-2), a=True, ws=True, t=t1)
cmds.xform('%s.cv[%i]' % (curve2, 1), a=True, ws=True, t=t2)

def sub(p1, p2):
return [
p1[0] - p2[0],
p1[1] - p2[1],
p1[2] - p2[2]
]

def interpolate(p1, p2, t):
x = p1[0] + ((p2[0] - p1[0]) * t)
y = p1[1] + ((p2[1] - p1[1]) * t)
z = p1[2] + ((p2[2] - p1[2]) * t)
return [x,y,z]

init()
No Comments to Script: Connect Maya Nurbs Curves

Add comment:


You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

By submitting a comment here you grant baseone:online a perpetual license to reproduce your words and name/web site in attribution. Inappropriate or irrelevant comments will be removed at an admin's discretion.