Ramp rig

December 7th, 2010

Occasionally in my Maya rigging work I want to use and animate a ramp (or many ramps) as part of a node network, but going through the ramp-editing interface is tedious, and the graph editor doesn’t give me the visual feedback I’d like. So I wrote a script that creates a simple interface for manipulating a monochrome ramp in the viewport.

Note that real-time feedback is only available in the viewport’s “High Quality” mode.

Download the file here: rampRig01.py


Python code for Maya:

### rampRig 01
### http://zoomy.net/2010/12/07/ramp-rig/

from pymel.core import *
import re
 
# make ramp
ramp = shadingNode("ramp", asTexture=1)
SN = shadingNode("place2dTexture", asUtility=1)
connectAttr(SN+".outUV", ramp+".uv")
connectAttr(SN+".outUvFilterSize", ramp+".uvFilterSize")

# make polyplane
plane = polyPlane(w=1, h=1, sx=1, sy=1, ax=(0, 1, 0), cuv=2, ch=1)[0]
rename(plane, ramp+"_plane")

# flip around so ramp looks right
setAttr(plane+".rotateY", 180)
move(plane, (.5, .5), moveXZ=1)
makeIdentity(apply=1, t=1, r=1, s=1, n=0)

# set display type to "reference" so you can't select it
setAttr(plane+".overrideEnabled", 1)
setAttr(plane+".overrideDisplayType", 2)

# make diamond
square = nurbsSquare(c=(0, 0, 0), nr=(0, 1, 0), sl1=0.1, sl2=0.1, sps=1, d=1, ch=1)
curves = square[0].listRelatives()
# make a single curve
ctrl0 = attachCurve(curves[0], curves[1], curves[2], curves[3], ch=1, rpo=0, kmk =1, m=0, bb=0.5, bki=0, p=0.1)[0]
delete(ctrl0, ch=1) # delete history
delete(square) # delete original curves
rename(ctrl0, ramp+"_ctrl")

setAttr(ctrl0+".rotateY", 45) # rotate 45 deg
makeIdentity(apply=1, t=1, r=1, s=1, n=0) # freeze xforms

# set limits
transformLimits(ctrl0, tx=(0, 1), etx=(1, 1))
transformLimits(ctrl0, ty=(.01, .01), ety=(1, 1))
transformLimits(ctrl0, tz=(0, 1), etz=(1, 1))

# make two more ctrls
ctrl1 = duplicate(rr=1)[0]
ctrl2 = duplicate(rr=1)[0]
ctrls = [ctrl0, ctrl1, ctrl2]

# connect ctrls' Z att to ramp's color position
# connect ctrls' X att to ramp's color value
for i, c in enumerate(ctrls):
  connectAttr(c+".translateZ", ramp+".colorEntryList[" + str(i) + "].position" )
  connectAttr(c+".translateX", ramp+".colorEntryList[" + str(i) + "].colorR" )
  connectAttr(c+".translateX", ramp+".colorEntryList[" + str(i) + "].colorG" )
  connectAttr(c+".translateX", ramp+".colorEntryList[" + str(i) + "].colorB" )

# make new lambert and shading group
shader = shadingNode("lambert", asShader=1)
sets(renderable=1, noSurfaceShader=1, empty=1, name=shader+"SG")
SG = shader+"SG"
connectAttr(shader+".outColor", SG+".surfaceShader", f=1)

# attach ramp to lambert's color
connectAttr(ramp+".outColor", shader+".color", force=1)

# apply lambert to plane
sets(SG, fe=plane) 

# group plane and ctrls
grp = group(plane, ctrl0, ctrl1, ctrl2)
rename(grp, ramp+"_ctrls")

# keep ctrl shape during group scale
for i in range(3):
  exp = ctrls[i] + ".scaleX = " + grp + ".scaleZ/" + grp + ".scaleX"
  expression(s=exp, ae=0)

### end rampRig 01


« previously: Hackery | Home | next: selectTrigger v01 »

Leave a Reply