[Bf-taskforce25] python console in Blender 2.5

Stani spe.stani.be at gmail.com
Tue Mar 31 18:40:10 CEST 2009


Ideasman asked me to share my research how to embed a python console in
Blender on this list. I'll give a proposal what could be done for 2.5 at
the end but first I sum up the different methods for 2.48 (none of them
require C, all are done in pure python):

1. Running in the blender console:
The disadvantage of this approach is that the python console takes over
the event loop and the Blender UI freezes. This is a little bit
compensated by hooking in a redraw after each interact statement. The
advantage is that the python consoles are fully functional (IPython
features multi-column autocompletion, debugging, ...).

1.1 standard python console
http://pythonide.blogspot.com/2009/03/standard-python-console-inside-blender.html

1.2 ipython console
http://pythonide.blogspot.com/2009/03/resumable-ipython-inside-blender.html


2. Running inside a blender text window:
In the python world the consoles (idle, pycrust, spe, ...) run in text
controls (for example scintilla), as they support by default copy&paste,
word wrapping, scrolling, syntax highlighting, ... It would make no
sense to try to add this functionality to a blender script window with
python. It will be just very slow (see for example Ideasman python
shell) and moreover there is already a text window in Blender. The issue
which remains to be solved, is how to capture keys. It's worth to try
the demo's, as otherwise it might be a bit abstract. I have tried three
ways:

2.1 extra script panel
The mouse is placed on on extra script panel which captures the key
codes to interact with the Blender shell text window. This works the
best as the whole Blender UI stays responsive. The inconvenience to move
your mouse above a script window for typing in another text window, is
worth the effort for having a nicely working IPython console. 

2.2 with qevents
The blender event loop is hijacked by python which sends the events to
its subwindows. If the mouse is above the text window, the user can
interact with the terminal. The disadvantage is that any Blender button
(menu, buttons, ...) become irresponsive although interacting and
manipulating objects till work. This method feels inappropiate as I
can't imagine hijacking the event loop is a good idea.

2.3 with 3d view event space handler
Here the mouse needs to be placed above the 3dview window to interact
with the terminal. This would seem more appropriate as 2.1 as no extra
script window is needed and it can easily be turned on and off through
the view menu. However in the end it is totally useless as for strange
reasons space handlers can't capture TAB, SPACE, J or Q keys, which are
vital for a console.

2.4 Blender text plugins
A feature of Blender text plugins was to connect custom shortcuts to
python methods for the blender text window. However this feature seems
broken (at least here on Blender28a Ubuntu). The only way to activate
them is through the Text menu, which is not an option.

3. Running a socket server inside Blender and using a remote control
These methods I tried both with a standard python console in an external
terminal and with a wxPython pycrust shell (which features visual auto
completion, ...)

3.1 with a timer scriptlink in the game engine
This works perfectly, but unfortunately only when the game engine is
running.

3.2 with a framechanged scriptlink
This method uses the animation loop (started with Alt-A) as an event
loop. It works ok, the view can still be rotated and is refreshed, but
no interaction with Blender is possible.

3.3 with a redraw script link
This method uses a trick. The wxpython console needs to overlap with the
Blender window. Everytime an interaction statement is entered, the
wxpython console alters its position with one pixel. This triggers a
redraw event inside Blender so the socket server can do its work.

The code for 1 is on my blog. 

The code for 2 can be downloaded with bazaar: 
http://sd-2469.dedibox.fr/ar/#installation
The code is fully documented with Sphinx:
http://sd-2469.dedibox.fr/ar/modules/ar.blender.bipython.html
(I still need to add tab completion for Windows.)
Try the demo ar/blender/ipython.blend (move your mouse above the script
window!)

I didn't bother to publish the code for 3 yet, as it was more for my own
research. However I have no problem in releasing it in case someone
would be interested.

Until here Blender 2.48

For Blender 2.5 it should get more easy to implement a fully python
console. I would have liked to propose we use IPython (pure python code,
BSD license) as a console in Blender 2.5  I could make my current
IPython wrapper compatible with Blender 2.5 In this wrapper all
functionality comes from IPython, the wrapper just interfaces with
Blender, but unfortunately there is a problem. The only problem is that
AFAIK IPython does not support yet python 3. However it might when
Blender 2.5 is released. In case it doesn't we can fallback on the
standard python console, for which I can also write a wrapper and maybe
add some IPython features such as tab completion.

This is what I need in Blender 2.5 Python API exposed in order to make a
python console work (maybe it is there already):
- clear the text window
- move the text cursor to a certain line and column
- insert text at cursor position
- make a certain line visible
- replace text with another text (for example for backspace or auto
completion)
- capture any key:
  * only when the mouse is above the text window
  * especially TAB, SPACE, ENTER, ARROW keys, HOME and END
  * preferably with an ascii values for characters instead of keycodes

Optionally:
- selecting text
- a nice mechanism to redirect stdout and stderr (right now it do this
in python, but it will swallow everything)
- python syntax highlighting or custom coloring of characters

Note that a separate event loop is not required, just key capture. The
python console can just be driven by the ENTER key.

If wished, I can commit myself to do the python coding for this. This
gives you one less issue to worry about. Of course someone else needs to
expose the above api functionality from c. As I develop SPE (a python
IDE which was originally targeted at Blender), I have plenty of
experience concerning python IDE's and consoles.

Best regards,
Stani




More information about the Bf-taskforce25 mailing list