Create object via MEL

111 views
Skip to first unread message

joiec...@gmail.com

unread,
Aug 26, 2021, 3:06:18 AM8/26/21
to maya_he3d
Hi there,
I need to create several objects and connect their translation to an attribute of corresponding node.
So in its simplest form, it would be like this:

createNode -n Loc1 "locator";
createNode -n Pos1 "locator";
connectAttr Loc1.translateX Pos1.scaleX;

You get the idea. I create two objects and connect the attribute from one to the attribute on the other one.

But I want to do this in a loop. So I need each object's name to be unique (obvious).
So I would need something like the above example, but with names like "Loc_[n]" instead (so the [n] is a number).

How could I do that in a loop?

Thanks in advance for your help.

stephenkmann

unread,
Aug 26, 2021, 11:23:57 AM8/26/21
to maya...@googlegroups.com

Welcome to the world of scripting !

okay. so Loops in mel  and python are fairly simple. but you do want to know a little bit about variables first. and how certain commands return what kinds of variables. 

maya has a unique command to create locators..   spaceLocator
this command returns a string array    ( an array is just like a box of stuff. while a string / int / float  are individual items.. so a string array is a box of string variables)

you'll want to capture that string array so you can use it in the loop. 
In Mel. .while it can be implicit, it's good to be explicit with variable declarations ( this is not necc in python) .. to indicate an array you use brackets []
(... also in Mel vairables must start with a dollar sign     ie  $myVariable   or  $myVariable[] )


string $loc[] = `spaceLocator -name "loc"`;
string $pos[] = `spaceLocator -name "pos"`;

and then once you've created those string array variables  you'll want access to the item you want.. you can use a #  as to choose which item.. ie : $loc[0] is the first item, and $loc[1] is the second item 

the connection part is pretty easy, basicaly what you have, but replace strings with variables
connectAttr ( $loc[0] + "." + "scaleX") ( $pos[0] + "." + "scaleX") ;


and then lastly make a loop.   so a for loop will run once for each item given..    this is the more common .. however, if you just want to run a number of times. you need to use a while loop.. 


while loops can be dangerous, as you can easily get into an infinite loop, so be sure to increment your item  ( shorthand for this is ++ ) 


and then lastly, I strongly recommend always putting your MEL code in scope. that way you don't create global variables.. Unlike Python, MEL cannot change variable types, so once you make one. it's that type forever. 
if it is run scoped.. then it only exists within that scope.  ( * what's between the curly brackets) 


{
int $i = 0 ;
while ($i <= 10)
    {
    string $loc[] = `spaceLocator -name "loc"`;
    string $pos[] = `spaceLocator -name "pos"`;  
    connectAttr ( $loc[0] + "." + "scaleX")  ($pos[0] + "." + "scaleX") ;
   
    $i++;
     
    }
}



okay, so Should kickstart what you are looking to do.. Funny part is I used to write MEL in  my sleep, but since I converted to PYTHON a few years back,  my mel is getting quite rusty. 

good luck. hope that helps, safe travels, save often !

-=s




















































--
You received this message because you are subscribed to the Google Groups "maya_he3d" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maya_he3d+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/maya_he3d/4e6936d4-2776-4acd-9550-bb000933b744n%40googlegroups.com.


--

bobrobertuma

unread,
Aug 26, 2021, 11:48:08 AM8/26/21
to maya...@googlegroups.com

Great job showing mel scripting process in simple terms.

SEQUENZ | Gerstenmaier

unread,
Aug 26, 2021, 11:57:01 AM8/26/21
to maya...@googlegroups.com
Yes, your explanation lures all weak souls into the world of scripting! 

Thanks for your effort,
Lars

________ 

SEQUENZ GmbH 

________ 

joie

unread,
Aug 27, 2021, 1:01:20 AM8/27/21
to maya...@googlegroups.com
Man, that is probably the best explanation on loops and variables I ever saw!
Very thank you! :)

You received this message because you are subscribed to a topic in the Google Groups "maya_he3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/maya_he3d/mVDRmlE6hZ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to maya_he3d+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/maya_he3d/CAASZO1eTqTgwDuG_51o31v5K9poCeag%2BZ1Z7aQ_zYYFao5dotw%40mail.gmail.com.


--
Visita mi blog: 3djoie.blogspot.com

matt estela

unread,
Aug 27, 2021, 1:15:56 AM8/27/21
to maya...@googlegroups.com

desig...@gmail.com

unread,
Aug 31, 2021, 10:38:43 AM8/31/21
to maya...@googlegroups.com

elvis

unread,
Aug 31, 2021, 1:29:34 PM8/31/21
to maya...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages