MEL scripts


#1

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.


#2
  • a great site dedicated to MEL with an introduction to MEL - http://www.ewertb.com/maya/mel_intro.html

  • 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 http://www.highend3d.com/maya/mel/

  • 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 + "
");
for($i = 0; $i < $numOfDuplicates;$i++) {
  $newpos = `rand &lt;&lt;$withinMinX,$withinMinY,$withinMinZ&gt;&gt; &lt;&lt;$withinMaxX,$withinMaxY,$withinMaxZ&gt;&gt;`;
  $newrot = `rand &lt;&lt;$rotateMinX,$rotateMinY,$rotateMinZ&gt;&gt; &lt;&lt;$rotateMaxX,$rotateMaxY,$rotateMaxZ&gt;&gt;`;
  $newSurface = `duplicate $selectedSurface`;
  print("New duplicates position: " + $newpos.x + ":" + $newpos.y + ":" + $newpos.z + "
");
  $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
		duplicate;
	} 
	else
	{
		// instance
		instance;
	}
	$i++;
}
}
// e.g. duplicate the current selection 5 times - evenly distributed between frame 1 and 240
animatedDuplication(1, 240, 5, 0);


#3

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);


#4

Dudes and dudetts!
I have to say that this is a freaking awesome place!
Keep it up.

.


#5

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:p)

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

3 in the Expression Editor write
code:

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:D this is just for really n00b ones… dont sue me!


#6

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!


#7

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 :slight_smile:

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

cheers

alexx


//********************************************
//	color particles 3D
//
//	author:		alexx (bigott@gmx.de)
//		
//	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];

print($sel);

//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("
"+$i+" particles of "+$partCount+" processed");
	}
}

select -clear;
print("

----- Particles colored.
");

//**************************************************


#8

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

[edited]
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


#9

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 ???


#10

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 :thumbsup:


#11

yes sure :slight_smile: but the most of them will be really to big for posting here and comment, because of the character limit. :hmm:


#12

word! if they exceed the limit

here’s a link for the german speaking audience - a li’l tutorial on MEL basics

http://216.40.198.154/content/mayaforum/9054


#13

hey stuundman,

thanx a lot for the editing tip… really missed that button :slight_smile:

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

alexx


#14

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.


#15

Originally posted by InnerVortex
well… ill not post any more thing like that… or ill be killed by some pro:D 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!


#16

one of the coolest places for tips and tricks for mel.
such a lot of useful explanations and tips… this really helped me over some sleepless nights :slight_smile:

http://www.ewertb.com/maya/


#17

a handy one, maybe someone can add a read out for the value data types this one doesn’t print out


// list all readable attributes for the named node, if showExtendedInfo is set to true (anything except 0) the attribute's type and value (if possible) will be appended
proc lsAttributesForNode(string $theNodeName, int $showExtendedInfo) {
	string $eachParameter;
	string $NodeName = $theNodeName;
	string $NodeAttributes[] = `listAttr -read $NodeName`;
	string $NodeAttributeType;

	for ($eachParameter in $NodeAttributes) {
		// query attribute's type
		$NodeAttributeType = `getAttr -type ($NodeName + "." + $eachParameter)`;		
		print($NodeName + "." + $eachParameter + " ");
		if($showExtendedInfo) 
		{
			switch ($NodeAttributeType)
			{
				case "byte":
					// pass on
				case "long":
					// pass on
				case "enum":
					// pass on
				case "enum":
					// pass on
				case "bool":
					// pass on
				case "float":
					// pass on
				case "string":
					print("[value = \"" + `getAttr ($NodeName + "." + $eachParameter)` + "\"]");
					break;			
				default:
					print("[value = i'm not yet smart enough to handle this one]");
					break;
			}
			print(",[type = \"" + $NodeAttributeType + "\"]");
		}
		print("
"); // line feed
	}
}
// e.g. list all attributes, values and types from the default lambert1 shader node
lsAttributesForNode("lambert1", 1);


#18

doesnt the DG node reference in the manuals derive this one obsolete?


#19

Originally posted by alexx
doesnt the DG node reference in the manuals derive this one obsolete?

hmm, i have no clue what you are referring to

maybe it is - are you referring to a MEL command that already does this job?


#20

nope.

in the online docs to maya there is a thing calle “DG nodes reference” which holds all the information to a specific node and all its attributes.
e.g. you want to change the values in the render globals from a mel script: you wont get any helpfull feedback in the script editor. but al can be found there under “defaultRendergloabsl”…

have a look. there is even some info in, that is not in the regular manuals. very handy!

cheers

alexx