Introduction to Maya - Modeling Fundamentals Vol 2
This course will look in the fundamentals of modeling in Maya with an emphasis on creating good topology. It's aimed at people that have some modeling experience in Maya but are having trouble with complex objects.
# 1 01-06-2003 , 07:34 PM
Darkware's Avatar
Subscriber
Join Date: Oct 2002
Location: USA
Posts: 1,172

random translateY for selected objects

I need a mel or script or something that will translate selected objects randomly on a single axis. I thought I could just select the objects, open the script editor, and type translateY = rand (1-9); but it doesn't work.

# 2 01-06-2003 , 08:12 PM
kbrown's Avatar
Moderator
Join Date: Sep 2002
Location: London, UK
Posts: 3,198
quick, ugly and untested :p
Code:
global proc randomTY()
{
	string $list[] = `ls -tr -sl`;
	int $size = size($list), $i;

	if ($size > 0)
		for($i = 0; $i < $size; $i++)
			setAttr ($list[$i] + ".ty") (rand(1,9));
}
Copy-paste that in a text editor, save it as randomTY.mel under your script folder. Type source randomTY.mel in the command line (this needs to be done only once). Then you can just select objects and type randomTY; on the command line.


Kari
- My Website
- My IMDB

Do a lot, Fail a lot and Learn a lot!
# 3 01-06-2003 , 08:21 PM
adldesigner's Avatar
Registered User
Join Date: Sep 2002
Location: CCS, Venezuela
Posts: 3,363
<adl keeps on crying>
Remember, if it doesn´t work ... just shout at the machine. It´ll work afterwards.

# 4 01-06-2003 , 08:48 PM
Darkware's Avatar
Subscriber
Join Date: Oct 2002
Location: USA
Posts: 1,172
lol. You're still crying adl? lol

kbrown and I spoke in the chat room and figured out what I was doing wrong. You don't havbe to type the "global proc randomTY()." That's what was messing me up. It works like a charm now. Thanks kman!

# 5 01-06-2003 , 08:54 PM
kbrown's Avatar
Moderator
Join Date: Sep 2002
Location: London, UK
Posts: 3,198
Hehe, don't cry. It's not that serious :p
Anyway, I though I'd break down this little script with comment lines so you could possibly learn something from it...

Code:
// Define a global procedure called randomTY which takes nothing as input
global proc randomTY()
{
	// Define a string array variable $list[] and store a list of selected transform nodes in it
	string $list[] = `ls -tr -sl`;
	// Define an integer variable $size and store the size of the $list[] array in it.
	// Also we define another integer variable $i for later use.
	int $size = size($list), $i;

	// If there is something in the $list[] then go on. Otherwise just quit
	if ($size > 0)
		// Iterate though the list ($i goes from 0 to $size - 1)
		for($i = 0; $i < $size; $i++)
			// Set the .translateY (aka .ty) attribute of each object in the list to a random value of 1-9
			// Note the extra parenthesis'. They are needed so the clauses inside gets executed first before the setAttr command.
			setAttr ($list[$i] + ".ty") (rand(1,9));
	// End of story
}


Kari
- My Website
- My IMDB

Do a lot, Fail a lot and Learn a lot!
# 6 01-06-2003 , 10:39 PM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161

translateY = rand (1-9);

The solution can be ALMOST that simple. There's no reason to check the size of the array or loop by index. A for-in loop does that all for you:

Code:
global proc randomTY() {
    string $sel_list[] = `ls -tr -sl`;

    for ($curr_sel in $sel_list) {
        setAttr ($curr_sel + ".ty") (rand(1, 9));
    }
}


-- Mark


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 7 01-06-2003 , 10:40 PM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
Also, what's up with the quote marks at the beginning and ending of a block of code? Is that a vBulletin thing??

-- Mark


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 8 02-06-2003 , 03:04 AM
olivermagno's Avatar
Registered User
Join Date: Mar 2003
Location: Philippines
Posts: 94
just curious,

in the for in loop;

for ($curr_sel in $sel_list)

