Making connection to the next available multi-attribute index in Python.

Making connection to the next available multi-attribute index in Python.

morinnicolas8944
Advocate Advocate
3,940 Views
2 Replies
Message 1 of 3

Making connection to the next available multi-attribute index in Python.

morinnicolas8944
Advocate
Advocate

I couldn't find a direct way to make a connection to a multi-attribute in Python when I don't know if there are connections already. I am trying to connect to a Set node's "usedBy" attribute, but it looks like its indexMatters flag is not set to false, so using "connectAttr" with "nextAvailable=True" fails.

 

I ended up using MEL's "getNextFreeMultiIndex" to find the next available index as shown below, but going to MEL within a Python script feels awkward and I was wondering if there was a more direct way to do it.

 

Here is how I am handling it now:

import maya.cmds as cmds
import maya.mel as mel

idx = mel.eval('getNextFreeMultiIndex "' + theSet + '.usedBy" 0')
cmds.connectAttr(myNode + '.message', theSet + '.usedBy[' + str(idx) + ']')

Did I miss a Python command that can handle this?

 

Thanks!

0 Likes
Accepted solutions (2)
3,941 Views
2 Replies
Replies (2)
Message 2 of 3

cheng_xi_li
Autodesk Support
Autodesk Support
Accepted solution

Hi,

 

I don't think there is a Python command available for it directly. Calling MEL in C++/Python is quite normal, it is designed to have some commands could only be called with MEL. maya.cmds is basically MEL commands and classes like MFileIO is a wrapper for file command with MGlobal::executeCommand.  

 

Yours,

Li

0 Likes
Message 3 of 3

kevin.picott
Alumni
Alumni
Accepted solution

To add more information to this, maya.cmds is actually a Python binding to C++ code. Utility scripts, such as getNextFreeMultiIndex, are written in Mel and use some C++ commands to do their operation. Here's the source (which you can find in your install directory):

 

 

global proc int getNextFreeMultiIndex( string $attr, int $start )
{
	// We find the next unconnected multi index starting at
	// the passed in index.
	int $i;
	// assume a max of 10 million connections
	for( $i = $start; $i < 10000000; $i++ ){
		string $con = `connectionInfo -sfd ($attr + "["+$i+"]")`;
		if( size( $con ) == 0){
			return( $i );
		}
	}
	return(0);
}

As you can see there's nothing magically MEL about it, the implementer just chose to use MEL over Python for any number of reasons - they were familiar with the language, they copied from an existing script, Python hadn't yet been integrated into Maya.

 

 

Personally I prefer Python as it's more of a standard language and it is far more powerful. The above script could be rewritten in Python as:

 

def get_next_free_multi_index( attr_name, start_index ):
	'''Find the next unconnected multi index starting at the passed in index.'''
	# assume a max of 10 million connections
	while start_index < 10000000:
		if len( cmds.connectionInfo( '{}[{}]'.format(attr_name,start_index), sfd=True ) or [] ) == 0:
			return start_index
		start_index += 1

	# No connections means the first index is available
	return 0


Kevin "Father of the DG" Picott

Senior Principal Engineer