From 957604f8954fe369e1841bb922a102d632f0952c Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 18 Dec 2012 14:11:19 +0000 Subject: functions to make a string representation of a property & assignment. --- source/blender/windowmanager/WM_api.h | 3 ++ source/blender/windowmanager/intern/wm_operators.c | 45 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'source/blender') diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h index a4579ea31bc..d0fcad00e14 100644 --- a/source/blender/windowmanager/WM_api.h +++ b/source/blender/windowmanager/WM_api.h @@ -56,6 +56,7 @@ struct wmOperatorType; struct wmOperator; struct rcti; struct PointerRNA; +struct PropertyRNA; struct EnumPropertyItem; struct MenuType; struct wmDropBox; @@ -257,6 +258,8 @@ int WM_operator_last_properties_store(struct wmOperator *op); /* operator as a python command (resultuing string must be freed) */ char *WM_operator_pystring(struct bContext *C, struct wmOperatorType *ot, struct PointerRNA *opptr, int all_args); +char *WM_prop_pystring(struct PointerRNA *ptr, struct PropertyRNA *prop, int index); +char *WM_prop_pystring_assign(struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA *prop, int index); void WM_operator_bl_idname(char *to, const char *from); void WM_operator_py_idname(char *to, const char *from); diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c index b16eed9df2c..7316e494b88 100644 --- a/source/blender/windowmanager/intern/wm_operators.c +++ b/source/blender/windowmanager/intern/wm_operators.c @@ -560,6 +560,51 @@ char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, i return cstring; } +char *WM_prop_pystring(PointerRNA *ptr, PropertyRNA *prop, int index) +{ + char *id_path; + char *data_path = NULL; + + char *ret; + + if (!ptr->id.data) { + return NULL; + } + + + /* never fails */ + id_path = RNA_path_from_ID_python(ptr->id.data); + + data_path = RNA_path_from_ID_to_property(ptr, prop); + + if ((index == -1) || (RNA_property_array_check(prop) == FALSE)) { + ret = BLI_sprintfN("%s.%s", + id_path, data_path); + } + else { + ret = BLI_sprintfN("%s.%s[%d]", + id_path, data_path, index); + } + + return ret; +} + +char *WM_prop_pystring_assign(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index) +{ + char *lhs = WM_prop_pystring(ptr, prop, index); + char *rhs = RNA_property_as_string(C, ptr, prop, index); + char *ret; + + if (!lhs) { + return NULL; + } + + ret = BLI_sprintfN("%s = %s", lhs, rhs); + MEM_freeN(lhs); + MEM_freeN(rhs); + return ret; +} + void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot) { RNA_pointer_create(NULL, ot->srna, NULL, ptr); -- cgit v1.2.3