MEL scripts

Become a member of the CGSociety

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

Thread Tools Display Modes
  07 July 2002
MEL scripts

After asking a few of you here I've decided to start off a thread entirely for MEL script submissions. Similar to the Tutorial thread below but entirely focused on Maya's Embedded Language.

Hopefully overtime more and more people will add to this thread building up a nice collection of usefull MEL scripts for both novice and advanced users. Im still learning about MEL and its capabilities, I just started reading the manual. Some of you have more experience with MEL and I hope you can share some tips with the rest of us. To start things off I'll add the MEL script I posted in a previous message so everyone can see it.

Heres a little script for creating a box from curves ( by Chris Clay )

curve -d 1 -p 9.26493 1.180816 1.180816 -p 9.26493 1.180816 -1.180816 -p 6.903298 1.180816 -1.180816 -p 6.903298 1.180816 1.180816 -p 9.26493 1.180816 1.180816 -p 9.26493 -1.180816 1.180816 -p 6.903298 -1.180816 1.180816 -p 6.903298 1.180816 1.180816 -p 6.903298 1.180816 -1.180816 -p 6.903298 -1.180816 -1.180816 -p 6.903298 -1.180816 1.180816 -p 9.26493 -1.180816 1.180816 -p 9.26493 -1.180816 -1.180816 -p 6.903298 -1.180816 -1.180816 -p 6.903298 1.180816 -1.180816 -p 9.26493 1.180816 -1.180816 -p 9.26493 -1.180816 -1.180816 -k 0 -k 1 -k 2 -k 3 -k 4 -k 5 -k 6 -k 7 -k 8 -k 9 -k 10 -k 11 -k 12 -k 13 -k 14 -k 15 -k 16 ; xform -cp;

You can take this script and place it in the Script Editor, then drag & drop it into your Shelf. Now you have a button to create a curve box.

I'll add more soon.
Chris 'sigma' Smart

Home | Portfolio | My Book | Photography | About

Last edited by sigma : 07 July 2002 at 03:15 AM.
  07 July 2002
- a great site dedicated to MEL with an introduction to MEL -

- the manual - there are two parts in the manual - one is a general description of MEL - and the other one is the MEL Command Reference which documents both the intrinsic MEL commands (like print, match, ...) and procedures that are related to the functionality of Maya (like selecting objects in scenes, creating particles, creating cubes, ...) - their purpose, syntax and semantics

- a great place to learn from are the collected MEL scripts on

* a script to scatter objects within bounds and the possibility to apply a random rotation to every duplicate *

