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; | |
} |
Thank you for your help. I have solved it perfectly.
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
I don't think this is a documented command. However you can see it is a runtime command in the PyMel documentation: https://download.autodesk.com/global/docs/maya2014/en_us/PyMel/generated/functions/pymel.core.runtime/pymel.core.runtime.ConvertSelectionToFaces.html
Mel equivalent
ConvertSelectionToFaces;
is undocumented, however when you check your Script Editor, you can see this command callsPolySelectConvert 1;
which is located in PolySelectConvert.mel file inside your main Maya application. Infact, if you monitor your script editor, select an object, CONTROL + Right Click > To Faces >To Faces, and you convert to faces this way, the script editor callsConvertSelectionToFaces;
which just runsPolySelectConvert 1;
With Python, you can also do:
Same thing. I guess in Python,
Will just call the pymel command:
But you're right, it's not really documented.