+ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+}
+
+/* ******************** Separate operator ********************** */
+
+/* returns 1 if its OK */
+static int node_group_separate_selected(bNodeTree *ntree, bNode *gnode, int make_copy)
+{
+ bNodeLink *link, *link_next;
+ bNode *node, *node_next, *newnode;
+ bNodeTree *ngroup;
+ ListBase anim_basepaths = {NULL, NULL};
+
+ ngroup = (bNodeTree *)gnode->id;
+ if (ngroup == NULL) return 0;
+
+ /* deselect all nodes in the target tree */
+ for (node = ntree->nodes.first; node; node = node->next)
+ node_deselect(node);
+
+ /* clear new pointers, set in nodeCopyNode */
+ for (node = ngroup->nodes.first; node; node = node->next)
+ node->new_node = NULL;
+
+ /* add selected nodes into the ntree */
+ for (node = ngroup->nodes.first; node; node = node_next) {
+ node_next = node->next;
+ if (!(node->flag & NODE_SELECT))
+ continue;
+
+ if (make_copy) {
+ /* make a copy */
+ newnode = nodeCopyNode(ngroup, node);
+ }
+ else {
+ /* use the existing node */
+ newnode = node;
+ }
+
+ /* keep track of this node's RNA "base" path (the part of the path identifying the node)
+ * if the old nodetree has animation data which potentially covers this node
+ */
+ if (ngroup->adt) {
+ PointerRNA ptr;
+ char *path;
+
+ RNA_pointer_create(&ngroup->id, &RNA_Node, newnode, &ptr);
+ path = RNA_path_from_ID_to_struct(&ptr);
+
+ if (path)
+ BLI_addtail(&anim_basepaths, BLI_genericNodeN(path));
+ }
+
+ /* ensure valid parent pointers, detach if parent stays inside the group */
+ if (newnode->parent && !(newnode->parent->flag & NODE_SELECT))
+ nodeDetachNode(newnode);
+
+ /* migrate node */
+ BLI_remlink(&ngroup->nodes, newnode);
+ BLI_addtail(&ntree->nodes, newnode);
+
+ /* ensure unique node name in the node tree */
+ nodeUniqueName(ntree, newnode);
+
+ newnode->locx += gnode->locx;
+ newnode->locy += gnode->locy;
+ }
+
+ /* add internal links to the ntree */
+ for (link = ngroup->links.first; link; link = link_next) {
+ int fromselect = (link->fromnode && (link->fromnode->flag & NODE_SELECT));
+ int toselect = (link->tonode && (link->tonode->flag & NODE_SELECT));
+ link_next = link->next;
+
+ if (make_copy) {
+ /* make a copy of internal links */
+ if (fromselect && toselect)
+ nodeAddLink(ntree, link->fromnode->new_node, link->fromsock->new_sock, link->tonode->new_node, link->tosock->new_sock);
+ }
+ else {
+ /* move valid links over, delete broken links */
+ if (fromselect && toselect) {
+ BLI_remlink(&ngroup->links, link);
+ BLI_addtail(&ntree->links, link);
+ }
+ else if (fromselect || toselect) {
+ nodeRemLink(ngroup, link);
+ }
+ }
+ }
+
+ /* and copy across the animation,
+ * note that the animation data's action can be NULL here */
+ if (ngroup->adt) {
+ LinkData *ld, *ldn = NULL;
+
+ /* now perform the moving */
+ BKE_animdata_separate_by_basepath(&ngroup->id, &ntree->id, &anim_basepaths);
+
+ /* paths + their wrappers need to be freed */
+ for (ld = anim_basepaths.first; ld; ld = ldn) {
+ ldn = ld->next;
+
+ MEM_freeN(ld->data);
+ BLI_freelinkN(&anim_basepaths, ld);
+ }
+ }
+
+ ntree->update |= NTREE_UPDATE_NODES | NTREE_UPDATE_LINKS;
+ if (!make_copy)
+ ngroup->update |= NTREE_UPDATE_NODES | NTREE_UPDATE_LINKS;
+
+ return 1;
+}
+
+typedef enum eNodeGroupSeparateType {
+ NODE_GS_COPY,
+ NODE_GS_MOVE
+} eNodeGroupSeparateType;
+
+/* Operator Property */
+EnumPropertyItem node_group_separate_types[] = {
+ {NODE_GS_COPY, "COPY", 0, "Copy", "Copy to parent node tree, keep group intact"},
+ {NODE_GS_MOVE, "MOVE", 0, "Move", "Move to parent node tree, remove from group"},
+ {0, NULL, 0, NULL, NULL}
+};
+
+static int node_group_separate_exec(bContext *C, wmOperator *op)
+{
+ SpaceNode *snode = CTX_wm_space_node(C);
+ bNode *gnode;
+ int type = RNA_enum_get(op->ptr, "type");
+
+ ED_preview_kill_jobs(C);
+
+ /* are we inside of a group? */
+ gnode = node_tree_get_editgroup(snode->nodetree);
+ if (!gnode) {
+ BKE_report(op->reports, RPT_WARNING, "Not inside node group");
+ return OPERATOR_CANCELLED;
+ }
+
+ switch (type) {
+ case NODE_GS_COPY:
+ if (!node_group_separate_selected(snode->nodetree, gnode, 1)) {
+ BKE_report(op->reports, RPT_WARNING, "Can't separate nodes");
+ return OPERATOR_CANCELLED;
+ }
+ break;
+ case NODE_GS_MOVE:
+ if (!node_group_separate_selected(snode->nodetree, gnode, 0)) {
+ BKE_report(op->reports, RPT_WARNING, "Can't separate nodes");
+ return OPERATOR_CANCELLED;
+ }
+ break;
+ }
+
+ /* switch to parent tree */
+ snode_make_group_editable(snode, NULL);
+
+ ntreeUpdateTree(snode->nodetree);
+
+ snode_notify(C, snode);
+ snode_dag_update(C, snode);
+
+ return OPERATOR_FINISHED;
+}
+
+static int node_group_separate_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
+{
+ uiPopupMenu *pup = uiPupMenuBegin(C, "Separate", ICON_NONE);
+ uiLayout *layout = uiPupMenuLayout(pup);
+
+ uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
+ uiItemEnumO(layout, "NODE_OT_group_separate", NULL, 0, "type", NODE_GS_COPY);
+ uiItemEnumO(layout, "NODE_OT_group_separate", NULL, 0, "type", NODE_GS_MOVE);
+
+ uiPupMenuEnd(C, pup);
+
+ return OPERATOR_CANCELLED;
+}
+
+void NODE_OT_group_separate(wmOperatorType *ot)
+{
+ /* identifiers */
+ ot->name = "Separate";
+ ot->description = "Separate selected nodes from the node group";
+ ot->idname = "NODE_OT_group_separate";
+
+ /* api callbacks */
+ ot->invoke = node_group_separate_invoke;
+ ot->exec = node_group_separate_exec;
+ ot->poll = ED_operator_node_active;
+
+ /* flags */
+ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+ RNA_def_enum(ot->srna, "type", node_group_separate_types, NODE_GS_COPY, "Type", "");