Guest User

Untitled

a guest
Mar 26th, 2023
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. """
  2. https://groups.google.com/g/python_inside_maya/c/hAw5rxiHbgM/m/gwo8KoCoAQAJ
  3. """
  4. import maya.cmds as mc
  5.  
  6.  
  7. def do_constraint(obj):
  8.     has_trans = mc.getAttr(obj + '.translate', settable=True)
  9.     has_rot = mc.getAttr(obj + '.rotate', settable=True)
  10.  
  11.     if not has_trans and not has_rot:
  12.         # no trans or rot?
  13.         return
  14.  
  15.     newLoc = mc.spaceLocator()
  16.  
  17.     # check if there is rotations
  18.     # create a loc for ech selected obj and constraint to it
  19.     if has_rot and has_trans:
  20.         newCon = mc.parentConstraint(obj, newLoc, mo=0)
  21.  
  22.     elif not has_rot and has_trans:
  23.         newCon = mc.parentConstraint(obj, newLoc, mo=0, sr=['x', 'y', 'z'])
  24.  
  25.     elif has_rot and not has_trans:
  26.         newCon = mc.parentConstraint(obj, newLoc, mo=0, st=['x', 'y', 'z'])
  27.  
  28.     start = mc.playbackOptions(q=True, min=True)
  29.     end = mc.playbackOptions(q=True, max=True)
  30.     mc.bakeResults(newLoc, sm=1, sr=1, t=(start, end))
  31.  
  32.     mc.delete(newCon)
  33.  
  34.     # reverse connection
  35.     if has_rot and has_trans:
  36.         mc.parentConstraint(newLoc, obj, mo=True, w=1)
  37.    
  38.     elif not has_rot and has_trans:
  39.         mc.parentConstraint(newLoc, obj, mo=True, w=1, sr=['x', 'y', 'z'])
  40.    
  41.     elif has_rot and not has_trans:
  42.         mc.parentConstraint(newLoc, obj, mo=True, w=1, st=['x', 'y', 'z'])
  43.  
  44.  
  45. def main():
  46.     user_sel = mc.ls(sl=1)
  47.  
  48.     if not user_sel:
  49.         mc.warning('Please make a selection')
  50.         return
  51.  
  52.     for obj in user_sel:
  53.         do_constraint(obj)
  54.  
  55.  
  56. main()
  57.  
Tags: python maya
Advertisement
Add Comment
Please, Sign In to add comment