Substitute a material with another and connect previous material texture with MEL


#1

Hello, long time checking this website and after a while of being stuck (3 days) I thought about asking directly here.

I am new with MEL, i hardly know what I’m doing, so I check scripts online from other people that has similar problems, but yet I haven’t found a solution. This is what I’m trying to achieve:

Change all lambert materials to redShift materials.

What I could achieve so far:
Make the redshift material and connect the texture node from the lambert to the redShift material.

What I need to finish:
Assign the material to its respective object. This I could do, but the problem is that it selects the objects in order and then it doesn’t assign the materials properly.


select -r `listTransforms -geometry`;
string $selection[] = `ls -selection -long`; 
/* loop through all objects */
string $object;
int $contador = 0;
string $nomObjecte = ("RedShift" + $contador);
int $jaHePassat = 0;

    // list/get all lambert-based shaders in the scene
    string $shaders[] = `ls -type "lambert"`;
    	
for ( $object in $selection )
{        

  // iterate through each shader in the array:
      if ($jaHePassat == 0)
       {
            for ($item in $shaders)
            {        
                
                // find out what is connected to the shaders "color" attribute
            	string $colorTex[] = `listConnections ( $item + ".color" )`;
            	
            	// if nothing conncted, ignore:
            	if (!`size $colorTex`)	continue;
            	
            	// create a node of type "gammaCorrect"
            	string $gammaNode = `shadingNode -asShader RedshiftArchitectural`;
            	string $SG = `sets -renderable true -noSurfaceShader true -empty -name $nomObjecte`;
            	
            	// connect this gamma node between texture and shader:
            	connectAttr -f ( $colorTex[0] + ".outColor" ) ( $gammaNode + ".diffuse" );
            	connectAttr -f ( $colorTex[0] + ".outAlpha" ) ( $gammaNode + ".transparency" );
            	connectAttr -f ( $gammaNode + ".outColor" ) ( $SG + ".surfaceShader" );
            	
            	$jaHePassat = 1;
            }
         }
    	
        sets -forceElement $nomObjecte $object;
        $contador = $contador + 1;
        $nomObjecte = ("RedShift" + $contador);
}


I’ll gladly answer any questions regarding the code, thanks ina dvance for reading this.


#2

Fixed it!! I found out how to enter the connections of the lambert, I just had a revelation when I woke up.


//select -r `listTransforms -geometry`;
string $selection[] = `ls -selection -long`; 
/* loop through all objects */
string $object;
int $contador = 0;
string $nomObjecte = ("RedShift" + $contador);
   /* // list/get all lambert-based shaders in the scene
    string $shaders[] = `ls -type "lambert"`;
    
    // iterate through each shader in the array:
    for ($item in $shaders)
    {      
    	// list/get all lambert-based shaders in the scene
       // string $shaders[] = `ls -type "lambert"`;
        
        // find out what is connected to the shaders "color" attribute
    	string $colorTex[] = `listConnections ( $item + ".color" )`;
    	
    	// if nothing conncted, ignore:
    	if (!`size $colorTex`)	continue;
    	
    	// create a node of type "gammaCorrect"
    	string $gammaNode = `shadingNode -asShader RedshiftArchitectural`;
    	string $SG = `sets -renderable true -noSurfaceShader true -empty -name $nomObjecte`;
    	
    	// connect this gamma node between texture and shader:
    	connectAttr -f ( $colorTex[0] + ".outColor" ) ( $gammaNode + ".diffuse" );
    	connectAttr -f ( $colorTex[0] + ".outAlpha" ) ( $gammaNode + ".transparency" );
    	connectAttr -f ( $gammaNode + ".outColor" ) ( $SG + ".surfaceShader" );
    	
    }*/
    	
    	
for ( $object in $selection )
{        
        string $nomObjecte = ("RedShift" + $contador);
        string $shaders[] = `listRelatives $object`;
        string $myMaterial[] = `listConnections -type "shadingEngine" $shaders`;
        string $colorMaterial[] = `listConnections -type "lambert" $myMaterial[0]`;
    	string $colorTex[] = `listConnections ( $colorMaterial[0] + ".color" )`;
            	
    	// create a node of type "gammaCorrect"
    	string $gammaNode = `shadingNode -asShader RedshiftArchitectural`;
    	string $SG = `sets -renderable true -noSurfaceShader true -empty -name $nomObjecte`;
    	
    	// connect this gamma node between texture and shader:
    	connectAttr -f ( $colorTex[0] + ".outColor" ) ( $gammaNode + ".diffuse" );
    	connectAttr -f ( $colorTex[0] + ".outAlpha" ) ( $gammaNode + ".cutout_opacity" );
    	connectAttr -f ( $gammaNode + ".outColor" ) ( $SG + ".surfaceShader" );
        updateFileNodeSwatch($SG);
        
        
    	//assignCreatedShader "RedshiftArchitectural" "" $nomObjecte $object;
        sets -e -forceElement $nomObjecte $object;
        $contador = $contador + 1;
}

This code grabs the lambert material the selected object has, makes a new material and inputs the conections of the other material into the new one, and then renames the Shader Group to make it easier to attach it to the object again. If anyone knows how to actually remove the previous material or even make it check if it has another material if it doesn’t have a lambert go on ahead.