From 4d10642e8bd7a85256f26952283dda97cfd1b22a Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 2 May 2012 12:14:27 +0000 Subject: Python/context: allow overriding window/screen/area/region context for running operators from python, this is useful to run an operator in a particular place in the UI. --- source/blender/blenkernel/intern/context.c | 501 ++++++++++++++++------------- 1 file changed, 270 insertions(+), 231 deletions(-) (limited to 'source/blender/blenkernel') diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c index 7a5b4ef9b24..1b4a9cb7f5a 100644 --- a/source/blender/blenkernel/intern/context.c +++ b/source/blender/blenkernel/intern/context.c @@ -202,6 +202,7 @@ void CTX_store_free_list(ListBase *contexts) } /* is python initialied? */ + int CTX_py_init_get(bContext *C) { return C->data.py_init; @@ -220,237 +221,6 @@ void CTX_py_dict_set(bContext *C, void *value) C->data.py_context= value; } -/* window manager context */ - -wmWindowManager *CTX_wm_manager(const bContext *C) -{ - return C->wm.manager; -} - -wmWindow *CTX_wm_window(const bContext *C) -{ - return C->wm.window; -} - -bScreen *CTX_wm_screen(const bContext *C) -{ - return C->wm.screen; -} - -ScrArea *CTX_wm_area(const bContext *C) -{ - return C->wm.area; -} - -SpaceLink *CTX_wm_space_data(const bContext *C) -{ - return (C->wm.area)? C->wm.area->spacedata.first: NULL; -} - -ARegion *CTX_wm_region(const bContext *C) -{ - return C->wm.region; -} - -void *CTX_wm_region_data(const bContext *C) -{ - return (C->wm.region)? C->wm.region->regiondata: NULL; -} - -struct ARegion *CTX_wm_menu(const bContext *C) -{ - return C->wm.menu; -} - -struct ReportList *CTX_wm_reports(const bContext *C) -{ - if (C->wm.manager) - return &(C->wm.manager->reports); - - return NULL; -} - -View3D *CTX_wm_view3d(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_VIEW3D) - return C->wm.area->spacedata.first; - return NULL; -} - -RegionView3D *CTX_wm_region_view3d(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_VIEW3D) - if (C->wm.region) - return C->wm.region->regiondata; - return NULL; -} - -struct SpaceText *CTX_wm_space_text(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_TEXT) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceConsole *CTX_wm_space_console(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_CONSOLE) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceImage *CTX_wm_space_image(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_IMAGE) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceButs *CTX_wm_space_buts(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_BUTS) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceFile *CTX_wm_space_file(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_FILE) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceSeq *CTX_wm_space_seq(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_SEQ) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceOops *CTX_wm_space_outliner(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_OUTLINER) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceNla *CTX_wm_space_nla(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_NLA) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceTime *CTX_wm_space_time(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_TIME) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceNode *CTX_wm_space_node(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_NODE) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceLogic *CTX_wm_space_logic(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_LOGIC) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceIpo *CTX_wm_space_graph(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_IPO) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceAction *CTX_wm_space_action(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_ACTION) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceInfo *CTX_wm_space_info(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_INFO) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceUserPref *CTX_wm_space_userpref(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_USERPREF) - return C->wm.area->spacedata.first; - return NULL; -} - -struct SpaceClip *CTX_wm_space_clip(const bContext *C) -{ - if (C->wm.area && C->wm.area->spacetype==SPACE_CLIP) - return C->wm.area->spacedata.first; - return NULL; -} - -void CTX_wm_manager_set(bContext *C, wmWindowManager *wm) -{ - C->wm.manager= wm; - C->wm.window= NULL; - C->wm.screen= NULL; - C->wm.area= NULL; - C->wm.region= NULL; -} - -void CTX_wm_window_set(bContext *C, wmWindow *win) -{ - C->wm.window= win; - C->wm.screen= (win)? win->screen: NULL; - if (C->wm.screen) - C->data.scene= C->wm.screen->scene; - C->wm.area= NULL; - C->wm.region= NULL; -} - -void CTX_wm_screen_set(bContext *C, bScreen *screen) -{ - C->wm.screen= screen; - if (C->wm.screen) - C->data.scene= C->wm.screen->scene; - C->wm.area= NULL; - C->wm.region= NULL; -} - -void CTX_wm_area_set(bContext *C, ScrArea *area) -{ - C->wm.area= area; - C->wm.region= NULL; -} - -void CTX_wm_region_set(bContext *C, ARegion *region) -{ - C->wm.region= region; -} - -void CTX_wm_menu_set(bContext *C, ARegion *menu) -{ - C->wm.menu= menu; -} - -void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg) -{ - C->wm.operator_poll_msg= msg; -} - -const char *CTX_wm_operator_poll_msg_get(bContext *C) -{ - return C->wm.operator_poll_msg; -} - /* data context utility functions */ struct bContextDataResult { @@ -460,6 +230,22 @@ struct bContextDataResult { short type; /* 0: normal, 1: seq */ }; +static void *ctx_wm_python_context_get(const bContext *C, const char *member, void *fall_through) +{ +#ifdef WITH_PYTHON + bContextDataResult result; + + if (C && CTX_py_dict_get(C)) { + memset(&result, 0, sizeof(bContextDataResult)); + BPY_context_member_get((bContext*)C, member, &result); + if(result.ptr.data) + return result.ptr.data; + } +#endif + + return fall_through; +} + static int ctx_data_get(bContext *C, const char *member, bContextDataResult *result) { int done= 0, recursion= C->data.recursion; @@ -745,6 +531,259 @@ short CTX_data_type_get(bContextDataResult *result) return result->type; } + + +/* window manager context */ + +wmWindowManager *CTX_wm_manager(const bContext *C) +{ + return C->wm.manager; +} + +wmWindow *CTX_wm_window(const bContext *C) +{ + return ctx_wm_python_context_get(C, "window", C->wm.window); +} + +bScreen *CTX_wm_screen(const bContext *C) +{ + return ctx_wm_python_context_get(C, "screen", C->wm.screen); +} + +ScrArea *CTX_wm_area(const bContext *C) +{ + return ctx_wm_python_context_get(C, "area", C->wm.area); +} + +SpaceLink *CTX_wm_space_data(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + return (sa)? sa->spacedata.first: NULL; +} + +ARegion *CTX_wm_region(const bContext *C) +{ + return ctx_wm_python_context_get(C, "region", C->wm.region); +} + +void *CTX_wm_region_data(const bContext *C) +{ + ARegion *ar = CTX_wm_region(C); + return (ar)? ar->regiondata: NULL; +} + +struct ARegion *CTX_wm_menu(const bContext *C) +{ + return C->wm.menu; +} + +struct ReportList *CTX_wm_reports(const bContext *C) +{ + if (C->wm.manager) + return &(C->wm.manager->reports); + + return NULL; +} + +View3D *CTX_wm_view3d(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_VIEW3D) + return sa->spacedata.first; + return NULL; +} + +RegionView3D *CTX_wm_region_view3d(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_VIEW3D) + if (C->wm.region) + return C->wm.region->regiondata; + return NULL; +} + +struct SpaceText *CTX_wm_space_text(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_TEXT) + return sa->spacedata.first; + return NULL; +} + +struct SpaceConsole *CTX_wm_space_console(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_CONSOLE) + return sa->spacedata.first; + return NULL; +} + +struct SpaceImage *CTX_wm_space_image(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_IMAGE) + return sa->spacedata.first; + return NULL; +} + +struct SpaceButs *CTX_wm_space_buts(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_BUTS) + return sa->spacedata.first; + return NULL; +} + +struct SpaceFile *CTX_wm_space_file(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_FILE) + return sa->spacedata.first; + return NULL; +} + +struct SpaceSeq *CTX_wm_space_seq(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_SEQ) + return sa->spacedata.first; + return NULL; +} + +struct SpaceOops *CTX_wm_space_outliner(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_OUTLINER) + return sa->spacedata.first; + return NULL; +} + +struct SpaceNla *CTX_wm_space_nla(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_NLA) + return sa->spacedata.first; + return NULL; +} + +struct SpaceTime *CTX_wm_space_time(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_TIME) + return sa->spacedata.first; + return NULL; +} + +struct SpaceNode *CTX_wm_space_node(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_NODE) + return sa->spacedata.first; + return NULL; +} + +struct SpaceLogic *CTX_wm_space_logic(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_LOGIC) + return sa->spacedata.first; + return NULL; +} + +struct SpaceIpo *CTX_wm_space_graph(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_IPO) + return sa->spacedata.first; + return NULL; +} + +struct SpaceAction *CTX_wm_space_action(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_ACTION) + return sa->spacedata.first; + return NULL; +} + +struct SpaceInfo *CTX_wm_space_info(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_INFO) + return sa->spacedata.first; + return NULL; +} + +struct SpaceUserPref *CTX_wm_space_userpref(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_USERPREF) + return sa->spacedata.first; + return NULL; +} + +struct SpaceClip *CTX_wm_space_clip(const bContext *C) +{ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype==SPACE_CLIP) + return sa->spacedata.first; + return NULL; +} + +void CTX_wm_manager_set(bContext *C, wmWindowManager *wm) +{ + C->wm.manager= wm; + C->wm.window= NULL; + C->wm.screen= NULL; + C->wm.area= NULL; + C->wm.region= NULL; +} + +void CTX_wm_window_set(bContext *C, wmWindow *win) +{ + C->wm.window= win; + C->wm.screen= (win)? win->screen: NULL; + if (C->wm.screen) + C->data.scene= C->wm.screen->scene; + C->wm.area= NULL; + C->wm.region= NULL; +} + +void CTX_wm_screen_set(bContext *C, bScreen *screen) +{ + C->wm.screen= screen; + if (C->wm.screen) + C->data.scene= C->wm.screen->scene; + C->wm.area= NULL; + C->wm.region= NULL; +} + +void CTX_wm_area_set(bContext *C, ScrArea *area) +{ + C->wm.area= area; + C->wm.region= NULL; +} + +void CTX_wm_region_set(bContext *C, ARegion *region) +{ + C->wm.region= region; +} + +void CTX_wm_menu_set(bContext *C, ARegion *menu) +{ + C->wm.menu= menu; +} + +void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg) +{ + C->wm.operator_poll_msg= msg; +} + +const char *CTX_wm_operator_poll_msg_get(bContext *C) +{ + return C->wm.operator_poll_msg; +} + /* data context */ Main *CTX_data_main(const bContext *C) -- cgit v1.2.3