Showing posts with label MEL Script. Show all posts
Showing posts with label MEL Script. Show all posts

Friday, February 3, 2012

MEL Script -"Unfreezing" Transformations

Someone on CGTalk had 100+ objects in their scene where the transformations had been reset.  He needed their world space positions restored.  Plainly speaking, he needed to unfreeze transformations.  Technically that isn't possible, but there's a way to cheat everything.

There's probably a more efficient way to do this.  If someone reply's to my post on CGTalk with a better version, I'll post their version, credit them, and provide a link to their post.  In any case, here's the "Unfreeze Transformations" script.  I hope it helps someone.


//Get all of the selected items, and the size of the selection
string $selection[] = `ls -sl`;
int $size = size($selection);
int $i;
//for each object in the selection, do the following things:
for ($i = 0; $i < size($selection); $i++){
     //Create two locators.  #1 stays at the origin (0 0 0) while #2 moves to the center of the selected object.
     string $zeroLoc[] = `spaceLocator`;
     string $loc[] = `spaceLocator`;
     string $ptCon[] = `pointConstraint $selection[$i] $loc`;
     string $orCon[] = `orientConstraint $selection[$i] $loc`;
   
     //Get the translation and rotation values of the locator we just moved.  These are the values that we'll give to the object in a few steps.
     float $tran[] = `xform -q -t -ws $loc`;
     float $rot[] = `xform -q -ro -ws $loc`;
   
     //Delete the constraints, we don't need them anymore
     delete $ptCon;
     delete $orCon;
   
     //Move the selected object to the 0 0 0 locator (that's at the origin), then delete the constraints.
     string $ptConZero[] = `pointConstraint $zeroLoc $selection[$i]`;
     string $orConZero[] = `orientConstraint $zeroLoc $selection[$i]`;
     delete $ptConZero;
     delete $orConZero;
     //Freeze Transformations on the object.  Now the objects values are zeroed out and it's at the origin  
     makeIdentity -apply true -t 1 -r 1 $selection[$i];
   
     //Move the object back to it's original position, using the translation/rotation values from the first locator we created and moved.
     setAttr ($selection[$i] + ".translate") $tran[0] $tran[1] $tran[2];
     setAttr ($selection[$i] + ".rotate") $rot[0] $rot[1] $rot[2];
   
     //Delete the two locators we created, we don't need them anymore.
     delete $zeroLoc;
     delete $loc;
}

Thursday, January 26, 2012

Two new MEL scripts

I recently finished two MEL scripts that I hope will help you guys out.  Both of these are for Windows users.  Thanks to the kind people at CGTalk that helped me when I was stuck.

Snapshot
1)  Snapshot.  Creates a snapshot of the active viewport and saves the image in a Snapshot folder, located in your project directory.  This should save you the hassle of taking and editing screenshots.
NOTE:  Because of a glitch in Maya 2012, the only image format you can have is an .iff.  I'm working on this.  Hopefully I can get a workaround working soon.


Match It!
2) MatchIt!  Will match the translation/rotation of one or more objects to your source object.  You're able to choose the affected axes.

Let me know if you want one of them and I'll send them to you.

~Melissa

PS  Sorry about nCloth for Novices #2.  I noticed and error and deleted it.  It should be up again shortly.

Wednesday, August 4, 2010

My First MEL Script - isn't it cute?!

So apparently it's in my best interest to learn MEL and Python.

Thanks for telling me this with 6 months 'til graduation, folks!  I've been cramming for a while and though I'm still intimidated by MEL, I don't hide in a corner and quiver with fear when I come face to face with it.  At least not all the time.

Anyway, I was watching a rigging tutorial and the guy made a Renaming Script.  I thought it was pretty nifty, but I could never remember what order he selected things in and so I'd make a mistake and rename things in the wrong order.  I thought to myself, "Self, why don't you just make a renaming script where all the fields are labeled 'rename ___ to:'?"  Plus, I wanted to see if I could do it on my own, without looking at the tutorial again.

So, after a lot of trial and error (and help from a guy on CGTalk) I did.  Yay!

Behold, my first MEL Script!

-----

//Renaming Script

//Creates a window

string $RenameWindow = `window -widthHeight 450 200 -rtf true -title "Rename Selected Objects"`;

//Makes the window able to scroll

scrollLayout
            -horizontalScrollBarThickness 16
            -verticalScrollBarThickness   16;

//Makes the window a row column layout
rowColumnLayout
        -numberOfColumns 2 -columnAttach 1 "right" 0
        -columnWidth 1 150 -columnWidth 2 250;

//Queries selections

string $sel[] = `ls -sl`;

//For loop - so we can access and apply the command to each selected object individually

for ($each in $sel) {

    //Name Field Label - Acquires and lists the original name of the selected objects
     text -label ("Rename " + $each + " to:");
    //Renaming Field - Allow the selected object to be renamed
    nameField -o $each;
}

//Button on the window for closing the Renaming Window

button -l "RENAME!" -c "deleteUI -window $RenameWindow";

//This shows the Renaming Window
showWindow $RenameWindow;

-----

Have I done something that hasn't already been done 530950394208539 times before?  Nope.  But I did it, and I understand what it does and I can recreate it if I need to.  That's the fun thing ^^;

~Melissa