Maya: Select -X Half of Object using object space pivot orientation in Maya Python?

Hey TA.org,

Forgive me I’m quite new to scripting and this seems really complicated… not sure where to start.

Is it possible to select vertices in the -x direction of an objects current pivot orientation?

I’m trying to use that function in delete half and mirror, delete half and mirror instance scripts

I looked at polyselectscontraints and xform but I don’t think they do what I want…

You can use xform, but you’ll have to take the rotation pivot into account as well.
I think the snippet below will do what you need.

from maya import cmds, OpenMaya

node = "pSphere1"
vertices = cmds.ls("{}.vtx[*]".format(node), fl=True)
verticesMirrored = []

# get rotation pivot
pivotOffset = cmds.getAttr("{}.rotatePivot".format(node))[0]
pivotOffset = OpenMaya.MVector(*pivotOffset)

# loop vertices 
for vtx in vertices:
    # query position in local space
    pos = cmds.xform(vtx, q=True, os=True, t=True)
    pos = OpenMaya.MVector(*pos)
    
    # subtract the rotation pivot from the local space position
    pos -= pivotOffset
    
    # check x position is lower than 0
    if pos.x < 0:
        verticesMirrored.append(vtx)

# select vertices to mirror
cmds.select(verticesMirrored)

If you run this on a sphere you get something like this.
The plane in the picture is where the pivot point is.

mirror_verts

1 Like

Thank you R.Joosten this helps a lot! I’m having trouble converting this to for current selection though.

I don’t get why I can’t make it work for my current selected object?

If i write

node = cmds.ls(sl=True)

I get this error:

Error: Syntax error: unexpected end [ at position 1 while parsing:
[].vtx[]
^
: [].vtx[
]
Traceback (most recent call last):
File “”, line 6, in
RuntimeError: Syntax error: unexpected end [ at position 1 while parsing:
[].vtx[]
^
: [].vtx[
] #

Is this not because cmds.ls(sl=True) returns a list with the selected objects name in it? So you should just have to add [0] at the end of it.

so: cmds.ls(sl=True)[0]

2 Likes

ah geeze, basic mistake. thanks Boogey