2009-11-06 23:53:40 +00:00
|
|
|
# ##### BEGIN GPL LICENSE BLOCK #####
|
|
|
|
#
|
|
|
|
# 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 2
|
|
|
|
# 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, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-11-06 23:53:40 +00:00
|
|
|
#
|
|
|
|
# ##### END GPL LICENSE BLOCK #####
|
|
|
|
|
2011-10-23 12:17:24 +00:00
|
|
|
# <pep8-80 compliant>
|
2009-11-06 23:53:40 +00:00
|
|
|
import sys
|
|
|
|
import bpy
|
|
|
|
|
2011-10-23 12:17:24 +00:00
|
|
|
language_id = "python"
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-07-28 23:24:17 +00:00
|
|
|
# store our own __main__ module, not 100% needed
|
|
|
|
# but python expects this in some places
|
|
|
|
_BPY_MAIN_OWN = True
|
2009-12-04 17:54:48 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
|
2009-11-06 23:53:40 +00:00
|
|
|
def add_scrollback(text, text_type):
|
2011-10-23 12:17:24 +00:00
|
|
|
for l in text.split("\n"):
|
|
|
|
bpy.ops.console.scrollback_append(text=l.replace("\t", " "),
|
2012-10-08 08:28:05 +00:00
|
|
|
type=text_type)
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-12-04 17:54:48 +00:00
|
|
|
|
2011-02-27 14:08:33 +00:00
|
|
|
def replace_help(namespace):
|
2011-04-03 23:11:00 +00:00
|
|
|
def _help(*args):
|
2011-02-27 14:08:33 +00:00
|
|
|
# because of how the console works. we need our own help() pager func.
|
|
|
|
# replace the bold function because it adds crazy chars
|
|
|
|
import pydoc
|
|
|
|
pydoc.getpager = lambda: pydoc.plainpager
|
|
|
|
pydoc.Helper.getline = lambda self, prompt: None
|
|
|
|
pydoc.TextDoc.use_bold = lambda self, text: text
|
|
|
|
|
2011-04-03 23:11:00 +00:00
|
|
|
pydoc.help(*args)
|
2011-02-27 14:08:33 +00:00
|
|
|
|
|
|
|
namespace["help"] = _help
|
|
|
|
|
|
|
|
|
2009-11-06 23:53:40 +00:00
|
|
|
def get_console(console_id):
|
2012-07-03 09:02:41 +00:00
|
|
|
"""
|
2009-11-06 23:53:40 +00:00
|
|
|
helper function for console operators
|
2011-10-17 06:58:07 +00:00
|
|
|
currently each text data block gets its own
|
2010-06-10 21:31:39 +00:00
|
|
|
console - code.InteractiveConsole()
|
2009-11-06 23:53:40 +00:00
|
|
|
...which is stored in this function.
|
|
|
|
|
|
|
|
console_id can be any hashable type
|
2012-07-03 09:02:41 +00:00
|
|
|
"""
|
2009-11-06 23:53:40 +00:00
|
|
|
from code import InteractiveConsole
|
|
|
|
|
2010-05-03 16:00:42 +00:00
|
|
|
consoles = getattr(get_console, "consoles", None)
|
2010-09-02 04:53:05 +00:00
|
|
|
hash_next = hash(bpy.context.window_manager)
|
2010-05-03 16:00:42 +00:00
|
|
|
|
|
|
|
if consoles is None:
|
2009-11-06 23:53:40 +00:00
|
|
|
consoles = get_console.consoles = {}
|
2010-08-03 03:53:36 +00:00
|
|
|
get_console.consoles_namespace_hash = hash_next
|
2010-06-10 21:31:39 +00:00
|
|
|
else:
|
2011-10-17 06:58:07 +00:00
|
|
|
# check if clearing the namespace is needed to avoid a memory leak.
|
2010-06-10 21:31:39 +00:00
|
|
|
# the window manager is normally loaded with new blend files
|
|
|
|
# so this is a reasonable way to deal with namespace clearing.
|
|
|
|
# bpy.data hashing is reset by undo so cant be used.
|
|
|
|
hash_prev = getattr(get_console, "consoles_namespace_hash", 0)
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-06-10 21:31:39 +00:00
|
|
|
if hash_prev != hash_next:
|
|
|
|
get_console.consoles_namespace_hash = hash_next
|
|
|
|
consoles.clear()
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-05-03 16:00:42 +00:00
|
|
|
console_data = consoles.get(console_id)
|
|
|
|
|
|
|
|
if console_data:
|
|
|
|
console, stdout, stderr = console_data
|
|
|
|
|
2011-06-10 09:44:27 +00:00
|
|
|
# XXX, bug in python 3.1.2, 3.2 ? (worked in 3.1.1)
|
2011-10-23 12:17:24 +00:00
|
|
|
# seems there is no way to clear StringIO objects for writing, have to
|
|
|
|
# make new ones each time.
|
2010-07-29 00:44:53 +00:00
|
|
|
import io
|
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
2010-05-03 16:00:42 +00:00
|
|
|
else:
|
2010-07-28 23:24:17 +00:00
|
|
|
if _BPY_MAIN_OWN:
|
|
|
|
import types
|
|
|
|
bpy_main_mod = types.ModuleType("__main__")
|
|
|
|
namespace = bpy_main_mod.__dict__
|
|
|
|
else:
|
|
|
|
namespace = {}
|
2010-09-07 15:17:42 +00:00
|
|
|
|
2010-07-28 23:24:17 +00:00
|
|
|
namespace["__builtins__"] = sys.modules["builtins"]
|
|
|
|
namespace["bpy"] = bpy
|
2012-06-20 08:49:59 +00:00
|
|
|
|
|
|
|
# weak! - but highly convenient
|
2010-07-28 23:24:17 +00:00
|
|
|
namespace["C"] = bpy.context
|
2012-06-20 08:49:59 +00:00
|
|
|
namespace["D"] = bpy.data
|
2011-01-01 07:20:34 +00:00
|
|
|
|
2011-02-27 14:08:33 +00:00
|
|
|
replace_help(namespace)
|
|
|
|
|
2011-10-23 12:17:24 +00:00
|
|
|
console = InteractiveConsole(locals=namespace,
|
|
|
|
filename="<blender_console>")
|
2010-09-07 15:17:42 +00:00
|
|
|
|
2011-02-23 01:20:40 +00:00
|
|
|
console.push("from mathutils import *")
|
|
|
|
console.push("from math import *")
|
|
|
|
|
2010-07-28 23:24:17 +00:00
|
|
|
if _BPY_MAIN_OWN:
|
|
|
|
console._bpy_main_mod = bpy_main_mod
|
2009-11-06 23:53:40 +00:00
|
|
|
|
|
|
|
import io
|
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
|
|
|
|
consoles[console_id] = console, stdout, stderr
|
|
|
|
|
|
|
|
return console, stdout, stderr
|
|
|
|
|
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
# Both prompts must be the same length
|
|
|
|
PROMPT = '>>> '
|
|
|
|
PROMPT_MULTI = '... '
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-12-04 17:54:48 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
def execute(context):
|
|
|
|
sc = context.space_data
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
try:
|
2010-05-30 15:17:33 +00:00
|
|
|
line_object = sc.history[-1]
|
2009-11-17 12:21:41 +00:00
|
|
|
except:
|
2009-12-24 19:50:43 +00:00
|
|
|
return {'CANCELLED'}
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
console, stdout, stderr = get_console(hash(context.region))
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
# redirect output
|
|
|
|
sys.stdout = stdout
|
|
|
|
sys.stderr = stderr
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2011-10-17 06:58:07 +00:00
|
|
|
# don't allow the stdin to be used, can lock blender.
|
2010-05-30 14:05:58 +00:00
|
|
|
stdin_backup = sys.stdin
|
|
|
|
sys.stdin = None
|
|
|
|
|
2010-07-28 23:24:17 +00:00
|
|
|
if _BPY_MAIN_OWN:
|
|
|
|
main_mod_back = sys.modules["__main__"]
|
|
|
|
sys.modules["__main__"] = console._bpy_main_mod
|
|
|
|
|
2011-10-17 06:58:07 +00:00
|
|
|
# in case exception happens
|
|
|
|
line = "" # in case of encoding error
|
2010-05-30 15:17:33 +00:00
|
|
|
is_multiline = False
|
|
|
|
|
|
|
|
try:
|
2010-08-11 05:21:43 +00:00
|
|
|
line = line_object.body
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2011-10-17 06:58:07 +00:00
|
|
|
# run the console, "\n" executes a multi line statement
|
2010-05-30 15:17:33 +00:00
|
|
|
line_exec = line if line.strip() else "\n"
|
|
|
|
|
|
|
|
is_multiline = console.push(line_exec)
|
|
|
|
except:
|
|
|
|
# unlikely, but this can happen with unicode errors for example.
|
|
|
|
import traceback
|
|
|
|
stderr.write(traceback.format_exc())
|
2010-06-09 19:12:03 +00:00
|
|
|
|
2010-07-28 23:24:17 +00:00
|
|
|
if _BPY_MAIN_OWN:
|
|
|
|
sys.modules["__main__"] = main_mod_back
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
stdout.seek(0)
|
|
|
|
stderr.seek(0)
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
output = stdout.read()
|
|
|
|
output_err = stderr.read()
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
# cleanup
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
sys.last_traceback = None
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
# So we can reuse, clear all data
|
|
|
|
stdout.truncate(0)
|
|
|
|
stderr.truncate(0)
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-10-01 17:55:35 +00:00
|
|
|
# special exception. its possible the command loaded a new user interface
|
|
|
|
if hash(sc) != hash(context.space_data):
|
2011-08-25 17:59:37 +00:00
|
|
|
return {'FINISHED'}
|
2010-10-01 17:55:35 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
bpy.ops.console.scrollback_append(text=sc.prompt + line, type='INPUT')
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
if is_multiline:
|
|
|
|
sc.prompt = PROMPT_MULTI
|
|
|
|
else:
|
|
|
|
sc.prompt = PROMPT
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
# insert a new blank line
|
|
|
|
bpy.ops.console.history_append(text="", current_character=0,
|
2012-10-08 08:28:05 +00:00
|
|
|
remove_duplicates=True)
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
# Insert the output into the editor
|
|
|
|
# not quite correct because the order might have changed,
|
|
|
|
# but ok 99% of the time.
|
|
|
|
if output:
|
|
|
|
add_scrollback(output, 'OUTPUT')
|
|
|
|
if output_err:
|
|
|
|
add_scrollback(output_err, 'ERROR')
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-05-30 14:05:58 +00:00
|
|
|
# restore the stdin
|
|
|
|
sys.stdin = stdin_backup
|
2011-01-01 07:20:34 +00:00
|
|
|
|
2010-11-20 05:11:10 +00:00
|
|
|
# execute any hooks
|
|
|
|
for func, args in execute.hooks:
|
|
|
|
func(*args)
|
2010-05-30 14:05:58 +00:00
|
|
|
|
2009-12-24 19:50:43 +00:00
|
|
|
return {'FINISHED'}
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-11-20 05:11:10 +00:00
|
|
|
execute.hooks = []
|
|
|
|
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
def autocomplete(context):
|
|
|
|
from console import intellisense
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
sc = context.space_data
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
console = get_console(hash(context.region))[0]
|
2009-11-21 23:55:14 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
if not console:
|
2009-12-24 19:50:43 +00:00
|
|
|
return {'CANCELLED'}
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2011-10-17 06:58:07 +00:00
|
|
|
# don't allow the stdin to be used, can lock blender.
|
|
|
|
# note: unlikely stdin would be used for autocomplete. but its possible.
|
2010-05-30 14:05:58 +00:00
|
|
|
stdin_backup = sys.stdin
|
|
|
|
sys.stdin = None
|
2010-06-09 19:12:03 +00:00
|
|
|
|
2010-05-30 15:17:33 +00:00
|
|
|
scrollback = ""
|
|
|
|
scrollback_error = ""
|
2010-05-30 14:05:58 +00:00
|
|
|
|
2010-08-01 22:54:39 +00:00
|
|
|
if _BPY_MAIN_OWN:
|
|
|
|
main_mod_back = sys.modules["__main__"]
|
|
|
|
sys.modules["__main__"] = console._bpy_main_mod
|
|
|
|
|
2010-05-30 15:17:33 +00:00
|
|
|
try:
|
|
|
|
current_line = sc.history[-1]
|
2010-08-11 05:21:43 +00:00
|
|
|
line = current_line.body
|
2010-05-30 15:17:33 +00:00
|
|
|
|
2011-10-17 06:58:07 +00:00
|
|
|
# This function isn't aware of the text editor or being an operator
|
|
|
|
# just does the autocomplete then copy its results back
|
2011-03-14 06:48:51 +00:00
|
|
|
result = intellisense.expand(
|
|
|
|
line=line,
|
2010-05-30 15:17:33 +00:00
|
|
|
cursor=current_line.current_character,
|
|
|
|
namespace=console.locals,
|
2012-03-31 00:59:17 +00:00
|
|
|
private=bpy.app.debug_python)
|
2011-03-14 06:48:51 +00:00
|
|
|
|
|
|
|
line_new = result[0]
|
|
|
|
current_line.body, current_line.current_character, scrollback = result
|
|
|
|
del result
|
|
|
|
|
2011-10-17 06:58:07 +00:00
|
|
|
# update selection. setting body should really do this!
|
2011-03-14 06:48:51 +00:00
|
|
|
ofs = len(line_new) - len(line)
|
|
|
|
sc.select_start += ofs
|
|
|
|
sc.select_end += ofs
|
2010-05-30 15:17:33 +00:00
|
|
|
except:
|
|
|
|
# unlikely, but this can happen with unicode errors for example.
|
|
|
|
# or if the api attribute access its self causes an error.
|
|
|
|
import traceback
|
|
|
|
scrollback_error = traceback.format_exc()
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-08-01 22:54:39 +00:00
|
|
|
if _BPY_MAIN_OWN:
|
|
|
|
sys.modules["__main__"] = main_mod_back
|
|
|
|
|
2011-10-17 06:58:07 +00:00
|
|
|
# Separate autocomplete output by command prompts
|
2010-02-18 17:11:12 +00:00
|
|
|
if scrollback != '':
|
2011-10-23 12:17:24 +00:00
|
|
|
bpy.ops.console.scrollback_append(text=sc.prompt + current_line.body,
|
|
|
|
type='INPUT')
|
2010-02-18 17:11:12 +00:00
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
# Now we need to copy back the line from blender back into the
|
2011-10-17 06:58:07 +00:00
|
|
|
# text editor. This will change when we don't use the text editor
|
2009-11-17 12:21:41 +00:00
|
|
|
# anymore
|
|
|
|
if scrollback:
|
|
|
|
add_scrollback(scrollback, 'INFO')
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-05-30 15:17:33 +00:00
|
|
|
if scrollback_error:
|
|
|
|
add_scrollback(scrollback_error, 'ERROR')
|
|
|
|
|
2010-05-30 14:05:58 +00:00
|
|
|
# restore the stdin
|
|
|
|
sys.stdin = stdin_backup
|
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
context.area.tag_redraw()
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-12-24 19:50:43 +00:00
|
|
|
return {'FINISHED'}
|
2009-11-06 23:53:40 +00:00
|
|
|
|
|
|
|
|
2012-08-19 21:32:18 +00:00
|
|
|
def copy_as_script(context):
|
|
|
|
sc = context.space_data
|
|
|
|
lines = [
|
|
|
|
"import bpy",
|
2012-10-12 04:00:41 +00:00
|
|
|
"from bpy import data as D",
|
|
|
|
"from bpy import context as C",
|
2012-08-19 21:32:18 +00:00
|
|
|
"from mathutils import *",
|
|
|
|
"from math import *",
|
|
|
|
"",
|
|
|
|
]
|
|
|
|
|
|
|
|
for line in sc.scrollback:
|
|
|
|
text = line.body
|
|
|
|
type = line.type
|
2012-09-26 21:19:51 +00:00
|
|
|
|
2012-08-19 21:32:18 +00:00
|
|
|
if type == 'INFO': # ignore autocomp.
|
|
|
|
continue
|
|
|
|
if type == 'INPUT':
|
|
|
|
if text.startswith(PROMPT):
|
|
|
|
text = text[len(PROMPT):]
|
|
|
|
elif text.startswith(PROMPT_MULTI):
|
|
|
|
text = text[len(PROMPT_MULTI):]
|
|
|
|
elif type == 'OUTPUT':
|
|
|
|
text = "#~ " + text
|
|
|
|
elif type == 'ERROR':
|
|
|
|
text = "#! " + text
|
|
|
|
|
|
|
|
lines.append(text)
|
|
|
|
|
|
|
|
context.window_manager.clipboard = "\n".join(lines)
|
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
2009-11-17 12:21:41 +00:00
|
|
|
def banner(context):
|
|
|
|
sc = context.space_data
|
|
|
|
version_string = sys.version.strip().replace('\n', ' ')
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2010-12-10 00:20:32 +00:00
|
|
|
add_scrollback("PYTHON INTERACTIVE CONSOLE %s" % version_string, 'OUTPUT')
|
|
|
|
add_scrollback("", 'OUTPUT')
|
2010-11-25 14:55:03 +00:00
|
|
|
add_scrollback("Command History: Up/Down Arrow", 'OUTPUT')
|
|
|
|
add_scrollback("Cursor: Left/Right Home/End", 'OUTPUT')
|
|
|
|
add_scrollback("Remove: Backspace/Delete", 'OUTPUT')
|
|
|
|
add_scrollback("Execute: Enter", 'OUTPUT')
|
2013-02-08 10:15:44 +00:00
|
|
|
add_scrollback("Autocomplete: Ctrl-Space", 'OUTPUT')
|
|
|
|
add_scrollback("Zoom: Ctrl +/-, Ctrl-Wheel", 'OUTPUT')
|
2011-10-23 12:17:24 +00:00
|
|
|
add_scrollback("Builtin Modules: bpy, bpy.data, bpy.ops, "
|
|
|
|
"bpy.props, bpy.types, bpy.context, bpy.utils, "
|
|
|
|
"bgl, blf, mathutils",
|
|
|
|
'OUTPUT')
|
|
|
|
add_scrollback("Convenience Imports: from mathutils import *; "
|
|
|
|
"from math import *", 'OUTPUT')
|
2012-07-29 12:07:06 +00:00
|
|
|
add_scrollback("Convenience Variables: C = bpy.context, D = bpy.data",
|
|
|
|
'OUTPUT')
|
2009-11-17 12:21:41 +00:00
|
|
|
add_scrollback("", 'OUTPUT')
|
|
|
|
sc.prompt = PROMPT
|
2009-11-06 23:53:40 +00:00
|
|
|
|
2009-12-24 19:50:43 +00:00
|
|
|
return {'FINISHED'}
|