Untitled

mail@pastecode.io avatar
unknown
python
14 days ago
3.3 kB
2
Indexable
Never
from maya.api import OpenMaya as om
from maya import cmds
import importlib
import skincluster_curve
importlib.reload(skincluster_curve)


def split_biped_chain_joints(mesh):

    sides = 'LR'
    limbs = 'arm', 'leg'
    spine_indices = [0, 1, 2, 3, 4]
    neck_indices = [0, 1, 2]
    upper_limb_indices = [0, 1, 2, 3]
    lower_limb_indices = [5, 6, 7, 8]

    if not cmds.objExists(mesh):
        cmds.error(f'mesh does not exist: {mesh}')

    skin_cluster = (cmds.ls(cmds.listHistory(mesh), typ='skinCluster') or [None])[0]
    if not skin_cluster:
        cmds.error(f'mesh does not have a skinCluster: {mesh}')

    infs = cmds.skinCluster(skin_cluster, q=True, inf=True) or []

    # ----- split spine weights
    spine_jnts = [f'spine_C0_{i}_jnt' for i in spine_indices]
    for jnt in spine_jnts:
        if not cmds.objExists(jnt):
            cmds.error(f'joint does not exist: {jnt}')

        if jnt not in infs:
            cmds.skinCluster(skin_cluster, e=True, ai=jnt, wt=0.0)

    spine_positions = [om.MVector(cmds.xform(jnt, q=True, ws=True, t=True)) for jnt in spine_jnts]

    start_dir = spine_positions[0] - spine_positions[1]
    end_dir = spine_positions[-1] - spine_positions[-2]

    start_cv_pos = spine_positions[0] + start_dir
    end_cv_pos = spine_positions[-1] + end_dir

    inbetween_positions = []
    for i, position in enumerate(spine_positions[1:-1]):
        spine_dir = spine_positions[i + 2] - position
        inbetween_positions.append(position + spine_dir * 0.5)

    cv_positions = [start_cv_pos] + inbetween_positions + [end_cv_pos]
    crv = cmds.curve(p=cv_positions, k=[0, 0, 1, 2, 3, 3], d=2)
    skincluster_curve.split_with_curve(mesh, spine_jnts, crv, d=3)

    cmds.delete(crv)

    # ----- split neck weights
    for i in neck_indices:
        jnt = f'neck_C0_{i}_jnt'
        if not cmds.objExists(jnt):
            cmds.error(f'joint does not exist: {jnt}')

        if jnt not in infs:
            cmds.skinCluster(skin_cluster, e=True, ai=jnt, wt=0.0)

    start_pos = cmds.xform(f'neck_C0_{neck_indices[0]}_jnt', q=True, ws=True, t=True)
    end_pos = cmds.xform(f'neck_C0_{neck_indices[-1]}_jnt', q=True, ws=True, t=True)
    crv = cmds.curve(p=[start_pos, end_pos], k=[0, 1], d=1)
    skincluster_curve.split_with_curve(mesh, [f'neck_C0_{i}_jnt' for i in neck_indices], crv, d=2)

    cmds.delete(crv)

    # ----- split arm and leg weights
    for side in sides:
        for limb in limbs:
            for indices in upper_limb_indices, lower_limb_indices:
                for i in indices:
                    jnt = f'{limb}_{side}0_{i}_jnt'
                    if not cmds.objExists(jnt):
                        cmds.error(f'joint does not exist: {jnt}')

                    if jnt not in infs:
                        cmds.skinCluster(skin_cluster, e=True, ai=jnt, wt=0.0)

                start_pos = cmds.xform(f'{limb}_{side}0_{indices[0]}_jnt', q=True, ws=True, t=True)
                end_pos = cmds.xform(f'{limb}_{side}0_{indices[-1]}_jnt', q=True, ws=True, t=True)
                crv = cmds.curve(p=[start_pos, end_pos], k=[0, 1], d=1)
                skincluster_curve.split_with_curve(mesh, [f'{limb}_{side}0_{i}_jnt' for i in indices], crv, d=2)

                cmds.delete(crv)
Leave a Comment