Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Proxy Skinning Utilities
- Example:
- # Select faces on a mesh
- from proxy import extractFaces
- newMesh = extractFaces()
- # Select Source Mesh and Destination Mesh
- from proxy import skinTo
- newSkinCluster = skinTo()
- """
- from maya import cmds
- from functools import wraps
- __author__ = 'Sean.Nolan'
- __doc__ = 'Utility functions for Proxy skinning'
- def d_undo(func):
- """
- Undo Decorator
- :param func: incoming function
- :return:
- """
- @wraps(func)
- def _undo_func(*args, **kwargs):
- try:
- cmds.undoInfo(ock=True)
- return func(*args, **kwargs)
- finally:
- cmds.undoInfo(cck=False)
- cmds.undo()
- return _undo_func
- @d_undo
- def extractFaces(faceList=None, newName=None, keepOriginal=False, copySkinning=False):
- """
- Extract faces from mesh and make a copy
- :param faceList: List of faces to extract
- :type : list
- :param newName: New mesh name
- :type: str
- :param keepOriginal: Make a copy of mesh before extraction
- :type: bool
- :param copySkinning: Copy skin weights over to extracted mesh
- :type: bool
- :return: Extracted mesh name
- """
- if not faceList:
- selectedFaces = cmds.ls(sl=True)
- else:
- selectedFaces = faceList
- if not bool(cmds.filterExpand(selectedFaces, ex=True, sm=34)) or []:
- raise RuntimeError("Must select one or more Faces")
- shape = cmds.listRelatives(p=1)
- curMesh = cmds.listRelatives(shape, p=1)[0]
- cmds.select(curMesh + '.f[:]', tgl=1)
- extractFaces = cmds.ls(sl=1)
- # Save current pose
- infList = cmds.skinCluster(curMesh, q=True, wi=True)
- curPose = cmds.dagPose(infList, save=True, name='extractInPose')
- # Go to bind pose before copying weights
- bindPose = cmds.dagPose(infList, q=True, bindPose=True)
- cmds.dagPose(bindPose, restore=True)
- newMesh = cmds.duplicate(curMesh)[0]
- # Rename mesh
- if newName:
- newMesh = cmds.rename(newMesh, newName)
- # Copy skin weights over to new mesh
- if copySkinning:
- skinTo(curMesh, newMesh)
- if not keepOriginal:
- cmds.delete(selectedFaces)
- # Swap current mesh to new mesh
- for i in range(len(extractFaces)):
- extractFaces[i] = extractFaces[i].replace(curMesh, newMesh)
- cmds.delete(extractFaces)
- # Clean new mesh
- if copySkinning:
- cmds.bakePartialHistory(curMesh, prePostDeformers=True)
- cmds.bakePartialHistory(newMesh, prePostDeformers=True)
- else:
- cmds.delete(newMesh, ch=1)
- cmds.dagPose(curPose, restore=True)
- cmds.delete(curPose)
- cmds.select(newMesh, r=1)
- return newMesh
- def skinTo(srcMesh=None, destMesh=None):
- """
- Skin from source to destination mesh
- :param srcMesh: Source mesh
- :type : str
- :param destMesh: Destination Mesh
- :type: str
- :return: New skinCluster name
- """
- # Test for selection
- if not srcMesh or not destMesh:
- meshes = cmds.ls(sl=1)
- if meshes:
- if len(meshes) >= 2:
- srcMesh = meshes[0]
- destMesh = meshes[1]
- else:
- raise RuntimeError("Two meshes need to be selected!")
- else:
- raise RuntimeError("Two meshes need to be selected!")
- # Get source skinCluster
- srcSkin = cmds.ls(cmds.listHistory(srcMesh), type='skinCluster')
- if not srcSkin:
- raise RuntimeError("{} does not have a skinCluster".format(srcSkin))
- # Check destination skinCluster
- dstSkin = cmds.ls(cmds.listHistory(destMesh), type='skinCluster')
- # Save current pose
- infList = cmds.skinCluster(srcSkin, q=True, wi=True)
- curPose = cmds.dagPose(infList, save=True, name='skinToPose')
- # Go to bind pose before copying weights
- bindPose = cmds.dagPose(infList, q=True, bindPose=True)
- if bindPose:
- cmds.dagPose(bindPose[0], restore=True)
- # Build destination skinCluster
- if not dstSkin:
- dstPrefix = destMesh.split(':')[-1]
- srcInfList = cmds.skinCluster(srcSkin[0], q=True, inf=True)
- dstSkin = cmds.skinCluster(srcInfList, destMesh,
- toSelectedBones=True, rui=False, n=dstPrefix + '_skinCluster')
- # Copy skin weights
- cmds.copySkinWeights(sourceSkin=str(srcSkin[0]),
- destinationSkin=str(dstSkin[0]),
- surfaceAssociation='closestPoint',
- influenceAssociation='name',
- noMirror=True)
- cmds.dagPose(curPose, restore=True)
- cmds.delete(curPose)
- cmds.select(cl=1)
- # Return result
- return dstSkin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement