Skip to content

Instantly share code, notes, and snippets.

@JFlynnXYZ
Last active November 28, 2015 16:45
Show Gist options
  • Save JFlynnXYZ/6aa2a7c652f88f2d8335 to your computer and use it in GitHub Desktop.
Save JFlynnXYZ/6aa2a7c652f88f2d8335 to your computer and use it in GitHub Desktop.
Creates a ctrl at a selected joint
import maya.cmds as cmds
def getInheritedRotation(obj):
rotation = [0,0,0]
p = cmds.listRelatives(obj, allParents=True)
while p != None:
if cmds.nodeType(p) != "joint":
rotation = [x + y for x,y in zip(rotation, cmds.xform(p, q=True, rotation=True))]
else:
rotation = [x + y for x,y in zip(rotation, cmds.joint(p, q=True, orientation=True))]
p = cmds.listRelatives(p, allParents=True)
return rotation
def getChildLevel(obj):
p = cmds.listRelatives(obj, allParents=True, type="joint")
level = 1
while p != None:
level += 1
p = cmds.listRelatives(p, allParents=True, type="joint")
return level
def generateColour(level, lenLevels, red=True, green=True, blue=False, redSub=False, greenSub=True, blueSub=False):
div = 1.0/lenLevels
col = [0,0,0]
if red and not redSub:
col[0] = div*level
elif red:
col[0] = 1-div*level
if green and not greenSub:
col[1] = div*level
elif green:
col[1] = 1-div*level
if blue and not blueSub:
col[2] = div*level
if blue:
col[2] = 1-div*level
return col
def generateColourList(lenLevels, red=True, green=True, blue=False, redSub=False, greenSub=True, blueSub=False):
colours = []
for i in range(lenLevels):
colours.append(generateColour(i, lenLevels, red, green, blue))
return colours
if __name__ == "__main__":
jnts = cmds.ls(selection=True, type="joint")
colours = generateColourList(len(jnts))
for i,jnt in enumerate(jnts):
jntPos = cmds.joint(jnt, q=True, p=True)
jntOri = cmds.joint(jnt, q=True, o=True)
jntOri[1] += 90
finalJntOri = [x + y for x,y in zip(jntOri, getInheritedRotation(jnt))]
ctrl = cmds.circle(name="{}_ctrl".format(jnt))
ctrl[1] = cmds.listRelatives(ctrl[0], shapes=True, type="nurbsCurve")[0]
cmds.setAttr("{}.overrideEnabled".format(ctrl[1]), 1)
cmds.setAttr("{}.overrideRGBColors".format(ctrl[1]), 1)
cmds.setAttr("{}.overrideColorRGB".format(ctrl[1]), *colours[i], type="double3")
cmds.xform(ctrl, translation=jntPos, rotation=finalJntOri)
cmds.delete(ctrl, ch=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment