[IPython-dev] Porting IPython to Blender

Laurent Dufrechou laurent.dufrechou@gmail....
Fri Apr 3 10:02:24 CDT 2009


Hi Stani,

Really interesting. For your issue can I propose a different approach?

Have you taken a look at IPython/frontend or IPython/Gui/wx directory?
There is various implementation of interface with wx / cocoa for example and
I think you will find some interesting things in it. :)

Perhpas I'm out of subjetc but,if I well understood you need control over
in/out of ipython because your terminal is not fully compatible with
clasical ipython output.
So perhaps it will be a good idea to use for example prefilterfrontend.py or
(a code that i bettter undersatnd :)) ipshell_nonblocking.py

Just to give you a global idea:

For example in my case, ipshell_nonblocking.py instanciate ipython0 shell
and I feed it via

self.IP.doExecute(lines_to_execute.encode(ENCODING))

completion is done like this:

completed, possibilities = self.IP.complete(self.text_ctrl.getCurrentLine())


output is redirected to cout class parameter:

        self.IP =
WxNonBlockingIPShell(self,                                     <- this is
aspecialized WX shell derived from nonblocking one
                                    cout = self.cout, cerr = self.cout,
                                    ask_exit_handler = self.askExitCallback)

        self.text_ctrl = WxConsoleView(self,
                                       self.IP.getPrompt(),
                                       intro=welcome_text,
                                       background_color=background_color)

        self.cout.write = self.text_ctrl.asyncWrite

You can take a look at the code inside these function to see of they works.

I think prefilterfrontend.py will be the starting point you need. (in
/fontend/)
It is a newer version that will try in the future to follow ipython1
impelmetation.
Gael has written it so he is more aware about this.

Using one of these class you'll be able to feed a ipython instance with your
input and get output result more easily.
You will not have to go inside the ipython internals becuase they manage
this for you.
Thus you'll have full control over your terminal.(if you've got access to
your terminal display functions, if not all my text above is unusable :) )

And you'll be able not to use temporary files ;)

If you still want to use IPShellEmbeded perhaps __init__ of
ipshell_nonblocking.py
can answer some of your question as I felt in almost the same issue..
Feel free to ask more queston :)

Cheers,
Laurent


2009/4/3 Stani <spe.stani.be@gmail.com>

> 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<http://bazaar.launchpad.net/%7Estani/ar/trunk/annotate/head%%0A3A/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
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-dev/attachments/20090403/71b2d615/attachment.html 


More information about the IPython-dev mailing list