Cleanup: Use new helpers for passing IDs from drag & drop to operators
There are now some generalized helpers for passing IDs from drag & drop to operators via operator properties, mostly introduced in917c096be6and8f79fa9c67. These can be used in a bunch of places to reduce duplicated code and explicitly share a common solution. Side-effect: The "Name" property won't show up in the Adjust Last Operation anymore, and its value won't be remembered over multiple executions of the operator. Both were not at all useful from what I can tell, and I doubt this was done intentionally.
This commit is contained in:
@@ -2142,11 +2142,8 @@ static int ui_drop_material_exec(bContext *C, wmOperator *op)
|
||||
{
|
||||
Main *bmain = CTX_data_main(C);
|
||||
|
||||
if (!RNA_struct_property_is_set(op->ptr, "session_uuid")) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
const uint32_t session_uuid = (uint32_t)RNA_int_get(op->ptr, "session_uuid");
|
||||
Material *ma = (Material *)BKE_libblock_find_session_uuid(bmain, ID_MA, session_uuid);
|
||||
Material *ma = (Material *)WM_operator_properties_id_lookup_from_name_or_session_uuid(
|
||||
bmain, op->ptr, ID_MA);
|
||||
if (ma == NULL) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
@@ -2184,16 +2181,7 @@ static void UI_OT_drop_material(wmOperatorType *ot)
|
||||
ot->exec = ui_drop_material_exec;
|
||||
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
|
||||
|
||||
PropertyRNA *prop = RNA_def_int(ot->srna,
|
||||
"session_uuid",
|
||||
0,
|
||||
INT32_MIN,
|
||||
INT32_MAX,
|
||||
"Session UUID",
|
||||
"Session UUID of the data-block to assign",
|
||||
INT32_MIN,
|
||||
INT32_MAX);
|
||||
RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);
|
||||
WM_operator_properties_id_lookup(ot, false);
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
||||
@@ -1729,8 +1729,7 @@ static int object_instance_add_invoke(bContext *C, wmOperator *op, const wmEvent
|
||||
RNA_int_set(op->ptr, "drop_y", event->xy[1]);
|
||||
}
|
||||
|
||||
if (!RNA_struct_property_is_set(op->ptr, "name") &&
|
||||
!RNA_struct_property_is_set(op->ptr, "session_uuid")) {
|
||||
if (!WM_operator_properties_id_lookup_is_set(op->ptr)) {
|
||||
return WM_enum_search_invoke(C, op, event);
|
||||
}
|
||||
return op->type->exec(C, op);
|
||||
@@ -1762,16 +1761,7 @@ void OBJECT_OT_collection_instance_add(wmOperatorType *ot)
|
||||
ot->prop = prop;
|
||||
ED_object_add_generic_props(ot, false);
|
||||
|
||||
prop = RNA_def_int(ot->srna,
|
||||
"session_uuid",
|
||||
0,
|
||||
INT32_MIN,
|
||||
INT32_MAX,
|
||||
"Session UUID",
|
||||
"Session UUID of the collection to add",
|
||||
INT32_MIN,
|
||||
INT32_MAX);
|
||||
RNA_def_property_flag(prop, (PropertyFlag)(PROP_SKIP_SAVE | PROP_HIDDEN));
|
||||
WM_operator_properties_id_lookup(ot, false);
|
||||
|
||||
object_add_drop_xy_props(ot);
|
||||
}
|
||||
@@ -1868,16 +1858,7 @@ void OBJECT_OT_collection_external_asset_drop(wmOperatorType *ot)
|
||||
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
|
||||
|
||||
/* properties */
|
||||
prop = RNA_def_int(ot->srna,
|
||||
"session_uuid",
|
||||
0,
|
||||
INT32_MIN,
|
||||
INT32_MAX,
|
||||
"Session UUID",
|
||||
"Session UUID of the collection to add",
|
||||
INT32_MIN,
|
||||
INT32_MAX);
|
||||
RNA_def_property_flag(prop, (PropertyFlag)(PROP_SKIP_SAVE | PROP_HIDDEN));
|
||||
WM_operator_properties_id_lookup(ot, false);
|
||||
|
||||
ED_object_add_generic_props(ot, false);
|
||||
|
||||
|
||||
@@ -432,26 +432,16 @@ void NODE_OT_add_group(wmOperatorType *ot)
|
||||
/** \name Add Node Object Operator
|
||||
* \{ */
|
||||
|
||||
static Object *node_add_object_get_and_poll_object_node_tree(Main *bmain, wmOperator *op)
|
||||
{
|
||||
if (RNA_struct_property_is_set(op->ptr, "session_uuid")) {
|
||||
const uint32_t session_uuid = (uint32_t)RNA_int_get(op->ptr, "session_uuid");
|
||||
return (Object *)BKE_libblock_find_session_uuid(bmain, ID_OB, session_uuid);
|
||||
}
|
||||
|
||||
char name[MAX_ID_NAME - 2];
|
||||
RNA_string_get(op->ptr, "name", name);
|
||||
return (Object *)BKE_libblock_find_name(bmain, ID_OB, name);
|
||||
}
|
||||
|
||||
static int node_add_object_exec(bContext *C, wmOperator *op)
|
||||
{
|
||||
Main *bmain = CTX_data_main(C);
|
||||
SpaceNode *snode = CTX_wm_space_node(C);
|
||||
bNodeTree *ntree = snode->edittree;
|
||||
Object *object;
|
||||
|
||||
if (!(object = node_add_object_get_and_poll_object_node_tree(bmain, op))) {
|
||||
Object *object = reinterpret_cast<Object *>(
|
||||
WM_operator_properties_id_lookup_from_name_or_session_uuid(bmain, op->ptr, ID_OB));
|
||||
|
||||
if (!object) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
@@ -508,8 +498,6 @@ static bool node_add_object_poll(bContext *C)
|
||||
|
||||
void NODE_OT_add_object(wmOperatorType *ot)
|
||||
{
|
||||
PropertyRNA *prop;
|
||||
|
||||
/* identifiers */
|
||||
ot->name = "Add Node Object";
|
||||
ot->description = "Add an object info node to the current node editor";
|
||||
@@ -523,17 +511,7 @@ void NODE_OT_add_object(wmOperatorType *ot)
|
||||
/* flags */
|
||||
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
|
||||
|
||||
RNA_def_string(ot->srna, "name", "Object", MAX_ID_NAME - 2, "Name", "Data-block name to assign");
|
||||
prop = RNA_def_int(ot->srna,
|
||||
"session_uuid",
|
||||
0,
|
||||
INT32_MIN,
|
||||
INT32_MAX,
|
||||
"Session UUID",
|
||||
"Session UUID of the data-block to assign",
|
||||
INT32_MIN,
|
||||
INT32_MAX);
|
||||
RNA_def_property_flag(prop, (PropertyFlag)(PROP_HIDDEN | PROP_SKIP_SAVE));
|
||||
WM_operator_properties_id_lookup(ot, true);
|
||||
}
|
||||
|
||||
/** \} */
|
||||
@@ -542,27 +520,16 @@ void NODE_OT_add_object(wmOperatorType *ot)
|
||||
/** \name Add Node Collection Operator
|
||||
* \{ */
|
||||
|
||||
static Collection *node_add_collection_get_and_poll_collection_node_tree(Main *bmain,
|
||||
wmOperator *op)
|
||||
{
|
||||
if (RNA_struct_property_is_set(op->ptr, "session_uuid")) {
|
||||
const uint32_t session_uuid = (uint32_t)RNA_int_get(op->ptr, "session_uuid");
|
||||
return (Collection *)BKE_libblock_find_session_uuid(bmain, ID_GR, session_uuid);
|
||||
}
|
||||
|
||||
char name[MAX_ID_NAME - 2];
|
||||
RNA_string_get(op->ptr, "name", name);
|
||||
return (Collection *)BKE_libblock_find_name(bmain, ID_GR, name);
|
||||
}
|
||||
|
||||
static int node_add_collection_exec(bContext *C, wmOperator *op)
|
||||
{
|
||||
Main *bmain = CTX_data_main(C);
|
||||
SpaceNode &snode = *CTX_wm_space_node(C);
|
||||
bNodeTree *ntree = snode.edittree;
|
||||
Collection *collection;
|
||||
|
||||
if (!(collection = node_add_collection_get_and_poll_collection_node_tree(bmain, op))) {
|
||||
Collection *collection = reinterpret_cast<Collection *>(
|
||||
WM_operator_properties_id_lookup_from_name_or_session_uuid(bmain, op->ptr, ID_GR));
|
||||
|
||||
if (!collection) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
@@ -619,8 +586,6 @@ static bool node_add_collection_poll(bContext *C)
|
||||
|
||||
void NODE_OT_add_collection(wmOperatorType *ot)
|
||||
{
|
||||
PropertyRNA *prop;
|
||||
|
||||
/* identifiers */
|
||||
ot->name = "Add Node Collection";
|
||||
ot->description = "Add an collection info node to the current node editor";
|
||||
@@ -634,18 +599,7 @@ void NODE_OT_add_collection(wmOperatorType *ot)
|
||||
/* flags */
|
||||
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
|
||||
|
||||
RNA_def_string(
|
||||
ot->srna, "name", "Collection", MAX_ID_NAME - 2, "Name", "Data-block name to assign");
|
||||
prop = RNA_def_int(ot->srna,
|
||||
"session_uuid",
|
||||
0,
|
||||
INT32_MIN,
|
||||
INT32_MAX,
|
||||
"Session UUID",
|
||||
"Session UUID of the data-block to assign",
|
||||
INT32_MIN,
|
||||
INT32_MAX);
|
||||
RNA_def_property_flag(prop, (PropertyFlag)(PROP_HIDDEN | PROP_SKIP_SAVE));
|
||||
WM_operator_properties_id_lookup(ot, true);
|
||||
}
|
||||
|
||||
/** \} */
|
||||
@@ -776,18 +730,6 @@ void NODE_OT_add_file(wmOperatorType *ot)
|
||||
/** \name Add Mask Node Operator
|
||||
* \{ */
|
||||
|
||||
static ID *node_add_mask_get_and_poll_mask(Main *bmain, wmOperator *op)
|
||||
{
|
||||
if (RNA_struct_property_is_set(op->ptr, "session_uuid")) {
|
||||
const uint32_t session_uuid = (uint32_t)RNA_int_get(op->ptr, "session_uuid");
|
||||
return BKE_libblock_find_session_uuid(bmain, ID_MSK, session_uuid);
|
||||
}
|
||||
|
||||
char name[MAX_ID_NAME - 2];
|
||||
RNA_string_get(op->ptr, "name", name);
|
||||
return BKE_libblock_find_name(bmain, ID_MSK, name);
|
||||
}
|
||||
|
||||
static bool node_add_mask_poll(bContext *C)
|
||||
{
|
||||
SpaceNode *snode = CTX_wm_space_node(C);
|
||||
@@ -801,7 +743,7 @@ static int node_add_mask_exec(bContext *C, wmOperator *op)
|
||||
SpaceNode &snode = *CTX_wm_space_node(C);
|
||||
bNode *node;
|
||||
|
||||
ID *mask = node_add_mask_get_and_poll_mask(bmain, op);
|
||||
ID *mask = WM_operator_properties_id_lookup_from_name_or_session_uuid(bmain, op->ptr, ID_MSK);
|
||||
if (!mask) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
@@ -827,8 +769,6 @@ static int node_add_mask_exec(bContext *C, wmOperator *op)
|
||||
|
||||
void NODE_OT_add_mask(wmOperatorType *ot)
|
||||
{
|
||||
PropertyRNA *prop;
|
||||
|
||||
/* identifiers */
|
||||
ot->name = "Add Mask Node";
|
||||
ot->description = "Add a mask node to the current node editor";
|
||||
@@ -841,17 +781,7 @@ void NODE_OT_add_mask(wmOperatorType *ot)
|
||||
/* flags */
|
||||
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
|
||||
|
||||
RNA_def_string(ot->srna, "name", "Mask", MAX_ID_NAME - 2, "Name", "Data-block name to assign");
|
||||
prop = RNA_def_int(ot->srna,
|
||||
"session_uuid",
|
||||
0,
|
||||
INT32_MIN,
|
||||
INT32_MAX,
|
||||
"Session UUID",
|
||||
"Session UUID of the data-block to assign",
|
||||
INT32_MIN,
|
||||
INT32_MAX);
|
||||
RNA_def_property_flag(prop, (PropertyFlag)(PROP_HIDDEN | PROP_SKIP_SAVE));
|
||||
WM_operator_properties_id_lookup(ot, true);
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
||||
@@ -788,6 +788,11 @@ void WM_operator_properties_id_lookup_set_from_id(PointerRNA *ptr, const ID *id)
|
||||
struct ID *WM_operator_properties_id_lookup_from_name_or_session_uuid(struct Main *bmain,
|
||||
PointerRNA *ptr,
|
||||
enum ID_Type type);
|
||||
/**
|
||||
* Check if either the "session_uuid" or "name" property is set inside \a ptr. If this is the case
|
||||
* the ID can be looked up by #WM_operator_properties_id_lookup_from_name_or_session_uuid().
|
||||
*/
|
||||
bool WM_operator_properties_id_lookup_is_set(PointerRNA *ptr);
|
||||
/**
|
||||
* Adds "name" and "session_uuid" properties so the caller can tell the operator which ID to act
|
||||
* on. See #WM_operator_properties_id_lookup_from_name_or_session_uuid(). Both properties will be
|
||||
|
||||
@@ -262,6 +262,12 @@ ID *WM_operator_properties_id_lookup_from_name_or_session_uuid(Main *bmain,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool WM_operator_properties_id_lookup_is_set(PointerRNA *ptr)
|
||||
{
|
||||
return RNA_struct_property_is_set(ptr, "session_uuid") ||
|
||||
RNA_struct_property_is_set(ptr, "name");
|
||||
}
|
||||
|
||||
void WM_operator_properties_id_lookup(wmOperatorType *ot, const bool add_name_prop)
|
||||
{
|
||||
PropertyRNA *prop;
|
||||
|
||||
Reference in New Issue
Block a user