View Full Version : Texture channel/resolution question

01 January 2013, 03:23 PM
Hey guys!
I'm wondering if someone of you guys can help me to change Texture channel and Texture resolution attribute values in Shader via mel/python. I've tried to use what Maya says in script Editor when changing this values via UI but without success.

01 January 2013, 04:35 PM
This is actually not quite trivial. I've scanned the MEL files involved, and there's a lot going on behind the scene.

Most importantly, all the functionality is built into local procedures in that MEL file, so you can't access them from your own scripts.

Instead of trying to replicate the MEL code that the developers put in there (which would be timeconsuming and rather complex), I suggest simply interacting with the given UI via MEL, i.e. make Maya believe you actually clicked on the channels or resolution menus.

Because the actual code gets invoked on the -selectionChanged command of the optionMenuGrp (the dropdown menu in the attribute editor), simply changing what value is selected via MEL will do the trick:

// define the shader you want to perform this procedure on
$shader = "lambert2";
// select it so it displays in the Attribute Editor
select $shader;
// get its nodeType (e.g. lambert, blinn, etc.)
$shaderType = `nodeType $shader`;
// create the name for the menu attached to this AETemplate's Texture Channel
$channelMenu = ($shaderType + "_channelMenu");
// create the name for the menu attached to this AETemplate's Texture Filter
$qualityMenu = ($shaderType + "_qualityMenu");

// now we can select the values we'd like
// note that $_Value have to be set to valid values you'd find in that menu

// valid channels are "None", "Color", "Transparency", etc.
string $channelValue = "Color";
// valid quality values are 1 - 5 (Default, Low ... Highest)
int $qualityValue = 5;

// set the Texture Channel. Note that if no texture is connected to the Channel
// you defined, the value will jump back to the previous value or "None"
if(`optionMenuGrp -q -ex $channelMenu`) {
// we eval Deferred so that the code doesn't get executed before the AE UI is built
evalDeferred("optionMenuGrp -e -value " + $channelValue + " " + $channelMenu);

// set the Texture Resolution. Note that this is only possible if
// a texture is actually connected to the shader

if( (`optionMenuGrp -q -ex $qualityMenu`)
&& (`optionMenuGrp -q -en $qualityMenu`) ) {
evalDeferred("optionMenuGrp -e -select " + $qualityValue + " " + $qualityMenu);

See if that does the trick for you. It's a rather ugly hack but given the right circumstances (i.e. prior knowledge of your shader and its texture connections) it should work.

Making this more robust would involve querying to see what kind of textures are connected to which channels on your shader and only executing certain code if a given set of conditions is met; after all, you can't change the resolution on a texture if there is no texture connected to that particular channel; also, it is not valid to select a channel that has no texture connected to it.

01 January 2013, 08:49 PM
Wow! Thank you very much! Broke my brain looking for solution :). Your code works perfectly! Like you said valid channels are only "None", Color" and "Transparency". But what about "Combined Textures"? I get error about arguments - expected 1 but got 2 instead.

01 January 2013, 08:50 PM
Please paste the error from Maya so I can take a look.

01 January 2013, 09:38 PM
Here is the error and the line where it occurs:

optionMenuGrp -e -value Combined Textures lambert_channelMenu;
// Error: line 1: Too many arguments. Expected 1, found 2. //

I have changed "Color" to "Combined Textures"

01 January 2013, 02:13 AM
you have to enclose it in quoatation marks "":

optionMenuGrp -e -value "Combined Textures" lambert_channelMenu

01 January 2013, 12:59 PM
Thank you! Now it works perfectly. I guess this is because "Combined Textures" is two strings and "Color" only one.

CGTalk Moderation
01 January 2013, 12:59 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.