Categories
scripting softimage

XSI SDK: Picking forever with Python YIELD

What’s the smart way to pick any arbitrary number of items with XSI’s PickElement or PickObject commands? Read below… 😀

si = Application

def pickForever(**kwargs):
    '''
    Pick forever, and ever, and ever, and ever...
    until you rightclick.
    '''

    # Default options
    leftMessage = kwargs.setdefault('leftMsg', 'Pick something')
    middleMessage = kwargs.setdefault('middleMsg', leftMessage)
    selFilter = kwargs.setdefault('selFilter', 'object')

    # Loop and yield forever until complete:
    while 1:
        out = si.PickElement(selFilter, leftMessage, middleMessage)
        obj = out("PickedElement")
        buttonPressed = out("ButtonPressed")
        modifier = out("ModifierPressed")

        if obj:
            yield obj, buttonPressed, modifier
        else:
            break


# _______________________________________
# USAGE EXAMPLES:

def processIndividually():
    '''
    Deal with each pick, one object at a time...
    '''
    for obj, button, modifier in pickForever(leftMsg='Pick Object', selFilter='polygonmesh'):
        button = ['left','middle'][button-1]
        # (-1 because 0 means right, but is never returned.)
        si.LogMessage( "Picked %s with your %s mouse button!" % (obj.FullName, button) )


def listExample():
    '''
    Getting a list of picked objects, only after picking is complete...
    '''
    pickedObjects = zip(*list( pickForever(leftMsg='Pick Object') ))[0]
    si.LogMessage( pickedObjects )


# Cool, eh? :)

How or why does this work? What the heck is yield doing?

In Python, you can think of a “yield” statement like a “return” one, except it doesn’t finish the function evaluation.

A function using yield will act as a “generator“, returning one thing at a time each time it is called, until it stops yielding. (In our snippet, returning one tuple containing three pieces of data.) When iterating/looping in Python, when nothing is returned it stops, therefore when a yielding function stops returning, the loop ends.

Hat tip to Xavier Lapointe for originally showing me the trick and to Patrick Boucher for helping me remember the yield logic.