Mel limitation or bug (vector datatype as proc parameter) ?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 06 June 2003   #1
Mel limitation or bug (vector datatype as proc parameter) ?

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 Im 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
 
Old 06 June 2003   #2
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);
}

Last edited by ajk48n : 06 June 2003 at 08:30 PM.
 
Old 06 June 2003   #3
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
__________________
Mark R. Wilkins
author of MEL Scripting for Maya Animators
 
Old 06 June 2003   #4
Quote: 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
 
Old 06 June 2003   #5
Quote: 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 its only done for array parameters.

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

matthias
 
Old 06 June 2003   #6
Quote: 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
 
Old 06 June 2003   #7
Quote: It seems like mel _can_ pass parameters by reference, but its 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
__________________
Mark R. Wilkins
author of MEL Scripting for Maya Animators
 
Old 06 June 2003   #8
Quote: 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
 
Old 01 January 2006   #9
Thread automatically closed

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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 03:04 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.