How can i select a object and delete its material and material connections nodes with mel script?

How can i select a object and delete its material and material connections nodes with mel script?

jhonatan.chambi
Enthusiast Enthusiast
1,516 Views
3 Replies
Message 1 of 4

How can i select a object and delete its material and material connections nodes with mel script?

jhonatan.chambi
Enthusiast
Enthusiast

Hello everyone i'm new en MEL script and i want to know how can i delete materials and all connections on it? then i would like to have just lambert by deafult,  i try and try and i can do that. greetings for your help.

jhonatanchambi_0-1715312952916.png

Greetings from Machu Picchu- Peru

0 Likes
1,517 Views
3 Replies
Replies (3)
Message 2 of 4

FirespriteNate
Advocate
Advocate

You have to be really careful when you start thinking about deleting shading nodes, especially outside of very basic test environments. You can't often just delete entire connected node networks safely, because you have to make sure parts of them are not being used by other objects in the scene. How do you know the material is not being used by another object you forgot to select? Maybe the connected material is only used by the selected object, but what if it uses a texture that is also used by another material? etc. A lot of node network traversal is involved and a lot of cross-checking and it gets pretty tedious pretty quick.

 

Additionaly, the material node you typically see and edit is not what's directly connected to the mesh, it's attached via a shadingGroup/shadingEngine node.

 

Making scripts like this is all about using the listConnections and listHistory commands (multiple times) to traverse the node graph and make decisions about what you find as you go.

Here's a simple example that will simply print the material nodes found for the selected mesh objects:

{
    // get the selected meshes
    string $selection[] = `ls -sl -type mesh -dag`;
    // first we have to find out what shadingEngine(s) are connected 
    string $engines[] = `listConnections -type shadingEngine $selection`;
    // now we have to find what materials the engines are connected to:
    string $materials[] = `listConnections -type lambert $engines`;
    // we may have multiple materials, so iterate over each individually
    for ($mat in $materials)
    {
        print("Material: "+$mat+"\n");
        // listConnections is useful for the immediate connection info, 
        // but if we want more network nodes, we can also use listHistory
        string $network[] = `listHistory $mat`;
        print "nodes:\n";
        print $network;
        print "\n";
    }
}
Message 3 of 4

jhonatan.chambi
Enthusiast
Enthusiast

hi sensei, i have a this message on script editor

jhonatanchambi_0-1715564047022.png

Thanks for your help. Greetings

0 Likes
Message 4 of 4

FirespriteNate
Advocate
Advocate

I'm not even sure how that can happen 🤔, MEL doesn't even support an array of arrays iirc, so I don't know how $mat could be an array if we're iterating over $materials. MEL is a bit picky about changing a variable's type once it's initialised though, so it could be you already have a string array named $mat from somewhere else, or maybe you tried running individual lines of the test code in isolation? (Python doesn't have this issue).

Make sure you only execute the complete block of code in one go, not individual lines, and try it from a clean boot of Maya, so you don't have any cross-contamination of variable names (this is also why the example code is inside { curly } braces). You could also just try renaming $mat to something unique in the two places it appears in the code.

That test code works fine for me on 2022 and 2024, maybe there's something non-standard in your scene I've not accounted for? but that seems unlikely based on the reported error...