use 'bpy.context' when printing properties in the info window.

This commit is contained in:
Campbell Barton
2012-12-18 16:20:30 +00:00
parent ae15de6816
commit 54bab21ca0
3 changed files with 122 additions and 1 deletions

View File

@@ -867,6 +867,8 @@ char *RNA_path_from_ID_to_property(PointerRNA *ptr, PropertyRNA *prop);
char *RNA_path_full_ID_py(struct ID *id);
char *RNA_path_full_struct_py(struct PointerRNA *ptr);
char *RNA_path_full_property_py(struct PointerRNA *ptr, struct PropertyRNA *prop, int index);
char *RNA_path_struct_property_py(struct PointerRNA *ptr, struct PropertyRNA *prop, int index);
char *RNA_path_property_py(struct PointerRNA *ptr, struct PropertyRNA *prop, int index);
/* Quick name based property access
*

View File

@@ -4233,6 +4233,54 @@ char *RNA_path_full_property_py(PointerRNA *ptr, PropertyRNA *prop, int index)
return ret;
}
/**
* Get the struct.property as a python representation, eg:
* some_struct.some_prop[10]
*/
char *RNA_path_struct_property_py(PointerRNA *ptr, PropertyRNA *prop, int index)
{
char *data_path;
char *ret;
if (!ptr->id.data) {
return NULL;
}
data_path = RNA_path_from_ID_to_property(ptr, prop);
if ((index == -1) || (RNA_property_array_check(prop) == FALSE)) {
ret = BLI_sprintfN("%s",
data_path);
}
else {
ret = BLI_sprintfN("%s[%d]",
data_path, index);
}
return ret;
}
/**
* Get the struct.property as a python representation, eg:
* some_prop[10]
*/
char *RNA_path_property_py(PointerRNA *UNUSED(ptr), PropertyRNA *prop, int index)
{
char *ret;
if ((index == -1) || (RNA_property_array_check(prop) == FALSE)) {
ret = BLI_sprintfN("%s",
RNA_property_identifier(prop));
}
else {
ret = BLI_sprintfN("%s[%d]",
RNA_property_identifier(prop), index);
}
return ret;
}
/* Quick name based property access */
int RNA_boolean_get(PointerRNA *ptr, const char *name)

View File

@@ -560,11 +560,82 @@ char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, i
return cstring;
}
/* return NULL if no match is found */
static char *wm_prop_pystring_from_context(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index)
{
/* loop over all context items and do 2 checks
*
* - see if the pointer is in the context.
* - see if the pointers ID is in the context.
*/
ListBase lb = CTX_data_dir_get(C);
LinkData *link;
const char *member_found = NULL;
const char *member_id = NULL;
char *prop_str = NULL;
char *ret = NULL;
for (link = lb.first; link; link = link->next) {
const char *identifier = link->data;
PointerRNA ctx_ptr = CTX_data_pointer_get(C, identifier);
if (ptr->id.data == ctx_ptr.id.data) {
if ((ptr->data == ctx_ptr.data) &&
(ptr->type == ctx_ptr.type))
{
/* found! */
member_found = identifier;
break;
}
else if (RNA_struct_is_ID(ctx_ptr.type)) {
/* we found a reference to this ID,
* so fallback to it if there is no direct reference */
member_id = identifier;
}
}
}
/* grr, CTX_data_dir_get skips scene */
if ((member_id == NULL) &&
(ptr->id.data != NULL) &&
(GS(((ID *)ptr->id.data)->name) == ID_SCE) &&
(CTX_data_scene(C) == ptr->id.data))
{
member_id = "scene";
}
if (member_found) {
prop_str = RNA_path_property_py(ptr, prop, index);
ret = BLI_sprintfN("bpy.context.%s.%s", member_found, prop_str);
MEM_freeN(prop_str);
}
else if (member_id) {
prop_str = RNA_path_struct_property_py(ptr, prop, index);
ret = BLI_sprintfN("bpy.context.%s.%s", member_id, prop_str);
MEM_freeN(prop_str);
}
BLI_freelistN(&lb);
return ret;
}
char *WM_prop_pystring_assign(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index)
{
char *lhs, *rhs, *ret;
lhs = RNA_path_full_property_py(ptr, prop, index);
lhs = C ? wm_prop_pystring_from_context(C, ptr, prop, index) : NULL;
if (lhs == NULL) {
/* fallback to bpy.data.foo[id] if we dont find in the context */
lhs = RNA_path_full_property_py(ptr, prop, index);
}
if (!lhs) {
return NULL;
}