Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created March 20, 2020 09:41
Show Gist options
  • Save BigRoy/8f04edac8696ef4c0f0ce01b2cfc774f to your computer and use it in GitHub Desktop.
Save BigRoy/8f04edac8696ef4c0f0ce01b2cfc774f to your computer and use it in GitHub Desktop.
Snap multiple objects by vertex index to target vertex.
"""Snap multiple objects by vertex index to target vertex.
1. Select all objects you want to snap
2. Add the target vertex to your selection.
3. Run script
Make sure each object has the same topological order because
this just simply snaps the same vertex number of each object
to the selected target vertex.
It could be for example used for correcting freeze transformed
blendshapes back to their original position, see:
https://nilouco.blogspot.com/2020/03/dptecnica-08-frozen-blendshapes.html
This does not take into account any rotations and just snaps
the translation. It is possible to take into account rotation
and scale too with three points that are not in a single plane.
But that has some edge cases which would fail, e.g. all three
points residing in a single 3D flat plane.
"""
from maya import cmds
import maya.api.OpenMaya as om
selection = cmds.ls(sl=1)
assert len(selection) > 1, "Must select at least two objects"
target_component = selection.pop()
assert ".vtx[" in target_component, "Make sure to select a vertex of the target object as last object in the selection"
target_pos = cmds.xform(target_component, query=True, worldSpace=True, translation=True)
target, component = target_component.split(".")
for src in selection:
src_component = "{0}.{1}".format(src, component)
src_pos = cmds.xform(src_component, query=True, worldSpace=True, translation=True)
diff = list(om.MVector(target_pos) - om.MVector(src_pos))
cmds.xform(src, relative=True, worldSpace=True, translation=diff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment