[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19142] branches/blender2.5/blender/source /blender: Python experimental UI API

Campbell Barton ideasman42 at gmail.com
Sat Feb 28 14:27:47 CET 2009


Revision: 19142
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19142
Author:   campbellbarton
Date:     2009-02-28 14:27:45 +0100 (Sat, 28 Feb 2009)

Log Message:
-----------
Python experimental UI API
Can draw panels in the scripts space containing RNA and operator buttons.
* Added bpyui.register() so scripts can draw buttons and panels into the scripts space type.
* wrapped drawBlock, drawPanels and matchPanelsView2d
* Operator buttons take a python dictionary used to set the button defaults.
* BPY_getFileAndNum utility function to get the filename and line number python is currently running.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/editors/space_script/space_script.c
    branches/blender2.5/blender/source/blender/python/BPY_extern.h
    branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h
    branches/blender2.5/blender/source/blender/python/intern/bpy_ui.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_util.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_util.h

Modified: branches/blender2.5/blender/source/blender/editors/space_script/space_script.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/space_script/space_script.c	2009-02-28 12:49:18 UTC (rev 19141)
+++ branches/blender2.5/blender/source/blender/editors/space_script/space_script.c	2009-02-28 13:27:45 UTC (rev 19142)
@@ -60,6 +60,10 @@
 
 #include "script_intern.h"	// own include
 
+
+//static script_run_python(char *funcname, )
+
+
 /* ******************** default callbacks for script space ***************** */
 
 static SpaceLink *script_new(const bContext *C)
@@ -139,7 +143,7 @@
 static void script_main_area_draw(const bContext *C, ARegion *ar)
 {
 	/* draw entirely, view changes should be handled here */
-	// SpaceScript *sscript= (SpaceScript*)CTX_wm_space_data(C);
+	SpaceScript *sscript= (SpaceScript*)CTX_wm_space_data(C);
 	View2D *v2d= &ar->v2d;
 	float col[3];
 	
@@ -151,7 +155,12 @@
 	UI_view2d_view_ortho(C, v2d);
 		
 	/* data... */
+	// BPY_run_python_script(C, "/root/blender-svn/blender25/test.py");
 	
+	if (sscript->script) {
+		//BPY_run_python_script_space(scpt->script.filename);
+		BPY_run_script_space_draw(C, sscript);
+	}
 	
 	/* reset view matrix */
 	UI_view2d_view_restore(C);
@@ -190,6 +199,8 @@
 static void script_main_area_listener(ARegion *ar, wmNotifier *wmn)
 {
 	/* context changes */
+	// XXX - Todo, need the ScriptSpace accessible to get the python script to run.
+	// BPY_run_script_space_listener()
 }
 
 /* only called once, from space/spacetypes.c */
@@ -213,7 +224,7 @@
 	art->init= script_main_area_init;
 	art->draw= script_main_area_draw;
 	art->listener= script_main_area_listener;
-	art->keymapflag= ED_KEYMAP_VIEW2D;
+	art->keymapflag= ED_KEYMAP_VIEW2D|   ED_KEYMAP_UI|ED_KEYMAP_FRAMES; // XXX need to further test this ED_KEYMAP_UI is needed for button interaction
 
 	BLI_addhead(&st->regiontypes, art);
 	

Modified: branches/blender2.5/blender/source/blender/python/BPY_extern.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/BPY_extern.h	2009-02-28 12:49:18 UTC (rev 19141)
+++ branches/blender2.5/blender/source/blender/python/BPY_extern.h	2009-02-28 13:27:45 UTC (rev 19142)
@@ -95,7 +95,14 @@
 	int BPY_menu_do_python( short menutype, int event );
 	int BPY_menu_do_shortcut( short menutype, unsigned short key, unsigned short modifiers );
 	int BPY_menu_invoke( struct BPyMenu *pym, short menutype );
-	void BPY_run_python_script( struct bContext *C, const char *filename );
+	
+	/* 2.5 UI Scripts */
+	void BPY_run_python_script( struct bContext *C, const char *filename ); // 2.5 working
+	int BPY_run_script_space_draw(struct bContext *C, struct SpaceScript * sc); // 2.5 working
+//	int BPY_run_script_space_listener(struct bContext *C, struct SpaceScript * sc, struct ARegion *ar, struct wmNotifier *wmn); // 2.5 working
+	
+	
+	
 	int BPY_run_script(struct Script *script);
 	void BPY_free_compiled_text( struct Text *text );
 

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2009-02-28 12:49:18 UTC (rev 19141)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2009-02-28 13:27:45 UTC (rev 19142)
@@ -12,8 +12,11 @@
 
 #include "bpy_rna.h"
 #include "bpy_operator.h"
+#include "bpy_ui.h"
 
+#include "DNA_space_types.h"
 
+
 /*****************************************************************************
 * Description: This function creates a new Python dictionary object.
 *****************************************************************************/
@@ -107,3 +110,130 @@
 	
 	//BPY_end_python();
 }
