Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active April 9, 2021 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/69d7849b905244cfbaf0c51cecd271e3 to your computer and use it in GitHub Desktop.
Save BigRoy/69d7849b905244cfbaf0c51cecd271e3 to your computer and use it in GitHub Desktop.
Maya - Reset lattice tweaks on selected lattice points by a percentage (quick 'n' dirty script)
import itertools
import maya.api.OpenMaya as om
from maya import cmds
def grouper(iterable, n, fillvalue=None):
"""Collect data into fixed-length chunks or blocks
Examples:
grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
"""
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
def reset_lattice_tweaks(points, percent):
points = [pt for pt in points if cmds.nodeType(pt) == "lattice"]
points = cmds.ls(points, flatten=True)
if not points:
raise RuntimeError("No valid lattice points passed.")
lattices = list(set(cmds.ls(points, objectsOnly=True, type="lattice")))
points = set(points)
# Store all points of the lattices
pts_current = {}
pts_original = {}
for lattice in lattices:
all_points = cmds.ls(lattice + ".pt[*][*][*]", flatten=True)
# Current points
positions = cmds.xform(all_points, query=True, objectSpace=True, t=True)
positions = list(grouper(positions, 3))
for pt, pos in zip(all_points, positions):
pts_current[pt] = pos
# Remove all lattice tweaks
cmds.lattice(lattice, edit=True, removeTweaks=True)
# Non-deformed points
positions = cmds.xform(all_points, query=True, objectSpace=True, t=True)
positions = list(grouper(positions, 3))
for pt, pos in zip(all_points, positions):
pts_original[pt] = pos
# Perform blend and restore positions
for pt in all_points:
if pt in points:
# Reset by percentage
current = om.MVector(pts_current[pt])
original = om.MVector(pts_original[pt])
diff = original - current
current += diff * percent
current = list(current)
cmds.xform(pt, objectSpace=True, t=current)
else:
# Keep position before reset tweak
cmds.xform(pt, objectSpace=True, t=pts_current[pt])
sel = cmds.ls(selection=True)
reset_lattice_tweaks(sel, percent=0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment