Find component selection center

958 views
Skip to first unread message

Matteo Di Lena

unread,
Sep 16, 2014, 12:05:11 PM9/16/14
to python_in...@googlegroups.com
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

Mahmoodreza Aarabi

unread,
Sep 16, 2014, 12:25:38 PM9/16/14
to python_in...@googlegroups.com

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


--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/f43d48da-903b-4905-83e1-189228765579%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


Bests,
madoodia

Matteo Di Lena

unread,
Sep 16, 2014, 2:59:30 PM9/16/14
to python_in...@googlegroups.com
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 <dilena...@gmail.com> 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 python_inside_maya+unsub...@googlegroups.com.



--


Bests,
madoodia

iouzzr

unread,
Jun 21, 2024, 2:10:35 AMJun 21
to Python Programming for Autodesk Maya
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)

Reply all
Reply to author
Forward
0 new messages