Python dir(context) now gives the items from the data context
too, modified context callbacks to also return a list of items
in the context.
This commit is contained in:
Brecht Van Lommel 2009-06-20 14:55:28 +00:00
parent 980dab9028
commit ad07fc19c0
9 changed files with 143 additions and 14 deletions

@ -126,12 +126,14 @@ void CTX_wm_menu_set(bContext *C, struct ARegion *menu);
/* Data Context
- note: listbases consist of LinkData items and must be
freed with BLI_freelistN! */
- listbases consist of CollectionPointerLink items and must be
freed with BLI_freelistN!
- the dir listbase consits of LinkData items */
PointerRNA CTX_data_pointer_get(bContext *C, const char *member);
ListBase CTX_data_collection_get(bContext *C, const char *member);
void CTX_data_get(bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb);
PointerRNA CTX_data_pointer_get(const bContext *C, const char *member);
ListBase CTX_data_collection_get(const bContext *C, const char *member);
ListBase CTX_data_dir_get(const bContext *C);
void CTX_data_get(const bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb);
void CTX_data_id_pointer_set(bContextDataResult *result, struct ID *id);
void CTX_data_pointer_set(bContextDataResult *result, struct ID *id, StructRNA *type, void *data);
@ -139,7 +141,10 @@ void CTX_data_pointer_set(bContextDataResult *result, struct ID *id, StructRNA *
void CTX_data_id_list_add(bContextDataResult *result, struct ID *id);
void CTX_data_list_add(bContextDataResult *result, struct ID *id, StructRNA *type, void *data);
void CTX_data_dir_set(bContextDataResult *result, const char **member);
int CTX_data_equals(const char *member, const char *str);
int CTX_data_dir(const char *member);
/*void CTX_data_pointer_set(bContextDataResult *result, void *data);
void CTX_data_list_add(bContextDataResult *result, void *data);*/

@ -280,6 +280,7 @@ void CTX_wm_menu_set(bContext *C, ARegion *menu)
struct bContextDataResult {
PointerRNA ptr;
ListBase list;
const char **dir;
};
static int ctx_data_get(bContext *C, const char *member, bContextDataResult *result)
@ -360,7 +361,7 @@ static int ctx_data_collection_get(const bContext *C, const char *member, ListBa
return 0;
}
PointerRNA CTX_data_pointer_get(bContext *C, const char *member)
PointerRNA CTX_data_pointer_get(const bContext *C, const char *member)
{
bContextDataResult result;
@ -375,7 +376,7 @@ PointerRNA CTX_data_pointer_get(bContext *C, const char *member)
}
ListBase CTX_data_collection_get(bContext *C, const char *member)
ListBase CTX_data_collection_get(const bContext *C, const char *member)
{
bContextDataResult result;
@ -389,7 +390,7 @@ ListBase CTX_data_collection_get(bContext *C, const char *member)
}
}
void CTX_data_get(bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb)
void CTX_data_get(const bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb)
{
bContextDataResult result;
@ -403,11 +404,75 @@ void CTX_data_get(bContext *C, const char *member, PointerRNA *r_ptr, ListBase *
}
}
static void data_dir_add(ListBase *lb, const char *member)
{
LinkData *link;
if(strcmp(member, "scene") == 0) /* exception */
return;
for(link=lb->first; link; link=link->next)
if(strcmp(link->data, member) == 0)
return;
link= MEM_callocN(sizeof(LinkData), "LinkData");
link->data= (void*)member;
BLI_addtail(lb, link);
}
ListBase CTX_data_dir_get(const bContext *C)
{
bContextDataResult result;
ListBase lb;
int a;
memset(&lb, 0, sizeof(lb));
if(C->wm.store) {
bContextStoreEntry *entry;
for(entry=C->wm.store->entries.first; entry; entry=entry->next)
data_dir_add(&lb, entry->name);
}
if(C->wm.region && C->wm.region->type && C->wm.region->type->context) {
memset(&result, 0, sizeof(result));
C->wm.region->type->context(C, "", &result);
if(result.dir)
for(a=0; result.dir[a]; a++)
data_dir_add(&lb, result.dir[a]);
}
if(C->wm.area && C->wm.area->type && C->wm.area->type->context) {
memset(&result, 0, sizeof(result));
C->wm.area->type->context(C, "", &result);
if(result.dir)
for(a=0; result.dir[a]; a++)
data_dir_add(&lb, result.dir[a]);
}
if(C->wm.screen && C->wm.screen->context) {
bContextDataCallback cb= C->wm.screen->context;
memset(&result, 0, sizeof(result));
cb(C, "", &result);
if(result.dir)
for(a=0; result.dir[a]; a++)
data_dir_add(&lb, result.dir[a]);
}
return lb;
}
int CTX_data_equals(const char *member, const char *str)
{
return (strcmp(member, str) == 0);
}
int CTX_data_dir(const char *member)
{
return (strcmp(member, "") == 0);
}
void CTX_data_id_pointer_set(bContextDataResult *result, ID *id)
{
RNA_id_pointer_create(id, &result->ptr);
@ -451,6 +516,11 @@ int ctx_data_list_count(const bContext *C, int (*func)(const bContext*, ListBase
return 0;
}
void CTX_data_dir_set(bContextDataResult *result, const char **dir)
{
result->dir= dir;
}
/* data context */
Main *CTX_data_main(const bContext *C)

@ -42,7 +42,15 @@ int ed_screen_context(const bContext *C, const char *member, bContextDataResult
Scene *scene= sc->scene;
Base *base;
if(CTX_data_equals(member, "scene")) {
if(CTX_data_dir(member)) {
static const char *dir[] = {
"scene", "selected_objects", "selected_bases", "active_base",
"active_object", "edit_object", NULL};
CTX_data_dir_set(result, dir);
return 1;
}
else if(CTX_data_equals(member, "scene")) {
CTX_data_id_pointer_set(result, &scene->id);
return 1;
}

@ -492,8 +492,17 @@ int buttons_context(const bContext *C, const char *member, bContextDataResult *r
return 0;
/* here we handle context, getting data from precomputed path */
if(CTX_data_dir(member)) {
static const char *dir[] = {
"world", "object", "meshe", "armature", "lattice", "curve",
"meta_ball", "lamp", "camera", "material", "material_slot",
"texture", "texture_slot", "bone", "edit_bone", "particle_system",
"cloth", "soft_body", "fluid", NULL};
if(CTX_data_equals(member, "world")) {
CTX_data_dir_set(result, dir);
return 1;
}
else if(CTX_data_equals(member, "world")) {
set_pointer_type(path, result, &RNA_World);
return 1;
}

@ -301,7 +301,11 @@ static int image_context(const bContext *C, const char *member, bContextDataResu
{
SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
if(CTX_data_equals(member, "edit_image")) {
if(CTX_data_dir(member)) {
static const char *dir[] = {"edit_image", NULL};
CTX_data_dir_set(result, dir);
}
else if(CTX_data_equals(member, "edit_image")) {
CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
return 1;
}

@ -283,7 +283,12 @@ static int node_context(const bContext *C, const char *member, bContextDataResul
{
SpaceNode *snode= (SpaceNode*)CTX_wm_space_data(C);
if(CTX_data_equals(member, "selected_nodes")) {
if(CTX_data_dir(member)) {
static const char *dir[] = {"selected_nodes", NULL};
CTX_data_dir_set(result, dir);
return 1;
}
else if(CTX_data_equals(member, "selected_nodes")) {
bNode *node;
for(next_node(snode->edittree); (node=next_node(NULL));) {

@ -293,7 +293,12 @@ static int text_context(const bContext *C, const char *member, bContextDataResul
{
SpaceText *st= CTX_wm_space_text(C);
if(CTX_data_equals(member, "edit_text")) {
if(CTX_data_dir(member)) {
static const char *dir[] = {"edit_text", NULL};
CTX_data_dir_set(result, dir);
return 1;
}
else if(CTX_data_equals(member, "edit_text")) {
CTX_data_id_pointer_set(result, &st->text->id);
return 1;
}

@ -565,7 +565,17 @@ static int view3d_context(const bContext *C, const char *member, bContextDataRes
if(v3d==NULL) return 0;
if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
if(CTX_data_dir(member)) {
static const char *dir[] = {
"selected_objects", "selected_bases" "selected_editable_objects",
"selected_editable_bases" "visible_objects", "visible_bases",
"active_base", "active_object", "visible_bones", "editable_bones",
"selected_bones", "selected_editable_bones" "visible_pchans",
"selected_pchans", "active_bone", "active_pchan", NULL};
CTX_data_dir_set(result, dir);
}
else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
int selected_objects= CTX_data_equals(member, "selected_objects");
for(base=scene->base.first; base; base=base->next) {

@ -875,6 +875,19 @@ static PyObject *pyrna_struct_dir(BPy_StructRNA * self)
RNA_property_collection_end(&iter);
}
if(self->ptr.type == &RNA_Context) {
ListBase lb = CTX_data_dir_get(self->ptr.data);
LinkData *link;
for(link=lb.first; link; link=link->next) {
pystring = PyUnicode_FromString(link->data);
PyList_Append(ret, pystring);
Py_DECREF(pystring);
}
BLI_freelistN(&lb);
}
return ret;
}