- added bpy.context to the python module

- made the console banner printing function into a python operator (includes sys.version)
- added 'C' into the consoles default namespace for convenience
This commit is contained in:
Campbell Barton 2009-11-05 11:17:09 +00:00
parent cacd68c335
commit 247f72fcc0
3 changed files with 46 additions and 21 deletions

@ -127,7 +127,7 @@ def get_console(console_id):
return console, stdout, stderr
class CONSOLE_OT_exec(bpy.types.Operator):
class ConsoleExec(bpy.types.Operator):
'''Execute the current console line as a python expression.'''
bl_idname = "console.execute"
bl_label = "Console Execute"
@ -210,7 +210,7 @@ class CONSOLE_OT_exec(bpy.types.Operator):
return ('FINISHED',)
class CONSOLE_OT_autocomplete(bpy.types.Operator):
class ConsoleAutocomplete(bpy.types.Operator):
'''Evaluate the namespace up until the cursor and give a list of
options or complete the name if there is only one.'''
bl_idname = "console.autocomplete"
@ -256,9 +256,36 @@ class CONSOLE_OT_autocomplete(bpy.types.Operator):
return ('FINISHED',)
class ConsoleBanner(bpy.types.Operator):
bl_idname = "console.banner"
def execute(self, context):
sc = context.space_data
version_string = sys.version.strip().replace('\n', ' ')
add_scrollback(" * Python Interactive Console %s *" % version_string, 'OUTPUT')
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')
add_scrollback("Autocomplete: Ctrl+Space", 'OUTPUT')
add_scrollback("Ctrl +/- Wheel: Zoom", 'OUTPUT')
add_scrollback("Builtin Modules: bpy, bpy.data, bpy.ops, bpy.props, bpy.types, bpy.context, Mathutils, Geometry, BGL", 'OUTPUT')
add_scrollback("", 'OUTPUT')
add_scrollback("", 'OUTPUT')
sc.prompt = ConsoleExec.PROMPT
# Add context into the namespace for quick access
console = get_console(hash(context.region))[0]
console.locals["C"] = bpy.context
return ('FINISHED',)
bpy.types.register(CONSOLE_HT_header)
bpy.types.register(CONSOLE_MT_console)
bpy.types.register(CONSOLE_MT_report)
bpy.ops.add(CONSOLE_OT_exec)
bpy.ops.add(CONSOLE_OT_autocomplete)
bpy.ops.add(ConsoleExec)
bpy.ops.add(ConsoleAutocomplete)
bpy.ops.add(ConsoleBanner)

@ -164,21 +164,9 @@ static void console_main_area_draw(const bContext *C, ARegion *ar)
View2DScrollers *scrollers;
//float col[3];
/* add helper text, why not? */
if(sc->scrollback.first==NULL) {
console_scrollback_add_str(C, " * Python Interactive Console *", 0);
console_scrollback_add_str(C, "Command History: Up/Down Arrow", 0);
console_scrollback_add_str(C, "Cursor: Left/Right Home/End", 0);
console_scrollback_add_str(C, "Remove: Backspace/Delete", 0);
console_scrollback_add_str(C, "Execute: Enter", 0);
console_scrollback_add_str(C, "Autocomplete: Ctrl+Space", 0);
console_scrollback_add_str(C, "Ctrl +/- Wheel: Zoom", 0);
console_scrollback_add_str(C, "Builtin Modules: bpy, bpy.data, bpy.ops, bpy.props, bpy.types, bpy.ui", 0);
if(sc->scrollback.first==NULL)
WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
/* This is normally set by python but to start with its easier just to set it like this rather then running python with no args */
strcpy(sc->prompt, ">>> ");
}
/* clear and setup matrix */
//UI_GetThemeColor3fv(TH_BACK, col);
//glClearColor(col[0], col[1], col[2], 0.0);

@ -101,8 +101,6 @@ void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
if(py_call_level==1) {
BPY_update_modules(); /* can give really bad results if this isnt here */
if(C) { // XXX - should always be true.
BPy_SetContext(C);
bpy_import_main_set(CTX_data_main(C));
@ -111,6 +109,8 @@ void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n");
}
BPY_update_modules(); /* can give really bad results if this isnt here */
#ifdef TIME_PY_RUN
if(bpy_timer_count==0) {
/* record time from the beginning */
@ -171,6 +171,7 @@ void BPY_free_compiled_text( struct Text *text )
/*****************************************************************************
* Description: Creates the bpy module and adds it to sys.modules for importing
*****************************************************************************/
static BPy_StructRNA *bpy_context_module= NULL; /* for fast access */
static void bpy_init_modules( void )
{
PyObject *mod;
@ -204,6 +205,15 @@ static void bpy_init_modules( void )
bpy_import_test("bpy_ext"); /* extensions to our existing types */
}
/* bpy context */
{
bpy_context_module= ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
RNA_pointer_create(NULL, &RNA_Context, NULL, &bpy_context_module->ptr);
PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
}
/* stand alone utility modules not related to blender directly */
Geometry_Init();
Mathutils_Init();
@ -220,7 +230,7 @@ void BPY_update_modules( void )
/* refreshes the main struct */
BPY_update_rna_module();
bpy_context_module->ptr.data= (void *)BPy_GetContext();
}
/*****************************************************************************