[Bf-python] Integrating IPython with blender

Campbell Barton ideasman42 at gmail.com
Mon Sep 16 02:16:17 CEST 2013


On Mon, Sep 16, 2013 at 6:20 AM, HartsAntler <goatman.py at gmail.com> wrote:
> Gabriel,
> You could try compiling blender as bpy.so, blender as a dynamic library that
> you can import into normal Python.  Rather than try to make IPython work
> inside of the Python embedded in Blender.
>
>
> On Sun, Sep 15, 2013 at 12:01 PM, Gabriel Elkind <gwelkind at wesleyan.edu>
> wrote:
>>
>> Hello!
>>
>> I am still getting familiar with blender's backend. In all other coding I
>> do with python, I use IPython, which allows me to log all input and output,
>> issue bash commands quickly and simply, query docstrings and sourcecode of
>> functions within the interpreter and use a robust tab completion. I have
>> been searching for a way to integrate blender and ipython for about a week
>> now and running into many roadblocks. I hope it's alright that I am emailing
>> you all about it, as (from my queries to forums) this seems like a feature
>> that many people want, and I believe that it could catalyze 3rd party
>> blender development in a big way by making coding with blender much easier
>> to learn.
>>
>> I understand that an identical venture has found success in 2009 in
>> integrating ipython with blender, however this solution no longer works.
>> Perhaps there is a way of tweaking it to work, but I'm afraid I do not
>> understand what is happening well enough to know where to begin. I have
>> tried a couple other approaches:
>>
>> First, I attempted to use the internal blender console to embed IPython. I
>> append my standard python3 sys.path to the internal blender sys.path, then I
>> do:
>>
>> import IPython
>> IPython.embed()
>>
>> and I get the following traceback:
>> >>> npath =  ['',
>> ...  '/usr/bin',
>> ...  '/usr/lib/python3.3',
>> ...  '/usr/lib/python3.3/plat-x86_64-linux-gnu',
>> ...  '/usr/lib/python3.3/lib-dynload',
>> ...  '/usr/local/lib/python3.3/dist-packages',
>> ...  '/usr/lib/python3/dist-packages',
>> ...  '/usr/lib/python3/dist-packages/IPython/extensions']
>> >>> import sys
>> >>> for p in npath:
>> ...     sys.path.append(p)
>> ...
>> >>> import IPython
>> >>> IPython.embed()
>> Traceback (most recent call last):
>>   File "<blender_console>", line 1, in <module>
>>   File
>> "/usr/lib/python3/dist-packages/IPython/frontend/terminal/embed.py", line
>> 282, in embed
>>     _embedded_shell = InteractiveShellEmbed(**kwargs)
>>   File
>> "/usr/lib/python3/dist-packages/IPython/frontend/terminal/embed.py", line
>> 97, in __init__
>>     display_banner=display_banner
>>   File
>> "/usr/lib/python3/dist-packages/IPython/frontend/terminal/interactiveshell.py",
>> line 360, in __init__
>>     user_module=user_module, custom_exceptions=custom_exceptions
>>   File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py",
>> line 455, in __init__
>>     self.init_readline()
>>   File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py",
>> line 1844, in init_readline
>>     self.refill_readline_hist()
>>   File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py",
>> line 1853, in refill_readline_hist
>>     stdin_encoding = sys.stdin.encoding or "utf-8"
>> AttributeError: 'NoneType' object has no attribute 'encoding'
>>
>> I have tried to use the -d option which (according to the blender man
>> pages) should ensure that sys.stdin is not set to None, however, in both
>> 2.66 and 2.68a sys.stdin evaluates to None even with this option set.
>>
>> My other route of action has been to use the terminal I am running blender
>> from as the interpreter using `blender --python-console`. Unfortunately,
>> this option results in a blank window and when I attempt to embed IPython I
>> get the following traceback:
>>
>> >>> import IPython
>> >>> IPython.embed()
>>
>> ---------------------------------------------------------------------------
>> KeyError                                  Traceback (most recent call
>> last)
>> /usr/lib/python3.3/code.py in runcode(self, code)
>>      88         """
>>      89         try:
>> ---> 90             exec(code, self.locals)
>>      91         except SystemExit:
>>      92             raise
>>
>> <console> in <module>()
>>
>> /usr/lib/python3/dist-packages/IPython/frontend/terminal/embed.py in
>> embed(**kwargs)
>>     281     if _embedded_shell is None:
>>     282         _embedded_shell = InteractiveShellEmbed(**kwargs)
>> --> 283     _embedded_shell(header=header, stack_depth=2)
>>
>> /usr/lib/python3/dist-packages/IPython/frontend/terminal/embed.py in
>> __call__(self, header, local_ns, module, dummy, stack_depth, global_ns)
>>     155         # Call the embedding code with a stack depth of 1 so it
>> can skip over
>>     156         # our call and get the original caller's namespaces.
>> --> 157         self.mainloop(local_ns, module, stack_depth=stack_depth,
>> global_ns=global_ns)
>>     158
>>     159         self.banner2 = self.old_banner2
>>
>> /usr/lib/python3/dist-packages/IPython/frontend/terminal/embed.py in
>> mainloop(self, local_ns, module, stack_depth, display_banner, global_ns)
>>     202             if module is None:
>>     203                 global_ns = call_frame.f_globals
>> --> 204                 module = sys.modules[global_ns['__name__']]
>>     205
>>     206         # Save original namespace and module so we can restore
>> them after
>>
>> KeyError: '__console__'
>>
>> I am using Linux Mint 15, IPython 3.3.1, blender 2.66 and 2.68a (getting
>> the same results in all cases with both versions of blender).
>>
>> Thank you very much :)
>>
>> ~Gabriel

Don't try embed ipython in an existing python REPL.

You can save "import IPython; IPython.embed()" into a file and run
blender --python test.py

Or run those 2 lines from blenders text editor.



More information about the Bf-python mailing list