Monday, March 23, 2009

Standard Python Console inside Blender

Blender did not provide an interactive shell by default in the past. This changed a bit with the Interactive Python Console of Campbell Barton (aka ideasman42), which is accessible from the script window (Scripts > System > Interactive Python Console). Unfortunately this console does not support copy&paste, word wrapping, prompts, ...

I wrote a quick script in case you want to interact with Blender from a real Python console. Save it in your Blender home scripts folder (~/.Blender/scripts) as "terminal.py".
#!BPY

"""
Name: 'Terminal - Interactive Python Console in terminal'
Blender: 248
Group: 'System'
Tooltip: 'Interactive Python Console in terminal'
"""

__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 Python console will
appear in the terminal. The namespace will be persistent
between console sessions within one Blender session.

Press Ctrl-D to quit.
"""

# -*- 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/

import code, sys
import Blender, bpy

class InteractiveConsole(code.InteractiveConsole):
def interact(self, banner=None):
try:
sys.ps1
except AttributeError:
sys.ps1 = ">>> "
try:
sys.ps2
except AttributeError:
sys.ps2 = "... "
cprt = 'Type "help", "copyright", "credits" or "license"'+\
' for more information.'
if banner is None:
self.write("Python %s on %s\n%s\n(%s)\n" %
(sys.version, sys.platform, cprt,
self.__class__.__name__))
else:
self.write("%s\n" % str(banner))
self.write('Press Ctrl-D to quit.\n')
more = 0
while 1:
try:
if more:
prompt = sys.ps2
else:
prompt = sys.ps1
try:
line = self.raw_input(prompt)
except EOFError:
self.write("\n")
break
else:
more = self.push(line)
except KeyboardInterrupt:
self.write("\nKeyboardInterrupt\n")
self.resetbuffer()
more = 0
Blender.Redraw()
Blender.Registry.SetKey('terminal.locals', self.locals)

locals = Blender.Registry.GetKey('terminal.locals')
if not locals:
locals = {'Blender':Blender, 'bpy':bpy}
console = InteractiveConsole(locals=locals)
console.interact()
To use it, you need to start Blender from a terminal (with compiz turned off):
$ blender -w
This is very important, otherwise Blender will be blocked waiting for input at an invisible terminal. The terminal console can be started from the script window: Scripts > System > Terminal. The modules 'Blender' and 'bpy' are preloaded into the console, so you don't need to import them manually. 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:
>>> cube = bpy.data.objects['Cube']
>>> cube.LocX = -1
You can quit the interactive console by pressing Ctrl-D. You can restart the terminal as many times as you wish and automagically all your local variables will be kept (such as 'cube' in the example).

The disadvantage is that the Blender window itself becomes unresponsive. For example you can not rotate the view during a Python console session. Also it would be much nicer to use IPython instead of the standard Python shell. However in a future blog post I'll have good news, if you want IPython integration inside Blender.

1 comment:

  1. Nice! It would be possible to use IPython? I'm addicted to autocompletion.

    ReplyDelete

Filter by topic: spe, python, ubuntu