PDA

View Full Version : Mel limitation or bug (vector datatype as proc parameter) ?


Octagon
06-04-2003, 11:38 AM
Hi,
can someone explain this to me?
When using a vector datatype for a proc parameter one cannot output values from the proc to a calling context. (or Iīm doing something terribly wrong)

Consider the following statements:

{
// using proc parameter, does NOT work, but WHY?
global proc setVector(vector $vec)
{
vector $set = << 1,1,1 >>;
$vec = $set;
}

vector $zero = << 0,0,0 >>;
setVector ($zero);
print ("$zero is: "+$zero);
}




// using global var, this works
{
global proc setVector()
{
global vector $zero;
vector $set = << 1,1,1 >>;
$zero = $set;
}

global vector $zero = << 0,0,0 >>;
setVector ();
print ("$zero is: "+$zero);
}


Any Ideas?
matthias

ajk48n
06-04-2003, 09:28 PM
Your original procedure doesn't actually return anything when it gets called. Try this:

{
// using proc parameter, does NOT work, but WHY?
global proc vector setVector(vector $vec) //this doesn't need to be global if all of this gets called at the same time
{
vector $sec = $vec;
$sec = << 1,1,1 >>;
return $sec;
}

vector $zero = << 0,0,0 >>;
$newZero = `setVector ($zero)`;
print ("$zero is: "+$newZero);
}

mark_wilkins
06-04-2003, 11:35 PM
In Maya, data only goes IN through a procedure's arguments. Data never goes OUT without an explicit return statement and return value declaration like the previous poster demonstrated, and changes to input argument variables don't propagate back to the calling procedure.

To see what this is like in other languages I'll give some examples.

In C, this is normally handled manually:


int my_function(int a; int *b)
{
a = 5;
*b = 6;
}


In this instance, if I were to call my_function like this


int x = 0;
int y = 0;
my_function (x, &y);


then x would remain 0 and y would change to 6. The reason for the * and & is that C requires you to manually manage "pointers," or memory addresses, for variables you want to change. This is an intermediate-level technique that a lot of beginners find confusing, and MEL offers no similar capability.


In Pascal, this is handled a LITTLE more naturally:


PROCEDURE my_proc ( A : Integer; var B : Integer);

BEGIN;
A := 5;
B := 6
END;


in this instance the var keyword says to pass changes to B back to the calling procedure -- in other words, it automates what the C code was doing by hand with pointers (* and &).

Again, MEL doesn't support anything like this, so global variables or a single function return value are the alternate choices you have.

-- Mark

Octagon
06-05-2003, 07:54 AM
Originally posted by mark_wilkins
In Maya, data only goes IN through a procedure's arguments. Data never goes OUT without an explicit return statement and return value declaration like the previous poster demonstrated,

-- Mark

Thx for your answers! All of you!

Btw, I know about how things are done in C and C++, Mark. I'm really disappointed there's no way to use proc paramaters in a simliar way in mel.

The fact that you can only use the return statement to do that is so limiting. I mean, this tends to lead to usage of global vars all over the place (in subprocs etc.) just to get data out of procs easily. (i'm talking about bigger scripts here with complex data)

I definately need to take some time to look at the API. Thanx again, guys.

matthias

Octagon
06-06-2003, 12:18 PM
Originally posted by Octagon
Thx for your answers! All of you!

Btw, I know about how things are done in C and C++, Mark. I'm really disappointed there's no way to use proc paramaters in a simliar way in mel.
matthias

I read up a little on the pass parameter by value / reference issue.

It seems like mel _can_ pass parameters by reference, but itīs only done for array parameters.

(see www.davidgould.com, end of errata section for his programming maya book)

matthias

alexx
06-06-2003, 12:37 PM
Originally posted by Octagon
I mean, this tends to lead to usage of global vars all over the place (in subprocs etc.) just to get data out of procs easily. (i'm talking about bigger scripts here with complex data)


i think using return statements is not so extremely limiting.. sure: referencing and pointers would be nice to have for some procedures but i think that is something acceptable..

i would really get away from the idea doing the trick with global variables since they really tend to get you into trouble (maya really remembers them very well - even after doing a "new" in the scene..
so if you ever have different globals in different script jobbed scenes you can get into deep trouble..

cheers

alexx

mark_wilkins
06-06-2003, 06:06 PM
It seems like mel _can_ pass parameters by reference, but itīs only done for array parameters.

More likely it's fairer to say that Maya's behavior of passing arrays by reference is a side-effect of its implementation, and relying on this side-effect to pass stuff out of your script is guaranteed to produce mystifying code...

-- Mark

Octagon
06-07-2003, 04:47 PM
Originally posted by mark_wilkins
arrays by reference is a side-effect of its implementation, and

-- Mark

Maybe. It could also be a working -but somewhat underappreaciated -feature, that is overlooked often, couldn't it? It would be nice to get some definate info from AW concerning this (or a really complete mel documentation ;)

Alex: I agree, global vars can get you into trouble. But sometimes there simply no other way of doing things. And if you use a naming scheme for all your global vars throughout all your scripts you are very unlikely to get vars conflicting each other.
Of course this doesn't help with the issue of global vars being modified by code from all over the place. But if you have a guideline for this (when and how to you access globar vars) it's not really a problem. I personally only use global vars to output data from procs, I always input all my data using the proc parameters.


cheers,
matthias

CGTalk Moderation
01-15-2006, 09:00 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.