+
+
+/* TODO - move into bpy_space.c ? */
+/* GUI interface routines */
+
+/* Copied from Draw.c */
+static void exit_pydraw( SpaceScript * sc, short err )
+{
+	Script *script = NULL;
+
+	if( !sc || !sc->script )
+		return;
+
+	script = sc->script;
+
+	if( err ) {
+		PyErr_Print(  );
+		script->flags = 0;	/* mark script struct for deletion */
+		SCRIPT_SET_NULL(script);
+		script->scriptname[0] = '\0';
+		script->scriptarg[0] = '\0';
+// XXX 2.5		error_pyscript();
+// XXX 2.5		scrarea_queue_redraw( sc->area );
+	}
+
+#if 0 // XXX 2.5
+	BPy_Set_DrawButtonsList(sc->but_refs);
+	BPy_Free_DrawButtonsList(); /*clear all temp button references*/
+#endif
+
+	sc->but_refs = NULL;
+	
+	Py_XDECREF( ( PyObject * ) script->py_draw );
+	Py_XDECREF( ( PyObject * ) script->py_event );
+	Py_XDECREF( ( PyObject * ) script->py_button );
+
+	script->py_draw = script->py_event = script->py_button = NULL;
+}
+
+static int bpy_run_script_init(bContext *C, SpaceScript * sc)
+{
+	if (sc->script==NULL) 
+		return 0;
+	
+	if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0')
+		BPY_run_python_script(C, sc->script->scriptname);
+		
+	if (sc->script->py_draw==NULL)
+		return 0;
+	
+	return 1;
+}
+
+int BPY_run_script_space_draw(bContext *C, SpaceScript * sc)
+{
+	if (bpy_run_script_init(C, sc)) {
+		PyGILState_STATE gilstate = PyGILState_Ensure();
+		PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
+		PyGILState_Release(gilstate);
+		
+		if (result==NULL)
+			exit_pydraw(sc, 1);
+	}
+	return 1;
+}
+
+// XXX - not used yet, listeners dont get a context
+int BPY_run_script_space_listener(bContext *C, SpaceScript * sc)
+{
+	if (bpy_run_script_init(C, sc)) {
+		PyGILState_STATE gilstate = PyGILState_Ensure();
+		
+		PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL );
+		PyGILState_Release(gilstate);
+		
+		if (result==NULL)
+			exit_pydraw(sc, 1);
+	}
+	return 1;
+}
+
+#if 0
+/* called from the the scripts window, assume context is ok */
+int BPY_run_python_script_space(const char *modulename, const char *func)
+{
+	PyObject *py_dict, *py_result= NULL;
+	char pystring[512];
+	PyGILState_STATE gilstate;
+	
+	/* for calling the module function */
+	PyObject *py_func, 
+	
+	gilstate = PyGILState_Ensure();
+	
+	py_dict = CreateGlobalDictionary(C);
+	
+	PyObject *module = PyImport_ImportModule(scpt->script.filename);
+	if (module==NULL) {
+		PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename);
+	}
+	else {
+		py_func = PyObject_GetAttrString(modulename, func);
+		if (py_func==NULL) {
+			PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func);
+		}
+		else {
+			if (!PyCallable_Check(py_func)) {
+				PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func);
+			}
+			else {
+				py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually
+			}
+		}
+	}
+	
+	if (!py_result)
+		PyErr_Print();
+	else
+		Py_DECREF( py_result );
+	
+	Py_XDECREF(module);
+	
+	
+	PyGILState_Release(gilstate);
+	return 1;
+}
+#endif

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h	2009-02-28 12:49:18 UTC (rev 19141)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h	2009-02-28 13:27:45 UTC (rev 19142)
@@ -35,9 +35,10 @@
 extern PyTypeObject pyop_base_Type;
 extern PyTypeObject pyop_func_Type;
 
