Try this:
import maya.cmds as cmds
def get_poly_bounding_box_center(object):
# object can be object or components
bbox = cmds.xform(object, query=True, boundingBox=True, worldSpace=True)
# bbox = cmds.exactWorldBoundingBox(object)
center_x = (bbox[0] + bbox[3]) / 2.0
center_y = (bbox[1] + bbox[4]) / 2.0
center_z = (bbox[2] + bbox[5]) / 2.0
return [center_x, center_y, center_z]
def show_bounding_box_center():
"""
Adds a locator at bounding box center.
"""
selection = cmds.ls(selection=True, flatten=True)
if not selection:
cmds.warning("No objects or components selected.")
return
poly_objects = []
poly_components = []
for item in selection:
if ".vtx[" in item or ".e[" in item or ".f[" in item:
poly_components.append(item)
elif cmds.ls(item, type="transform"):
poly_objects.append(item)
if poly_objects:
for obj in poly_objects:
center = get_poly_bounding_box_center(obj)
locator = cmds.spaceLocator(name=f"{obj}BoundingBoxCenter")[0]
cmds.xform(locator, translation=center, worldSpace=True)
if poly_components:
center = get_poly_bounding_box_center(poly_components)
locator = cmds.spaceLocator(name="ComponentsBoundingBoxCenter")[0]
cmds.xform(locator, translation=center, worldSpace=True)
On Wednesday, September 17, 2014 at 2:59:30 AM UTC+8 Matteo Di Lena wrote:
> Thanks a lot Mahmoodreza,
> clever way to solve it :) I found a way, after quite a few time spent
> searching, to query a manipulator position and get its world space values.
> Anyway your solution is way more intuitive than that!
>
> Thank you again.
> -Matteo
>
>
> On Tuesday, September 16, 2014 6:25:38 PM UTC+2, Mahmoodreza Aarabi wrote:
>
>> Hey
>> if your mean is find world position of some selected component (vertex
>> here)
>> you should use an script like this:
>>
>> import maya.cmds as cmds
>>
>> selVerts = cmds.ls(sl=True)
>> clst = cmds.cluster()
>> pos = cmds.xform(clst[1], q=True, ws=True, rp=True)
>> loc = cmds.spaceLocator()
>> cmds.move(pos[0], pos[1], pos[2])
>> cmds.delete(clst[1])
>>
>> Now you have a locator that show you center of your selection.you can use
>> pos variable for finding the location of center.
>>
>> Good luck
>>
>>
> On Tue, Sep 16, 2014 at 7:35 PM, Matteo Di Lena <[email protected]>
>> wrote:
>>
> Hi guys,
>>>
>>> I'm having troubles trying to find the center of a component selection
>>> (vertex, in this case) with
>>> python. I tried looking into commands reference, but I could't find
>>> anything useful.
>>>
>>> Is this possible with maya cmds or do I need some API?
>>>
>>> Thanks a lot for any help.
>>> -Matteo
>>>
>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Python Programming for Autodesk Maya" group.
>>>
>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to [email protected].
>>
>>
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/f43d48da-903b-4905-83e1-189228765579%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/python_inside_maya/f43d48da-903b-4905-83e1-189228765579%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>>
>>
>> Bests,
>> madoodia
>>
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/d01a229f-ffb3-4a63-b8db-78b0e7f106c9n%40googlegroups.com.