Home Coding, Scripting, Shaders

Maya - Select all instance objects in a scene.

sculptn
polycounter lvl 7
Offline / Send Message
sculptn polycounter lvl 7
Just wondering how to - Select all instanced objects in a scene.

Thanks 

Replies

  • sculptn
    Offline / Send Message
    sculptn polycounter lvl 7
    I tried this seemed like it could give you a list of all the instances but could not get it to work. 

    http://lesterbanks.com/2010/06/uninstance-all-instances-in-maya/
  • pasha_sevez
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    This will select all instances of selected object

    string $sel[] = `ls -sl`;
    string $shapes[] = `listRelatives -s $sel[0]`;
    select `listRelatives -ap $shapes[0]`;
  • pasha_sevez
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    sculptn said:
    I tried this seemed like it could give you a list of all the instances but could not get it to work. 

    http://lesterbanks.com/2010/06/uninstance-all-instances-in-maya/
    This page shows code with broken formatting. This code is pretty neat and works well too. To use it - run this corrected Python code:

    # Python code
    import maya.cmds as mc
    import maya.OpenMaya as om

    def getInstances():
        instances = []
        iterDag = om.MItDag(om.MItDag.kBreadthFirst)
        while not iterDag.isDone():
            instanced = om.MItDag.isInstanced(iterDag)
            if instanced:
                instances.append(iterDag.fullPathName())
            iterDag.next()
        return instances

    def uninstance():
        instances = getInstances()
        while len(instances):
            parent = mc.listRelatives(instances[0], parent=True)[0]
            mc.duplicate(parent, renameChildren=True)
            mc.delete(parent)
            instances = getInstances()

    uninstance()
  • sculptn
    Offline / Send Message
    sculptn polycounter lvl 7
    Thanks for fixing that pasha_sevez :) That script seems to Uninstance all the instances in a scene very handy but I what i want to do select all instances in a scene or get a list of them. If that makes sense ? Not sure if that's really possible though.
  • Axi5
    Offline / Send Message
    Axi5 interpolator
    Just call mc.select(getInstances()) instead, you don't need the uninstance function. Like so:

    import maya.cmds as mc
    import maya.OpenMaya as om
    
    def getInstances():
        instances = []
        iterDag = om.MItDag(om.MItDag.kBreadthFirst)
        while not iterDag.isDone():
            instanced = om.MItDag.isInstanced(iterDag)
            if instanced:
                instances.append(iterDag.fullPathName())
            iterDag.next()
        return instances
        
        
    mc.select(getInstances())

  • pasha_sevez
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    Axi5 said:
    Just call mc.select(getInstances()) instead, you don't need the uninstance function. 
    Yup, forgot to mention ) Just fixed the code from the link above )) Btw, really handy code, saved it for myself too. It finds ALL instances and tries to make them copies instead. 

    sculptn said:
    Thanks for fixing that pasha_sevez :) That script seems to Uninstance all the instances in a scene very handy but I what i want to do select all instances in a scene or get a list of them. If that makes sense ? Not sure if that's really possible though.
    As @Axi5 said, just use getInstances() - but adjust the usage a bit:

    list( set( mc.listRelatives( getInstances(), ap=1 ) ) )

    That's because getInstances() returns you a list of shapes, not transforms.
  • pasha_sevez
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    I've tweaked the procedure to print transforms directly, here it is:

    import maya.api.OpenMaya as om
    def getInstances():
    	instances = []
    	iterDag = om.MItDag(om.MItDag.kBreadthFirst)
    	while not iterDag.isDone():
    		instanced = om.MItDag.isInstanced(iterDag)
    		if instanced:
    			transformFn = om.MFnTransform()
    			transformFn.setObject( iterDag.getPath().transform() )
    			instances.append( transformFn.fullPathName() )
    		iterDag.next()
    	return instances
    
    print getInstances()

  • sculptn
    Offline / Send Message
    sculptn polycounter lvl 7
    That's great guys thanks for the help :) pasha_sevez, Axi5

  • morrisolmsted
    Offline / Send Message
    morrisolmsted polycounter lvl 5
    1. import maya.api.OpenMaya as om
    2. def getInstances():
    3. instances = []
    4. iterDag = om.MItDag(om.MItDag.kBreadthFirst)
    5. while not iterDag.isDone():
    6. instanced = om.MItDag.isInstanced(iterDag)
    7. if instanced:
    8. transformFn = om.MFnTransform()
    9. transformFn.setObject( iterDag.getPath().transform() )
    10. instances.append( transformFn.fullPathName() )
    11. iterDag.next()
    12. return instances
    13.  
    14. list( set( mc.listRelatives( getInstances(), ap=1 ) ) )

    When I use this, I only get this error...<bound method Instance_Tools.getInstances of <instanceTools.Instance_Tools object at 0x000002279E3FE888>>


    any idea why?

Sign In or Register to comment.