PDA

View Full Version : Transfer node connections error?


Andrei2k
11-26-2010, 07:45 PM
Not sure what is happening but this script below will function properly if I run it line by line but not as a proc. Not really sure what to think. I did notice that even running it line by line is will list a "." character at the end of the Result: message from disconnecting and connecting Attrs. I can explain more if needed. Any ideas?

Description: Takes connections from first selected node(based on the string match criteria in the argument) and transfers them on the second selected node.
Usage:

Open test.ma scene I attached.
Source: as_switchConnections.mel (below)


// SCRIPT: as_switchConnections.mel
//

//

// DATE: 11/25/2010

//

// DESCRIPTION:

// Swaps source connections for an attribute triple (i.e. scaleX, scaleY, scaleZ) to a new target.

//

// USE: Select node with current connections and shift-select node to transfer connection(s) to.

//

// Initial 1.0:

// - User can specify an attribute and the old node attribute wil be reset once connections are transferred

// - Useful for transferring connections between nodes in similar or same hierarchies.

// - Detects for proper selection and if connections exist for transfer.

//

//

//-------------------------------------------------------------------------------------------------------------------------------------//

//

// Example use: as_switchConnections scale

//

//-------------------------------------------------------------------------------------------------------------------------------------//

global proc as_switchConnections(string $attribute)
{
// string $attribute = "scale";
string $object[]=`ls -sl -tr`;
int $defValue = 0;
string $attrs[];
if (`gmatch $attribute "scale"`) $defValue = 1;
if (`size($object)`==2)
{
$attrs = `listAttr -s -k -c -st ($attribute +"*") $object[0]`;
for ($i=0; $i < size($attrs);$i++)
{
$oldNode = `listConnections -d off -s on -p on -scn 1 ($object[0] + "." + $attrs[$i])`;
if (`size($oldNode)`==0) error "No connection on source node to transfer!";
string $oldNodeToken[];
catchQuiet(`tokenize $oldNode[0] "." $oldNodeToken`);
// print $oldNodeToken;
disconnectAttr ($oldNodeToken[0]+"."+$oldNodeToken[1]) ($object[0] + "." + $attrs[$i]);
setAttr ($object[0] + "." + $attrs[$i]) $defValue;
connectAttr ($oldNode[0]) ($object[1] + "." + $attrs[$i]);
}
}
else
{
error "You must select two objects to swap.";
}
}



Then run:


select -r Trunk_jnt_01_EXP ;
select -add Trunk_jnt_01 ;
as_switchConnections scale;

Andrei2k
11-26-2010, 08:05 PM
As extra proof of the confusing issue I strang the three loops together instead of using a single loop as a test and it works. This becomes more puzzling..


Usage:
select -r Trunk_jnt_01_EXP ;
select -add Trunk_jnt_01 ;
(run code below)


