Blog

Create Null Above Object, Constrain Null To Secondary Object, Control Constraint With Attribute on Original Object, Maya, Python Script

Posted by:

I frequently have to have an object follow another object but that original object still has to be able to be controlled in the “local space” of the parent object, but then sometimes be independent of the parent object.  Think an IK hand control.

Anyway, I have to do this so often that I thought I would write a script so I never have to do it again.

  1. #jfm_null_prnt_const_object_control
  2. #Author : Seth Meshko, 3danimationartist.com
  3. #Creates a null above an object, then parent constrains the null to a second object, creates an attribute
  4. #on the shape of the original object to control the blend of the parent constraint
  5. #to use, select the target object then the object to be constrained
  6.  
  7.  
  8. import maya.cmds as cmds
  9.  
  10. def snapToObj(target, objToSnap):
  11. tempPointConstraint = cmds.pointConstraint(target, objToSnap, maintainOffset = False)
  12. cmds.delete(tempPointConstraint)
  13. cmds.makeIdentity(objToSnap, apply = True, translate = True, rotate = True, scale = True) #Freeze Transforms
  14.  
  15. listSelected = cmds.ls(selection = True)
  16.  
  17. if len(listSelected) == 2:
  18. targetObject = listSelected[0]
  19. constrainedObject = listSelected[1]
  20.  
  21. cmds.addAttr(constrainedObject, longName = 'Rig_Follow', attributeType = 'float' , minValue = 0, maxValue = 1, defaultValue = 1, keyable = True)
  22.  
  23. constrainedNull = cmds.group(empty = True, name = constrainedObject + 'Null')
  24.  
  25. snapToObj(constrainedObject, constrainedNull)
  26. cmds.parent(constrainedObject, constrainedNull)
  27.  
  28. parentConstraint = cmds.parentConstraint(targetObject, constrainedNull, maintainOffset = True)
  29.  
  30. cmds.connectAttr(str(constrainedObject) + '.Rig_Follow', parentConstraint[0].encode('utf8') + '.' + targetObject + 'W0')
  31.  
  32.  
  33. else:
  34. print('You have to select exactly two objects')
0
  Related Posts

You must be logged in to post a comment.