Part 1 – Modules

Technically any python file (a file with the “.py” extension) can be imported as a module.

The PyQt4.QtGui we were using earlier was a module and so is the win32com.client we have in our plugins. Anything you can “import” can technically be called a module.

Why make a module?

Most modules group a set of similar functions. This makes those functions a lot easier to find since they’re all grouped in the specific module but it also keeps your code neater since the various functions are sorted into various modules. Modular programming does not nescessarily refer to creating as many modules as possible but it does mean grouping things that can be and turning code that is used more than once into a function rather than just typing out the same set of commands a few times. And modular programming is definately something to aspire to, for the same reasons that modules are created. It just makes for more efficient and managable programs.

e.g: the python “os” module contains varous functions that deal with the current opperating system. Like the command os.makedirs which makes a specified folder and os.listdir which lists all the files and folders within a specified folder etc.

How to make a module?

Making modules is as simple as saving a python file, literally. So lets create out own module called “test.py” and put in a function that prints test:

def Print_ItemsInList(list):
    for item in list:
        print item

Now save that as “Test.py”

Importing a custom module:

We have four ways to do this.

1. The easiest is to put the module we want to import into the same folder as the script we’re running and then just import the module’s file name.

Note this doesn’t work if you’re using Xsi’s Script Editor though but you can try it with IDLE which is the IDE (Script Editor) that comes with python. I prefer using Notepad++ when I develope outside of Xsi and there’s this awesome post here on how to get Notepad++ to run python.

So in IDLE, Notepad++ or whatever IDE you decide to use (except the Xsi Script Editor), type the following code:

import Test
my_list = [1, 2, 3, 4, 5, 6]
Test.Print_ItemsInList(my_list)

Then save that in the same folder as the “Test.py” module you created and run it.

You should see something like this outputted:

1
2
3
4
5
6

The above method is the ONLY method that DOESN’T work with Xsi. All the following methods do.

 

2. Adding the module’s path to the system path (its a lot easier than it probably sounds :p )

In python there is a list stored in the sys module that contains all of the folders to look in that have modules to load. All we need to do is add our module’s folder path to the sys.path and we’ll be able to import it.

Note: This method DOES work for Xsi.

In my case mt Test.pt module is saved in “C:\Users\jared.glass\Autodesk\Softimage_2011_Subscription_Advantage_Pack_SP1\Data\Scripts” but note that with python, a “\” needs to be doubled to “\\”. This only happens with the backslash because it can act as an escape character. You don’t need to worry about that too much just remember that with python to double up your backslashes.

At the top of your script you run the code:

import sys
sys.path.append(“C:\\Users\\jared.glass\\Autodesk\\Softimage_2011_Subscription_Advantage_Pack_SP1\\Data\\Scripts”)

import Test

And the output should be the same as before.

 

You can also use python’s imp module. It allows you to import a module directly into a variable and can be safer than just adding a module to your python path if you have many modules in different locations, but that have the same name.

import imp

Test  = imp.load_source(‘Test’, “C:\\Users\\jared.glass\\Autodesk\\Softimage_2011_Subscription_Advantage_Pack_SP1\\Data\\Scripts”)

etc

 

3. Python’s site-packages folder.

When you install a python module (like PyQt) the parts of that module that python uses get placed in here. On my system it’s C:\Python26\Lib\site-packages.

For this method all you need to do is copy your module into that folder and you’ll be able to import it.:

import Test

etc

The reason I mentioned this method last though is that since you are placing your module in a local folder, if you create a plugin or tool that people use from other computers, they will have to copy the file too. So I reccomend you rather place the modules you want to import in a folder on your server and use the imp or sys.path methods so that no matter which computer your plugin is being run from, it will be able to see and use your module.

2 thoughts on “Part 1 – Modules

  1. Pingback: Start Here!!! « Conquering the Code in Softimage Xsi

  2. Pingback: Start Here!!! | Conquering the Code in Softimage Xsi

Leave a comment