Center Pivot Based on Components

import math, sys, os
import maya.cmds as cmds
import maya.mel as mel

##Convert selection to verts
sF= ( (sl=1))
print (sF)
sV= (cmds.polyListComponentConversion( ( (sl=1, flatten=1)), tv=True))
print (sV) (sV)

##make list of selected verts
selectedItems = ( (flatten=True, selection=True))
lengthOfList = len(selectedItems)
print ("***Number of items in list " + str(lengthOfList))

##average verts
##print (selectedItems)
for i in xrange(len(selectedItems)):
  ##print ("iteration" + str((i)+1))
  print ("Selected Item in iteration " + str((i)+1) + ": " + str(selectedItems[i]))
  print (cmds.pointPosition (selectedItems[i]))
  xTotal = xTotal + (cmds.pointPosition (selectedItems[i])[0])
  yTotal = yTotal + (cmds.pointPosition (selectedItems[i])[1])
  zTotal = zTotal + (cmds.pointPosition (selectedItems[i])[2])

xNew = (xTotal)/(lengthOfList)
yNew = (yTotal)/(lengthOfList)
zNew = (zTotal)/(lengthOfList)
print ("center of selected items " + str(xNew) + " " + str(yNew) + " " + str(zNew))
##move pivot of selected obj to average of selected verts
parentObj= ( (hilite=True))
##print (parentObj[0])
scalePivotNew = (str(parentObj[0]) + ".scalePivot")
rotatePivotNew = (str(parentObj[0]) + ".rotatePivot")
##print (scalePivotNew)
##print (rotatePivotNew)
cmds.move ((xNew), (yNew), (zNew), scalePivotNew, rotatePivotNew)

##revert to original selection (sF)

##revert to original selection's parent obj (parentObj)

One thought on “Center Pivot Based on Components

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s