Support tagging operator properties as 'advanced'

This will later be used to show advanced operator properties separate from
basic (as in non-advanced) ones in the UI.

Tagging a single operator property in C should be done via
`WM_operatortype_prop_tag()`. It does additional checks for type safety
that `RNA_def_property_tags()` doesn't do.

To avoid having to tag each advanced property individually, multiple
ones can be tagged by wrapping them into
`WM_operatortype_props_advanced_bein()` and
`WM_operatortype_props_advanced_end()` calls. It's also possible to only
call `_begin()`, all properties added after this will get tagged then.
In most cases this last approach should be sufficient.

Example of Python usage:
`my_float = bpy.props.FloatProperty(name="Some Float", tags={'ADVANCED'})`
This commit is contained in:
Julian Eisel
2017-11-23 13:58:05 +01:00
parent 23d148ecaf
commit 60cbdb0152
7 changed files with 99 additions and 0 deletions

View File

@@ -790,6 +790,7 @@ bool RNA_struct_idprops_unset(PointerRNA *ptr, const char *identifier);
PropertyRNA *RNA_struct_find_property(PointerRNA *ptr, const char *identifier); PropertyRNA *RNA_struct_find_property(PointerRNA *ptr, const char *identifier);
bool RNA_struct_contains_property(PointerRNA *ptr, PropertyRNA *prop_test); bool RNA_struct_contains_property(PointerRNA *ptr, PropertyRNA *prop_test);
unsigned int RNA_struct_count_properties(StructRNA *srna);
/* lower level functions for access to type properties */ /* lower level functions for access to type properties */
const struct ListBase *RNA_struct_type_properties(StructRNA *srna); const struct ListBase *RNA_struct_type_properties(StructRNA *srna);

View File

@@ -108,6 +108,7 @@ extern const EnumPropertyItem rna_enum_motionpath_bake_location_items[];
extern const EnumPropertyItem rna_enum_event_value_items[]; extern const EnumPropertyItem rna_enum_event_value_items[];
extern const EnumPropertyItem rna_enum_event_type_items[]; extern const EnumPropertyItem rna_enum_event_type_items[];
extern const EnumPropertyItem rna_enum_operator_return_items[]; extern const EnumPropertyItem rna_enum_operator_return_items[];
extern const EnumPropertyItem rna_enum_operator_property_tags[];
extern const EnumPropertyItem rna_enum_brush_sculpt_tool_items[]; extern const EnumPropertyItem rna_enum_brush_sculpt_tool_items[];
extern const EnumPropertyItem rna_enum_brush_vertex_tool_items[]; extern const EnumPropertyItem rna_enum_brush_vertex_tool_items[];

View File

@@ -727,6 +727,23 @@ bool RNA_struct_contains_property(PointerRNA *ptr, PropertyRNA *prop_test)
return found; return found;
} }
unsigned int RNA_struct_count_properties(StructRNA *srna)
{
PointerRNA struct_ptr;
unsigned int counter = 0;
RNA_pointer_create(NULL, srna, NULL, &struct_ptr);
RNA_STRUCT_BEGIN (&struct_ptr, prop)
{
counter++;
UNUSED_VARS(prop);
}
RNA_STRUCT_END;
return counter;
}
/* low level direct access to type->properties, note this ignores parent classes so should be used with care */ /* low level direct access to type->properties, note this ignores parent classes so should be used with care */
const struct ListBase *RNA_struct_type_properties(StructRNA *srna) const struct ListBase *RNA_struct_type_properties(StructRNA *srna)
{ {

View File

@@ -442,6 +442,11 @@ const EnumPropertyItem rna_enum_operator_return_items[] = {
{0, NULL, 0, NULL, NULL} {0, NULL, 0, NULL, NULL}
}; };
const EnumPropertyItem rna_enum_operator_property_tags[] = {
{OP_PROP_TAG_ADVANCED, "ADVANCED", 0, "Advanced", "The property is advanced so UI is suggested to hide it"},
{0, NULL, 0, NULL, NULL}
};
/* flag/enum */ /* flag/enum */
const EnumPropertyItem rna_enum_wm_report_items[] = { const EnumPropertyItem rna_enum_wm_report_items[] = {
{RPT_DEBUG, "DEBUG", 0, "Debug", ""}, {RPT_DEBUG, "DEBUG", 0, "Debug", ""},
@@ -1285,6 +1290,7 @@ static StructRNA *rna_Operator_register(
/* create a new operator type */ /* create a new operator type */
dummyot.ext.srna = RNA_def_struct_ptr(&BLENDER_RNA, dummyot.idname, &RNA_Operator); dummyot.ext.srna = RNA_def_struct_ptr(&BLENDER_RNA, dummyot.idname, &RNA_Operator);
RNA_def_struct_flag(dummyot.ext.srna, STRUCT_NO_IDPROPERTIES); /* operator properties are registered separately */ RNA_def_struct_flag(dummyot.ext.srna, STRUCT_NO_IDPROPERTIES); /* operator properties are registered separately */
RNA_def_struct_property_tags(dummyot.ext.srna, rna_enum_operator_property_tags);
RNA_def_struct_translation_context(dummyot.ext.srna, dummyot.translation_context); RNA_def_struct_translation_context(dummyot.ext.srna, dummyot.translation_context);
dummyot.ext.data = data; dummyot.ext.data = data;
dummyot.ext.call = call; dummyot.ext.call = call;
@@ -1639,6 +1645,7 @@ static void rna_def_operator(BlenderRNA *brna)
RNA_def_struct_ui_text(srna, "Operator Properties", "Input properties of an Operator"); RNA_def_struct_ui_text(srna, "Operator Properties", "Input properties of an Operator");
RNA_def_struct_refine_func(srna, "rna_OperatorProperties_refine"); RNA_def_struct_refine_func(srna, "rna_OperatorProperties_refine");
RNA_def_struct_idprops_func(srna, "rna_OperatorProperties_idprops"); RNA_def_struct_idprops_func(srna, "rna_OperatorProperties_idprops");
RNA_def_struct_property_tags(srna, rna_enum_operator_property_tags);
RNA_def_struct_flag(srna, STRUCT_NO_DATABLOCK_IDPROPERTIES); RNA_def_struct_flag(srna, STRUCT_NO_DATABLOCK_IDPROPERTIES);
} }

View File

@@ -293,6 +293,14 @@ void WM_operatortype_append_macro_ptr(void (*opfunc)(struct wmOperatorType *, v
void WM_operatortype_remove_ptr(struct wmOperatorType *ot); void WM_operatortype_remove_ptr(struct wmOperatorType *ot);
bool WM_operatortype_remove(const char *idname); bool WM_operatortype_remove(const char *idname);
void WM_operatortype_last_properties_clear_all(void); void WM_operatortype_last_properties_clear_all(void);
void WM_operatortype_props_advanced_begin(struct wmOperatorType *ot);
void WM_operatortype_props_advanced_end(struct wmOperatorType *ot);
#define WM_operatortype_prop_tag(property, tags) \
{ \
CHECK_TYPE(tags, eOperatorPropTags); \
RNA_def_property_tags(prop, tags); \
} (void)0
struct wmOperatorType *WM_operatortype_append_macro(const char *idname, const char *name, const char *description, int flag); struct wmOperatorType *WM_operatortype_append_macro(const char *idname, const char *name, const char *description, int flag);
struct wmOperatorTypeMacro *WM_operatortype_macro_define(struct wmOperatorType *ot, const char *idname); struct wmOperatorTypeMacro *WM_operatortype_macro_define(struct wmOperatorType *ot, const char *idname);

View File

@@ -164,6 +164,12 @@ enum {
WM_OP_EXEC_SCREEN WM_OP_EXEC_SCREEN
}; };
/* property tags for RNA_OperatorProperties */
typedef enum eOperatorPropTags {
OP_PROP_TAG_ADVANCED = (1 << 0),
} eOperatorPropTags;
#define OP_PROP_TAG_ADVANCED ((eOperatorPropTags)OP_PROP_TAG_ADVANCED)
/* ************** wmKeyMap ************************ */ /* ************** wmKeyMap ************************ */
/* modifier */ /* modifier */

View File

@@ -119,6 +119,8 @@
#include "wm_window.h" #include "wm_window.h"
static GHash *global_ops_hash = NULL; static GHash *global_ops_hash = NULL;
/** Counter for operator-properties that should not be tagged with #OP_PROP_TAG_ADVANCED. */
static int ot_prop_basic_count = -1;
#define UNDOCUMENTED_OPERATOR_TIP N_("(undocumented operator)") #define UNDOCUMENTED_OPERATOR_TIP N_("(undocumented operator)")
@@ -164,10 +166,15 @@ void WM_operatortype_iter(GHashIterator *ghi)
static wmOperatorType *wm_operatortype_append__begin(void) static wmOperatorType *wm_operatortype_append__begin(void)
{ {
wmOperatorType *ot = MEM_callocN(sizeof(wmOperatorType), "operatortype"); wmOperatorType *ot = MEM_callocN(sizeof(wmOperatorType), "operatortype");
BLI_assert(ot_prop_basic_count == -1);
ot->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_OperatorProperties); ot->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_OperatorProperties);
RNA_def_struct_property_tags(ot->srna, rna_enum_operator_property_tags);
/* Set the default i18n context now, so that opfunc can redefine it if needed! */ /* Set the default i18n context now, so that opfunc can redefine it if needed! */
RNA_def_struct_translation_context(ot->srna, BLT_I18NCONTEXT_OPERATOR_DEFAULT); RNA_def_struct_translation_context(ot->srna, BLT_I18NCONTEXT_OPERATOR_DEFAULT);
ot->translation_context = BLT_I18NCONTEXT_OPERATOR_DEFAULT; ot->translation_context = BLT_I18NCONTEXT_OPERATOR_DEFAULT;
return ot; return ot;
} }
static void wm_operatortype_append__end(wmOperatorType *ot) static void wm_operatortype_append__end(wmOperatorType *ot)
@@ -177,6 +184,9 @@ static void wm_operatortype_append__end(wmOperatorType *ot)
ot->name = N_("Dummy Name"); ot->name = N_("Dummy Name");
} }
/* Allow calling _begin without _end in operatortype creation. */
WM_operatortype_props_advanced_end(ot);
/* XXX All ops should have a description but for now allow them not to. */ /* XXX All ops should have a description but for now allow them not to. */
RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description : UNDOCUMENTED_OPERATOR_TIP); RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description : UNDOCUMENTED_OPERATOR_TIP);
RNA_def_struct_identifier(&BLENDER_RNA, ot->srna, ot->idname); RNA_def_struct_identifier(&BLENDER_RNA, ot->srna, ot->idname);
@@ -531,6 +541,55 @@ void WM_operatortype_last_properties_clear_all(void)
} }
} }
/**
* Tag all operator-properties of \a ot defined after calling this, until
* the next #WM_operatortype_props_advanced_end call (if available), with
* #OP_PROP_TAG_ADVANCED. Previously defined ones properties not touched.
*
* Calling this multiple times without a call to #WM_operatortype_props_advanced_end,
* all calls after the first one are ignored. Meaning all propereties defined after the
* first call are tagged as advanced.
*
* This doesn't do the actual tagging, #WM_operatortype_props_advanced_end does which is
* called for all operators during registration (see #wm_operatortype_append__end).
*/
void WM_operatortype_props_advanced_begin(wmOperatorType *ot)
{
if (ot_prop_basic_count == -1) { /* Don't do anything if _begin was called before, but not _end */
ot_prop_basic_count = RNA_struct_count_properties(ot->srna);
}
}
/**
* Tags all operator-properties of \ot defined since the first #WM_operatortype_props_advanced_begin
* call, or the last #WM_operatortype_props_advanced_end call, with #OP_PROP_TAG_ADVANCED.
* Note that this is called for all operators during registration (see #wm_operatortype_append__end).
* So it does not need to be explicitly called in operator-type definition.
*/
void WM_operatortype_props_advanced_end(wmOperatorType *ot)
{
PointerRNA struct_ptr;
int counter = 0;
if (ot_prop_basic_count == -1) {
/* WM_operatortype_props_advanced_begin was not called. Don't do anything. */
return;
}
RNA_pointer_create(NULL, ot->srna, NULL, &struct_ptr);
RNA_STRUCT_BEGIN (&struct_ptr, prop)
{
counter++;
if (counter > ot_prop_basic_count) {
WM_operatortype_prop_tag(prop, OP_PROP_TAG_ADVANCED);
}
}
RNA_STRUCT_END;
ot_prop_basic_count = -1;
}
/* SOME_OT_op -> some.op */ /* SOME_OT_op -> some.op */
void WM_operator_py_idname(char *to, const char *from) void WM_operator_py_idname(char *to, const char *from)
{ {