Python: importing modules into plugins

New in the Softimage 2011 Subscription Advantage Pack

The siutils Python module makes it easier to import modules into your self-installing plugins. Just put your modules in the same location as your plugin file , and you can use the __sipath__ variable to specify the module location.

__sipath__ is always defined in the plugin namespace, so no matter where you put a plugin, you can simply use __sipath__ to specify the location.

Here’s a simple example that shows how to import a module into your plugin.

  • Line 04: Import the siutils module
  • Line 39: Use add_to_syspath() to add __sipath__ to the Python sys path.
    If the module was located in a subfolder of the plugin location, you could use siutils.add_subfolder_to_syspath( __sipath__, ‘mysubfolder’ )

  • Line 40: Import the module
import win32com.client
from win32com.client import constants

import siutils

null = None
false = 0
true = 1

def XSILoadPlugin( in_reg ):
	in_reg.Author = "blairs"
	in_reg.Name = "TestPlugin"
	in_reg.Major = 1
	in_reg.Minor = 0

	#RegistrationInsertionPoint - do not remove this line

	return true

def XSIUnloadPlugin( in_reg ):
	strPluginName = in_reg.Name
	Application.LogMessage(str(strPluginName) + str(" has been unloaded."),constants.siVerbose)
	return true

def Test_Init( in_ctxt ):
	oCmd = in_ctxt.Source
	oCmd.Description = ""
	oCmd.ReturnValue = true

	return true

def Test_Execute(  ):

	Application.LogMessage("Test_Execute called",constants.siVerbose)

#	print __sipath__ 

	siutils.add_to_syspath( __sipath__ )
	import TestModule
	TestModule.test( "hello world" )

	return true