Softimage SDK Programmer's Guide > Script Development > Python Scripting >

# Working with Custom Python Modules in Softimage

Python modules are convenient for writing reusable code for Softimage plug-ins. To import a module in a Softimage plug-in, you first need to tell Python where to find the module by adding the module folder path to the Python system path (sys.path). Once Python knows where to find your module file, you can import it into your plug-in.

    #---------

    import sys

    sys.path.append( 'd:\MyModules' )

    import moduleA

    --------

The example above imports moduleA defined in d:\MyModules. You can add more than one path to sys.path, but keep in mind that the order in which the paths are added determines the search order that Python uses to look for your modules.

Adding specific paths to sys.path is not always the most convenient method. This is particularly true for plug-ins that need to be installed to different locations. For instance, when your modules are located with your plug-in file, the plug-in path must be added to sys.path. In this case, you can use the variable __sipath__ which gets set by Softimage to the plug-in file folder path. This variable is always defined in the plug-in file namespace and can be used as is without going through the Softimage Application object.

    #---------

    import sys

    if __sipath__ not in sys.path:

    	sys.path.append( __sipath__ )

    import moduleA

    --------

In the example above, Python will search the plug-in folder and import moduleA. The code also ensures that __sipath__ gets added to sys.path only (this check is required because a plug-in can be loaded multiple times during a Softimage session). __sipath__ can also be used to specify any folder paths related to your plug-in folder.

    #---------

    import sys

    import os

    myModules = __sipath__ + os.sep + 'MyModules'

    if myModules not in sys.path:

    	sys.path.append( myModules )

    import moduleA

    --------

The Softimage siutils module contains functions to help you specify your module paths more easily. It also contains functions for importing source code files as modules, which can be handy if versionning has to be considered when writing plug-ins. The siutils module file is located in the Softimage application folder: <install_folder>\Application\bin\siutils.py.

The following examples demonstrates how to use the functions defined in siutils:

    #---------
    Example 1:

    # note: for the sake of the example, the plug-in's __sipath__ variable is set to d:\Application\Plugins

    # import the Softimage utility module

    import siutils

    #add some paths to python system path

    siutils.add_to_syspath( __sipath__ )

    siutils.add_subfolder_to_syspath( __sipath__, 'mysubfolder' )

    # import eggs from d:\Application\Plugins

    import eggs

    def XSILoadPlugin( in_reg ):

    	in_reg.Author = "Softimage"

    	in_reg.Name = "TestPlugin"

    	in_reg.Major = 1

    	in_reg.Minor = 0

    	in_reg.RegisterCommand( "TestCommand", "TestCommand")

    	return True

    def TestCommand_Execute(  ):

    	# INFO: d:\Application\Plugins\eggs.pyc

    	LogMessage( 'eggs.__siplugin_file__ ' + eggs.__siplugin_file__  )

    	# import foo from d:\Application\Plugins\mysubfolder

    	import foo

    	LogMessage( 'foo.__siplugin_file__ ' + foo.__siplugin_file__  )

    	return True

    Example 2:

    import siutils

    siutils.add_to_syspath( __sipath__ )

    # import foo.py from d:\Application\Plugins

    # INFO: d:\Application\Plugins\foo.pyc

    import foo

    LogMessage( foo.__siplugin_file__ )

    # import foo.py from d:\Application\Plugins\mysubfolderA

    foo = siutils.import_source( __sipath__ + os.sep + 'mysubfolderA' + os.sep + 'foo.py' )

    # INFO: d:\Application\Plugins\mysubfolderA\foo.pyc

    LogMessage( foo.__siplugin_file__ )

    # import foo.py from d:\Application\Plugins\mysubfolderB

    foo = siutils.import_source( __sipath__ + os.sep + 'mysubfolderB' + os.sep + 'foo.py' )

    # INFO: d:\Application\Plugins\mysubfolderB\foo.pyc

    LogMessage( foo.__siplugin_file__ )

    ---------