Skip to content

Instantly share code, notes, and snippets.

@AndresMWeber
Created March 15, 2017 00:08
Show Gist options
  • Save AndresMWeber/812926c9abed82a6bdfb3dfbeabbb2da to your computer and use it in GitHub Desktop.
Save AndresMWeber/812926c9abed82a6bdfb3dfbeabbb2da to your computer and use it in GitHub Desktop.
Add weights from influence based on distance
import maya.cmds as mc
import math
def mesh_verts_distance_from_point(point, transform_with_shape):
distance_between = lambda p1, p2: math.sqrt(sum([(a - b) ** 2 for a, b in zip(p1, p2)]))
shapes = mc.listRelatives(transform_with_shape, s=True, c=True, type='mesh')
distances = {}
if shapes:
mesh_verts = mc.ls("{MESH}.vtx[*]".format(MESH=shapes[0]), fl=True)
return {vert:distance_between(mc.xform(vert, ws=True, t=True, q=True), point) for vert in mesh_verts}
return distances
def normalize_vert_distance_dict(vert_dist_dict, tolerance=4.1):
for vert in vert_dist_dict.keys():
distance = vert_dist_dict[vert]
if distance <= tolerance :
vert_dist_dict[vert] = 1 - (distance /tolerance)
else:
vert_dist_dict.pop(vert)
return vert_dist_dict
def set_influence_based_on_distance(transform_with_mesh, influences, tolerance=3, additive=False):
skin_clusters = mc.ls(mc.listHistory(transform_with_mesh), type='skinCluster')
if skin_clusters:
skin_cluster = skin_clusters[0]
for influence in influences:
vert_distance_table = normalize_vert_distance_dict(mesh_verts_distance_from_point(mc.xform(influence, ws=True, t=True, q=True), transform_with_mesh), tolerance=tolerance)
for vert in vert_distance_table:
value = vert_distance_table[vert]
if additive:
value += mc.skinPercent(skin_cluster, vert, transform=influence, q=True)
mc.skinPercent(skin_cluster, vert, tv=(influence, value), normalize=True )
# Select a mesh that has a skinCluster and then any joints/influences that are already a part of the cluster influences.
# Play with the tolerance value to change the distance of mesh affected.
# Use the additive flag to add onto preexisting weights that were already assigned.
set_influence_based_on_distance(mc.ls(sl=True)[0], mc.ls(sl=True)[1:], tolerance=3, additive=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment