Trying to move images connected to attributes from one shader to another using mel


#1

I’m fairly new to coding but I’m trying to write a mel script that will copy attributes from one shader to a different type of shader.

This is what I’ve got so far:

//Script selects color value and reflection roughness value from first selected (vray) shader object and applys it to second (redshift) shader object
//select materials from objects
hyperShade -smn;
//put selections into an array
string $selection[] = `ls -selection`;
//create a variable that has the value of the first shader .color slot
vector $attrcolor = `getAttr ($selection[0] + ".color")`;
//create a variable that has the value of the first shader .reflectionGlossiness slot
float $attrroughness = `getAttr ($selection[0] + ".reflectionGlossiness")`;
//apply the value of the first shaders .color attribute to the second shaders .diffuse_color attribute
setAttr ($selection[1] + ".diffuse_color") ($attrcolor.x) ($attrcolor.y) ($attrcolor.z);
//apply the value of the first shaders .reflectionGlossiness attribute to the second shaders .refl_roughness attribute
setAttr (($selection[1] + ".refl_roughness"), $attrroughness);

Ok. So this works to take a color (which is an array value for R, G, and B values) and copy it to the second shader. It also works with numerical values (float values like reflection roughness) and applies those to the second shader. What if there were an image attached to the diffuse color slot though rather than a color? What kind of code would copy that over? I think it can somehow be done with a connectioninfo command but I can’t figure it out.

Any help would be greatly appreciated!


#2

You will have to check which components are connected.
For these types of tasks I heavily recommend python together with pymel, it makes life easier:


shaderA = pm.ls("shaderA")[0]
shaderB = pm.ls("shaderB")[0]

shaderB.color.set(shaderA.color.get()) # copy color values
shaderB.reflectionGlossyness.set(shaderA.reflectionGlossyness.get()) # copy other values

inputs = shaderA.color.inputs(p=True) # get the connection attribute e.g. file1.outColor
if len(inputs[0]) > 0: # check if we have a connection at all
    inputs[0] >> shaderB.color # connect it to the second shader


#3

Unfortunately, I’m stuck using mel because I’m updating an already existing script.

I figured it out though using a connectAttr command to copy the texture to the target.
Thanks!