View Full Version : Applying textures

03 March 2008, 02:50 PM
Is there any easy way to apply/change a texture to a simple NURB in MEL? I've found some examples on forums, but I'm still quite hazy about the process. The MEL command reference doesn't seem to have any texture commands of the sort and google yielded nothing useful. When I tried echoing all commands, Maya uses setAttr, but it never seems to specify what texture is actually being applied.

My goal is to have a window with a modelEditor and a few buttons that toggle in turn, say, "wood", "metal" and "stone".


03 March 2008, 07:52 AM
hi wjpiovano,

well you can change between different textures to your obj : by switching the file texture name on the file node.

say you have blinn1 shader with color mapped to file1 node (this is the file node which reads ur texture rite ?) so you can either just change your texture path from say:

sourceimages/wood.jpg <> sourceimages/metal.jpg <> sourceimages/stone.jpg

by using
setAttr -type "string" file1.fileTextureName "sourceimages/wood.jpg"; or
setAttr -type "string" file1.fileTextureName "sourceimages/stone.jpg"; or
setAttr -type "string" file1.fileTextureName "sourceimages/metal.jpg";

OR if you are looking at changing between different shaders, you have to find the shading groups of each of the shader like for example for your blinn1 shader you wil have blinn1SG

so you can assign your object corresponding shader by :

sets -e -forceElement blinn2SG $object; or
sets -e -forceElement blinn1SG $object; and so on..

hope this helps ?

03 March 2008, 08:28 AM
Hi wjpiovano,

maybe these 2 threads can give you some ideas... :)
Assigning texture -->
get faces from object based on shader... -->

hope that helps a bit... :)

kind regards


03 March 2008, 03:52 AM
Thanks, both very informative answers. I've taken a look at all the stuff, and while much is clearer to me, I still have some hazy spots. First of all it seems to be safer to use the setAttr as opposed to the AE function, since it's in-built.

When I create a nurb, it gets the default shading node. It seems I can use shadingNode command to assign the blinn shader to the nurb, to start with. Does this guarantee the presence of the "file1" which controls what texture is applied? I'm wondering if my idea is even correct; that is, the "file1" is present only when a shader has been applied and its textureName attribute determines what texture is loaded.

It seems to be heading to something like this:
proc texture()
string $sel[] = `ls -sl -l`;
string $obj[] = `listRelatives -s -typ mesh $sel[0]`;
string $shadersOnObj[] = `listConnections -type "shadingEngine" -et 1 -s 0 -d 1 $obj[0]`;
string $shaderType = nodeType(`connectionInfo -sfd ($shadersOnObj[0]+".surfaceShader")`);
string $shader[] = `listConnections -t $shaderType $shadersOnObj[0]`;
string $shaderFile[]= `listConnections -t file $shader[0]`;

if((`size $shaderFile[0]`) > 0)
setAttr -type "string" $shaderFile[0].fileTextureName "C:/Program Files/Maya PLE/brushImagesBrick2.jpg";
else if((`size $shaderFile[0]`) == 0)
$shaderFile[0] = `createNode "file"`;
connectAttr -f ($shaderFile[0]+".outColor") ($shader[0]+".color");
setAttr -type "string" $shaderFile[0].fileTextureName "C:/Program Files/Maya PLE/brushImagesBrick2.jpg";

button -l "brick" -c "texture(\"c:/bricks.jpg\")";
button -l "wood" -c "texture(\"c:/wood.jpg\")";
Though it doesn't seem to like the setAttr. Hope I'm not confusing the whole issue. Also, I'm performing this on a NURB; does it make a difference?

Thanks for the patience, I'm still newish to MEL. :D

03 March 2008, 09:50 PM
Hi Will,

jup if using nurbs to get the shape you need to change mesh to nurbsSurface instead...


string $obj[] = `listRelatives -s -typ mesh $sel[0]`;

should be

string $obj[] = `listRelatives -s -typ nurbsSurface $sel[0]`;


or you can make a function where it can handle both poly and nurbs... :)

string $sel[] = `ls -sl -l`;
string $typ = objectType(`listRelatives -s $sel[0]`);
string $obj[] = `listRelatives -s -typ $typ $sel[0]`;

should make it work with both nurbs and poly's...
if it doesn't let me know and I will have a closer look at it... :)

kind regards


CGTalk Moderation
03 March 2008, 09:50 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.