PDA

View Full Version : swaping shape nodes


EigenPuff
03-22-2006, 12:14 AM
I'm porting a max utility for my artists which swaps out the mesh of one object for another. I originally started to implement this technique with replaceNode (http://www.ewertb.com/maya/mel/mel.php?howto=69) but that is not working exactly the way I want.

What I would like to do is to take a transform node, and replace it's shape node inplace, maintaining the original orientation & position of the transform's shape node. I figured that all of the orientation information would automatically be inherited from the transform parent node, but it seems I'm missing a concept here.

Here's a simplified version of what I have:


//---------------------------------------------------------
//ripped from ewertb.com MEL How To #8 - Thanks, Brian!
//---------------------------------------------------------
proc string[] GetShapes( string $xform )
{
string $shapes[];

$shapes[0] = $xform;

if ( "transform" == `nodeType $xform` )
// If given node is not a transform, assume it is a shape
// and pass it through
{
$shapes = `listRelatives -fullPath -shapes $xform`;
}

return $shapes;
}

//---------------------------------------------------------
// Replaces the child-shape node of the destination with a
// copy of the one found under source
//---------------------------------------------------------
global proc ReplaceXFormShape(string $destxform, string $sourcexform)
{
//get shapes from transform parents
string $oldshapes[] = GetShapes($destxform);
string $newshapes[] = GetShapes($sourcexform);

if(size($newshapes) != 1)
print("WARNING: This script only handles transforms with a single child shape\n");

//copy the source shape so that we leave it intact for future operations
string $dupshapes[] = `duplicate $newshapes[0]`;

//replace the old shape with the duplicate of the new one
replaceNode $oldshapes[0] $dupshapes[0];
parent -shape $dupshapes[0] $destxform;
delete $oldshapes;
rename $dupshapes[0] $oldshapes[0];
}


Anybody see what I'm missing? Thanks!

john_homer
03-22-2006, 02:35 AM
// parents the shape node of 1 object to another
// select 2 transform nodes, shape first, parent last

string $sel[] = `ls -flatten -sl`;
string $oldTransform[] = `parent $sel[0] $sel[1]`;
makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 $oldTransform[0];
string $shapeList[] = `listRelatives -f -s $oldTransform[0]`;
$shapeList = `parent -r -s $shapeList[0] $sel[1]`;
rename $shapeList[0] ($sel[1] + "Shape");
delete $oldTransform[0];


.j

sam_g
03-22-2006, 05:22 PM
Something that I've used lately is to connect the replacer.outMesh to target.inMesh. The mesh is then replaced. Break the connection, and that's it.

Be forewarned that sometimes I've been unable to undo this maneuver. That is, if you're into that kind of thing.

Sam

john_homer
03-22-2006, 08:31 PM
Something that I've used lately is to connect the replacer.outMesh to target.inMesh. The mesh is then replaced. Break the connection, and that's it.

Be forewarned that sometimes I've been unable to undo this maneuver. That is, if you're into that kind of thing.

Sam

that will work, and take UV's too.. ;^)

however, it assumes the transforms match and undo works only if you make the connection manually..
ie if you do it with this...
string $sel[] = `ls -sl`;
connectAttr -f ($sel[0] + ".outMesh") ($sel[1] + ".inMesh");
it will not undo.. ever.

.j

EigenPuff
03-28-2006, 02:33 AM
Thank you for your responses! I just got back from GDC, and had an opportunity to try them out. The biggest part that I was missing was that li'l ol' "-r" on my parent command - thanks John for bringing that to my attention. After consulting with a teammate over here, here's what I got:


global proc ReplaceXFormShape(string $sourcexform, string $destxform)
{
//get shapes from transform parents
string $newshapes[] = GetShapes($sourcexform);
string $oldshapes[] = GetShapes($destxform);

if(size($newshapes) != 1)
print("WARNING: This script only handles transforms with a single child shape\n");

//copy the source shape so that we leave it intact for future operations
// Note that duplicating the shape automatically gives us a new transform
string $dupxform[] = `duplicate -upstreamNodes $newshapes[0]`;

//replace the old shape with the duplicate of the new one
string $dupshapename[] = `listRelatives -shapes $dupxform[0]`;
string $curshapename[] = `listRelatives -shapes $destxform`;

parent -removeObject -shape $oldshapes;
parent -relative -shape $dupshapename[0] $destxform;

rename $dupshapename[0] $curshapename[0];
delete $dupxform;
}

CGTalk Moderation
03-28-2006, 02:33 AM
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.