Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Programming Beginner hit a wall...

elliot.joe.sylvester
Community Visitor

Programming Beginner hit a wall...

elliot.joe.sylvester
Community Visitor
Community Visitor

Hi Everyone.

 

I'm attempting to write a neat little script that cleans up geometry with per-face shader assignment.

 

What I have so far selects the shaders and makes quick selection sets based on the faces of each shader.

This works great but I want to take it a step further.

 

Ideally there are a few things I want to achieve that I have not up until now.

 

I want the geometry to be extracted and separated without breaking the shader assignment.

I want to have all the geometry with the same shader assigned be merged and named as per it's original shader assignment.

(see link at the bottom for what I currently have)

 

I'm currently dealing with a lot of poorly made per-face shader assignment assets and saw the opportunity.

 

Thanks in advance.

 

Elliot.

 

 https://docs.google.com/document/d/1j9LvZkETmg4i9abdB3TPtNpC-mkfi0DMmNBwZR6cjsY/edit?usp=sharing

 

0 Likes
Reply
413 Views
1 Reply
Reply (1)

Anonymous
Not applicable

Hi elliot

 

I dont have access to your test data so its difficult to know exactly what you are dealing with, but I have thrown together an example of an alternative way of separating polygons by material assignments for you:

 

import maya.cmds as cmds

meshes = maya.cmds.ls( type="mesh" ) #select all geo objects and combine
if len(meshes) > 1:
    cmds.select( meshes )
    combined = cmds.polyUnite( n='combinedGeometry', ch=False )
elif len(meshes) == 1:
    combined = meshes[0]
else:
    cmds.error("No meshes in scene")

sgs = cmds.ls(type="shadingEngine")
for sg in sgs:
    faces = cmds.sets(sg, q=True)
    if faces is not None and len(faces):
        cmds.select(sg)
        cmds.polyChipOff(ch=False, kft=True, dup=False, off=0)
    
cmds.polySeparate(combined, ch=False)

In essence the script combines the meshes like yours did and then iterates over all shading groups (materials) in the scene, selects each set of faces and runs the polyChipOff command on those faces which cuts them out of the mesh. At the end of this process it just runs the polySeparate method which extracts all of the meshe shells as separate polygon shape objects.

 

The only thing this doesn't do which you may want, is that it doesn't then go back and recombine objects which share the same material. I am sure you can do this quite easily though just by re-iterating the material sets and combining those objects which share the same material.

 

Hope it helps

 

Cheers

 

Mike

0 Likes