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".
To use it, you need to start Blender from a terminal (with compiz turned off):#!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()
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:$ blender -w
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).>>> cube = bpy.data.objects['Cube']
>>> cube.LocX = -1
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.
Nice! It would be possible to use IPython? I'm addicted to autocompletion.
ReplyDelete