SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.


SM Forums / Maya Questions / Programming / random translateY for selected objects

Programming MEL Scripts, Expressions & Programming Related Topics

Join the conversation. Reply to post
 
Thread Tools
01-06-2003, 07:34 PM   #1
Darkware
Subscriber
 
Darkware's Avatar
 
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.
__________________
www.Darkware3D.com
  Reply with quote
01-06-2003, 08:12 PM   #2
kbrown
Moderator
 
kbrown's Avatar
 
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!
  Reply with quote
01-06-2003, 08:21 PM   #3
adldesigner
Registered User
 
adldesigner's Avatar
 
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.
__________________
adldesigner
Caracas, Venezuela
Hell .. not gone perse, but with a certainly lower post count per day.

  Reply with quote
01-06-2003, 08:48 PM   #4
Darkware
Subscriber
 
Darkware's Avatar
 
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!
__________________
www.Darkware3D.com
  Reply with quote
01-06-2003, 08:54 PM   #5
kbrown
Moderator
 
kbrown's Avatar
 
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!
  Reply with quote
01-06-2003, 10:39 PM   #6
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
Join Date: Jan 2003
Posts: 161
Quote:
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
  Reply with quote
01-06-2003, 10:40 PM   #7
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
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
  Reply with quote
02-06-2003, 03:04 AM   #8
olivermagno
Registered User
 
olivermagno's Avatar
 
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.
  Reply with quote
02-06-2003, 12:28 PM   #9
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
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
  Reply with quote
02-06-2003, 02:43 PM   #10
olivermagno
Registered User
 
olivermagno's Avatar
 
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.
  Reply with quote
02-06-2003, 06:59 PM   #11
kbrown
Moderator
 
kbrown's Avatar
 
Join Date: Sep 2002
Location: London, UK
Posts: 3,198
Quote:
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...

Quote:
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...
__________________
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.
  Reply with quote
03-06-2003, 09:16 AM   #12
Hugh
Registered User
 
Hugh's Avatar
 
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
  Reply with quote
03-06-2003, 11:38 AM   #13
kbrown
Moderator
 
kbrown's Avatar
 
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

I'll promise to try to improve my manners
__________________
Kari
- My Website
- My IMDB

Do a lot, Fail a lot and Learn a lot!
  Reply with quote
03-06-2003, 10:10 PM   #14
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
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
  Reply with quote
04-06-2003, 12:30 AM   #15
Hugh
Registered User
 
Hugh's Avatar
 
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
__________________
Hugh Macdonald

UnFramed Productions
Goodnight Opus - TD Lead

VFXTalk.com - Online Compositing Community - Admin
  Reply with quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
MAYA - create objects in random order marcopongo Maya Modeling 1 29-04-2015 12:48 PM
Random local rotations on multiple objects. Chris Miller Maya Modeling 0 27-04-2015 09:20 AM
Random smooth flickering ieoie Programming 0 03-02-2013 07:55 PM
Strange Problem - Unable to change attributes for multiple objects anthonysw3 Maya Technical Issues 4 04-12-2010 11:56 PM
Animating 2500 objects easily Darkware Animation 2 01-06-2003 01:26 PM