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 tutorials

How to read an XSIADDON file in Python

Ever wondered what’s the deal with *.xsiaddon files? What are they?

Well, they’re good ol’ XML with a bit of metadata and the files embedded in them are compressed with the zlib library and base64 encoded for safe plaintext storage.

How would we parse such a file? Check this out:

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

URL QR Code Bookmarklet: Copy a URL to your phone without browser extensions

First things first, you need a QR code scanner app for your smartphone, be it an iPhone, Android, Blackberry or Nokia.

Then highlight the bit of code below and drag it to your bookmarks bar on your browser.

javascript:location.href='http://chart.apis.google.com/chart?chs=300x300&cht=qr&chl='+encodeURIComponent(location.href);

Next time you want to open a page you’re browsing in your phone, press it, a QR code shows up, scan it and the url will be on your phone.

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…