Introduction to Maya - Modeling Fundamentals Vol 1
This course will look at the fundamentals of modeling in Maya with an emphasis on creating good topology. We'll look at what makes a good model in Maya and why objects are modeled in the way they are.
# 1 27-05-2012 , 03:09 PM
Registered User
Join Date: Apr 2012
Posts: 32

very simple Melscript needed...move selected object to origin (0,0,0)

Hi

How can I write a simple melscript, which takes the selected object and moves it to 0,0,0..And then maybe centers the pivot also to the object

thanks

# 2 27-05-2012 , 03:47 PM
Sircharles's Avatar
Registered User
Join Date: Dec 2010
Location: Uk
Posts: 193
that would be a handy one alright..

# 3 27-05-2012 , 04:01 PM
Registered User
Join Date: Apr 2012
Posts: 32

that would be a handy one alright..

this is as close as I have got...doesnt work tho:

string $selection[] = `ls -selection`;
setAttr $selection.translateX 0;
setAttr $selection.translateY 0;
setAttr $selection.translateZ 0;


what is wrong here?

# 4 27-05-2012 , 04:18 PM
honestdom's Avatar
The Nurb Herd
Join Date: Oct 2007
Location: London
Posts: 2,381
i take it you cant just select all the objects and type 0 in the translate attributes?

# 5 27-05-2012 , 07:03 PM
ctbram's Avatar
Moderator
Join Date: Jan 2004
Location: Michigan, USA
Posts: 2,997
The issue with your script is selection[] is an array and you are using it as a single variable.

As suggested you could just select all the objects and in the channel box set their translation values for (x y z) to 0.

But for completeness here is what the script should look like...

Code:
string $sel[] = `ls -sl`;
string $obj;
for ($obj in $sel)
{
    setAttr ($obj + ".translateX") 0;
    setAttr ($obj + ".translateY") 0;
    setAttr ($obj + ".translateZ") 0;
}
Here is a version that centers the pivot of each object and then moves it to the global origin and it has been made into a global procedure so you can call it with "centerObjects();"

Code:
// procedure: centerObjects()
// usage: centerObject();
// author: ctbram
// date: 05272012
//
// Place this script into your maya folders scripts folder and name it
// "centerObjects.mel" and then use by selecting objects that you want 
// to center  pivot and move to the global origin ( 0 0 0 ) and then 
// enter the command "centerObjects();" on the command line.
//
// Not sure how useful this is as you can achieve the same thing by
// selecting objects and then just setting their x,y,z coordinates to 
// zero in the channel box.  But here is how to do it programatically.

global proc centerObjects()
{
    string $sel[] = `ls -sl`;
    string $obj;
    for ($obj in $sel)
    {
        // center $obj's pivot
        xform -cp $obj;

        // move $obj to the global origin (0,0,0)
        setAttr ($obj + ".translateX") 0;
        setAttr ($obj + ".translateY") 0;
        setAttr ($obj + ".translateZ") 0;
    }
}


"If I have seen further it is by standing on the shoulders of giants." Sir Isaac Newton, 1675

Last edited by ctbram; 28-05-2012 at 09:08 AM.
# 6 27-05-2012 , 08:17 PM
Registered User
Join Date: Apr 2012
Posts: 32
nice thanks man, worked great..did you code this yourself? I am starting to get into MEL scripting

# 7 27-05-2012 , 08:52 PM
ctbram's Avatar
Moderator
Join Date: Jan 2004
Location: Michigan, USA
Posts: 2,997
Yeah, I coded it. Once you learn the basics mel is pretty straight forward. Especially for stuff you can do manually because you can copy the code right from the script editor.

Learning when to group things in parenthesis can be tricky for instance $obj.translateX and $obj + ".translateX" are both different from ($obj + ".translateX").

and then I use google a lot. )


"If I have seen further it is by standing on the shoulders of giants." Sir Isaac Newton, 1675
# 8 27-05-2012 , 09:16 PM
Jay's Avatar
Lead Modeler - Framestore
Join Date: Feb 2003
Location: UK
Posts: 6,287
err yeah I agree with Dom....why cant you just type 0 for each in the translates??

J

# 9 27-05-2012 , 09:20 PM
ctbram's Avatar
Moderator
Join Date: Jan 2004
Location: Michigan, USA
Posts: 2,997
Yep, I also suggested that he just select all the objects and set the translation values to zero, but made the script anyway just to help folks that want to understand mel scripting.


"If I have seen further it is by standing on the shoulders of giants." Sir Isaac Newton, 1675

Last edited by ctbram; 28-05-2012 at 09:10 AM.
# 10 28-05-2012 , 08:54 AM
Registered User
Join Date: Apr 2012
Posts: 32

err yeah I agree with Dom....why cant you just type 0 for each in the translates??

J

why? number 1..I want to learn Melscripting, this is a good place to start...and number 2, I want to move multiple objects with the click of a button, no typing on the keyboard

# 11 28-05-2012 , 09:20 AM
Jay's Avatar
Lead Modeler - Framestore
Join Date: Feb 2003
Location: UK
Posts: 6,287
not denying your learning of mel, I just agree that in this particular case, hitting 0 in the AE or CB is far easier

anyway good luck with the mel scripting, its good fun once you get going

J

# 12 28-05-2012 , 09:23 AM
honestdom's Avatar
The Nurb Herd
Join Date: Oct 2007
Location: London
Posts: 2,381
A. i dont think you made it very clear that you wanted to learn mel. you just asked for a script

B. using the channel box is a sensible and quick option. what you are asking wasn't a great example of how one would go about learning mel. how about starting by doing something that is more laborious and would be easier if it were automated.

C. user added image


Last edited by honestdom; 28-05-2012 at 09:41 AM.
# 13 28-05-2012 , 05:43 PM
Sircharles's Avatar
Registered User
Join Date: Dec 2010
Location: Uk
Posts: 193
But if you have deleted history/freeze transformations on certain objects, wouldn't it then be necessary? which is often the case,

# 14 28-05-2012 , 08:58 PM
ctbram's Avatar
Moderator
Join Date: Jan 2004
Location: Michigan, USA
Posts: 2,997
doh! sirCharles just pointed out a flaw in the original script. If you freeze the transforms on any object using setAttr with the x y z transforms to zero will not move the object at all so I made the following changes to the script.

I use the mel command "move -rpr 0 0 0 $obj" to move the object (relative to its rotate pivot) to the global 0 0 0 position. This means the pivot needs to be centered first which is accomplished with the "xform -cp" command.

Since this is a learning exercise I found the move -rpr command by creating an object and then snapping it to the global origin holding the x-key down and then looked in the script editor to see the command that was generated, then went to the mel reference and read up a little on the move command. This is how a figure out most commands in mel. I did the same thing for the freeze transform block of code. I picked an object and froze its transforms then looked at what was generated in the script editor.

I also added the parameter $ft to control if the objects transforms are frozen after being moved to the global origin. (0 -- don't freeze, 1 -- freeze)

I tack "rm" in front of scripts I write so I can tell at a glance what scripts are mine in the scripts folder so changed the name to rmCenterObjects, and you call it using rmCenterObjects 0 | 1; You can chage it to whatever you want just remember the proc name and the file name must match.

I am not a mel programmer and there are many ways to do things in maya so this may not be the best way but it seems to work.

Code:
// procedure: rmCenterObjects()
// usage: rmCenterObjecst(0|1);
// author: ctbram
// date: 05272012
//
// Place this script into your maya folders scripts folder and use by 
// selecting objects that you want to center  pivot and move to the 
// global origin ( 0 0 0 ).
//
// examples:
// 1. rmCenterObjects(0) moves the selected objects to the global origin while
// 2. rmCenterObjects(1) moves the selected objects to the global origin and
// freezes the objects transforms
//
// alternatively you can drop the parenthesis and use the format...
//
// rmCenterObjects 0; or rmCenterObjects 1;
//
// modified 05282012
//   added the $ft flag
//   use the move command rather then setAttr to account 
//     for objects that have had their transforms frozen

global proc rmCenterObjects(int $ft)
{
    string $sel[] = `ls -sl`;
    string $obj;

    // center each objects pivot, move it to the global origin and if
    // the $ft parameter is 1 then freeze its transforms...
    for ($obj in $sel)
    {
        // center $obj's pivot
        xform -cp $obj;
        
        // move the $obj to the global origin using move
        // relative to the rotation pivot -rpr
        move -rpr 0 0 0 $obj;
        
        // if $ft is 1 then freeze transforms after moving 
        // $obj to theglobal origin
        if ($ft == 1)
        {
           select -r $obj ;
           FreezeTransformations;
           makeIdentity -apply true -t 1 -r 1 -s 1 -n 0;
        }     
    }
    
    // restore the original selection
    select -r $sel;
}


"If I have seen further it is by standing on the shoulders of giants." Sir Isaac Newton, 1675

Last edited by ctbram; 28-05-2012 at 10:41 PM.
# 15 28-05-2012 , 09:44 PM
Registered User
Join Date: Apr 2012
Posts: 32


C. user added image

lol..why are you guys feeling so strongly about typing 0? sometimes it's nice to have buttons which do stuff, with no need to touch the keyboard..this is a good place to start..basics of a function...translations..now Im going to try a drop to floor script

p.s. user added image

Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads