PDA

View Full Version : Instance Replacement Utility


smoluck
10-21-2005, 09:00 AM
I need to find a Mel Script that can help me to replace a list of several polyshape Instance; who get different Tx Ty Tz and Rx Ry Rz; by a new one...

For expemple you've done a city in maya composed by several items and several Trees who are just instanced several times and spread on that city. So now I want to change the initial shape of that tree. Should I have to search a mel script that can help me ( by selecting an other polyshape |new tree shape| in the scene and select the original treeshape that I want to update, to change all the instanced object) or I have just to find the node who get connected the original PolyShape of the tree and all the different transform of the instance object to change the connection ?

I really need that type of tool, cause I 'm currently updating several times a game environement set... Should I can find that kind of things ?

*** :lightbulb It will be pleasent to have in the mel script an option who can Delete all the Shading / texture / file node connected to the old shape :lightbulb ***

As reason as we used an home made exporter, we can't use reference principle because we can't resolve some export issue, so Referencing was not a solution for me. Amel script would be good enouth.

smoluck
10-21-2005, 09:34 AM
As you can see on that thread...Nicool done a little script to do Geometry replacement ( not Instance replacement. I have already tested that script and find that there is just a bunch of simple copy of the object , not instanced copies.

http://forums.cgsociety.org/showthread.php?t=154417

Nicool Code:
string $replacementShape = "myReplacementShapeName";

string $objectsToReplace[] = `ls -sl`; // This array can be build the way you

for($object in $objectsToReplace) {

// Get data of this object
float $objectT[] = `xform -q -a -t $object`;
float $objectR[] = `xform -q -a -ro $object`;
float $objectS[] = `xform -q -r -s $object`;

// Duplicate and place a replacement shape from data
string $duplicatedReplacementShape[] = `duplicate $replacementShape`;
xform -a -t $objectT[0] $objectT[1] $objectT[2] $duplicatedReplacementShape;
xform -a -ro $objectR[0] $objectR[1] $objectR[2] $duplicatedReplacementShape;
xform -s $objectS[0] $objectS[1] $objectS[2] $duplicatedReplacementShape;

setAttr ($object + ".visibility") 0;
//delete $object; Use this line to delete the replaced object
//print ($object + " replaced by " + $replacementShape + ".\n");
}

As you can try, if you create severeral cube by instance copies, of that one, and spread it around with different T R or S. And after you use that script to replace all the cube by a sphere shape, you shopuld see that you lost the instance depedency. for exemple, if you move a vertex on the sphere shape, all the new copies doesn't take effect of that modification. It was what I want to avoid.I would like to get a shape dependency between all the spherecopies like an instanced object.

thematt
10-21-2005, 10:32 AM
Not tested but should work.

Create your new tree and connect the outMesh of your newtree Shape to the inMesh of the one you want to change.
Then delete history on your instance obj to kill the connection.

thematt
10-21-2005, 10:42 AM
it works just tested it..
try that .

{

// select the object you want to repalce and then the new Object



//list your all your object.

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

string $shapes[] =`listRelatives -s $obj`;

// make the appropriate connections

connectAttr -f ($shapes[1]+".outMesh") ($shapes[0]+".inMesh");

delete -ch $obj[0];

}



cheers

thematt
10-21-2005, 10:47 AM
I was thinking, is there a reason you don't use the instancer? must be but just making sure.

Considering the shading group I supposed it would be possible to copy all shader from one object to the other.
There are probably already existing mel for that.
search highend.


cheers

smoluck
10-21-2005, 01:23 PM
New Post : Sorry It Works Well... I do a mistake. :banghead:

--------------------------

Old Post:
your script .. works well for just copies.. but not for instance. :cry:

I have done a test replacing the tree shape by a sphere, and now when I want to change / modify a vertex position, it does not take effect on the new one created.

Can we store all the Pos, Rot, and Scale of the first selection ( bunch of trees spread on the city ), and Create a new group of instance based on the new shape, who get the same number of Instance and same pos, rot, scale ?

--------------------------
I've done a mistake on my scene...Load the Simple Copies scene...lol

It Works Really Fine...:bowdown: And will be used to update my scene with time cost... I will use it to create a simple model scene for Fine tuning my Ambient Occlusion baking , Lightmaps and other ...

smoluck
10-21-2005, 01:45 PM
The only Problem , was that when you execute the procedure, you lost all the Shading node connection between Faces and Materials... What can I do if I would like to update a mesh with mutiple material assigned to several face groups ?:cry:


ou should change your name by Themaster .. Thematt ;-)

Nicool
10-21-2005, 10:29 PM
Hi Smoluck,

I received your private message and, for your task, I'd reply to replace duplicate with instance (or such a function like this, relative to the duplicate one in the F1 doc) function in this line of my script :

string $duplicatedReplacementShape[] = `duplicate $replacementShape`;

It would prevent any instancing issue (and the use of thematt script, as good as it is).

Note : At the time I posted this script, I wasn't really skilled (and quite young). A guy on CGtalk was talking about a simple, but tricky, method playing with node networking to replace a bucnh of object (actually made on a shape node and a transform node).

Second, and quite out of topic you was talking about creating an UI for the script. I think a UI is really not needed here.

thematt
10-22-2005, 03:48 PM
The only Problem , was that when you execute the procedure, you lost all the Shading node connection between Faces and Materials... What can I do if I would like to update a mesh with mutiple material assigned to several face groups ?
best method would be to write a script for that, not that hard.

But I think for your problem a simpler solution could be done, by just having a script that redo all the instance with the new object reading the transform associated with the main shape you want to replace.
Longer for sure but more efficient.

I could take a look at it but not monday.


cheers

thematt
10-22-2005, 08:50 PM
ok I had a quick look at it.

This script below will work .
It will not work if you object are group right now but it could be add if necessary.
So there would some limitation.
There could be other way to recall the translate of the instance if necessary with xform for exemple but at least you have a start of a working solution.

hope taht help.

cheers

global proc changeInstance()

// how to use it:

// source teh script then select first your object with instance, and 2nd

// the new object you want to be instance instead.

// changeInstance;

// this script will not work if your instance have been group along the way

{

// get your both object

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

// retreive the shape of the one which have instance associated too

string $shape[] =`listRelatives -s $obj[0]`;

// get a list of the transform

string $transform[] =`listRelatives -ap $shape[0]`;

// get the initial transform out of the way

string $instances[] = stringArrayRemove($obj, $transform);

string $i;

// loop through all instance to copy create the new one

for ($i in $instances)

{

//get the translate

float $transX =`getAttr ($i+".translateX")`;

float $transY =`getAttr ($i+".translateY")`;

float $transZ =`getAttr ($i+".translateZ")`;

//get the rotate

float $rotX =`getAttr ($i+".rotateX")`;

float $rotY =`getAttr ($i+".rotateY")`;

float $rotZ =`getAttr ($i+".rotateZ")`;

// get teh scale

float $scaleX =`getAttr ($i+".scaleX")`;

float $scaleY =`getAttr ($i+".scaleY")`;

float $scaleZ =`getAttr ($i+".scaleZ")`;

// duplicate the new object for each instance

string $du[] =`duplicate -ilf $obj[1]`;

// set the attribute for the new object translate

setAttr ($du[0]+".translateX") $transX;

setAttr ($du[0]+".translateY") $transY;

setAttr ($du[0]+".translateZ") $transZ;

// set the attribute for the new object rotate

setAttr ($du[0]+".rotateX") $rotX;

setAttr ($du[0]+".rotateY") $rotY;

setAttr ($du[0]+".rotateZ") $rotZ;

// set the attribute for the new object scale

setAttr ($du[0]+".scaleX") $scaleX;

setAttr ($du[0]+".scaleY") $scaleY;

setAttr ($du[0]+".scaleZ") $scaleZ;

// delete the old instance

delete $i;

// rename your instance as they where

rename $du $i;



}

}

drGonzo
10-22-2005, 09:15 PM
Ripping this apart might offer some ideas as well:

smoluck
10-24-2005, 09:58 AM
With all the respect that i have for Thematt; I think that the Nicool melscript work more safety as Thematt script ( Maya hangup to windows.. lol :shrug: ). And replace duplicate command by instance, resolve the shading group connection problem.


// --------- "Nicool" Instance Replacement MelScript ---------



string $replacementShape = "LOD_CubeShape";

string $objectsToReplace[] = `ls -sl`; // This array can be build the way you

for($object in $objectsToReplace) {

// Get data of this object

float $objectT[] = `xform -q -a -t $object`;

float $objectR[] = `xform -q -a -ro $object`;

float $objectS[] = `xform -q -r -s $object`;

// Duplicate and place a replacement shape from data

string $duplicatedReplacementShape[] = `instance $replacementShape`;

xform -a -t $objectT[0] $objectT[1] $objectT[2] $duplicatedReplacementShape;

xform -a -ro $objectR[0] $objectR[1] $objectR[2] $duplicatedReplacementShape;

xform -s $objectS[0] $objectS[1] $objectS[2] $duplicatedReplacementShape;

setAttr ($object + ".visibility") 0;

//delete $object; Use this line to delete the replaced object

//print ($object + " replaced by " + $replacementShape + ".\n");

};



Here that works Before and After.... And I can Change the geometry of the base shape of the instance ;-) ; and update all the Instanced object.

http://skeeter.mot.free.fr/cgtalkupload/InstanceReplacement/InstanceReplacement.gif

thematt
10-24-2005, 11:36 AM
cool..
yeah sure Nicool script should work just fine.
My last script is a bit differrent in a sence that you only have to select both object and it will make the new instance, finding automatically all instance object.You don't have to select all instance.
It works prefectly fine here.
But if you have a working solution then it's all that matter right.

cheers

smoluck
10-24-2005, 02:20 PM
thanks to Thematt for is help in chat.

Ok after I've retry the melscript from Thematt. it works and give better workflow , because you doesn't need to specify the ShapeName of the Target object. And the shading connection works... fine.

Finally Thematt solution Was my choice. :)

-------------------------------------:wavey:

CGTalk Moderation
10-24-2005, 02:20 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.