Form and Space

creating CG 3D since 2001

  • Connect @
  • Linked in
  • Vimeo
  • Twitter
Replace or Add Objects

MEL: Replace Add Objects

Matthew | November 15, 2013

No Comments

This script compliments the path constraint very nicely. Use this tool to replace objects for another object in a single click. You can choose to instantiate or duplicate the new object. This is ideal for replacing instances with duplicates and the antithesis. You also have the option to delete, hide, or do nothing to the objects being “replaced”. If there is a parent to the object being replaced, the new duplicate will parent itself to it. This is ideal for maintaining existing hierarchies, while able to swap out proxy objects or placeholders.

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

/*------------------------
Replace/Add Objects
version 0.1
November 13, 2013
Author: Matthew L. Stoehr
Source: formandspace.com

Objectives:
- Duplicate and match object transformations of all selected objects.
- Preserves inputs for duplicate objects.
- If there's a parent of the object to match, parent the new duplicate.
- Toggle instantiate or Duplicate.
- Toggle, hide, delete, or do nothing.

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

use:
Ideal for replacing a large amount of objects, such as leafs on a tree, or
fence posts.  
-------------------------------*/

//first argument, (0) instantiate or (1) duplicate
//second argument, (0) delete or (1) hide or (2) do nothing


mls_replaceAddObjects(0,2);


proc mls_replaceAddObjects(int $_di, int $_hd)
{
    string $c[] = `ls -sl`;    
    int $s = size($c);
    int $i;
    int $di = $_di;
    int $hideOrDelete = $_hd;
    //object to duplicate or instantiate
    string $d = $c[$s-1];
    //the new duplicate or instance
    string $dn = ($c[$s-1] + "_copy_");
    
    for ($i = 0; $i < $s-1; $i++)
    {
         //initialize
         string $dObject[];
         
         //get data
         string $thisParent = firstParentOf($c[$i]);  
         float $m[] = `xform -q -ws -m $c[$i]`;
         
        if($di)
         {
            $dObject = `duplicate -n $dn -un $d`;
         }else{
            $dObject = `instance $d`;
         }
         
         //set data
         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]) $dObject[0];
                      
        if($thisParent != "")
        {              
            parent $dObject[0] $thisParent;
        }
        
        switch( $hideOrDelete ) {
		case 0 :
			delete $c[$i]; 
			break;
		case 1 :
			hide $c[$i]; 
			break;
	    case 2 :
			// 
			break;
		default:
			// do default processing
			break;
		}
    }
}

Leave a Reply

Twitter

Subscribers

    396