+#define BPy_OperatorBase_Check(v)	(PyObject_TypeCheck(v, &pyop_base_Type))
 #define BPy_OperatorFunc_Check(v)	(PyObject_TypeCheck(v, &pyop_func_Type))
-#define BPy_PropertyRNA_Check(v)	(PyObject_TypeCheck(v, &pyop_func_Type))
 
+
 typedef struct {
 	PyObject_HEAD /* required python macro   */
 	bContext *C;

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_ui.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_ui.c	2009-02-28 12:49:18 UTC (rev 19141)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_ui.c	2009-02-28 13:27:45 UTC (rev 19142)
@@ -23,8 +23,11 @@
  */
 
 #include "bpy_ui.h"
+#include "bpy_util.h"
 #include "bpy_rna.h" /* for rna buttons */
+#include "bpy_operator.h" /* for setting button operator properties */
 #include "bpy_compat.h"
+#include "WM_types.h" /* for WM_OP_INVOKE_DEFAULT & friends */
 
 #include "BLI_dynstr.h"
 
@@ -33,6 +36,7 @@
 #include "BKE_context.h"
 
 #include "DNA_screen_types.h"
+#include "DNA_space_types.h" /* only for SpaceLink */
 #include "UI_interface.h"
 #include "WM_api.h"
 
@@ -74,14 +78,23 @@
 
 static PyObject *Method_defButO( PyObject * self, PyObject * args )
 {
-	PyObject *py_block;
+	uiBut *but;
+	PyObject *py_block, *py_keywords= NULL;
 	char *opname, *butname, *tip;
 	int exec, xco, yco, width, height;
 	
-	if( !PyArg_ParseTuple( args, "O!sisiiiis:defButO", &PyCObject_Type, &py_block, &opname, &exec, &butname, &xco, &yco, &width, &height, &tip))
+	if( !PyArg_ParseTuple( args, "O!sisiiiis|O!:defButO", &PyCObject_Type, &py_block, &opname, &exec, &butname, &xco, &yco, &width, &height, &tip, &PyDict_Type, &py_keywords))
 		return NULL;
 	
-	return PyCObject_FromVoidPtr(uiDefButO(PyCObject_AsVoidPtr(py_block), BUT, opname, exec, butname, xco, yco, width, height, tip), NULL );
+	but= uiDefButO(PyCObject_AsVoidPtr(py_block), BUT, opname, exec, butname, xco, yco, width, height, tip);
+	
+	/* Optional python doctionary used to set python properties, just like how keyword args are used */
+	if (py_keywords && PyDict_Size(py_keywords)) {
+		if (PYOP_props_from_dict(uiButGetOperatorPtrRNA(but), py_keywords) == -1)
+			return NULL;
+	}
+	
+	return PyCObject_FromVoidPtr(but, NULL);
 }
 
 static PyObject *Method_defAutoButR( PyObject * self, PyObject * args )
@@ -165,6 +178,40 @@
 	Py_RETURN_NONE;
 }
 
+static PyObject *Method_drawBlock( PyObject * self, PyObject * args )
+{
+	PyObject *py_context, *py_block;
+	
+	if( !PyArg_ParseTuple( args, "O!O!:drawBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_block) )
+		return NULL;
+	
+	uiDrawBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_block));
+	Py_RETURN_NONE;
+}
+
+static PyObject *Method_drawPanels( PyObject * self, PyObject * args )
+{
+	PyObject *py_context;
+	int align;
+	
+	if( !PyArg_ParseTuple( args, "O!i:drawPanels", &PyCObject_Type, &py_context, &align) )
+		return NULL;
+	
+	uiDrawPanels(PyCObject_AsVoidPtr(py_context), align);
+	Py_RETURN_NONE;
+}
+
+static PyObject *Method_matchPanelsView2d( PyObject * self, PyObject * args )
+{

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list