Skip to content

Instantly share code, notes, and snippets.

@masqu3rad3
Last active June 26, 2021 21:47
Show Gist options
  • Select an option

  • Save masqu3rad3/4ebf1cc27e27331e090ee0be1afaf1a2 to your computer and use it in GitHub Desktop.

Select an option

Save masqu3rad3/4ebf1cc27e27331e090ee0be1afaf1a2 to your computer and use it in GitHub Desktop.
find the in-between target values on a given base shape
from maya import cmds
import maya.OpenMayaAnim as oa
import maya.OpenMaya as om
def get_inbetween_values(blendshape_node, target_name):
# get the bs api mobject
bs_sel = om.MSelectionList()
bs_sel.add(blendshape_node)
bs_mobj = om.MObject()
bs_sel.getDependNode(0, bs_mobj)
# get affected shapes api MObject
transform_name = cmds.listConnections("{0}.outputGeometry".format(blendshape_node))[0]
mesh_name = cmds.listRelatives(transform_name, children=True)[0]
mesh_sel = om.MSelectionList()
mesh_sel.add(mesh_name)
mesh_dag = om.MDagPath()
mesh_mobj = om.MObject()
mesh_sel.getDagPath(0, mesh_dag)
mesh_sel.getDependNode(0, mesh_mobj)
mesh_mfnmesh = om.MFnMesh(mesh_dag)
# function set for Blendshape
m_bs_func = oa.MFnBlendShapeDeformer(bs_mobj)
# find the target index from name
attr = blendshape_node + '.w[{}]'
weightCount = cmds.blendShape(blendshape_node, q=True, wc=True)
for index in range(weightCount):
if cmds.aliasAttr(attr.format(index), q=True) == target_name:
target_index = index
break
if target_index == None:
raise Exception("Target name cannot be found in the blendshape node")
target_index = deformers.get_bs_index_by_name(blendshape_node, target_name)
# get the target item index list
m_target_arr = om.MIntArray()
m_bs_func.targetItemIndexList(target_index, mesh_mobj, m_target_arr)
if m_target_arr.length() == 1:
return None
# drop the last element of array
m_target_arr.remove(m_target_arr.length()-1)
# return the value in range 0.0 - 0.99
return [(x - 5000) / 1000.0 for x in m_target_arr]
test=get_inbetween_values("blendShape1", "target_sphere")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment