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? :)
Categories
scripting softimage

Python: Naming Objects for Numerophobes 101

You can call me a numerophobe when naming rig objects, but the reason I personally avoid numbers in my rigging naming conventions (99% of the time) is so that when I duplicate something, a number at the end won’t increase by itself because there isn’t one. (The issue can be circumvented by having numbers somewhere before the end, but I’m the kind of weirdo that prefers to go with letters altogether.)

We humans work numbers in what’s known as the “Numeral system“, also known as the “base 10” system (as we have that many fingers…) but hold on… the alphabet has 26 letters, not 10, so what do we call that system? It has a name: it’s the Hexavigesimal (or “base 26”) system.

As the ever-so-handy Wikipedia denotes,

Any number may be converted to base-26 by repeatedly dividing the number by 26.

Pretty easy stuff for Python.

This is how I’d do it:

Categories
scripting softimage

Python: Distance between 2 Position Vectors

This post surged from a question at a forum where somebody tried to use the ctr_dist() function — that returns distance between two object centers — in a Softimage script only to realise that actually it only exists for expressions, not scripting.

Here’s my take on said function for both Softimage and Maya…

Categories
tutorials

Benchmarking your Python experiments

Which is the fastest way to do this or that? Why not try them all and benchmark! 🙂

All you need to do is lock at the time.clock()