proc scatterObject(int $numOfDuplicates,
			float $withinMinX,
			float $withinMinY,
			float $withinMinZ,
			float $withinMaxX,
			float $withinMaxY,
			float $withinMaxZ,
			float $rotateMinX,
			float $rotateMinY,
			float $rotateMinZ,
			float $rotateMaxX,
			float $rotateMaxY,
			float $rotateMaxZ) {

vector $newpos;
vector $newrot;
int $numOfDuplicates = $numOfDuplicates;
int $i = 0;
string $selectedSurface[] = `ls -sl`;
string $newSurface[];
float $x;
float $y;
float $z;

//print("Scatter object(s): " + $selectedSurface + "\n");
for($i = 0; $i < $numOfDuplicates;$i++) {
  $newpos = `rand <<$withinMinX,$withinMinY,$withinMinZ>> <<$withinMaxX,$withinMaxY,$withinMaxZ>>`;
  $newrot = `rand <<$rotateMinX,$rotateMinY,$rotateMinZ>> <<$rotateMaxX,$rotateMaxY,$rotateMaxZ>>`;
  $newSurface = `duplicate $selectedSurface`;
  print("New duplicates position: " + $newpos.x + ":" + $newpos.y + ":" + $newpos.z + "\n");
  $x = $newpos.x;
  $y = $newpos.y;
  $z = $newpos.z;
  move -a $x $y $z $newSurface;
  $x = $newrot.x;
  $y = $newrot.y;
  $z = $newrot.z;
  rotate $x $y $z $newSurface;

// e.g. 100 duplicates (of selected object) within a 10 by 10 by 10 cube 
// all duplicates are rotated randomly within -45 to +45 on all axis 
scatterObject(100, 0, 0, 0, 10, 10, 10, -45, -45, -45, 45, 45, 45);

* animated snapshot puts every duplicated object in it's on transformation group which is kind of a pain if you want to make animated snapshots of grouped objects (maybe i missing something here - but it didn't take much to write my own thing)

this quickie will duplicate or instance the selected objects along it's animation

// animated duplicate/instance script
proc animatedDuplication(int $rangeStart, int $rangeEnd, int $numOfDuplicates, int $duplicateOrInstance) {

int $range_start = $rangeStart;
int $range_end = $rangeEnd;
int $num_of_duplicates = $numOfDuplicates;
int $step_size = ($range_end - $range_start) / $num_of_duplicates;
int $i = 0;
int $temp;

currentTime $range_start; // set to range start
string $selectedObjects[]; // to store selected objects to duplicate or instance
$selectedObjects = `ls -sl`; // store selected objects
select $selectedObjects;

while($i <= $num_of_duplicates) {
	$temp = $range_start + ($step_size * $i);
	currentTime ($temp);
	// seleced the objects to duplicate or instance
	select $selectedObjects;
	if($duplicateOrInstance == 0) 
		// dulicate
		// instance
// e.g. duplicate the current selection 5 times - evenly distributed between frame 1 and 240
animatedDuplication(1, 240, 5, 0);

Last edited by stunndman : 03 March 2003 at 04:52 AM.
  07 July 2002
anyone ever written a game for Maya with MEL - like tetris or some shooter?

another snapshot - this script puts every rigid object that matches the selection into it's distinct collission layer

i'm posting this because it's easy to adapt for any other purpose - like setting any other attribute on a selection of objects

proc putRigidBodiesInDistinctCollissionLayers(string $theSelection, int $theFirstLayer)
	string $rigids[] = `ls -sl -dag -leaf -type "rigidBody" $theSelection`;
	int $cLayer = $theFirstLayer;
	string $standIn = "cube";
	for ($r in $rigids) {
 		//setAttr  ($r+".collisions")  true;
		//setAttr  ($r+".standIn")  1;
		setAttr  ($r+".cl") $cLayer ;
		$cLayer ++;
// e.g. put every rigid object that matches the selection "shard*"
//  in it's own collission layer - starting with collission layer 1
putRigidBodiesInDistinctCollissionLayers("shard*", 1);
  07 July 2002
Dudes and dudetts!
I have to say that this is a freaking awesome place!
Keep it up.

*The lack of knowledge can be treacherous*
  07 July 2002
great stuff dud
now for some REALLY n00b stuff.. well.. im noob so..
this will make a light to blink with time, well is an expression i think it can be aplied here.. as for this is for n00bs im gonna explain it all.. (as i wished some one did it to me, anyways rtfm)

1 Create a point light(doesnt need to be point.. but do that for now)

2 in the channel box click intensity right click and click expressions...

3 in the Expression Editor write

pointLightShape1.intensity = rand ( 0.4 , 0.6 );

yes.. one line.. pretty simple, then hit creat and play, the light will blink, this line is pretty self explanatory, the right atrr to use apears in the Selected Obj & Attr line in the Expression Editor, so just play with it.
well.. ill not post any more thing like that.. or ill be killed by some pro this is just for really n00b ones.. dont sue me!
"Your strength is your weakness!"-Ninja das Caldas
  07 July 2002
InnerVortex, don't be afraid to post what you learn along the way. There will definitely be people just starting out who would love to know the simple tricks to make their lives easier. I'll definitely post some when I have time.
This list should is for the pro and novice alike, so keep the posts coming!
  07 July 2002
cool idea with the thread..

just to get it started:
here a small script that deals with particles. i found it quite hard to get into scripting them, since the references are quite rare.

here a script that takes a system of existing particles and a 3D texture node (like crater e.g.) and it assigns the particles the color from the 3d texture node. even in hardware buffer

the image shows the idea. but careful! it is not the fastest one, since the color gets rendered (!)



//	color particles 3D
//	author:		alexx (
//	date: 		25.10.2001
//	last change:	25.10.2001
//	description:
//		make it possible to have particles get the color from 3D textures
//	usage:	 
//		create a particle system and a 3D texture
//		select the particles first and then the 3D shader node.
//		run the script and see the colors :)
//	tip:
//		The particles MUST have the rgbPP attribute

// to see in the script editor how many particles have been processed. 
// feedback after value
int $feedbackCount = 100;

int $i;
int $partCount;
int $selSize;

string $part;
string $part3D;
float $color[3];
float $partPos[3];

string $sel[];

//select particle Shape node (! not really user proof !)
$sel = `ls -sl -dag -s`;
$part = $sel[0];

//select 3D Shader
$sel = `ls -sl`;
$selSize = size($sel);
$part3D = $sel[1];


//allow only one Particle Object
if ($selSize != 2)
	error("Select ONE particle Object first and THEN a 3D shader Node");

//make sure the selection tool is active before selecting single particles
// (otherwise a warning will be displayed for each particle)
global string $gSelect; setToolTo $gSelect;

//get Number of particles in system
$partCount = `getAttr ($part+".count")`;

for ($i = 0; $i <$partCount ; $i++)
	//select each particle and get its world position
	select ($part+".pt["+$i+"]"); 
	$partPos = `getParticleAttr -at wps`;
	//give the world position to the 3d shader and evaluater the color at the given position
	setAttr ($part3D+".rpc")  ($partPos[0]) ($partPos[1]) ($partPos[2]);
	$color = `getAttr ($part3D+".outColor")`;
	// now color the particle with the result
	setParticleAttr -at rgbPP -vv ($color[0]) ($color[1]) ($color[2]);
	// visual feedback for process
	if (($i % $feedbackCount) == 0)
		print("\n"+$i+" particles of "+$partCount+" processed");

select -clear;
print("\n\n----- Particles colored.\n");

Attached Images
File Type: jpg 3d_colorparticles.jpg (41.8 KB, 3778 views)

Last edited by alexx : 07 July 2002 at 07:33 PM.
  07 July 2002
i think we should paste mel scripts - links are prone to die

that's just a proposal - there are so many threads i've been searching up with so many dead links (mostly to pictures) - that's a pitty

there's a vB tag especially for posting code - it will use a smaller monospaced font that makes it easy to read code - enclose your code with the ubb tags

[ code ]
your code goes here
[ /code ]

remove the spaces within the brackets - i don't know how to escape vB tags properly

Last edited by stunndman : 07 July 2002 at 05:49 PM.
  07 July 2002
most of the scripts will be way to big to post them directly here i guess. sigma, is there a place on cgtalk we can upload the scripts. so it can stay foreever ????
heimwerker never die
  07 July 2002
wouldn't that defeat the purpose? - paste them and comment them - if anyone wants to publish a script they can do it at highend3d anyway and link there

but where's the sex then?

just my 2 €cent

Last edited by stunndman : 07 July 2002 at 05:15 PM.
  07 July 2002
yes sure but the most of them will be really to big for posting here and comment, because of the character limit.
heimwerker never die
  07 July 2002
word! if they exceed the limit

here's a link for the german speaking audience - a li'l tutorial on MEL basics
  07 July 2002
hey stuundman,

thanx a lot for the editing tip.. really missed that button

i am with you concerning the posting style but i think when it comes to some bigger scripts, i think svenip is right.. a structured script is fast over some hundred lines and that will spoil the complete thread besides the fact we cant post too many lines.

but asking for a place to put them here on the server might be a good idea.

so far

  07 July 2002
If scripts get too big you can always copy the text to a text file ( notepad ) and attach the file to your message. You can also attach zip files. The links wont go dead that way.
Chris 'sigma' Smart

Home | Portfolio | My Book | Photography | About
  07 July 2002
Quote: Originally posted by InnerVortex
well.. ill not post any more thing like that.. or ill be killed by some pro this is just for really n00b ones.. dont sue me! [/B]

hell.. i wished i had examples like that in the beginning.
this is far from something that you should not post.. a lot mel starters dont know any good point to start from. and this definitely is one. keep it coming!
reply 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
Society of Digital Artists

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

All times are GMT. The time now is 07:16 AM.

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