why is it that $curr_sel is not needed to be declared?

just asking, thanx.


------------------------------------------------
our decisions-not the conditions of our lives
determine our destiny.
# 9 02-06-2003 , 12:28 PM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
MEL does not force you to declare variables before using them. If you use a variable without having declared it, Maya will infer from the context what the data type is. After that, it must be used as that data type.

There are several reasons to declare variables whenever you can:

* Declaring a variable forces you to determine what the variable's data type actually should be.

* If you rely on the variable's first use to fix its data type without declaring it, you can run into nasty problems where you can't find where its data type is getting chosen, and your script crashes.

* since often a variable's first use is an assignment, that can be combined with a declaration without cluttering up the code.

In this instance I didn't declare $curr_sel to save a line of code. While it's best to declare variables before using them for the above reasons, variables used within loops are the least offensive context for this because the type is usually obvious.

-- Mark


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 10 02-06-2003 , 02:43 PM
olivermagno's Avatar
Registered User
Join Date: Mar 2003
Location: Philippines
Posts: 94
Wow thats a good explanation for a newbie mel scripter like me.

Thanx Mark.


------------------------------------------------
our decisions-not the conditions of our lives
determine our destiny.
# 11 02-06-2003 , 06:59 PM
kbrown's Avatar
Moderator
Join Date: Sep 2002
Location: London, UK
Posts: 3,198

Originally posted by mark_wilkins
[B]There's no reason to check the size of the array or loop by index. A for-in loop does that all for you:

Hehe, can you see the paranoid C programmer in me? :p

The only reason I could imagine why to check the size is that if you want to throw a warning message or something if there's nothing or no transform nodes selected...

Also, what's up with the quote marks at the beginning and ending of a block of code? Is that a vBulletin thing??

Dunno... to me it looks ok, no quote marks whatsoever...user added image


Kari
- My Website
- My IMDB

Do a lot, Fail a lot and Learn a lot!

Last edited by kbrown; 02-06-2003 at 07:01 PM.
# 12 03-06-2003 , 09:16 AM
Hugh's Avatar
Registered User
Join Date: Nov 2002
Location: Bristol, UK
Posts: 25
Personally, I've been taught to always declare variables - whether I need to or not - I find it makes my code a lot easier to debug...

Also, you could use Hungarian notation (I think that's what this is) for naming variables - then you always know what a variable is meant to be used for:

string $sObjectName;
int $iNumObjects;
bool $bFinished;
float $fValue;

and so on....

Then if you find yourself doing:

$iObject = "myObject";

You'll know you're doing something wrong, as you can see at a quick glance that $iObject is meant to hold an int....


Hugh Macdonald

UnFramed Productions
Goodnight Opus - TD Lead

VFXTalk.com - Online Compositing Community - Admin
# 13 03-06-2003 , 11:38 AM
kbrown's Avatar
Moderator
Join Date: Sep 2002
Location: London, UK
Posts: 3,198
Good points Hugh. I sometimes use that naming convention but usually I just mess around and do some quick tests and then when it works I just leave it alone. Bad habbit, I know user added image

I'll promise to try to improve my manners user added image


Kari
- My Website
- My IMDB

Do a lot, Fail a lot and Learn a lot!
# 14 03-06-2003 , 10:10 PM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
While I was careful to point out why declaring variables even when it's not necessary is a generally a good idea, the original poster was asking for a few simple lines of code that he could presumably type into the script editor on the fly, and maybe drag to a shelf. Such quick-and-dirty solutions don't necessarily warrant the full treatment, in my opinion.

-- Mark


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 15 04-06-2003 , 12:30 AM
Hugh's Avatar
Registered User
Join Date: Nov 2002
Location: Bristol, UK
Posts: 25
Mark: I do agree with you....

I hope I'm not getting a bad reputation with you - it seems that a lot of my posts could be taken to be me contradicting you - I don't mean it - really user added image


Hugh Macdonald

UnFramed Productions
Goodnight Opus - TD Lead

VFXTalk.com - Online Compositing Community - Admin
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads