View Full Version : Assign texture and PSD file dynamically

09-27-2008, 04:42 PM
I have a wall of tiles created dynamically, following the pattern of LW_1_1 to LW_1_10 for the first row, then LW_2_1 to LW_2_10 (you get the idea) and I want to create a script to assign them an individual texture dynamically. However, when I get to createRenderNode, I get entirely lost. I'll post what I've got below this. Hopefully my notes will make everything clear.

int $FCount = 1;
int $SCount = 1;
string $ObjBase = "LW_";
string $BaseNam = "LWS_";
string $FPath = "/Drawings/Lighbulb/Textures/LW/";

select -r ($ObjBase + $FCount + "_" + $SCount);

//Create Shader

shadingNode -asShader lambert -name ($BaseNam + $FCount + "_" + $SCount);

sets -renderable true -noSurfaceShader true -empty -name ($BaseNam + $FCount + "_" + $SCount + "SG");

//Select object to apply shader to

select -r ($ObjBase + $FCount + "_" + $SCount);

//Assign Shader to selected object

hyperShade -assign ($BaseNam + $FCount + "_" + $SCount);

//Currently not Fully understood, so DON'T TOUCH, need to learn more CURRENT ERROR IN DYNAMIC ALLOCATION. Wrong string/variable creation?

createRenderNodeCB -as2DTexture "" psdFileTex "defaultNavigation -force true -connectToExisting -source %node -destination LWS_1_1.color"

//rename file

rename psdFileTex1 ("psd_" + $FCount + "_" + $SCount) ;

//Assigns the correct image. Syntax is Psd Node in Maya assigned to "FilePath" Filepath is assigned via code to allow easy change of directory //ERROR IN DYNAMIC ALLOCATION. Wrong string/variable creation?

AEassignTextureCB "psd_1_1.fileTextureName" "/Drawings/Lightbulb/Textures/LW/1_1.psd" "Best Guess";

09-28-2008, 10:28 AM
Good thing: You tried to watch the stuff flooding out to the ScriptHistory when creating a shader, right? :]
I came across the very same road when starting mel back then... haaahhh the good ol times .... :D

okok: First thing here: It may work somehow (which it did in my case), but the actual workflow is much simpler! like:

string $texturePath = "c:/bla/blob.psd";

string $newShaderName = `shadingNode -asShader lambert`;
string $newFileNode = `shadingNode -asTexture psdFileTex`;
connectAttr -force ($newFileNode + ".outColor") ($newShaderName + ".color");
setAttr -type "string" ($newFileNode + ".fileTextureName") ($texturePath);

select -r $yourObjects;
hyperShade -assign $newShaderName;

Actually the hyperShade-command is bad style as its available in non-batch mode only and you need to select stuff for it. But it hanldes shadingGroup creation and connection which is quite useful. I mean you can handle this with a couple more lines as well. But its a little more tricky. Let me know if you need this.

09-29-2008, 09:26 PM
Ah you gotta use the ` marks for this stuff. Forgotten bout that. And yeh, that's much easier. The version I posted does occasionally work, it gets a bit stumped on occasions, telling me it can't find a node it should have pre-created... But the main problem was it wasn't very elegant, and good code usually is. I'll have a play with this bit and see if I can modify it to the way I want, no point getting it all done for me :) Think I understand the code, I'll let you know how I get on. Thanks loads.

09-30-2008, 09:03 AM
youre welcome! :)

The ` are for bundling a command-string and getting the return of it. There are actually 2 methods for this! Both have their benefits and drawbacks!!

string $bla = `getAttr "blaObj.attribute"`;

