1

I want to find the center (by center I mean the default pivot position) of a selection of vertices coming from multiple objects in Maya. I thought it was the centroid but it seems that this does not returns the correct coordinates.

Here is an example: enter image description here

I selected four vertices, the two poles of each sphere. In red is the "center" of this vertices, where Maya place the default pivot, and in blue is the centroid as I computed it.

Either my computations are wrong or the center of the vertices is not the centroid, anyway these are clearly not the same coordinates. Here is my code:

loc = cmds.spaceLocator ()[0]
vtxList = cmds.ls (sl = True, fl = True)

vtxPosSum = [0, 0, 0]
for vtx in vtxList:
    vtxPos = cmds.xform (vtx, q = True, ws = True, t = True)
    vtxPosSum[0] += vtxPos[0]
    vtxPosSum[1] += vtxPos[1]
    vtxPosSum[2] += vtxPos[2]

barycentre = [vtxPosSum[0] / len(vtxList),
              vtxPosSum[1] / len(vtxList),
              vtxPosSum[2] / len(vtxList)]

cmds.xform (loc, ws = True, t = barycentre)

So my question is, how can I find the position of the default Maya pivot when selecting vertices from multiple polygons?

Thanks

UKDP
  • 226
  • 5
  • 21

2 Answers2

2

I think maya uses the center of the bounding box as the pivot center when selecting multiple components... I could be wrong, but give this a try and see if it's the correct spot:

import maya.cmds as mc

sel = mc.ls(sl=True)
bb = mc.exactWorldBoundingBox(sel)
pos = ((bb[0] + bb[3]) / 2, (bb[1] + bb[4]) / 2, (bb[2] + bb[5]) / 2)

pos would be the world space xyz position to move your locator (or whatever you're testing with)

silent_sight
  • 492
  • 1
  • 8
  • 16
1

This post may be useful for you. It shows the following:

centroid=SUM(pos*volume)/SUM(volume)

¡Hope this helps!

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220