Difference between revisions of "Python in Modo"

From The Foundry MODO SDK wiki
Jump to: navigation, search
m (Packages in Modo's Python)
(Modo's Python Interpreter Version)
Line 41: Line 41:
| 901 || 2.7.3
| 901 || 2.7.3
|10.0 || 2.7.3
== Modo's PYTHONPATH ==  
== Modo's PYTHONPATH ==  

Latest revision as of 13:07, 16 May 2016


This guide intends to introduce the Modo users to the new Python API introduced in Modo 701.

It does assume that you have a minimum knowledge of Python and Modo GUI and Tools.
Even when it contains small introductions to them when is considered necessary.

The new Python API is composed of several modules, and allows the creation of Tools, Commands and other types of plug-ins explained a bit later.
Unlike the old Scripting API that created fire and forget scripts.This unleash new powerful ways to expand Modo.
The Python API Modules will be further explained, don't worry.

Note: You can create your own Python packages and modules to be used with the new API.


Kits were introduced in Modo 501 and are described here, have a read to that page first, and don't feel daunted by it. We will review some of those concepts here.

Kits are extremely useful to group a plug-in (or set of plug-ins), the images they need for their [GUI], etc. under a folder than the user can install very easily on their HD.


The index.cfg files for kits are written in the same way for the Python API or C++ plug-ins.

Modo's Python Interpreter Version

Modo includes an embedded Python interpreter.

This table resume the different versions of Modo and their Python interpreters:

Modo Version Python Version
601 2.6.2
701 2.6.2
801 2.7.3
901 2.7.3
10.0 2.7.3


Modo's Python interpreter resets the Python Path and points it to it's own Scripts directories.

There are several ways you can add your own modules and Python packages:


As we've seen a kit in Modo is a directory inside the user scripts directory, containing an index.cfg file and other resources like images, plug-ins, etc.. .
You can access to this directory from Modo's menu System > Open User Scripts Folder.

You can create a directory there with the name of your kit and inside that directory an index.cfg file that will contain xml code.
Really the index.cfg is a xml file with the extension changed to .cfg.

Inside that file you can add directives for your forms, plug-ins, etc..

  1. Open your User Scripts Folder from Modo's menu System > Open User Scripts Folder.
  2. Create a directory called mynewkit.
  3. Inside that directory create an index.cfg file and open it on you favourite text editor.
  4. Copy and paste this xml code on it:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Kit name and version number -->
<configuration kit="mynewkit" version="1.0">
  <!-- This auto-imports all the contents on the kits directory -->
  <!-- This imports a specific module inside of a directory of the kit -->

PYTHONPATH on a Python Module

In a module living on the user scripts directory add to the top part of your module.py file:

import sys
# Substitute [USER_NAME] for your user name
# Substitute [DIR]with the name of the directory containing your module.py

PYTHONPATH on a Python File

  1. Create a python script file that appends to sys.path and import that file at the top of your script:
import sys
# Substitute [USER_NAME] for your user name
# Substitute [DIR] with the name of the directory containing your module.py
import addtosysp

PYTHONPATH on a site-packages Directory

  1. Modo treats the user scripts directory as a site-packages folder. It even correctly processes .pth files contained there at start-up.
  2. It means is that you can install packages/modules into your system python & have them added to modo by adding a .pth file pointing at the system site-packages folder.

Note: For that your External Python version must be exactly the same one as the one used by modo internally.

Packages in Modo's Python

To create a Package in Python you create a directory and inside that directory a file called __init__.py.

1. For example in your user scripts directory create a directory called mypackage.

2. Inside that directory create a file called __init__.py with this content:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Initialization file of  mypackage.  """

3. Other modules can go in the same directory of the __init__ file.

4. To use the package in other modules you can use absolute imports:

import mypackage.mymodule
# Calling a function inside mymodule of mypackage

5. Or relative imports:

from mypackage import mymodule
# Calling a function inside mymodule of mypackage