PDA

View Full Version : Battling with a script to create specified number of color entries in a 2d ramp


LinchpinZA
03-28-2011, 06:09 PM
Hey guys.

I am a real newbie at scripting so excuse my stupidity. But basically I'm trying to create a script that creates a 2d ramp texture with a specified amount of color entries all evenly spaced.

So running the script with say a specified value of 300, would create a ramp texture with 300 colors , all evenly spaced (1/300 etc).

at the moment my script looks like this..

//////////////////////////////////////////////
{
createNode ramp -n "rampSpecial"

int $i;
for ($i = 0; $i<300; $i++) {
float $d = 1/300;
for ($d = 0; $d<1; $d++) {

setAttr ("rampSpecial"+".colorEntryList["+$i+"]"+".position"+$d+);
}
}

///////////////////////////////////////////

I'm currently getting a syntax error with the last line.
What I'm trying to do here is input an integer that counts from 1 to 300 in place of the colorEntryList value, and then the $d is my attempt at trying to evenly space the color entry values by inputting it into the ".position" input.

I'm not even sure if it's only my syntax that is wrong or whether its the way I'm trying to create more color entries which is incorrect aswell.

As you can tell I'm pretty clueless and I'm sure it may make some of you /ignore this post completely :p. But I would very much appreciate some help here, I know this is probably a really simple script to make.

Thanks,
Chris.

Robert Bateman
03-28-2011, 06:25 PM
You are missing a } at the end. You also seem to be adding ..... ?
setAttr ("rampSpecial"+".colorEntryList["+$i+"]"+".position" + $d + );

\edit
Personally I don't much care for the style of

for () {
}

You'll have less problems of this sort using:

for ()
{
}

just my 2cents...


\p.s.

this might be a nice thing to do:

setAttr -size 300 ("rampSpecial"+".colorEntryList);

and in addition, not sure what you are doing with $d? Should be 2 variables perhaps?


float $d = 1/300;
for ($d = 0; $d<1; $d++) {

LinchpinZA
03-28-2011, 10:33 PM
Hey Robert,
Thanks for the reply and the helpful tips. I've had another attempt at the script after your post and doing some more reading.

You are missing a } at the end. You also seem to be adding ..... ?
setAttr ("rampSpecial"+".colorEntryList["+$i+"]"+".position" + $d +);
Yeah over here I was trying to make the "colorEntryList" create more entries counting up in integers
to the specified maximum value of 300. And then I wanted each of these new entries to be evenly spaced along the ramp, so the final result would be 300 colors on a ramp with 299 equal spaces between each color. But as you can tell I failed :blush:

With $d I was trying to divide 1 by 300, to give me a float value for the spacings between the colors on the ramp. I then wanted to add this float value to each respective colorEntry's position to space out the 300 colors evenly. I know now that I was supposed to convert the numbers to floats to give me a float output and also divide by 299 instead...float $d = (float (1) /float (299));

Also I tried this like you said after creating a node called rampSpecial..
setAttr -size 300 ("rampSpecial"+".colorEntryList");
Maya ran that line fine with no complaints but it didn't create 300 color entries. The ramp remained remains the same with it's default 3 inputs.

I thought I should start with something simpler.. making a ramp with 6 colors spaced randomly and then trying to space these color entries evenly along the ramp into 5 equal divisions using a mel script.
I know the steps would be:
-list each color entry,
-for each consecutive entry add 0.2 to the position value.

so it gives me a result like ..

"rampSpecial.colorEntryList[0].position" 0;
"rampSpecial.colorEntryList[1].position" 0.2;
"rampSpecial.colorEntryList[2].position" 0.4;
"rampSpecial.colorEntryList[3].position" 0.6;
"rampSpecial.colorEntryList[4].position" 0.8;
"rampSpecial.colorEntryList[5].position" 1;

I know that using
int &i;
for ($i = 0; $i<6; $i++) {
}
creates a variable $i that counts up.


and that this bit of script
float $c = (float (1) /float (5));

{
}
would give me an output of 0.2 , but in the end I would need to ++ add this value to each consecutive colorEntry's position. How do I do this? :cry:


Any further help would be greatly appreciated :bowdown:

CGTalk Moderation
03-28-2011, 10:33 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.