* Added RNA for operators. This still uses ID properties internally,
  but through the RNA API now. The OP_get/set_* API that was used is
  replaced by the RNA API. Currently RNA properties for operators are
  defined at runtime since it means operator registration can be done
  in a single function.
* Changed the existing operators to use this system, I haven't defined
  user interface names yet though. I also think there need to be some
  conventions on which properties to expose to make these operators
  usable in macros, for example if mouse coordinates should be stored
  or not.
* When using ID properties through defined RNA properties, it now
  checks that the ID property actually matches the RNA property and
  removes/overwrites it otherwise. This ensures that you can safely
  get/set arrays for example without having to worry that some
  external thing may have changed the length.
* Documentation now has some information on RNA + ID properties.

http://wiki.blender.org/index.php/BlenderDev/Blender2.5/RNA
This commit is contained in:
Brecht Van Lommel 2008-11-21 19:14:38 +00:00
parent 129585285c
commit c6da2a59d8
16 changed files with 177 additions and 107 deletions

@ -44,6 +44,7 @@ CPPFLAGS += -I../../blenloader
CPPFLAGS += -I../../blenkernel
CPPFLAGS += -I../../blenlib
CPPFLAGS += -I../../makesdna
CPPFLAGS += -I../../makesrna
CPPFLAGS += -I../../imbuf
CPPFLAGS += -I../../python
CPPFLAGS += -I$(NAN_GUARDEDALLOC)/include

@ -4,7 +4,7 @@ Import ('env')
sources = env.Glob('*.c')
incs = '../include ../../blenlib ../../blenkernel ../../makesdna ../../imbuf'
incs += ' ../../blenloader ../../windowmanager ../../python'
incs += ' ../../blenloader ../../windowmanager ../../python ../../makesrna'
incs += ' #/intern/guardedalloc #/extern/glew/include'
env.BlenderLib ( 'bf_editors_screen', sources, Split(incs), [], libtype=['core','intern'], priority=[30, 35] )

