Python: Using Plugin.UserData to pass data to PPG callbacks

UPDATE: Please see the comment from Patrick for a tip. Thanks!

You can use Plugin.UserData to pass data into a plugin.

For example, from outside the plugin, you can store a list in the UserData:

p = Application.Plugins("MyTestPropertyPlugin")
p.UserData = ['a', 'b', 'mpilgrim', 'z', 'example']

In the plugin callbacks, you would access the UserData like this:

def MyTestProperty_Test_OnClicked( ):

    p = Application.Plugins("MyTestPropertyPlugin")

    if p.UserData == None:
        Application.LogMessage( "UserData is empty" )
        Application.LogMessage( p.UserData )
        Application.LogMessage( p.UserData[2] )

This will work with lists, but not with dictionaries.
Softimage cannot convert Python dictionaries into a COM Variant type. If you try to store a dict in User Data:

x = Application.Plugins("MyTestPropertyPlugin")
x.UserData = {"author":"blairs", "name":"testplugin"}

You’ll get this error:

# TypeError: Objects of type 'dict' can not be converted to a COM VARIANT

I’m afraid there’s no way around that error for Python dictionaries.
This also prevents you from saving dictionaries with SetGlobal or SetGlobalObject.

3 thoughts on “Python: Using Plugin.UserData to pass data to PPG callbacks

  1. To get around the dict limitation a sane way to find your way around your code and have things _look like_ a dict is to use constants to refer to indices:

    UD_LENGTH = 0
    UD_WIDTH = 1
    UD_NAME = 2

    userData = [None] * 3
    userData[UD_LENGTH] = 5.0
    userData[UD_WIDTH] = 4.365
    userData[UD_NAME] = ‘test’

    plugin.UserData = userData

    assert plugin.UserData[UD_NAME] == ‘test’, ‘WTF?’

  2. Here the example in Softimage SDK Documentation

    # How to access a plug-in user data
    from win32com.client.dynamic import Dispatch

    # Optional global variable to speed up data access
    g_userdata = None

    def GetUserData():
    if ( g_userdata == None ):
    # Dictionary not defined yet
    # Get the plug-in object storing the dictionary
    myPlugin = Application.Plugins(“My plugin”);
    if ( myPlugin.UserData == None ):
    # Dictionary not defined yet, create it and add some object
    dict = Dispatch( “Scripting.Dictionary” )
    dict[ ‘SurfPos’ ] = XSIMath.CreateVector3()
    dict[ ‘OutPos’ ] = XSIMath.CreateVector3()

    # Store the new dictionary in the plug-in
    myPlugin.UserData = dict

    # Cache the dictionary in this module for fast access
    g_userdata = myPlugin.UserData
    return g_userdata

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s