0

Is there a Mel or Python command that returns the average position of the currently selected objects and/or components? I know that in component selection mode, selecting a set of vertices will cause the manipulator context to move to accommodate the selected vertices, but this does not seem to work in object mode.

In either case, my goal is to be able to easily obtain the average translation of the selected objects/components without having to write code specific to each sort of thing that may be selected.

1 Answers1

0

you could try getting the world space position of all of the objects and then averaging it yourself - maybe something like this?

import maya.cmds as mc

sel = mc.ls(sl=True, fl=True)
count = len(sel)
sums = [0,0,0]
for item in sel:
    pos = mc.xform(item, q=true, t=True)
    sums[0] += pos[0]
    sums[1] += pos[1]
    sums[2] += pos[2]
center = [sums[0]/count, sums[1]/count, sums[2]/count]

*Note: edited based on some comments below

silent_sight
  • 492
  • 1
  • 8
  • 16
  • Yeah, that is essentially what I am doing for transform nodes, but components have to be handled separately, which is annoying. Also, I noticed that when selecting components (vertices, for example), Maya does not update the manipulator context with their average position, but rather, it appears to use the center of their bounding box. As far as I can tell, it appears that there isn't a command will get me what I need in all cases, so I guess I will just right code to handle each specific case. – SnippedPaws Jul 06 '17 at 16:37
  • The xform command works for components too... `mc.ls(sl=True, fl=True)` should get you all of your components separated out in your selection instead of clumping them together. This won't really help for edges or faces, but a bounding box center for an edge may end up being the center point of the edge... you may need to convert faces to verts to average those positions. You might also want to look at the `filterExpand` command - it can be used to help split up your selection to handle specific cases (so split out those few special cases and then handle all the rest the same) – silent_sight Jul 07 '17 at 02:25
  • you could also look at this question about [centroids](https://stackoverflow.com/questions/4824141/how-do-i-calculate-a-3d-centroid) to see if it would help... – silent_sight Jul 07 '17 at 02:36