Skip to content

Instantly share code, notes, and snippets.

@mstevenson
Created November 25, 2012 22:40
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mstevenson/4145716 to your computer and use it in GitHub Desktop.
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.
// 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;
}
@MineClever
Copy link

MineClever commented Jul 31, 2023

# 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