Form and Space

creating CG 3D since 2001

  • Connect @
  • Linked in
  • Vimeo
  • Twitter

MEL: Duplicating and Transforming Revisited

Matthew | October 30, 2012

No Comments

This tool will duplicate a single object and move it to multiple locations of a multi-selection. It could easily be modified to do other jobs, like replace all selected objects with instances.

I’ve been researching Maya MEL script more and more. I’ve been inspired lately, by a few projects and art going on around me, to tighten up my MEL. I’ve revisited the duplicate and transform tool. It’s now more efficient, does not use “select”, and uses the -matrix flag of the xform command. This makes a very succinct script and it is much faster. Lately, I’ve been reading the Complete Maya Programming by David Gould. After flailing around in MEL for nearly a year, this book is bringing all the information together for me. I highly recommend it. I know there is a big push to learn Python or PyMEL in Maya, but I’m finding it is a better transition into Maya through MEL, then onto more modern languages. After all, MEL is Maya’s first language.

Other links on the subject of object transforms in MEL script, Python or PyMEL:
Here
and here.

/*------------------------
Duplicate and Match Transforms
version 0.2
October 29, 2012
Author: Matthew L. Stoehr
Source: formandspace.com

Objective:
Duplicate and match object transformations of all selected objects.
Preserves inputs for duplicate objects.

How to use:
1. Select all objects to match.
2. Select object to duplicate last. (Last object selected is duplicated).
3. Run script.
-------------------------------*/

mls_dupAndTransform();

proc mls_dupAndTransform()
{
    string $c[] = `ls -sl`;    
    int $s = size($c);
    int $i;
    string $oldObj = $c[$s-1];
    string $newObj = ($c[$s-1] + "_copy_");
    
    for ($i = 0; $i < $s-1; $i++)
    {
            
         float $m[] = `xform -q -ws -m $c[$i]`;

         string $duplicatedObject[] = `duplicate -n $newObj -un $oldObj`;
            
         xform -ws -m ($m[0]) ($m[1]) ($m[2]) ($m[3])
                      ($m[4]) ($m[5]) ($m[6]) ($m[7])
                      ($m[8]) ($m[9]) ($m[10]) ($m[11])
                      ($m[12]) ($m[13]) ($m[14]) ($m[15]) $duplicatedObject;
        
    }
}

Leave a Reply

Twitter

Subscribers

    396