PDA

View Full Version : Count # of 'inputSurface' attrs


EightBit
11-26-2012, 11:45 PM
I'm connecting a pfxtoon to multiple objects. Each requires:
cmds.connectAttr ('objectShape.outMesh', 'pfxToonShape.inputSurface[3].surface') cmds.connectAttr ('objectShape.worldMatrix[0]', 'pfxToonShape.inputSurface[3].inputWorldMatrix')

How do count the number of 'inputSurfaces' existing on 'pfxToonShape' at any given time so I can create a new index?

Thx.

Keilun
11-27-2012, 03:46 PM
In this case, I don't think you need to. There is a flag -na/nextAvailable on connectAttr which for multi attrs will use the next available index (or create one if necessary). This only works if the indexMatters bit on the inputSurface attribute is false, which I believe is the case. I could be wrong though. Does that solve your issue?

Hope this helps.

Nyro
11-27-2012, 04:02 PM
Amount of indexes on a multi-attribute:

getAttr -size myNode.multiAttribute;


So, in your case:

int $indexCount = `getAttr -s pfxToonShape.inputSurface`;
int $nextIndex = indexCount - 1;

EightBit
11-27-2012, 05:22 PM
@keilun:
Where I previously did this:
cmds.connectAttr (shapeTmp+'.outMesh', 'pfxToonShape.inputSurface[' + str(toonIndex) + '].surface')
cmds.connectAttr (shapeTmp+'.worldMatrix[0]', 'pfxToonShape.inputSurface[' + str(toonIndex) + '].inputWorldMatrix')
I don't see how to incorporate 'na':
cmds.connectAttr (shapeTmp+'.outMesh', 'pfxToonShape.inputSurface.surface', na = 1)
cmds.connectAttr (shapeTmp+'.worldMatrix[0]', 'pfxToonShape.inputSurface.inputWorldMatrix', na = 1)
// Error: The destination attribute 'pfxToonShape.inputSurface.surface' cannot be found.

@Nyro:
Thanks for the syntax for counting. However, this won't work if there are numbers missing from the list of connections.
IE, if I my highest index number is inputSurface[10], but while working I have deleted the connections to inputSurfaces [3-7], the size will be 7, but the next safe number to use in a script would be 11.

Nyro
11-27-2012, 06:16 PM
Well, you can get around that problem too.

Find the smallest value i that is not an index of of pfxToon.inputSurface:

import maya.cmds as cmds

iCount = cmds.getAttr('pfxToon.inputSurface', size = True)
indices = cmds.getAttr('pfxToon.inputSurface', multiIndices = True)
i = 0;
while i <= iCount:
if i in indices:
i = i + 1
else:
break

After running this code, i holds the value of a free index you can use in the rest of your code.

Disclaimer: I don't really know python so I make the syntax up as I go along. There may very well be fancier ways of doing this, but the logic is sound.


// EDIT: //

Ha! Now I feel proud. My solution is exactly what Duncan himself proposed!

With an array attribute one can find the current number of entries using getAttr -size, for example:

getAttr -size pfxToonShape1.inputSurface

Note that these arrays can be sparse, which might happen if you had 2 objects connected to the toon node then deleted the first. The index for the second connection would still be 1, not zero and nothing would be connected to the zero index. The size of the array would now be 1.

Array attributes automatically grow when you connect to or set them. Thus if you connected a second object to index 10 it will expand the array size by 1. To find the array elements in use you can use:

getAttr -multiIndices pfxToonShape1.inputSurface

This will return all the indices that are have values. You can use the above call to find the first free index on the multi. (just the size alone won't work if the array is sparse)

int $ind[] = `getAttr -multiIndices pfxToonShape1.inputSurface`;
int $freeInd = 0;
while($freeInd < size( $ind)){
if( $freeInd != $ind[$freeInd] )
break;
$freeInd++;
}

(freeInd will then have the first unoccupied indice)

Uncanny how we say basically the same thing!

EightBit
11-28-2012, 02:39 PM
Ha! Now I feel proud. My solution is exactly what Duncan himself proposed!
Uncanny how we say basically the same thing!
Great minds think alike ;)
Thanks much.

CGTalk Moderation
11-28-2012, 02:39 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.