@ -42,6 +42,9 @@
#include "ED_screen.h"
#include "ED_screen_types.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "screen_intern.h" /* own module include */
/* ************** Exported Poll tests ********************** */
@ -463,11 +466,8 @@ static int area_move_init (bContext *C, wmOperator *op)
int x, y;
/* required properties */
if(!(OP_get_int(op, "x", &x) && OP_get_int(op, "y", &y)))
return 0;
/* default properties */
OP_verify_int(op, "delta", 0, NULL);
x= RNA_int_get(op->rna, "x");
y= RNA_int_get(op->rna, "y");
/* setup */
actedge= screen_find_active_scredge(C->screen, x, y);
@ -523,7 +523,7 @@ static void area_move_apply(bContext *C, wmOperator *op)
sAreaMoveData *md= op->customdata;
int delta;
OP_get_int(op, "delta", &delta);
delta= RNA_int_get(op->rna, "delta");
area_move_apply_do(C, md->origval, delta, md->dir, md->bigger, md->smaller);
}
@ -552,8 +552,8 @@ static int area_move_exec(bContext *C, wmOperator *op)
/* interaction callback */
static int area_move_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
OP_verify_int(op, "x", event->x, NULL);
OP_verify_int(op, "y", event->y, NULL);
RNA_int_default(op->rna, "x", event->x);
RNA_int_default(op->rna, "y", event->y);
if(!area_move_init(C, op))
return OPERATOR_PASS_THROUGH;
@ -568,7 +568,7 @@ static int area_move_cancel(bContext *C, wmOperator *op)
{
WM_event_remove_modal_handler(&C->window->handlers, op);
OP_set_int(op, "delta", 0);
RNA_int_set(op->rna, "delta", 0);
area_move_apply(C, op);
area_move_exit(C, op);
@ -583,14 +583,14 @@ static int area_move_modal(bContext *C, wmOperator *op, wmEvent *event)
md= op->customdata;
OP_get_int(op, "x", &x);
OP_get_int(op, "y", &y);
x= RNA_int_get(op->rna, "x");
y= RNA_int_get(op->rna, "y");
/* execute the events */
switch(event->type) {
case MOUSEMOVE:
delta= (md->dir == 'v')? event->x - x: event->y - y;
OP_set_int(op, "delta", delta);
RNA_int_set(op->rna, "delta", delta);
area_move_apply(C, op);
break;
@ -612,6 +612,8 @@ static int area_move_modal(bContext *C, wmOperator *op, wmEvent *event)
void ED_SCR_OT_area_move(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name= "Move area edges";
ot->idname= "ED_SCR_OT_area_move";
@ -622,6 +624,11 @@ void ED_SCR_OT_area_move(wmOperatorType *ot)
ot->modal= area_move_modal;
ot->poll= ED_operator_screen_mainwinactive; /* when mouse is over area-edge */
/* rna */
prop= RNA_def_property(ot->rna, "x", PROP_INT, PROP_NONE);
prop= RNA_def_property(ot->rna, "y", PROP_INT, PROP_NONE);
prop= RNA_def_property(ot->rna, "delta", PROP_INT, PROP_NONE);
}
/* ************** split area operator *********************************** */
@ -687,8 +694,7 @@ static int area_split_init(bContext *C, wmOperator *op)
if(C->area==NULL) return 0;
/* required properties */
OP_verify_float(op, "fac", 0.5f, NULL);
OP_verify_int(op, "dir", 'h', &dir);
dir= RNA_enum_get(op->rna, "dir");
/* minimal size */
if(dir=='v' && C->area->winx < 2*AREAMINX) return 0;
@ -740,8 +746,8 @@ static void area_split_apply(bContext *C, wmOperator *op)
float fac;
int dir;
OP_get_float(op, "fac", &fac);
OP_get_int(op, "dir", &dir);
fac= RNA_float_get(op->rna, "fac");
dir= RNA_enum_get(op->rna, "dir");
sd->narea= area_split(C->window, C->screen, sd->sarea, dir, fac);
@ -801,13 +807,13 @@ static int area_split_invoke(bContext *C, wmOperator *op, wmEvent *event)
/* prepare operator state vars */
if(sad->gesture_dir==AZONE_N || sad->gesture_dir==AZONE_S) {
dir= 'h';
OP_set_float(op, "fac", ((float)(event->x - sad->sa1->v1->vec.x)) / (float)sad->sa1->winx);
RNA_float_set(op->rna, "fac", ((float)(event->x - sad->sa1->v1->vec.x)) / (float)sad->sa1->winx);
}
else {
dir= 'v';
OP_set_float(op, "fac", ((float)(event->y - sad->sa1->v1->vec.y)) / (float)sad->sa1->winy);
RNA_float_set(op->rna, "fac", ((float)(event->y - sad->sa1->v1->vec.y)) / (float)sad->sa1->winy);
}
OP_set_int(op, "dir", dir);
RNA_enum_set(op->rna, "dir", dir);
/* general init, also non-UI case, adds customdata, sets area and defaults */
if(!area_split_init(C, op))
@ -870,8 +876,7 @@ static int area_split_modal(bContext *C, wmOperator *op, wmEvent *event)
/* execute the events */
switch(event->type) {
case MOUSEMOVE:
OP_get_int(op, "dir", &dir);
dir= RNA_enum_get(op->rna, "dir");
sd->delta= (dir == 'v')? event->x - sd->origval: event->y - sd->origval;
area_move_apply_do(C, sd->origval, sd->delta, dir, sd->bigger, sd->smaller);
@ -896,6 +901,9 @@ static int area_split_modal(bContext *C, wmOperator *op, wmEvent *event)
void ED_SCR_OT_area_split(wmOperatorType *ot)
{
PropertyRNA *prop;
static EnumPropertyItem prop_direction_items[] = {{'h', "HORIZONTAL", "Horizontal"}, {'v', "VERTICAL", "Vertical"}, {0, NULL, NULL}};
ot->name = "Split area";
ot->idname = "ED_SCR_OT_area_split";
@ -904,6 +912,15 @@ void ED_SCR_OT_area_split(wmOperatorType *ot)
ot->modal= area_split_modal;
ot->poll= ED_operator_screenactive; /* XXX should be area active */
/* rna */
prop= RNA_def_property(ot->rna, "dir", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_items(prop, prop_direction_items);
RNA_def_property_enum_default(prop, 'h');
prop= RNA_def_property(ot->rna, "fac", PROP_FLOAT, PROP_NONE);
RNA_def_property_range(prop, 0.0, 1.0);
RNA_def_property_float_default(prop, 0.5f);
}
/* ************** join area operator ********************************************** */
@ -956,10 +973,10 @@ static int area_join_init(bContext *C, wmOperator *op)
int x2, y2;
/* required properties, make negative to get return 0 if not set by caller */
OP_verify_int(op, "x1", -100, &x1);
OP_verify_int(op, "y1", -100, &y1);
OP_verify_int(op, "x2", -100, &x2);
OP_verify_int(op, "y2", -100, &y2);
x1= RNA_int_get(op->rna, "x1");
y1= RNA_int_get(op->rna, "y1");
x2= RNA_int_get(op->rna, "x2");
y2= RNA_int_get(op->rna, "y2");
sa1 = screen_areahascursor(C->screen, x1, y1);
sa2 = screen_areahascursor(C->screen, x2, y2);
@ -1035,10 +1052,10 @@ static int area_join_invoke(bContext *C, wmOperator *op, wmEvent *event)
return OPERATOR_PASS_THROUGH;
/* prepare operator state vars */
OP_set_int(op, "x1", sad->x);
OP_set_int(op, "y1", sad->y);
OP_set_int(op, "x2", event->x);
OP_set_int(op, "y2", event->y);
RNA_int_set(op->rna, "x1", sad->x);
RNA_int_set(op->rna, "y1", sad->y);
RNA_int_set(op->rna, "x2", event->x);
RNA_int_set(op->rna, "y2", event->y);
if(!area_join_init(C, op))
return OPERATOR_PASS_THROUGH;
@ -1163,6 +1180,8 @@ static int area_join_modal(bContext *C, wmOperator *op, wmEvent *event)
/* Operator for joining two areas (space types) */
void ED_SCR_OT_area_join(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name= "Join area";
ot->idname= "ED_SCR_OT_area_join";
@ -1173,6 +1192,16 @@ void ED_SCR_OT_area_join(wmOperatorType *ot)
ot->modal= area_join_modal;
ot->poll= ED_operator_screenactive;
/* rna */
prop= RNA_def_property(ot->rna, "x1", PROP_INT, PROP_NONE);
RNA_def_property_int_default(prop, -100);
prop= RNA_def_property(ot->rna, "y1", PROP_INT, PROP_NONE);
RNA_def_property_int_default(prop, -100);
prop= RNA_def_property(ot->rna, "x2", PROP_INT, PROP_NONE);
RNA_def_property_int_default(prop, -100);
prop= RNA_def_property(ot->rna, "y2", PROP_INT, PROP_NONE);
RNA_def_property_int_default(prop, -100);
}
/* ************** border select operator (test only) ***************************** */

@ -512,6 +512,9 @@ static SpaceLink *outliner_duplicate(SpaceLink *sl)
SpaceOops *soutliner= (SpaceOops *)sl;
SpaceOops *soutlinern= MEM_dupallocN(soutliner);
if(soutlinern->rnapath)
soutlinern->rnapath= MEM_dupallocN(soutlinern->rnapath);
return (SpaceLink *)soutlinern;
}

@ -44,6 +44,7 @@ CPPFLAGS += -I../../blenloader
CPPFLAGS += -I../../blenkernel
CPPFLAGS += -I../../blenlib
CPPFLAGS += -I../../makesdna
CPPFLAGS += -I../../makesrna
CPPFLAGS += -I../../imbuf
CPPFLAGS += -I../../python
CPPFLAGS += -I$(NAN_GUARDEDALLOC)/include

@ -4,6 +4,7 @@ Import ('env')
sources = env.Glob('*.c')
incs = '../include ../../blenlib ../../blenkernel ../../makesdna ../../imbuf'
incs += '../../windowmanager #/intern/guardedalloc #/extern/glew/include'
incs += ' ../../windowmanager #/intern/guardedalloc #/extern/glew/include'
incs += ' ../../makesrna'
env.BlenderLib ( 'bf_editors_space_time', sources, Split(incs), [], libtype=['core','intern'], priority=[35, 40] )

@ -42,6 +42,9 @@
#include "UI_interface.h"
#include "UI_view2d.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "WM_api.h"
#include "WM_types.h"
@ -50,10 +53,6 @@
static int change_frame_init(bContext *C, wmOperator *op)
{
SpaceTime *stime= C->area->spacedata.first;
int cfra;
if(!OP_get_int(op, "frame", &cfra))
return 0;
stime->flag |= TIME_CFRA_NUM;
@ -64,7 +63,7 @@ static void change_frame_apply(bContext *C, wmOperator *op)
{
int cfra;
OP_get_int(op, "frame", &cfra);
cfra= RNA_int_get(op->rna, "frame");
if(cfra < MINFRAME)
cfra= MINFRAME;
@ -120,7 +119,7 @@ static int frame_from_event(bContext *C, wmEvent *event)
static int change_frame_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
OP_verify_int(op, "frame", frame_from_event(C, event), NULL);
RNA_int_default(op->rna, "frame", frame_from_event(C, event));
change_frame_init(C, op);
change_frame_apply(C, op);
@ -141,7 +140,7 @@ static int change_frame_modal(bContext *C, wmOperator *op, wmEvent *event)
/* execute the events */
switch(event->type) {
case MOUSEMOVE:
OP_set_int(op, "frame", frame_from_event(C, event));
RNA_int_set(op->rna, "frame", frame_from_event(C, event));
change_frame_apply(C, op);
break;
@ -159,6 +158,8 @@ static int change_frame_modal(bContext *C, wmOperator *op, wmEvent *event)
void ED_TIME_OT_change_frame(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name= "Change frame";
ot->idname= "ED_TIME_OT_change_frame";
@ -168,6 +169,9 @@ void ED_TIME_OT_change_frame(wmOperatorType *ot)
ot->invoke= change_frame_invoke;
ot->cancel= change_frame_cancel;
ot->modal= change_frame_modal;
/* rna */
prop= RNA_def_property(ot->rna, "frame", PROP_INT, PROP_NONE);
}
/* ************************** registration **********************************/

@ -47,6 +47,8 @@ struct wmLocal;
struct bScreen;
struct uiBlock;
struct wmSubWindow;
struct StructRNA;
struct PointerRNA;
/* windowmanager is saved, tag WMAN */
typedef struct wmWindowManager {
@ -132,8 +134,8 @@ typedef struct wmOperatorType {
/* panel for redo and repeat */
void *(*uiBlock)(struct wmOperator *);
char *customname; /* dna name */
void *customdata; /* defaults */
/* rna for properties */
struct StructRNA *rna;
short flag;
@ -170,6 +172,7 @@ typedef struct wmOperator {
IDProperty *properties;
/* runtime */
struct PointerRNA *rna;
ListBase *modallist;
} wmOperator;

@ -31,6 +31,7 @@
#include "BLI_dynstr.h"
#include "BKE_idprop.h"
#include "BKE_utildefines.h"
#include "DNA_ID.h"
#include "DNA_windowmanager_types.h"
@ -107,6 +108,48 @@ static IDProperty *rna_idproperty_find(PointerRNA *ptr, const char *name)
return NULL;
}
static int rna_idproperty_verify_valid(PropertyRNA *prop, IDProperty *idprop)
{
/* this verifies if the idproperty actually matches the property
* description and otherwise removes it. this is to ensure that
* rna property access is type safe, e.g. if you defined the rna
* to have a certain array length you can count on that staying so */
switch(idprop->type) {
case IDP_ARRAY:
if(prop->arraylength != idprop->len)
return 0;
if(idprop->subtype == IDP_FLOAT && prop->type != PROP_FLOAT)
return 0;
if(idprop->subtype == IDP_INT && !ELEM3(prop->type, PROP_BOOLEAN, PROP_INT, PROP_ENUM))
return 0;
break;
case IDP_INT:
if(!ELEM3(prop->type, PROP_BOOLEAN, PROP_INT, PROP_ENUM))
return 0;
break;
case IDP_FLOAT:
case IDP_DOUBLE:
if(prop->type != PROP_FLOAT)
return 0;
break;
case IDP_STRING:
if(prop->type != PROP_STRING)
return 0;
break;
case IDP_GROUP:
if(prop->type != PROP_POINTER)
return 0;
break;
default:
return 0;
}
return 1;
}
IDProperty *rna_idproperty_check(PropertyRNA **prop, PointerRNA *ptr)
{
/* This is quite a hack, but avoids some complexity in the API. we
@ -117,8 +160,20 @@ IDProperty *rna_idproperty_check(PropertyRNA **prop, PointerRNA *ptr)
* pointer to the IDProperty. */
if((*prop)->magic == RNA_MAGIC) {
if((*prop)->flag & PROP_IDPROPERTY)
return rna_idproperty_find(ptr, (*prop)->identifier);
if((*prop)->flag & PROP_IDPROPERTY) {
IDProperty *idprop= rna_idproperty_find(ptr, (*prop)->identifier);
if(idprop && !rna_idproperty_verify_valid(*prop, idprop)) {
IDProperty *group= rna_idproperties_get(ptr->type, ptr->data, 0);
IDP_RemFromGroup(group, idprop);
IDP_FreeProperty(idprop);
MEM_freeN(idprop);
return NULL;
}
return idprop;
}
else
return NULL;
}

@ -9,6 +9,7 @@ sources = env.Glob('intern/*.c')
incs = '. ../editors/include ../python ../makesdna ../blenlib ../blenkernel'
incs += ' ../nodes ../imbuf ../blenloader ../render/extern/include'
incs += ' ../ftfont ../radiosity/extern/include ../../kernel/gen_system'
incs += ' ../makesrna'
incs += ' #/intern/guardedalloc #/intern/memutil #/intern/ghost #/intern/bmfont'
incs += ' #/intern/elbeem #/extern/glew/include'

@ -103,66 +103,6 @@ void WM_operator_cancel (struct bContext *C, ListBase *modalops, wmOperatorTyp
int WM_border_select_invoke (struct bContext *C, wmOperator *op, struct wmEvent *event);
int WM_border_select_modal (struct bContext *C, wmOperator *op, struct wmEvent *event);
/*
* Operator property api
*
* Some notes to take care:
*
* All the OP_set_* functions append a new property to the operator,
* if the property already exist, just replace it with the new
* value in other case make a new property and append it.
*
* The OP_get_string function is a "special case", this function
* return a pointer to property data, so don't change/resize/free
* the string, because probably we get a segfault.
* I really think that is better duplicate the string, so we are
* really sure that the property data don't change.
*
* OP_get_int/float/array return 1 on success (found the property)
* or 0 if can't find the property in the operator.
* The property value are store in the "value" pointer.
*
* OP_verify_* sets the value only if it wasn't set already, and
* returns the existing or new value.
*
* Both array function copy the property data into the "array"
* pointer, but you need init the len pointer to the "array" size.
*
* For example:
* int vec[] = { 1, 2, 3, 4 };
* OP_set_int_array (op, "vector", vec, 4);
*
* ...
*
* short len;
* int vec[4];
* len= 4; <---- set the size!!
* OP_get_int_array (op, "vector", vec, &len);
*/
void OP_set_int (wmOperator *op, char *name, int value);
void OP_set_float (wmOperator *op, char *name, float value);
void OP_set_string (wmOperator *op, char *name, char *str);
void OP_set_int_array(wmOperator *op, char *name, int *array, short len);
void OP_set_float_array(wmOperator *op, char *name, float *array, short len);
int OP_get_int (wmOperator *op, char *name, int *value);
int OP_get_float (wmOperator *op, char *name, float *value);
char *OP_get_string (wmOperator *op, char *name);
int OP_get_int_array(wmOperator *op, char *name, int *array, short *len);
int OP_get_float_array(wmOperator *op, char *name, float *array, short *len);
void OP_verify_int (wmOperator *op, char *name, int value, int *result);
void OP_verify_float (wmOperator *op, char *name, float value, int *result);
char *OP_verify_string(wmOperator *op, char *name, char *str);
void OP_verify_int_array(wmOperator *op, char *name, int *array, short len, int *resultarray, short *resultlen);
void OP_verify_float_array(wmOperator *op, char *name, float *array, short len, float *resultarray, short *resultlen);
/*
* Need call this function in the "exit callback"
* of the operator, but only if you use the property system.
**/
void OP_free_property(wmOperator *op);
/* Gesture manager API */
struct wmGesture *WM_gesture_new(struct bContext *C, struct wmEvent *event, int type);
void WM_gesture_end(struct bContext *C, struct wmGesture *gesture);

@ -51,6 +51,7 @@ CPPFLAGS += -I$(NAN_SOUNDSYSTEM)/include $(NAN_SDLCFLAGS)
CPPFLAGS += -I../../editors/include
CPPFLAGS += -I../../python
CPPFLAGS += -I../../makesdna
CPPFLAGS += -I../../makesrna
CPPFLAGS += -I../../blenlib
CPPFLAGS += -I../../blenkernel
CPPFLAGS += -I../../nodes

@ -53,7 +53,18 @@
void wm_operator_free(wmOperator *op)
{
OP_free_property(op);
if(op->properties) {
IDP_FreeProperty(op->properties);
/* This need change, when the idprop code only
* need call IDP_FreeProperty. (check BKE_idprop.h) */
MEM_freeN(op->properties);
op->properties= NULL;
}
if(op->rna)
MEM_freeN(op->rna);
MEM_freeN(op);
}
@ -63,6 +74,11 @@ void wm_operator_register(wmWindowManager *wm, wmOperator *op)
{
int tot;
if(op->rna) {
MEM_freeN(op->rna);
op->rna= NULL;
}
BLI_addtail(&wm->operators, op);
tot= BLI_countlist(&wm->operators);

@ -46,6 +46,8 @@
#include "ED_screen.h"
#include "ED_area.h"
#include "RNA_access.h"
#include "WM_api.h"
#include "WM_types.h"
#include "wm.h"
@ -290,6 +292,10 @@ int WM_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event)
op->type= ot;
op->rna= MEM_callocN(sizeof(PointerRNA), "wmOperatorPtrRNA");
op->rna->type= op->type->rna;
op->rna->data= op;
if(op->type->invoke)
retval= (*op->type->invoke)(C, op, event);
else if(op->type->exec)

@ -70,6 +70,8 @@
#include "UI_interface.h"
#include "RNA_define.h"
#include "WM_api.h"
#include "WM_types.h"
#include "wm.h"
@ -238,6 +240,8 @@ void WM_exit(bContext *C)
BLI_freelistN(&U.themes);
// XXX BIF_preview_free_dbase();
RNA_exit();
MEM_freeN(C);
if(MEM_get_memory_blocks_in_use()!=0) {

@ -42,6 +42,9 @@
#include "BKE_main.h"
#include "BKE_idprop.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "WM_api.h"
#include "WM_types.h"
@ -70,7 +73,9 @@ void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
wmOperatorType *ot;
ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
ot->rna= RNA_def_struct(&BLENDER_RNA, "", "Operator", "");
opfunc(ot);
RNA_def_struct_identifier(ot->rna, ot->idname, ot->name);
BLI_addtail(&global_ops, ot);
}
@ -144,10 +149,10 @@ static void border_select_apply(bContext *C, wmOperator *op)
rcti *rect= gesture->customdata;
/* operator arguments and storage. */
OP_verify_int(op, "xmin", rect->xmin, NULL);
OP_verify_int(op, "ymin", rect->ymin, NULL);
OP_verify_int(op, "xmax", rect->xmax, NULL);
OP_verify_int(op, "ymax", rect->ymax, NULL);
RNA_int_default(op->rna, "xmin", rect->xmin);
RNA_int_default(op->rna, "ymin", rect->ymin);
RNA_int_default(op->rna, "xmax", rect->xmax);
RNA_int_default(op->rna, "ymax", rect->ymax);
op->type->exec(C, op);
}