How to query any constraint's target list without knowing the constraint type?

Soviut picture Soviut · Apr 22, 2009 · Viewed 8.1k times · Source

In Maya, I have a list of constraints gathered by the following code. I want to iterate the constraints and query the targets for each of them:

cons = ls(type='constraint')
for con in cons:
    targets = constraint(query=True, targetList=True)

The problem, there is no general constraint command for manipulating all constraints. Instead, each constraint has its own unique MEL command associated with it.

Is there any way to query the targets on a constraint without having to type check each constraint and tediously run its respective MEL command?

Answer

user94546 picture user94546 · Apr 22, 2009

listConnections on the .target attr

the cleanup in mel:

string $cons[] = `ls -type "constraint"`;
for ( $con in $cons ){
    string $targetAttrString = ( $con+ ".target" );
    string $connections[] = `listConnections $targetAttrString`;
    string $connectionsFlattened[] = stringArrayRemoveDuplicates($connections);
    for ( $f in $connectionsFlattened )
        if ( $f != $con )
            print ( $f+ " is a target\n" );
}