PDA

View Full Version : Help with Array and interpolation

 bontempos09-04-2006, 04:02 AMHello. I'm new using Mel, and I have this problem. I'll try to explain: I want to create a script which interpolates values over time where I can define the amplitude and frequence of interpolation. Then, I think, I would have to create an Array with values with will interpolate, like: float $minmax = rand(1,10); int$amp_array[] = {$minmax,$minmax,$minmax,$minmax,$minmax...}; (I know this is not the way to do it...but i dont know how, I would like to create this automatically...) or, i could simply: int$amp_array[] = {1,9,3,6,5,7,3...); After that, I would have to define Frequence: if ($frame_count > 5){$frame_count = 1; >>>>ACTION HERE<<<<< }else{ $frame_count++; } in frame 1, the value would be 1. Then, in frame 5, the value would be 9 (using the example above with numbers within parenteses). And it would interpolate (linear) over these frames. How can I do it, please? :banghead: Hoju 09-04-2006, 10:16 AM I’m not sure I understood everything you said, but I think I might be able to help you out a little bit. To create an array of random values you could try... //A procedure that returns random integers. proc int randInt(int$min, int $max) { //Get the random integer. int$value = rand($min,$max);

//Return the random integer.
return $value; }; int$amp_array[] = {randInt(1, 10), randInt(1, 10), randInt(1, 10), randInt(1, 10), randInt(1, 10)};

JBarrett
09-04-2006, 02:08 PM
If you want a large number of elements in the array (say, 100), adding a literal call for every element would take up a lot of space. It's easy to automate it with a loop, though. Using the randInt procedure that Hoju posted, set up the loop something like this...

// initialize variables
int $i,$numElements, $amp_array[]; // set$numElements with number of desired elements in array
$numElements = 100; // add random numbers to array for ($i=0; $i<$numElements; $i++)$amp_array[$i] = randInt(1,10); bontempos 09-04-2006, 03:52 PM thanks for the posts, it will be very useful anyway but I was thinking...Probably I need only to numbers at time. One to be the First, One to be the Last, and the interpolation'd occur between theses numbers: \\it starts with user definitions: float$min = 1;
float $max = 10; int$freq = 5;

if ($frame_count >$freq){
$prev =$next
$next = rand($min,$max);$frame_count = 1;
} else {
// for the first frame (intro)
if (frame = 1){
float $prev = rand($min,$max); float$next = rand($min,$max);
} else {
float $add = ($next/$freq)$prev += $add }$frame_count++
}

But....still seems not to work :shrug:

JBarrett
09-04-2006, 04:35 PM
There are a couple syntax problems in the update you posted, both on the same line...
if (frame = 1){1: What is "frame"? If it's a variable, it needs to be written "$frame". On top of that, there's no other reference to that variable anywhere else in the script so far, so it's a little confusing how that fits into everything else. 2: What you've written inside the IF condition actually assigns the value 1 to "$frame". For comparisons, you need two equals signs, like so (also fixing the variable problem):

if (\$frame == 1){Aside from that, the key thing that you have yet to indicate is how exactly this script will be used. What is going to be modified by the results of all the random number generation? If we know the desired goal, it will be easier to provide the proper assistance.

bontempos
09-04-2006, 05:40 PM
Well, the property could be, the intensity of a pointlight, or the x position of an object. It should work for almost every keyframable property. And, with the word Frame, I meant the current frame number...I thought it was a global var. Well, all I wanted, was to create two values, to interpolate at certain frequency, and, after it reaches the last frame assigned in the frequency value, the "next" value would be transfered to the "prev", and a new value would be assigned to "next"..so, it could continue interpolating forever.
I think there is a better way to do it. I'm trying to adapt this from after effects, where I can simply write "wiggle (4,10)". where 4 represents the freq, and 10 the amplitude. Differently from Rand(x,x1) which chooses values at each time without interpolate them. I hope you understand my problem, I apologise, thanks =)

JBarrett
09-05-2006, 01:59 AM
Sounds like you want to make an expression, not a whole script. An expression can be attached to a specific attribute and affect it in a number of ways, including varying the value of the attribute over time.

Here's a simple expression that sounds like it might work for what you want:
nurbsSphere1.translateX = noise(time*5)*5
To use this example, create a NURBS sphere. With the sphere still selected, right-click on the Translate X attribute in the channel box and choose "Expressions..." in the popup menu. An expression dialog will appear. Copy and paste the code above into the "Expression" text box at the bottom of the dialog, then click the "Create" button. The Translate X attribute in the channel box will turn purple. Scrub the timeline marker and notice the movement on the sphere.

To change the amplitude, change the 5 at the end of the expression. Higher numbers will increase the amplitude, while lower numbers will reduce it. To change the frequency, change the 5 that is multiplied by "time" inside the parentheses. Higher numbers will increase the frequency, while lower numbers will reduce the frequency. To apply any changes you make to the expression, you'll need to click the "Edit" button on the expression dialog ("Edit" replaced "Create" once the expression was created).

Hopefully this will get you going in the direction you desire.

bontempos
09-05-2006, 02:59 AM
Thank you, Sir. That's it.
I didn't know about the noise comand, and yes it works the way I expected!
I'm very glad, and reading a lot of MEL tutorials, to bring you people less work here =)

See!

CGTalk Moderation
09-05-2006, 02:59 AM
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.