Created
November 25, 2012 22:40
-
-
Save mstevenson/4145716 to your computer and use it in GitHub Desktop.
Maya MEL script that breaks up a mesh based on materials. Created by Andrew Coggeshall at Basenji Games.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Poly Separate | |
// Copyright (C) 2012 Basenji Games | |
// Licensed under the MIT license | |
string $selection[] = `ls -sl`; | |
sepMat($selection[0]); | |
global proc sepMat(string $object){ | |
string $shadingGroups[] = getSGsFromShape($object); | |
string $ParentName = ($object + "_lightMap_Group"); | |
group -empty -n $ParentName; | |
for ($i = 0; $i < size($shadingGroups) ; $i = $i + 1) | |
{ | |
string $theMaterial[] = `listConnections -d off -s on $shadingGroups[$i]`; | |
string $clone = ($object + "_" + $theMaterial[0]); | |
duplicate -n $clone $object; | |
parent $clone $ParentName; | |
string $material[] = `listConnections -d off -s on $shadingGroups[$i]`; | |
select $clone; ConvertSelectionToFaces; | |
string $tempset = `sets`; | |
string $tempgrps[] = `listConnections -type shadingEngine $material[0]`; | |
select `sets -int $tempset $tempgrps[0]`; | |
InvertSelection; | |
delete; | |
} | |
delete $object; | |
} | |
proc string[] getSGsFromShape( string $shape ) | |
{ | |
string $myShapeNode[] = `listRelatives -children -shapes $shape`; | |
string $shadingEngines[]; | |
if ( `objExists $shape` ) | |
{ | |
string $dest[] = `listConnections -destination true -source false | |
-plugs false -type "shadingEngine" $myShapeNode[0]`; | |
if ( size( $dest ) ) | |
{ | |
string $select[] = `ls -sl`; | |
select -r -ne $dest; | |
$shadingEngines = `ls -sl`; | |
select -r $select; | |
} | |
} | |
return $shadingEngines; | |
} |
Thanks, very useful
Thanks, I was looking for such a thing. I took the liberty to do some corrections and cleaning.
I cannot correctly paste code here, so this is the link: https://pastebin.com/5vhV2EeT
# coding=utf-8
from maya import cmds
def getSGs(in_obj_name):
# type: (str) -> list[str]
if not cmds.objExists(in_obj_name): return []
shadingEngines = set() # type: set[str]
ShapeNodes=cmds.listRelatives(in_obj_name, shapes=1, children=1) or []# type: list[str]
if not ShapeNodes : return []
for shape in ShapeNodes:
dest=cmds.listConnections(shape, source=False, plugs=False, destination=True, type="shadingEngine") # type: list[str]
if not dest : continue
shadingEngines.update(dest)
return list(shadingEngines)
def sepMat(objectName, suffixName = "_Splitted"):
# type: (str, str) -> None
shadingGroups=getSGs(objectName)
if shadingGroups.__len__() <= 1: return
nodeParentName = (objectName + suffixName) # type: str
if not cmds.objExists(nodeParentName):
nodeParentName = cmds.group(empty=1, n=nodeParentName) # type: str
for i in range(shadingGroups.__len__()):
curMatSg=shadingGroups[i]
cloneObjName = (objectName + "_" + curMatSg) # type: str
cloneObjName = cmds.duplicate(objectName, n=cloneObjName)[0] # type: str
cloneObjName = cmds.parent(cloneObjName, nodeParentName)[0] # type: str
polyFacesSet = cmds.sets(cloneObjName + '.f[0:]') # type: str
sgFacesSet = cmds.sets(cmds.sets(curMatSg, un=polyFacesSet)) # type: str
cmds.delete(cmds.sets(polyFacesSet , subtract=sgFacesSet), polyFacesSet, sgFacesSet)
else: cmds.delete(objectName)
def runScript(*args, **kw):
selection = cmds.ls(sl=True, objectsOnly=True, noIntermediate=True)
for sel in selection:
try: sepMat(sel)
except : continue
cmds.select(cl=True)
if __name__ == "__main__":
runScript()
More fast && clean
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for your help. I have solved it perfectly.