MEL scripts


#401

for toggling values using NOT (!) helps reduce and simplify your code a lot:

for ($node in `ls -tr -sl`) 
	setAttr ($node+".doubleSided") (!`getAttr ($node+".doubleSided")`);

// and:

softSelect -sse (!`softSelect -q -sse`);

I’m not sure how useful a double sided toggle would really be though, rather than a simple “make singleSided”… but each to their own. :slight_smile:
:nathaN


#402

Hey cheers, I read about that method but couldn’t quite get it :smiley:


#403

This is mel i Use for playblast
playblast -startTime 8 -endTime 13 -format avi -filename “C:/playblasts/test” -forceOverwrite -sequenceTime 0 -clearCache 1 -viewer 1 -showOrnaments 0 -fp 4 -percent 100 -compression “MS-CRAM” -quality 95 -widthHeight 524 268;

I need startTime -endTime to selected / highlighted frame-range


#404

Hey all!

Trying to set a unique expression on every object after its creation, how to?

global proc randomPlacement(float $amount ,float $width ,float $depth)
{
    for ($i=0; $i<$amount; $i++)
    {
        polySphere -sx 16 -sy 8;
        
        //movement along the y-axis
        string $sel[] = `ls -sl`;
        expression -e -s ($sel[0] + ".translateY = sin(time);") -o $sel[0] ;
        
        //place on the xz-plane
        float $randomX = rand(-$width,$width);
        float $randomZ = rand(-$depth,$depth);
        move -x $randomX;
        move -z $randomZ;
    }
}

The expression-part does not work for me.


#405

hey guys
im trying to make hot keys for slide, edge select, and add edge loop but i cant figure out the mel script to paste in the hot key editor. i did delete edge loop just fine but i cant figure out the other three. any one know what part i need to copy ?

thanks


#406

Try this :

global proc randomPlacement(float $amount ,float $width ,float $depth)
{
for ( $i = 0; $i < $amount; $i++ )
{
select -cl;

string $newlyCreatedSphere[] = `polySphere -sx 16 -sy 8`;
    
   //movement along the y-axis
   
expression -o $newlyCreatedSphere[0] -s "translateY = sin(time);"  ;
    

   //place on the xz-plane
    
float $randomX = rand(-$width,$width);   
float $randomZ = rand(-$depth,$depth);
  

select $newlyCreatedSphere[0];

move -x $randomX;       
move -z $randomZ;
   
    
 }

}


#407

smee again! :smiley: I just found some script that I obviously got from the standard Maya scripts but altered a bit to fix an extremely annoying issue when switching panel styles: This keeps all the shading settings alive (xray, backfaceculling, wos…) and JUST switches the layout:

