Monday, December 17, 2012

Align pivots in Maya

This script allows you to copy object pivot translation and rotation from one object to another one. How to use:
 - copy code to the script editor "Python tab"
 - select source object
 - select target object
 - execute script
Works on multiple selection. Align all object pivots with last selected.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''
Align pivots by Serge Scherbakov.
Usage:
1. Select source object(s)
2. Add to selection target object
3. Run script by 'import copyPivot\n copyPivot.copyPivot()
'''
 
import maya.cmds as cmds
import maya.mel as mel
 
def copyPivot ():
    sourceObj = cmds.ls(sl = True)[len(cmds.ls(sl = True))-1]
    targetObj = cmds.ls(sl = True)[0:(len(cmds.ls(sl = True))-1)]
    parentList = []
    for obj in targetObj:
        if cmds.listRelatives( obj, parent = True):
            parentList.append(cmds.listRelatives( obj, parent = True)[0])
        else:
            parentList.append('')
    if len(cmds.ls(sl = True))<2:
        cmds.error('select 2 or more objects.')
    pivotTranslate = cmds.xform (sourceObj, q = True, ws = True, rotatePivot = True)
    cmds.parent(targetObj, sourceObj)
    cmds.makeIdentity(targetObj, a = True, t = True, r = True, s = True)
    cmds.xform (targetObj, ws = True, pivots = pivotTranslate)
    for ind in range(len(targetObj)):
        if parentList[ind] != '' : 
            cmds.parent(targetObj[ind], parentList[ind])
        else:
            cmds.parent(targetObj[ind], world = True)
     
 
copyPivot()

10 comments:

  1. Thanks a lot, realy helpful!

    ReplyDelete
  2. Why this is not part of Maya is beyond me! Thank you SOOOOOOOO much!!!!!!!!

    ReplyDelete
  3. Love your tutorials. A class mate showed me them and they where incredible helpful.

    ReplyDelete
  4. Very helpful. Thank you.

    ReplyDelete
  5. Below is faster for complicate hierarchy.
    1) select object to snap TO and then display>transform display>rotation pivot
    2) select the object to be snapped and then just v-snap to the rotation pivot in step one


    https://simplymaya.com/forum/showthread.php?t=33926

    ReplyDelete