Very nice to have the command string almost unchanged. Just a ` front and back and you go. But! : you cannot kaskade stuff like that `command `command2 "bla"`` For this you'd have to use the other method:

string $bla = getAttr("blaObj.attribute");

Here you are able to put other commands into the arguments of the outside commands. like: command(command2("bla")) But: Each argument has to be separate! Flags have to be in quotation marks as well. This is very uncomfortable with calls that have lots of flags and args: see:

string $sel[] = `ls -sl -tr "obj*"`;
string $sel[] = ls("-sl", "-tr", "obj*");

Of course its a matter of taste as well. But I use both styles at the appropiate place. And as I like inline stuff I tend to use the 2nd style quite often.

Long things short: You could have the example that way as well:

string $texturePath = "c:/bla/blob.psd";

string $newShaderName = shadingNode("-asShader", "lambert");
string $newFileNode = shadingNode("-asTexture", "psdFileTex");
connectAttr -force ($newFileNode + ".outColor") ($newShaderName + ".color");
setAttr -type "string" ($newFileNode + ".fileTextureName") ($texturePath);

select -r $yourObjects;
hyperShade -assign $newShaderName;

10-11-2008, 04:52 PM
Here's what I've come up with. The loops however don't work. Do I need to initialise the incremental counter at the start of the loop or something? Be grateful if someone could check the code too, it seems to work other than the loops but I've probably made a mistake somewhere. Oh, and if someone could explain what these variables are doing. My guess is they create a string of words that when they are executed they do code. But that's just a guess.


//DO NOT MOVE THESE: These must stay out of the loops as they're initialisation functions

//The number of repeats for the OUTER loop
int $FMax=1;

//The number of repeats for the INNER loop
int $SMax=1;

int $FNum=1;
int $SNum=1;

//Prefix will later be used for UI. User can input any string to use as a root for the sequence. Currently only editable by hand.
string $NamPrefix =("LW");

//Object name base
string $NamBase = ($NamPrefix + "_");

//Shader name base
string $BaseShadNam =($NamBase + "S_");

//Texture name base
string $BaseTexNam =($NamBase + "T_");

//String that combines $FNum and $SNum to ease use.
string $CurNum;

//Write in the versions for obj,shader and texture
string $CurObjNam;
string $CurShadNam;
string $CurTexNam;

//Initialise Outer Loop. Will run whilst inequality is satisfied. Each run increments $FNum by one, and then runs the inner loop. $SNum is reset every cycle by a statement outside the inner loop
for ($FNum<=$FMax; ++$FNum) {

//Initialise INNER loop.

for ($SNum<=$SMax; ++$SNum) {

//INCLUDE IN <INNER> LOOP: These need to be re-compiled every repeat, as they reference values that the loop changes

//Combine the current ints into a string form that can be used anywhere
string $CurNum= ($FNum + "_" + $SNum);

//Write in the versions for obj,shader and texture
string $CurObjNam = ($NamBase + $CurNum);
string $CurShadNam = ($BaseShadNam + $CurNum);
string $CurTexNam = ($BaseTexNam + $CurNum);

//Insert path to CONTAINING FILE where textures are.
string $texturePath=("/" + $CurNum+ ".psd");

//This is used to allow easy allocation of the shader. The shader is NOT created, attributes are defined for WHEN it is created. A name is assigned, type is specified.
string $newShaderName = `shadingNode -name $CurShadNam -asShader blinn`;

//Similar to the above variable, the psd node is NOT created here. It is defined ready to be called.
string $newFileNode = `shadingNode -name $CurTexNam -asTexture psdFileTex`;

//This is where the psd Node AND shader nodes are CREATED. Created by calling up a non-existing node, which is then auto-created. PSD node is force-connected to colour of shader.
connectAttr -force ($newFileNode + ".outColor") ($newShaderName + ".color");

//This connects the PSD node output to the PSD file. Dynamically generated.
setAttr -type "string" ($newFileNode + ".fileTextureName") ($texturePath);

//Selects current object. Dynamic.
select -r $CurObjNam;

//Assigns the shader by calling up the variable where it is defined. Working on why/how...
hyperShade -assign $newShaderName;

} //END of INNER loop

//Reset $SNum to 1 to enable repeat of INNER loop

} //END of OUTER loop


10-13-2008, 08:45 AM
What about Reading The Fine Manual? ;]


int $i;
for ($i = 10; $i > 0; $i--) {

Actually you can omit the predefinition. It works wel if you just define the looping-integer in the for definition. But if you need it afterwards then ok: then you have to define it before! Otherwise its only available in the loop.

for ($i = 0; $i < 10; $i++)

10-13-2008, 05:51 PM
I would have read it if I'd had access to it. None the less, it is now bookmarked ;) Thanks for the help.

CGTalk Moderation
10-13-2008, 05:51 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.