Tuesday, March 31, 2009

Resumable IPython inside Blender Terminal

Would it not be nice if IPython could run inside the Blender terminal in such way that it will remember the namespace history in between sessions? Why IPython? It gives you auto completion, deep reloads, object introspection, input history, access to your operating system commands with python variables, auto indent, ... In case you are not convinced, read this tutorial, reference or watch these screencasts on showmedo.

Most Linux distributions ship IPython in their repositories. For example installing on Ubuntu is as easy as:

$ sudo apt-get install ipython

Getting IPython on Windows or Mac is simple if you have setuptools installed:

$ easy_install IPython

Windows users should install pyreadline as well for autocompletion. Check first in a terminal if IPython is correctly installed:

__author__ = "Stani (SPE Python IDE)"__url__ = ["pythonide.stani.be", "www.blender.org", "www.python.org"]__bpydoc__ = """\This only works if you started Blender from a terminal.Otherwise Blender will freeze. The IPython console willappear in the terminal. The namespace will be persistentbetween console sessions within one Blender session.

Press Ctrl-D to pause the IPython session and return to Blender."""

# -*- coding: UTF-8 -*-

# ar - ARchitecture library# Copyright (C) 2009 www.stani.be## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see http://www.gnu.org/licenses/

Make sure you start Blender from a terminal and not from the start menu, as IPython will run in the terminal. You can start the IPython terminal from the Script window in Blender. (Scripts > System > Ipython (Terminal) ) Just like in my previous blog post, each time a Python statement is entered, the Blender window is updated. So you can move a cube and see the result, if you type:

In [1]: cube = bpy.data.objects['Cube']

In [2]: cube.LocX = -1

When you want to return to Blender, press Ctrl-D. If you restart later the IPython terminal, it will remember any commands you have typed or any variables you have declared (like cube in the example).

IPython has a different prompt which makes it easy to go back to previous input statements or output values:

In [1]: a=1

In [2]: print a1

In [3]: aOut[3]: 1

In [4]: exec _i2 # execute second command1

In [5]: exec In[2:4] # execute multiple previous commands1

In [6]: b=_3 # third ouput value

In [7]: a==bOut[7]: True

If you prefer a standard python prompt (>>>), just enter '%doctest_mode':

In [1]: %doctest_mode*** Pasting of code with ">>>" or "..." has been enabled.Exception reporting mode: PlainDoctest mode is: ON

As shown in the last example auto completion works even for import statements.

Altough the Blender window updates itself, the Blender user interface is still irresponsive when running an IPython session. In a future blog post I'll show how to run IPython inside the Blender window in such way that the user interface stays fully responsive.