[IPython-dev] Porting IPython to Blender

Stani spe.stani.be at gmail.com
Fri Apr 3 09:59:56 EDT 2009


Hi All,

As a decent python shell is missing in Blender, I looked for ways to
interact with IPython and Blender. One way is straightforward: just run
in the terminal from which Blender is started. I've blogged about it
here:
http://pythonide.blogspot.com/2009/03/resumable-ipython-inside-blender_31.html

However in this case IPython takes over the event loop and the Blender
user interface blocked.

Therefore I tried another approach to run IPython inside a Blender text
window. It took me a bit of digging in the IPython source code, but I
finally manged to create a 'IPShellBlender'. Apart from one
inconvenience (position of mouse cursor, nothing related to IPython, but
purely to Blender), this works really nice. It's fun to rotate or
manipulate 3d objects from the shell. Not everything of IPython is
supported, as I haven't port readline to the Blender text window.
However most important features are implemented such as (auto
completion).

I've documented everything with Sphinx. Here is a tutorial how to enable
IPython inside any Blender drawing (you need to install my ar package
first):
http://sd-2469.dedibox.fr/ar/tutorials/ipython.html
http://sd-2469.dedibox.fr/ar/pages/installation.html
http://ar.stani.be

These are the code documentation:
http://sd-2469.dedibox.fr/ar/modules/ar.blender.bipython.html
http://sd-2469.dedibox.fr/ar/modules/ar.blender.bipython.private.html

The actual code can be found in the bipython module (= blender+ipython):
http://bazaar.launchpad.net/~stani/ar/trunk/annotate/head%
3A/ar/blender/bipython.py

At the moment Blender is working on a major rewrite for Blender 2.5.
We'd like to include IPython as a default shell for IPython. The only
problem is that Blender 2.5 will use python 3.0, which is not yet
supported AFAIK for IPython. On the other hand I've only used some core
modules, so maybe they might be available for python 3.0 sooner before
all the extensions (twister, wx, qt, ...)

As you might be interested, I'll give some feedback on issues I run in
to. However I run through the IPython code quite quickly to study it, so
I might have done some false presumptions.

The main modules I used were:
from IPython.Shell import IPShellEmbed
from IPython import ipapi

1) I was happy to find a complete method in the IPShellEmbed, however it
turned out to be useless. The IP.complete method should have an
(optional) line_buffer argument. Without this many completers won’t work
such as the module completer. I think my complete function
complete(self, text, line_buffer) on line 624 would be a good proposal
for an IPython patch.

2) Output redirection now happens by duplicating stdout and stderr to
temporary files. This is not ideal. Better would be if there was a way
to hook into the IPython Term instance (Term.cout, Term.cerr, ...)

3) I had to fool IPython in order to force it not to use the readline
module os.environ['TERM']='dumb'

4)+5) I also needed to be able to clear and rewrite the text windows,
because of two reasons:
- text windows in Blender are a bit primitive so the text can't scroll
with the cursor. Therefore I needed to be able to display IPython in
reverse (last statement comes first)
- uses might accidently damage the ipython shell
These are shortcomings in the Blender Python API, which will be
addressed in a future version. 

4) Therefore I needed to be able to reconstruct the IPython shell at any
time. Here I faced the difficulty that the prompts can only be generated
for the current statement. A quick hack was to temporarily overwrite the
prompt_count int the output cache, see the IPShellBlender.get_prompt()
method and I needed these core modules to reconstruct prompts:
from IPython.Prompts import str_safe 
from IPython.Itpl import ItplNS

5) The Out list caches the history of the output values. In order to
redraw itself, the stdout string output is needed. Therefore the
IPBlenderShell caches this. If IPython stores this somewhere, I’d like
to know.

I would welcome any feedback on the above four points or on my approach
in general. I'm sure that more experienced IPython developers can hint
many improvements.

IPython is really the best shell out there, so keep up the great work!

Stani




More information about the IPython-dev mailing list