global proc setPaneLayout_keepPanelSettings( string $whichNamedLayout ) {
	global string $gMainPane;

	string $configName = `getPanel -cwl $whichNamedLayout`;
	string $panelType;
	string $panelName;
	string $visPanels[4];
	int	$exists = false;
	int	   $i,$numberOfPanels;

	if (`panelHistory -exists "mainPanelHistory"`)
		panelHistory -e -suspend true "mainPanelHistory";

	if (!`paneLayout -exists $gMainPane`)
		error ("$gMainPane: \"" + $gMainPane + "\" cannot be found!
");

	//  get state arrays.
	int $fixed[]		=	`panelConfiguration -q -isFixedState $configName`;
	string $labels[]	=	`panelConfiguration -q -labelStrings $configName`;
	string $types[]		=	`panelConfiguration -q -typeStrings $configName`;
	string $create[]	=	`panelConfiguration -q -createStrings $configName`;
	string $edit[]		=	`panelConfiguration -q -editStrings $configName`;
	
	// loop each panel in this configuration
	//  build an array of visible panels and make sure that 
	//  they are parented to the main pane.
	$numberOfPanels = `panelConfiguration -query -numberOfPanels $configName`;
	for ($i = 0; $i < $numberOfPanels; $i++) {
		$panelName = `getPanel -withLabel $labels[$i]`;
		// if panel exists:
		if ($panelName != "")
		{
			// if panel has no parent:
			if ("" == `panel -q -control $panelName`) {
				$panelType = `getPanel -typeOf $panelName`;
				eval ($panelType + " -e -p $gMainPane " + $panelName);
			}

			// omit if panel is torn off
			if (`panel -q -to $panelName`) {
				string $msg = (uiRes("m_setNamedPanelLayout.kCantUsePanel"));
				string $pnl = `panel -q -label $panelName`;
				warning `format -s $pnl $msg`;
				$visPanels[$i] = "";
			} else {
				$visPanels[$i] = $panelName;
			}

			if ($fixed[$i]) {
				//
				// Reset state.
				//
				string $editStr = ("int $menusOkayInPanels = `optionVar -q allowMenusInPanels`; string $editorName; string $panelName = \""+ $panelName + "\"; "+ $edit[$i]);
				// print("$editStr: \"" + $editStr + "\"
");
				// eval ($editStr);
			}
		}
	}
	
	paneLayout -e -manage false $gMainPane;
	for ($i = 0; $i < $numberOfPanels; $i++) {
		if ("" != $visPanels[$i]) {
			print ("paneLayout -e: " + $visPanels[$i] + " " + ($i+1) + "
");
			paneLayout -e -setPane $visPanels[$i] ($i+1) $gMainPane;
		}
	}
	eval `panelConfiguration -q -cfs $configName`;
	paneLayout -e -manage true $gMainPane;
	setFocus `paneLayout -q -p1 $gMainPane`;

	if (`panelHistory -exists "mainPanelHistory"`)
		panelHistory -e -suspend false "mainPanelHistory";

	updateToolbox();
}

I don’t know where I originally got it from. But I found it to be a blessing! I mean I switch styles maybe 1000 times a day and having the settings ffed up each time was SO horrible. So I though someone would like that too. Here you go :]


#408

Sounds great, I get this problem too. How do I use this script?


#409

He dnos! Thanks.

  1. Put the script in your userScriptDir
//python:
[color=white](cmds./mc./)[/color]internalVar(userScriptDir=1)
//mel:
internalVar -userScriptDir;

in a file called the same as the global proc name setPaneLayout_keepPanelSettings.mel (or make it both shorter if you want)
2. find what Layouts you want to switch to. You find the names on the toolBox buttons in the mouseOver tooltip or you RMB there and look through the list or you click Edit Layouts in this menu and goto the Layouts tab. There you could also select and copy/paste from the Label field.
3. Setup hotkeys or shelfButtons that call:

setPaneLayout_keepPanelSettings "yourLayoutName";

Unfortunately this does not simply overwrite the layout switching mechanism of Maya. I don’t know if thats possible. Maybe override setNamedPanelLayout to that? AH! Thats obviously the procedure that I ripped the parts from! Yea one could do that too. But you never know when Maya sources from the original files again.

Tell me if things work out :]


#410

Ah I’d love to be able to use it with the sidebar buttons than the shelf. I’ve made a custom UV/persp layout that I use all the time, but it looses the settings everytime.

Anyway to use those? Im on maya 2009 so I guess the UI aint as easy to modify as 2011+


#411

ok so im working on a small small for a animated scene. I have a modelled a lampost and used a a spotlight as the light source for it, and i have a point light source to represent the sun. What i need to do is create a Mel script to make the point light (sun) rise up on the Y axis and back down again, whist this is happening i need the spotlight to turn on and off accordingly. when the sun sets the light turns on, and when the rises up in the sky the light should turn off. another extra i need with this is to simulate a small effect when the sun sets and the light turns on, it should warm up. so it should turn red for a short second then change to its yellow color.

I understand very little about mel scripting and need some basic guidance on how to set up this sort of script and then use it as a animation with my scene.

Any help on this would be very appreciated, thanks.

float $time = currentTime -query ;
float $translateY = getAttr pointLight1.translateY ;

if( $translateY &gt;= 10 )
    {
    setAttr "spotLightShape1.intensity" 0 ;
    setAttr "pointLightShape1.intensity" 1 ;
    }
else
    {
    setAttr "pointLightShape1.intensity" 0 ;
    }
if( $time &lt; 80 )
    {
    setAttr "spotLightShape1.color" 2 1 1 ;
    setAttr "spotLightShape1.intensity" 1 ;
    }
else
    {
    setAttr "spotLightShape1.color" 1 1 1 ;
    }
if( $time &gt; 287 )
    {

    setAttr "spotLightShape1.intensity" 0 ;
    }
else
    {
    setAttr "spotLightShape1.intensity" 1 ;
    }

This is what ive got so far, but i need to loop this piece of code for the 1000frames of my animation and put it into a procedure if possible to simplify it. I really am crap at mel and cant figure it out :\


#412

I’m not sure if this is of importance to you anymore, since your post was some months old.
Anyway, the global string $gPlayBackSlider will give you the name of the slider and the command timeControl will let you query the selected frame range of that slider.

