#!/usr/bin/env python # -*- coding: utf-8 -*- ''' # Author: Wanshihui # Date: 2023-11-27 # Version: 1.0 # Description: Copy skinCluster from source models to target models ''' import maya.cmds as cmds def copy_skinCluster_to_target(): # Get all models that start with "CSW_" source_models = cmds.ls("CSW_*", type="transform") if not source_models: cmds.warning("No model starting with 'CSW_' was found in the scene.") return for src in source_models: # Get all models that start with "CSW_" suffix = src[4:] # Find the target model with the corresponding suffix. target = suffix if not cmds.objExists(target): cmds.warning(f"No corresponding target model found: {target}") continue # Find the skinCluster of the src model skin_clusters = cmds.ls(cmds.listHistory(src), type='skinCluster') if not skin_clusters: cmds.warning(f"{src} No skinCluster is bind.") continue skin_cluster = skin_clusters[0] # Copy skinCluster to the target model try: # First, delete any existing skinClusters in the target model (if have). target_skin_clusters = cmds.ls(cmds.listHistory(target), type='skinCluster') if target_skin_clusters: for tsc in target_skin_clusters: cmds.delete(tsc) # Perform replication of skinCluster weights # Bind a new skinCluster new_skin_cluster = cmds.skinCluster(cmds.skinCluster(skin_cluster, q=True, inf=True), target, toSelectedBones=True)[0] # copy skin weights cmds.copySkinWeights(ss=skin_cluster, ds=new_skin_cluster, noMirror=True, surfaceAssociation='closestPoint', influenceAssociation=['name', 'closestJoint']) print(f"From skinCluster to '{src}'copy'{target}' successful!") except Exception as e: cmds.warning(f"From'{src}'copy skinCluster to'{target}'failed: {e}") print("ALL skinCluster copy successful!") # If run this script directly if __name__ == "__main__": copy_skinCluster_to_target()