BPython:
- Made Blender.event var (previously only used by script links) hold ascii value -- where it applies -- of current event during events callback registered with Draw.Register(gui, events, button_events). Useful for gui scripts like Campbell's Python console. No problem using this var to hold the value, since in gui scripts it was not used (always None). - Updated Window and Window.Theme with new theme vars and the Time space. - Script links: -- Added "Render" event for script links (runs twice, second time as "PostEvent", for clean-up actions). Now FrameChanged links don't run when a single pic is rendered. -- Added "Enable Script Links" button in the script buttons tab. Now this bit gets saved in .blends along with the rest of G.f, so users can define per .blend if they are on or off by default. "blender -y" also disables all slinks as happened before with OnLoad ones only. -- Other small changes in the script buttons tab: When a link is added (button "new"), it becomes the active one for the window, no need to press a button to reach it. Also, a pupmenu showing all available texts is shown when "new" is pressed, so users can choose a text w/o having to type. Cancel the popup to leave the string button empty (link exists, but has no script assigned). A pulldown would be better UI-wise, but it's kinda weird to show both scripts and normal texts (Blender doesn't differentiate them) in a script links pulldown. With a popup we can show only texts ending in ".py" (not done in this commit, need opinions) and if the script has no or another extension, case of many in old and current .blend's, there's still the string box for writing its name. -- Implemented Ton's space handler script links: Right now only for the 3d View, but it's trivial to add for others. There are two types: EVENT, to receive 3d View events from a chosen window and DRAW, to draw on the window. Ton's idea was to give scripts a controlled way to integrate better within Blender. Here's how it works: - scripts must have a proper header, like: # SPACEHANDLER.VIEW3D.EVENT and then they are shown in 3d View's View menu, "Space Handler Scripts" submenu. Check (mark, click on it) a script to make it active. EVENT handlers should consult the Blender.event var to get the current event, which can be compared with values from the Draw module: import Blender from Blender import Draw evt = Blender.event if evt == Draw.AKEY: print "a" elif evt == Draw.LEFTMOUSE: print "left mouse button" else: return # ignore, pass event back to Blender Blender.event = None # tell Blender not to process itself the event DRAW handlers are free to draw to their owner 3D View. OpenGL attributes and modelview and projection matrices are pushed before running the handler and poped when it finishes. To communicate between EVENT and DRAW handler scripts we have the Blender.Registry module, as always. Still need to code some nice example, which should also serve to test properly space handlers. Simple tests went fine. - doc updates about the additions. ======= Note: the UI part of the space handlers and script links is of course open for changes, I just tried to make it understandable. Probably we won't use the scriptlinks icon for "None Available" (check 3d View -> View -> Space Handler Scripts), though it hints at what space handlers are. The tooltips may not be accepted either, since other menus don't use them. Opinions welcomed.
This commit is contained in:
@@ -43,6 +43,7 @@
|
||||
#include <MEM_guardedalloc.h>
|
||||
#include <BLI_blenlib.h> /* for BLI_last_slash() */
|
||||
|
||||
#include "BIF_gl.h" /* glPushAttrib, glPopAttrib for DRAW space handlers */
|
||||
#include <BIF_interface.h> /* for pupmenu() */
|
||||
#include <BIF_space.h>
|
||||
#include <BIF_screen.h>
|
||||
@@ -1062,6 +1063,8 @@ void BPY_do_pyscript( ID * id, short event )
|
||||
PyString_FromString( event_to_name
|
||||
( event ) ) );
|
||||
|
||||
if (event == SCRIPT_POSTRENDER) event = SCRIPT_RENDER;
|
||||
|
||||
for( index = 0; index < scriptlink->totscript; index++ ) {
|
||||
if( ( scriptlink->flag[index] == event ) &&
|
||||
( scriptlink->scripts[index] != NULL ) ) {
|
||||
@@ -1103,6 +1106,249 @@ void BPY_do_pyscript( ID * id, short event )
|
||||
}
|
||||
}
|
||||
|
||||
/* SPACE HANDLERS */
|
||||
|
||||
/* These are special script links that can be assigned to ScrArea's to
|
||||
* (EVENT type) receive events sent to a given space (and use or ignore them) or
|
||||
* (DRAW type) be called after the space is drawn, to draw anything on top of
|
||||
* the space area. */
|
||||
|
||||
/* How to add space handlers to other spaces:
|
||||
* - add the space event defines to DNA_scriptlink_types.h, as done for
|
||||
* 3d view: SPACEHANDLER_VIEW3D_EVENT, for example;
|
||||
* - add the new defines to Blender.SpaceHandler dictionary in Blender.c;
|
||||
* - check space.c for how to call the event handlers;
|
||||
* - check drawview.c for how to call the draw handlers;
|
||||
* - check header_view3d.c for how to add the "Space Handler Scripts" menu.
|
||||
* Note: DRAW handlers should be called with 'event = 0', chech drawview.c */
|
||||
|
||||
int BPY_has_spacehandler(Text *text, ScrArea *sa)
|
||||
{
|
||||
ScriptLink *slink;
|
||||
int index;
|
||||
|
||||
if (!sa || !text) return 0;
|
||||
|
||||
slink = &sa->scriptlink;
|
||||
|
||||
for (index = 0; index < slink->totscript; index++) {
|
||||
if (slink->scripts[index] && (slink->scripts[index] == (ID *)text))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BPY_is_spacehandler(Text *text, char spacetype)
|
||||
{
|
||||
TextLine *tline = text->lines.first;
|
||||
unsigned short type = 0;
|
||||
|
||||
if (tline && (tline->len > 10)) {
|
||||
char *line = tline->line;
|
||||
|
||||
/* Expected format: # SPACEHANDLER.SPACE.TYPE
|
||||
* Ex: # SPACEHANDLER.VIEW3D.DRAW
|
||||
* The actual checks are forgiving, so slight variations also work. */
|
||||
if (line && line[0] == '#' && strstr(line, "HANDLER")) {
|
||||
line++; /* skip '#' */
|
||||
|
||||
/* only done for 3D View right now, trivial to add for others: */
|
||||
switch (spacetype) {
|
||||
case SPACE_VIEW3D:
|
||||
if (strstr(line, "3D")) { /* VIEW3D, 3DVIEW */
|
||||
if (strstr(line, "DRAW")) type = SPACEHANDLER_VIEW3D_DRAW;
|
||||
else if (strstr(line, "EVENT")) type = SPACEHANDLER_VIEW3D_EVENT;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return type; /* 0 if not a space handler */
|
||||
}
|
||||
|
||||
int BPY_del_spacehandler(Text *text, ScrArea *sa)
|
||||
{
|
||||
ScriptLink *slink;
|
||||
int i, j;
|
||||
|
||||
if (!sa || !text) return -1;
|
||||
|
||||
slink = &sa->scriptlink;
|
||||
if (slink->totscript < 1) return -1;
|
||||
|
||||
for (i = 0; i < slink->totscript; i++) {
|
||||
if (text == (Text *)slink->scripts[i]) {
|
||||
|
||||
for (j = i; j < slink->totscript - 1; j++) {
|
||||
slink->flag[j] = slink->flag[j+1];
|
||||
slink->scripts[j] = slink->scripts[j+1];
|
||||
}
|
||||
slink->totscript--;
|
||||
/* like done in buttons_script.c we just free memory
|
||||
* if all slinks have been removed -- less fragmentation,
|
||||
* these should be quite small arrays */
|
||||
if (slink->totscript == 0) {
|
||||
if (slink->scripts) MEM_freeN(slink->scripts);
|
||||
if (slink->flag) MEM_freeN(slink->flag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BPY_add_spacehandler(Text *text, ScrArea *sa, char spacetype)
|
||||
{
|
||||
unsigned short handlertype;
|
||||
|
||||
if (!sa || !text) return -1;
|
||||
|
||||
handlertype = BPY_is_spacehandler(text, spacetype);
|
||||
|
||||
if (handlertype) {
|
||||
ScriptLink *slink = &sa->scriptlink;
|
||||
void *stmp, *ftmp;
|
||||
unsigned short space_event = SPACEHANDLER_VIEW3D_EVENT;
|
||||
|
||||
/* extend slink */
|
||||
|
||||
stmp= slink->scripts;
|
||||
slink->scripts= MEM_mallocN(sizeof(ID*)*(slink->totscript+1),
|
||||
"spacehandlerscripts");
|
||||
|
||||
ftmp= slink->flag;
|
||||
slink->flag= MEM_mallocN(sizeof(short*)*(slink->totscript+1),
|
||||
"spacehandlerflags");
|
||||
|
||||
if (slink->totscript) {
|
||||
memcpy(slink->scripts, stmp, sizeof(ID*)*(slink->totscript));
|
||||
MEM_freeN(stmp);
|
||||
|
||||
memcpy(slink->flag, ftmp, sizeof(short)*(slink->totscript));
|
||||
MEM_freeN(ftmp);
|
||||
}
|
||||
|
||||
switch (spacetype) {
|
||||
case SPACE_VIEW3D:
|
||||
if (handlertype == 1) space_event = SPACEHANDLER_VIEW3D_EVENT;
|
||||
else space_event = SPACEHANDLER_VIEW3D_DRAW;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
slink->scripts[slink->totscript] = (ID *)text;
|
||||
slink->flag[slink->totscript]= space_event;
|
||||
|
||||
slink->totscript++;
|
||||
slink->actscript = slink->totscript;
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BPY_do_spacehandlers( ScrArea *sa, unsigned short event,
|
||||
unsigned short space_event )
|
||||
{
|
||||
ScriptLink *scriptlink;
|
||||
int retval = 0;
|
||||
|
||||
if (!sa) return 0;
|
||||
|
||||
scriptlink = &sa->scriptlink;
|
||||
|
||||
if (scriptlink->totscript > 0) {
|
||||
PyObject *dict;
|
||||
PyObject *ret;
|
||||
int index, during_slink = during_scriptlink();
|
||||
|
||||
/* invalid scriptlinks (new .blend was just loaded), return */
|
||||
if (during_slink < 0) return 0;
|
||||
|
||||
/* tell we're running a scriptlink. The sum also tells if this script
|
||||
* is running nested inside another. Blender.Load needs this info to
|
||||
* avoid trouble with invalid slink pointers. */
|
||||
during_slink++;
|
||||
disable_where_scriptlink( during_slink );
|
||||
|
||||
/* set globals in Blender module to identify space handler scriptlink */
|
||||
PyDict_SetItemString(g_blenderdict, "bylink", EXPP_incr_ret_True());
|
||||
/* unlike normal scriptlinks, here Blender.link is int (space event type) */
|
||||
PyDict_SetItemString(g_blenderdict, "link", PyInt_FromLong(space_event));
|
||||
/* note: DRAW space_events set event to 0 */
|
||||
PyDict_SetItemString(g_blenderdict, "event", PyInt_FromLong(event));
|
||||
|
||||
/* now run all assigned space handlers for this space and space_event */
|
||||
for( index = 0; index < scriptlink->totscript; index++ ) {
|
||||
|
||||
/* for DRAW handlers: */
|
||||
if (event == 0) {
|
||||
glPushAttrib(GL_ALL_ATTRIB_BITS);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
}
|
||||
|
||||
if( ( scriptlink->flag[index] == space_event ) &&
|
||||
( scriptlink->scripts[index] != NULL ) ) {
|
||||
dict = CreateGlobalDictionary();
|
||||
ret = RunPython( ( Text * ) scriptlink->scripts[index], dict );
|
||||
ReleaseGlobalDictionary( dict );
|
||||
|
||||
if (!ret) { /* Failed execution of the script */
|
||||
BPY_Err_Handle( scriptlink->scripts[index]->name+2 );
|
||||
} else {
|
||||
Py_DECREF(ret);
|
||||
|
||||
/* an EVENT type (event != 0) script can either accept an event or
|
||||
* ignore it:
|
||||
* if the script sets Blender.event to None it accepted it;
|
||||
* otherwise the space's event handling callback that called us
|
||||
* can go on processing the event */
|
||||
if (event && (PyDict_GetItemString(g_blenderdict,"event") == Py_None))
|
||||
retval = 1; /* event was swallowed */
|
||||
}
|
||||
|
||||
/* If a scriptlink has just loaded a new .blend file, the
|
||||
* scriptlink pointer became invalid (see api2_2x/Blender.c),
|
||||
* so we stop here. */
|
||||
if( during_scriptlink( ) == -1 ) {
|
||||
during_slink = 1;
|
||||
if (event == 0) glPopAttrib();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* for DRAW handlers: */
|
||||
if (event == 0) {
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPopMatrix();
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPopMatrix();
|
||||
glPopAttrib();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
disable_where_scriptlink( during_slink - 1 );
|
||||
|
||||
PyDict_SetItemString(g_blenderdict, "bylink", EXPP_incr_ret_False());
|
||||
PyDict_SetItemString(g_blenderdict, "link", EXPP_incr_ret(Py_None));
|
||||
PyDict_SetItemString(g_blenderdict, "event", PyString_FromString(""));
|
||||
}
|
||||
|
||||
/* retval:
|
||||
* space_event is of type EVENT:
|
||||
* 0 - event was returned,
|
||||
* 1 - event was processed;
|
||||
* space_event is of type DRAW:
|
||||
* 0 always */
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Description:
|
||||
* Notes:
|
||||
@@ -1119,6 +1365,31 @@ void BPY_free_scriptlink( struct ScriptLink *slink )
|
||||
return;
|
||||
}
|
||||
|
||||
void BPY_free_screen_spacehandlers(struct bScreen *sc)
|
||||
{
|
||||
ScrArea *sa;
|
||||
|
||||
for (sa = sc->areabase.first; sa; sa = sa->next)
|
||||
BPY_free_scriptlink(&sa->scriptlink);
|
||||
}
|
||||
|
||||
static int CheckAllSpaceHandlers(Text *text)
|
||||
{
|
||||
bScreen *screen;
|
||||
ScrArea *sa;
|
||||
ScriptLink *slink;
|
||||
int fixed = 0;
|
||||
|
||||
for (screen = G.main->screen.first; screen; screen = screen->id.next) {
|
||||
for (sa = screen->areabase.first; sa; sa = sa->next) {
|
||||
slink = &sa->scriptlink;
|
||||
if (!slink->totscript) continue;
|
||||
if (BPY_del_spacehandler(text, sa) == 0) fixed++;
|
||||
}
|
||||
}
|
||||
return fixed;
|
||||
}
|
||||
|
||||
static int CheckAllScriptsFromList( ListBase * list, Text * text )
|
||||
{
|
||||
ID *id;
|
||||
@@ -1154,11 +1425,11 @@ int BPY_check_all_scriptlinks( Text * text )
|
||||
fixed += CheckAllScriptsFromList( &( G.main->mat ), text );
|
||||
fixed += CheckAllScriptsFromList( &( G.main->world ), text );
|
||||
fixed += CheckAllScriptsFromList( &( G.main->scene ), text );
|
||||
fixed += CheckAllSpaceHandlers(text);
|
||||
|
||||
return fixed;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Description:
|
||||
* Notes:
|
||||
|
||||
Reference in New Issue
Block a user