timeControl -q -ra $gPlayBackSlider;

If using this in a script don’t forget to declare the string as it’s global.
global string $gPlayBackSlider;

Bryan Ewert’s site has some really good info. That and the maya help command site is the resources I use most.
http://xyz2.net/mel/mel.027.htm

Good luck!


#413

I think you’re overthinking the problem; trying to write a mel script for this isn’t really necessary. You should be able to set up the effects you want with a few Set Driven Keys.

You can set it up so that the streetlamp turns on and off, and the pointlight changes color based on the pointlight’s position. Then all you have to do is animate the pointlight moving up and down, and you’ll get the desired effect.


#414

yeah but the part of my assignment requires this to be done without manual animation, but through scripting only, i could animate all this by hand easily but i just dont understand scripting one bit :cry:


#415

Alright then, if it’s scripting for an assignment you need, I’ll try to give you some tips to get you going.
(as a side note, I’ve not wrote mel for a while so I might have some syntax errors in my examples)

First off, to loop something, I’d use the modulus operator %, the modulus operator works by giving you the remainder of a division operation, so 5 % 2 = 1, 10 % 6 = 4, etc.

You can loop an animation by doing something like this:

$animLen = 24;
$time = currentTime -q;
$crntFrame = $time % $animLen;

This will give you a repeating sequence of numbers that goes from 0 to 23. You can then use $crntFrame to drive an attribute of an object.

setAttr aLight.translateY, (10 * $crntFrame);

I’d then suggest using expressions to drive the other behaviors you’re seeking. Their syntax is a bit different but they shouldn’t be too difficult to write.

Something like:

if ( sunLight.translateY > 20 )
streetLight.intensity = 1;
else
streetLight.intensity = 0;

If you need to write the expressions from within a script you can use the expression command in mel:

expression -s “aObj.translateX = aShape.translateY”;

If you have more questions, start out by looking at the mel command reference and the resources in Maya’s help files. There’s plenty of examples there that should be able to help guide you on your way.


#416

right ok thanks for the help, much appreciated :slight_smile: i will see what i put together.


#417

I may have found another solution to this: It seems the default maya layouts do not do have settings specified for xray/backface/etc and therefore they stay consistent when switching.

So I went into userprefs.mel and replaced a portion of the default persp/outliner with my custom layout (which was loaded with settings for xray/backface/etc).
Solved!


#418

Here’s a script for manually adjusting your skin color and clossyness. It’s a melscript GUI with limited sliders.

 if ( `window -exists MyGuiWindow` ) {
deleteUI MyGuiWindow;
}
{ 
// create window 
window -title "GUI" MyGuiWindow;
columnLayout;
 
// Skin attributes
text -l "Skin";
attrFieldSliderGrp -label "Red Skin – Yellow Skin" -minValue 15 -maxValue 40 -at ("hsvToRgb1.inHsvR");
attrFieldSliderGrp -label "Saturation" -minValue 0.2 -maxValue 0.5 -at ("hsvToRgb1.inHsvG");
attrFieldSliderGrp -label "Dark-Light Skin" -minValue 0.2 -maxValue 1 -at ("hsvToRgb1.inHsvB");
attrFieldSliderGrp -label " Dry – Greasy Skin " -minValue 0.0 -maxValue .3 -at ("hsvToRgb3.inHsvB");
 
showWindow MyGuiWindow;
}

You need to add 2 HSVToRGB Nodes to your skin material, one conected by color and one by specular. Then it should work.
Take care!


#419

Hi,

I try to create a simple script that uses the window command to rename a file.
The script is not finished but even the first step didn’t work. I am not a programmer so I got stuck early with some syntax problems.

string $winCMD = “REN”;
string $fileWithPath = " O:/3Dtest/01.jpg";
string $newFileName = " 03.jpg";
string $fullCMD;
$fullCMD = $fileWithPath + $newFileName;
system($winCMD + $fullCMD);

The mel script does this thing: REN O:\3Dtest\01.jpg 03.jpg"

It simply renames one image into another.
It works in a .bat file so it should theoretically work out of Maya but I get a syntax error message.

Any ideas how to fix the script?
Thank you!


#420

… or lets say I want to use the (system) copy command inside maya:

string $winCMD = “copy /y”;

string $fileA = " O:/3Dtest/saveTextureA/03.jpg";

string $fileB = " O:/3Dtest/tempTextureA/01.jpg";

system($winCMD + $fileA + $fileB);

gives a syntax error.
Why???