tech python maya
Maya Script: Select half of mesh vertices along a plane
2022-01-18
A script to select exactly half of the mesh's vertices along any axis, independently of where it's located in the scene. You mark the middle by selecting a vertex in the middle, and it will then select vertices to whichever side you tell it.
Someone on reddit asked about how they could select half of the mesh's vertices without trying to eyeball it (they wanted to recreate symmetry). Well, I decided that it could be simple enough of a script to write. Whipped it up in about 10 minutes, hope it will be useful to someone!
Usage :
- In the code change axis to one of the available options: +x, +y, +z, -x, -y, -z
- Select a vertex in the middle of the mesh. Not dead center, but middle of the axis you'd like to start selection from.
- Run the script
If people show interest might make a UI for it and add it into BroTools as a separate free tool 😉
# A script to select one half of the model's vertices along any axis, based on middle vertex selection
# Written by Michael Davydov
# https://michaeldavydov.com
# https://brotools.tech
# How to use
# 1. Choose axis to select by below by editing AXIS variable
# Options: +x, +y, +z, -x, -y, -z
# 2. Select single vertex at the center of your mesh (center plane)
# 3. Run the script
# OPTIONS:
AXIS = "+x"
THRESHOLD = 0.01
# ============= CODE ==============
from maya import cmds
center_vertex = cmds.ls(sl=True, fl=True)[0]
if 'vtx' not in center_vertex:
cmds.error("Please, select a single vertex at the center of your mesh:" + center_vertex)
center_t = cmds.xform(center_vertex, q=True, ws=True, t=True)
object = center_vertex.rpartition(".")[0]
cmds.select(object + ".vtx[*]")
vertices = cmds.ls(sl=True, fl=True)
to_select = []
axes = "xyz"
selected_axis_index = sai = axes.index(AXIS[1])
for v in vertices:
vt = cmds.xform(v, q=True, ws=True, t=True)
if AXIS[0] == "+" and vt[sai] > center_t[sai] + THRESHOLD:
to_select.append(v)
elif AXIS[0] == "-" and vt[sai] < center_t[sai] - THRESHOLD:
to_select.append(v)
cmds.select(to_select, r=True)