{
string $attribute = "scale";
string $object[]=`ls -sl -tr`;
int $defValue = 0;
string $attrs[];
if (`gmatch $attribute "scale"`) $defValue = 1;
if (`size($object)`==2)
{
$attrs = `listAttr -s -k -c -st ($attribute +"*") $object[0]`;
for ($i=0; $i < 1;$i++)
{
$oldNode = `listConnections -d off -s on -p on -scn 1 ($object[0] + "." + $attrs[$i])`;
if (`size($oldNode)`==0) error "No connection on source node to transfer!";
string $oldNodeToken[];
catchQuiet(`tokenize $oldNode[0] "." $oldNodeToken`);
print $oldNodeToken;
disconnectAttr ($oldNodeToken[0]+"."+$oldNodeToken[1]) ($object[0] + "." + $attrs[$i]);
setAttr ($object[0] + "." + $attrs[$i]) $defValue;
connectAttr ($oldNode[0]) ($object[1] + "." + $attrs[$i]);
}
for ($i=1; $i < 2;$i++)
{
$oldNode = `listConnections -d off -s on -p on -scn 1 ($object[0] + "." + $attrs[$i])`;
if (`size($oldNode)`==0) error "No connection on source node to transfer!";
string $oldNodeToken[];
catchQuiet(`tokenize $oldNode[0] "." $oldNodeToken`);
print $oldNodeToken;
disconnectAttr ($oldNodeToken[0]+"."+$oldNodeToken[1]) ($object[0] + "." + $attrs[$i]);
setAttr ($object[0] + "." + $attrs[$i]) $defValue;
connectAttr ($oldNode[0]) ($object[1] + "." + $attrs[$i]);
}
for ($i=2; $i < 3;$i++)
{
$oldNode = `listConnections -d off -s on -p on -scn 1 ($object[0] + "." + $attrs[$i])`;
if (`size($oldNode)`==0) error "No connection on source node to transfer!";
string $oldNodeToken[];
catchQuiet(`tokenize $oldNode[0] "." $oldNodeToken`);
print $oldNodeToken;
disconnectAttr ($oldNodeToken[0]+"."+$oldNodeToken[1]) ($object[0] + "." + $attrs[$i]);
setAttr ($object[0] + "." + $attrs[$i]) $defValue;
connectAttr ($oldNode[0]) ($object[1] + "." + $attrs[$i]);
}
}
}




ideas?

NaughtyNathan
11-26-2010, 10:21 PM
do you know about the nodeCast command?

:nathaN

Andrei2k
11-27-2010, 11:04 AM
Yes I do but the problem with nodeCast is it will do an entire swap. If I have the orients constrained and scales plugged and want to switch all the scales to another node but leave the orients alone to my knowledge nodeCast won't work.

rahulsg2901
12-01-2010, 06:42 AM
U just gave disconnect first attribute wrong this must be ($oldNode[0]) replace with this ($oldNodeToken[0]+"."+$oldNodeToken[1])

Andrei2k
12-20-2010, 09:36 AM
just saw response, sorry for the late reply. I will give that a try, thank you!

Mark-J
12-20-2010, 10:06 AM
Take a look at the copyAttrs command. It's something I've not used before but hidden in there is effectively nodeCast but for given attrs on source or destination. I've been looking at this last week and it does just what NodeCast should do, accept args rather than just cast the whole node. God only knows why these commands on in a cmd called copyAttr!

pixelranger
12-21-2010, 10:58 PM
This should work.


global proc as_switchConnections(string $attribute){
string $object[]=`ls -sl -tr`;
int $defValue = 0;
string $attrs[];
if (`gmatch $attribute "scale"`) $defValue = 1;
if (`size($object)`==2){
$attrs = `listAttr -s -k -c -st ($attribute +"*") $object[0]`;
for ( $i=0; $i< size($attrs); $i++){
string $oldNode[] = `listConnections -d off -s on -p on -scn 1 ($object[0] + "." + $attrs[$i])`;
if (!`size($oldNode)`){
print ( $object[0] + "." + $attrs[$i] + " has no connection to transfer!");
}else{
string $oldNodeToken[];
tokenize $oldNode[0] "." $oldNodeToken;
// print $oldNodeToken;
disconnectAttr ($oldNodeToken[0]+"."+$oldNodeToken[1]) ($object[0] + "." + $attrs[$i]);
setAttr ($object[0] + "." + $attrs[$i]) $defValue;
connectAttr ($oldNode[0]) ($object[1] + "." + $attrs[$i]);
}
}
}else{
error "You must select two objects to swap.";
}
}

as_switchConnections scale;

I've got a script that does this and more on my blog (http://andrehitsoy.tumblr.com/post/2359812416/08-transfer-channel-box-attributes-also-anim) (transfers values, anim, expressions or connections) including a description.

You are using $defValue to set the scale on the target before swapping connections? Why?
When you swap, the value will be controlled by the animCurve anyway.
If you DO need to set it, you might want to get it from the source first using float $defValue = `getAttr ($object[0] + "." + $attribute)`; .

CGTalk Moderation
12-21-2010, 10:58 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.