[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [48225] branches/pyapi_devel_26/source/ blender: initial python space extension, python can register a space and draw into the script window, but still very WIP.
Campbell Barton
ideasman42 at gmail.com
Sat Jun 23 21:27:44 CEST 2012
Revision: 48225
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48225
Author: campbellbarton
Date: 2012-06-23 19:27:41 +0000 (Sat, 23 Jun 2012)
Log Message:
-----------
initial python space extension, python can register a space and draw into the script window, but still very WIP.
Modified Paths:
--------------
branches/pyapi_devel_26/source/blender/blenkernel/BKE_screen.h
branches/pyapi_devel_26/source/blender/editors/screen/area.c
branches/pyapi_devel_26/source/blender/editors/space_script/space_script.c
branches/pyapi_devel_26/source/blender/makesdna/DNA_space_types.h
branches/pyapi_devel_26/source/blender/makesrna/intern/rna_space.c
branches/pyapi_devel_26/source/blender/windowmanager/WM_api.h
branches/pyapi_devel_26/source/blender/windowmanager/intern/wm.c
branches/pyapi_devel_26/source/blender/windowmanager/intern/wm_init_exit.c
Modified: branches/pyapi_devel_26/source/blender/blenkernel/BKE_screen.h
===================================================================
--- branches/pyapi_devel_26/source/blender/blenkernel/BKE_screen.h 2012-06-23 18:08:56 UTC (rev 48224)
+++ branches/pyapi_devel_26/source/blender/blenkernel/BKE_screen.h 2012-06-23 19:27:41 UTC (rev 48225)
@@ -51,6 +51,9 @@
struct wmNotifier;
struct wmWindow;
struct wmWindowManager;
+struct SpaceScript;
+struct SpaceTypeDynamic;
+struct SpaceDynamic;
#include "RNA_types.h"
@@ -106,6 +109,27 @@
} SpaceType;
+typedef struct SpaceTypeDynamic {
+ struct SpaceTypeDynamic *next, *prev;
+
+ char idname[BKE_ST_MAXNAME]; /* unique name */
+ char label[BKE_ST_MAXNAME]; /* for panel header */
+
+ int flag;
+
+ /* draw entirely, view changes should be handled here */
+ void (*draw)(const struct bContext *, struct ARegion *, struct SpaceScript *, struct SpaceDynamic *);
+
+ /* RNA integration */
+ ExtensionRNA ext;
+} SpaceTypeDynamic;
+
+
+typedef struct SpaceDynamic {
+ struct SpaceTypeDynamic *type;
+ /* PYSPACE_TODO - we could add ID properties here, so scripts can store space data here */
+} SpaceDynamic;
+
/* region types are also defined using spacetypes_init, via a callback */
typedef struct ARegionType {
Modified: branches/pyapi_devel_26/source/blender/editors/screen/area.c
===================================================================
--- branches/pyapi_devel_26/source/blender/editors/screen/area.c 2012-06-23 18:08:56 UTC (rev 48224)
+++ branches/pyapi_devel_26/source/blender/editors/screen/area.c 2012-06-23 19:27:41 UTC (rev 48225)
@@ -1486,6 +1486,8 @@
"|%l"
"|Python Console %x18"
+ /* PYSPACE_TODO - make dynamic */
+ "|Script %x14"
);
return IFACE_(types);
Modified: branches/pyapi_devel_26/source/blender/editors/space_script/space_script.c
===================================================================
--- branches/pyapi_devel_26/source/blender/editors/space_script/space_script.c 2012-06-23 18:08:56 UTC (rev 48224)
+++ branches/pyapi_devel_26/source/blender/editors/space_script/space_script.c 2012-06-23 19:27:41 UTC (rev 48225)
@@ -155,7 +155,13 @@
// BPY_script_exec(C, "/root/blender-svn/blender25/test.py", NULL);
#ifdef WITH_PYTHON
- if (sscript->script) {
+ if (sscript->idname[0] || 1) {
+ SpaceTypeDynamic *std = WM_spacetypedynamic_find("TEST", FALSE); /* sscript->idname */
+ if (std) {
+ SpaceDynamic sd = {0}; /* PYSPACE_TODO, make persistant? - Campbell */
+ sd.type = std;
+ std->draw(C, ar, sscript, &sd);
+ }
// BPY_run_script_space_draw(C, sscript);
}
#else
Modified: branches/pyapi_devel_26/source/blender/makesdna/DNA_space_types.h
===================================================================
--- branches/pyapi_devel_26/source/blender/makesdna/DNA_space_types.h 2012-06-23 18:08:56 UTC (rev 48224)
+++ branches/pyapi_devel_26/source/blender/makesdna/DNA_space_types.h 2012-06-23 19:27:41 UTC (rev 48225)
@@ -840,6 +840,9 @@
int pad1;
void *but_refs;
+
+ /* new 2.6 data */
+ char idname[64];
} SpaceScript;
/* Nodes Editor =========================================== */
Modified: branches/pyapi_devel_26/source/blender/makesrna/intern/rna_space.c
===================================================================
--- branches/pyapi_devel_26/source/blender/makesrna/intern/rna_space.c 2012-06-23 18:08:56 UTC (rev 48224)
+++ branches/pyapi_devel_26/source/blender/makesrna/intern/rna_space.c 2012-06-23 19:27:41 UTC (rev 48225)
@@ -75,6 +75,7 @@
{SPACE_CONSOLE, "CONSOLE", 0, "Python Console", ""},
{SPACE_USERPREF, "USER_PREFERENCES", 0, "User Preferences", ""},
{SPACE_CLIP, "CLIP_EDITOR", 0, "Clip Editor", ""},
+ /* PYSPACE_TODO - make dynamic enum */
{0, NULL, 0, NULL, NULL}
};
@@ -145,6 +146,146 @@
#include "IMB_imbuf_types.h"
+static void space_draw(const bContext *C, ARegion *ar, SpaceScript *sscript, SpaceDynamic *sd)
+{
+ extern FunctionRNA rna_Space_draw_func;
+
+ PointerRNA sdr;
+ ParameterList list;
+ FunctionRNA *func;
+
+ RNA_pointer_create(&CTX_wm_screen(C)->id, sd->type->ext.srna, sscript, &sdr);
+ func = &rna_Space_draw_func; /* RNA_struct_find_function(&sdr, "draw"); */
+
+ RNA_parameter_list_create(&list, &sdr, func);
+ RNA_parameter_set_lookup(&list, "context", &C);
+ RNA_parameter_set_lookup(&list, "region", &ar);
+ sd->type->ext.call((bContext *)C, &sdr, func, &list);
+
+ RNA_parameter_list_free(&list);
+}
+
+static void rna_Space_unregister(Main *UNUSED(bmain), StructRNA *type)
+{
+ SpaceTypeDynamic *std = RNA_struct_blender_type_get(type);
+
+ if (!std)
+ return;
+
+ RNA_struct_free_extension(type, &std->ext);
+
+ WM_spacetypedynamic_freelink(std);
+
+ RNA_struct_free(&BLENDER_RNA, type);
+
+ /* update while blender is running */
+ WM_main_add_notifier(NC_SCREEN | NA_EDITED, NULL);
+}
+
+static char _space_descr[RNA_DYN_DESCR_MAX];
+static StructRNA *rna_Space_register(Main *bmain, ReportList *reports, void *data, const char *identifier,
+ StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
+{
+ SpaceTypeDynamic *std, dummystd = {NULL};
+ SpaceScript dummyspace = {NULL};
+ PointerRNA dummyptr;
+ int have_function[2];
+ size_t over_alloc = 0; /* warning, if this becomes a bess, we better do another alloc */
+ size_t description_size = 0;
+
+ /* setup dummy space & space type to store static properties in */
+ dummyspace.spacetype = SPACE_SCRIPT;
+// dummyspace.type = &dummystd;
+// dummyspace.type->description = _space_descr;
+ RNA_pointer_create(NULL, &RNA_Space, &dummyspace, &dummyptr);
+
+ /* clear in case they are left unset */
+ _space_descr[0] = '\0';
+
+ /* validate the python class */
+ if (validate(&dummyptr, data, have_function) != 0)
+ return NULL;
+
+ // PYSPACE_TODO --- investigate a way around thus
+ strcpy(dummystd.idname, dummyspace.idname);
+
+ if (strlen(identifier) >= sizeof(dummystd.idname)) {
+ BKE_reportf(reports, RPT_ERROR, "registering space class: '%s' is too long, maximum length is %d",
+ identifier, (int)sizeof(dummystd.idname));
+ return NULL;
+ }
+
+ /* check if we have registered this space type before, and remove it */
+ std = WM_spacetypedynamic_find(dummystd.idname, TRUE);
+ if (std && std->ext.srna)
+ rna_Space_unregister(bmain, std->ext.srna);
+
+ /* create a new space type */
+ if (_space_descr[0]) {
+ description_size = strlen(_space_descr) + 1;
+ over_alloc += description_size;
+ }
+
+ std = MEM_callocN(sizeof(SpaceTypeDynamic) + over_alloc, "python buttons space");
+ memcpy(std, &dummystd, sizeof(dummystd));
+
+// if (_space_descr[0]) {
+// char *buf = (char *)(std + 1);
+// memcpy(buf, _space_descr, description_size);
+// std->description = buf;
+// }
+
+ std->ext.srna = RNA_def_struct(&BLENDER_RNA, std->idname, "Space");
+ std->ext.data = data;
+ std->ext.call = call;
+ std->ext.free = free;
+ RNA_struct_blender_type_set(std->ext.srna, std);
+ RNA_def_struct_flag(std->ext.srna, STRUCT_NO_IDPROPERTIES);
+
+ //std->poll = (have_function[0]) ? space_poll : NULL;
+ std->draw = (have_function[1]) ? space_draw : NULL;
+
+ WM_spacetypedynamic_add(std);
+
+ /* update while blender is running */
+ WM_main_add_notifier(NC_SCREEN | NA_EDITED, NULL);
+
+ return std->ext.srna;
+}
+
+void rna_Space_bl_idname_get(PointerRNA *ptr, char *value)
+{
+ SpaceLink *sl = (SpaceLink *)ptr->data;
+ if (sl->spacetype == SPACE_SCRIPT) {
+ BLI_strncpy(value, ((SpaceScript *)sl)->idname, sizeof(((SpaceScript *)sl)->idname));
+ }
+ else {
+ value[0] = '\0';
+ }
+}
+
+int rna_Space_bl_idname_length(PointerRNA *ptr)
+{
+ SpaceLink *sl = (SpaceLink *)ptr->data;
+ if (sl->spacetype == SPACE_SCRIPT) {
+ return BLI_strnlen(((SpaceScript *)sl)->idname, sizeof(((SpaceScript *)sl)->idname));
+ }
+ else {
+ return 0;
+ }
+}
+
+void rna_Space_bl_idname_set(PointerRNA *ptr, const char *value)
+{
+ SpaceLink *sl = (SpaceLink *)ptr->data;
+ if (sl->spacetype == SPACE_SCRIPT) {
+ BLI_strncpy(((SpaceScript *)sl)->idname, value, sizeof(((SpaceScript *)sl)->idname));
+ }
+ else {
+ return 0;
+ }
+}
+
static StructRNA *rna_Space_refine(struct PointerRNA *ptr)
{
SpaceLink *space = (SpaceLink *)ptr->data;
@@ -184,6 +325,11 @@
return &RNA_SpaceUserPreferences;
case SPACE_CLIP:
return &RNA_SpaceClipEditor;
+ case SPACE_SCRIPT:
+ /* special case - keep current type */
+ if (ptr->type->base == &RNA_Space) {
+ return ptr->type;
+ }
default:
return &RNA_Space;
}
@@ -1081,17 +1227,42 @@
{
StructRNA *srna;
PropertyRNA *prop;
+
+ FunctionRNA *func;
+ PropertyRNA *parm;
srna = RNA_def_struct(brna, "Space", NULL);
RNA_def_struct_sdna(srna, "SpaceLink");
RNA_def_struct_ui_text(srna, "Space", "Space data for a screen area");
RNA_def_struct_refine_func(srna, "rna_Space_refine");
+ RNA_def_struct_register_funcs(srna, "rna_Space_register", "rna_Space_unregister", NULL);
prop = RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "spacetype");
RNA_def_property_enum_items(prop, space_type_items);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "Type", "Space data type");
+
+ /* Registration */
+
+ /* draw */
+ func = RNA_def_function(srna, "draw", NULL);
+ RNA_def_function_ui_description(func, "Draw function for the operator");
+ //RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL);
+ parm = RNA_def_pointer(func, "context", "Context", "", "");
+ RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
+ parm = RNA_def_pointer(func, "region", "Region", "", "");
+ RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
+
+ RNA_define_verify_sdna(0);
+
+ prop = RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
+ //RNA_def_property_string_sdna(prop, NULL, "type->idname");
+ RNA_def_property_string_funcs(prop, "rna_Space_bl_idname_get", "rna_Space_bl_idname_length", "rna_Space_bl_idname_set");
+ RNA_def_property_flag(prop, PROP_REGISTER | PROP_NEVER_CLAMP);
+ RNA_def_property_ui_text(prop, "ID Name", "");
+
+ RNA_define_verify_sdna(1);
}
static void rna_def_space_image_uv(BlenderRNA *brna)
Modified: branches/pyapi_devel_26/source/blender/windowmanager/WM_api.h
===================================================================
--- branches/pyapi_devel_26/source/blender/windowmanager/WM_api.h 2012-06-23 18:08:56 UTC (rev 48224)
+++ branches/pyapi_devel_26/source/blender/windowmanager/WM_api.h 2012-06-23 19:27:41 UTC (rev 48225)
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list