Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import maya.cmds as cmds
  2. from collections import namedtuple
  3.  
  4. PivotCtrl = namedtuple("PivotCtrl", "jnt ctrl sdk offset")
  5.  
  6. def getInheritedRotation(obj):
  7.  
  8. rotation = [0,0,0]
  9.  
  10. p = cmds.listRelatives(obj, allParents=True)
  11.  
  12. while p != None:
  13. if cmds.nodeType(p) != "joint":
  14. rotation = [x + y for x,y in zip(rotation, cmds.xform(p, q=True, rotation=True))]
  15. else:
  16. rotation = [x + y for x,y in zip(rotation, cmds.joint(p, q=True, orientation=True))]
  17. p = cmds.listRelatives(p, allParents=True)
  18.  
  19. return rotation
  20.  
  21. def getChildLevel(obj):
  22. p = cmds.listRelatives(obj, allParents=True, type="joint")
  23. level = 1
  24. while p != None:
  25. level += 1
  26. p = cmds.listRelatives(p, allParents=True, type="joint")
  27.  
  28. return level
  29.  
  30. def generateColour(level, lenLevels, red=True, green=True, blue=False, redSub=False, greenSub=True, blueSub=False):
  31. div = 1.0/lenLevels
  32. col = [0,0,0]
  33.  
  34.  
  35. if red and not redSub:
  36. col[0] = div*level
  37. elif red:
  38. col[0] = 1-div*level
  39.  
  40. if green and not greenSub:
  41. col[1] = div*level
  42. elif green:
  43. col[1] = 1-div*level
  44.  
  45. if blue and not blueSub:
  46. col[2] = div*level
  47. if blue:
  48. col[2] = 1-div*level
  49.  
  50. return col
  51.  
  52.  
  53. def generateColourList(lenLevels, red=True, green=True, blue=False, redSub=False, greenSub=True, blueSub=False):
  54. colours = []
  55.  
  56. for i in range(1,lenLevels+1):
  57. colours.append(generateColour(i, lenLevels, red, green, blue))
  58.  
  59. return colours
  60.  
  61.  
  62.  
  63. if __name__ == "__main__":
  64. jnt = [cmds.ls(selection=True, type="joint")[0]]
  65.  
  66. jntHier = cmds.listRelatives(allDescendents=True, type="joint")[1:] + jnt
  67.  
  68. colours = generateColourList(len(jntHier))
  69.  
  70. allJntCtrls = []
  71.  
  72.  
  73. for i,jnt in enumerate(jntHier):
  74. jntPos = cmds.joint(jnt, q=True, p=True)
  75. jntOri = cmds.joint(jnt, q=True, o=True)
  76.  
  77.  
  78.  
  79. finalJntOri = [x + y for x,y in zip(jntOri, getInheritedRotation(jnt))]
  80.  
  81. ctrl = cmds.circle(name="{}_ctrl".format(jnt))
  82.  
  83. cmds.xform("{}.cv[0:]".format(ctrl[0]), rotation=(0,90,0))
  84.  
  85. ctrl[1] = cmds.listRelatives(ctrl[0], shapes=True, type="nurbsCurve")[0]
  86.  
  87. cmds.setAttr("{}.overrideEnabled".format(ctrl[1]), 1)
  88. cmds.setAttr("{}.overrideRGBColors".format(ctrl[1]), 1)
  89. cmds.setAttr("{}.overrideColorRGB".format(ctrl[1]), *colours[i], type="double3")
  90.  
  91. sdk = cmds.group(ctrl, name="{}_sdk".format(ctrl[0]))
  92. off = cmds.group(sdk, name="{}_0".format(ctrl[0]))
  93.  
  94. cmds.xform(off, translation=jntPos, rotation=finalJntOri)
  95.  
  96. allJntCtrls.append(PivotCtrl(jnt=jnt, ctrl=ctrl, sdk=sdk, offset=off))
  97.  
  98.  
  99.  
  100. for i in range(len(allJntCtrls)):
  101. if i < len(allJntCtrls)-1:
  102. cmds.parent(allJntCtrls[i].offset, allJntCtrls[i+1].ctrl[0])
  103.  
  104. # Just setting to orient constrain atm, need to change for other options
  105. cmds.orientConstraint(allJntCtrls[i].ctrl[0], allJntCtrls[i].jnt, name="{}_oc".format(allJntCtrls[i].jnt))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement