SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.


SM Forums / Lounges / Maya Basics & Newbie Lounge / very simple Melscript needed...move selected object to origin (0,0,0)

Maya Basics & Newbie Lounge Please post in this section ONLY if your question doesn't fit in anywhere else on the forum, or if you're not sure which section it's relevant to. We also have Free streaming trial available.

Join the conversation. Reply to post
Page 1 of 3 1 2 3
 
Thread Tools
27-05-2012, 03:09 PM   #1
juggler7
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
  Reply with quote
27-05-2012, 03:47 PM   #2
Sircharles
Registered User
 
Sircharles's Avatar
 
Join Date: Dec 2010
Location: Uk
Posts: 193
that would be a handy one alright..
  Reply with quote
27-05-2012, 04:01 PM   #3
juggler7
Registered User
 
Join Date: Apr 2012
Posts: 32
Quote:
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?
  Reply with quote
27-05-2012, 04:18 PM   #4
honestdom
The Nurb Herd
 
honestdom's Avatar
 
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?
  Reply with quote
27-05-2012, 07:03 PM   #5
ctbram
Moderator
 
ctbram's Avatar
 
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.
  Reply with quote
27-05-2012, 08:17 PM   #6
juggler7
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
  Reply with quote
27-05-2012, 08:52 PM   #7
ctbram
Moderator
 
ctbram's Avatar
 
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
  Reply with quote
27-05-2012, 09:16 PM   #8
Jay
Lead Modeler - Framestore
 
Jay's Avatar
 
Join Date: Feb 2003
Location: UK
Posts: 6,277
err yeah I agree with Dom....why cant you just type 0 for each in the translates??

J
  Reply with quote
27-05-2012, 09:20 PM   #9
ctbram
Moderator
 
ctbram's Avatar
 
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.
  Reply with quote
28-05-2012, 08:54 AM   #10
juggler7
Registered User
 
Join Date: Apr 2012
Posts: 32
Quote:
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
  Reply with quote
28-05-2012, 09:20 AM   #11
Jay
Lead Modeler - Framestore
 
Jay's Avatar
 
Join Date: Feb 2003
Location: UK
Posts: 6,277
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
  Reply with quote
28-05-2012, 09:23 AM   #12
honestdom
The Nurb Herd
 
honestdom's Avatar
 
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.

Last edited by honestdom; 28-05-2012 at 09:41 AM.
  Reply with quote
28-05-2012, 05:43 PM   #13
Sircharles
Registered User
 
Sircharles's Avatar
 
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,
  Reply with quote
28-05-2012, 08:58 PM   #14
ctbram
Moderator
 
ctbram's Avatar
 
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.
  Reply with quote
28-05-2012, 09:44 PM   #15
juggler7
Registered User
 
Join Date: Apr 2012
Posts: 32
Quote:

C.
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.
  Reply with quote
Reply
Page 1 of 3 1 2 3

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with texturing a simple object calltodestruction666 Maya Materials & Textures 1 28-03-2016 07:36 PM
Convert Camera Translation to Object rotation? jimvfx Animation 0 28-01-2015 10:06 PM
simple edge/face needed srlake314 Maya Basics & Newbie Lounge 1 11-02-2012 05:03 AM
Simple file conversion help needed Jr.Who Maya Basics & Newbie Lounge 8 31-01-2010 10:37 PM
needed help on simple model shape Kozmonut Maya Basics & Newbie Lounge 4 26-09-2003 02:01 AM