UI/Nodes: Adding node groups via drag & drop (e.g. from Asset Browser)
Adds `NODE_OT_add_group` operator to add a node group from a given name, and uses that to register a node editor drop-box. When dropping a node-group asset, the ID will be appended. This is what we do for other ID assets too. Should the node group insertion fail (e.g. the group is not compatible with the current tree, as checked by the poll), the appended data-block is removed. Differential Revision: https://developer.blender.org/D10405 Reviewed by: Jacques Lucke
This commit is contained in:
@@ -309,6 +309,111 @@ void NODE_OT_add_reroute(wmOperatorType *ot)
|
||||
RNA_def_int(ot->srna, "cursor", WM_CURSOR_CROSS, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
|
||||
}
|
||||
|
||||
/* ****************** Add Node Group Operator ******************* */
|
||||
|
||||
static bNodeTree *node_add_group_get_and_poll_group_node_tree(Main *bmain,
|
||||
wmOperator *op,
|
||||
bNodeTree *ntree)
|
||||
{
|
||||
char name[MAX_ID_NAME - 2];
|
||||
RNA_string_get(op->ptr, "name", name);
|
||||
|
||||
bNodeTree *node_group = (bNodeTree *)BKE_libblock_find_name(bmain, ID_NT, name);
|
||||
if (!node_group) {
|
||||
return NULL;
|
||||
}
|
||||
if ((node_group->type != ntree->type) || !nodeGroupPoll(ntree, node_group)) {
|
||||
if (RNA_boolean_get(op->ptr, "free_id_on_error")) {
|
||||
BKE_id_delete(bmain, node_group);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return node_group;
|
||||
}
|
||||
|
||||
static int node_add_group_exec(bContext *C, wmOperator *op)
|
||||
{
|
||||
Main *bmain = CTX_data_main(C);
|
||||
SpaceNode *snode = CTX_wm_space_node(C);
|
||||
bNodeTree *ntree = snode->edittree;
|
||||
bNodeTree *node_group;
|
||||
|
||||
if (!(node_group = node_add_group_get_and_poll_group_node_tree(bmain, op, ntree))) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
|
||||
|
||||
bNode *group_node = node_add_node(C,
|
||||
node_group_idname(C),
|
||||
(node_group->type == NTREE_CUSTOM) ? NODE_CUSTOM_GROUP :
|
||||
NODE_GROUP,
|
||||
snode->runtime->cursor[0],
|
||||
snode->runtime->cursor[1]);
|
||||
if (!group_node) {
|
||||
BKE_report(op->reports, RPT_WARNING, "Could not add node group");
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
group_node->id = &node_group->id;
|
||||
id_us_plus(group_node->id);
|
||||
|
||||
nodeSetActive(ntree, group_node);
|
||||
ntreeUpdateTree(bmain, node_group);
|
||||
ntreeUpdateTree(bmain, ntree);
|
||||
|
||||
snode_notify(C, snode);
|
||||
snode_dag_update(C, snode);
|
||||
|
||||
return OPERATOR_FINISHED;
|
||||
}
|
||||
|
||||
static int node_add_group_invoke(bContext *C, wmOperator *op, const wmEvent *event)
|
||||
{
|
||||
ARegion *region = CTX_wm_region(C);
|
||||
SpaceNode *snode = CTX_wm_space_node(C);
|
||||
|
||||
/* Convert mouse coordinates to v2d space. */
|
||||
UI_view2d_region_to_view(®ion->v2d,
|
||||
event->mval[0],
|
||||
event->mval[1],
|
||||
&snode->runtime->cursor[0],
|
||||
&snode->runtime->cursor[1]);
|
||||
|
||||
snode->runtime->cursor[0] /= UI_DPI_FAC;
|
||||
snode->runtime->cursor[1] /= UI_DPI_FAC;
|
||||
|
||||
return node_add_group_exec(C, op);
|
||||
}
|
||||
|
||||
void NODE_OT_add_group(wmOperatorType *ot)
|
||||
{
|
||||
PropertyRNA *prop;
|
||||
|
||||
/* identifiers */
|
||||
ot->name = "Add Node Group";
|
||||
ot->description = "Add an existing node group to the current node editor";
|
||||
ot->idname = "NODE_OT_add_group";
|
||||
|
||||
/* callbacks */
|
||||
ot->exec = node_add_group_exec;
|
||||
ot->invoke = node_add_group_invoke;
|
||||
ot->poll = ED_operator_node_editable;
|
||||
|
||||
/* 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_boolean(
|
||||
ot->srna,
|
||||
"free_id_on_error",
|
||||
false,
|
||||
"Free Group on Error",
|
||||
"Free the named node group data-block if it could not be added to the tree");
|
||||
RNA_def_property_flag(prop, PROP_HIDDEN);
|
||||
}
|
||||
|
||||
/* ****************** Add File Node Operator ******************* */
|
||||
|
||||
static bool node_add_file_poll(bContext *C)
|
||||
|
||||
Reference in New Issue
Block a user