Pivot Snap Script

Hi!

I wrote this script that allows animators to change the pivot point of a control. This could really come in handy for changing the center of gravity of a character.

I ran into some problems with this one, but as always, I was able to solve them by simplifying the problem. Something I find that usually works for me is to simplify things into 1 Dimensional space. Usually, if I can find out how to make it work in one dimensional space, I can make it work in 3D space.

So here it is. It’s a 3 node setup. You have your object and two nodes that are children of it. One of those nodes controls the Rotate Pivot position of its parent, and the other is just along for the ride. When you want to move the pivot, you take the second node and put it in the desired location. When then you run this script. It will move the first node to the local position of the object and the object to the difference between the world and local position of the object (that’s what I had to simplify to find out).

Here’s the script with more comments to explain:

import maya.cmds as cmds

"""
bm_pivotSnap
by Ben Morgan
Version 1 8-26-16

This script allows the user to change the pivot point of an object. 
Requirements:
1. You must have some sort of transform node that is the child of the object you want to change the pivot of. 
    - This transform node's Translate values must be connected to the Rotate Pivot value of the object.
2. You must also have a second pivot that is also the child of the object you want to change the pivot of. 
    - This pivot is used to freely move the pivot without affecting the translate values of the object. 
3. Pass this script your object's name, your object's pivot name, and the pivotMover name and run it.

(local position of pivotMover) = (local position of objectPivot)
(world position of pivotMover) - (local position of pivotMover) = new position of obj

"""


def movePivot(obj,pivot,mover):
    # Query the world position of the "mover" node
    loc2WorldPos = cmds.xform(mover,q=1,t=1,ws=1)
    # Query the local position of the "mover" node
    loc2LocalPos = cmds.xform(mover,q=1,t=1)
    # Move the pivot node to the local position of the "mover" node. This can offset our object, so...
    cmds.xform(pivot,t=loc2LocalPos)
    
    # Now we do some math. Through some 1-dimensional trial and error, I found this to be the right equation.
    objPos = []
    a = 0
    while a < 3:
        # We want the object's position to be the difference between the world and local positions
        objPos.append(loc2WorldPos[a] - loc2LocalPos[a])
        a += 1
    # Now translate the object to that position
    cmds.xform(obj,t=objPos)
    
changePivot("pCube1","locator1","locator2")
Advertisements
Image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s