2012-08-01 19:11:17 +00:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2005 Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): David Millan Escriva, Juho Vepsäläinen, Nathan Letwory
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2012-08-22 16:44:32 +00:00
|
|
|
/** \file blender/editors/space_node/node_add.c
|
2012-08-01 19:11:17 +00:00
|
|
|
* \ingroup spnode
|
|
|
|
*/
|
|
|
|
|
2012-11-04 11:57:49 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
#include "DNA_node_types.h"
|
|
|
|
|
2012-11-04 11:57:49 +00:00
|
|
|
#include "BLI_listbase.h"
|
2012-08-01 19:11:17 +00:00
|
|
|
#include "BLI_math.h"
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
#include "BLT_translation.h"
|
2012-10-26 17:32:50 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
#include "BKE_context.h"
|
|
|
|
#include "BKE_image.h"
|
|
|
|
#include "BKE_library.h"
|
|
|
|
#include "BKE_main.h"
|
|
|
|
#include "BKE_node.h"
|
|
|
|
#include "BKE_report.h"
|
|
|
|
|
2012-08-01 20:39:14 +00:00
|
|
|
#include "ED_node.h" /* own include */
|
2012-08-01 19:11:17 +00:00
|
|
|
#include "ED_screen.h"
|
|
|
|
#include "ED_render.h"
|
|
|
|
|
|
|
|
#include "RNA_access.h"
|
|
|
|
#include "RNA_define.h"
|
|
|
|
#include "RNA_enum_types.h"
|
|
|
|
|
|
|
|
#include "WM_api.h"
|
|
|
|
#include "WM_types.h"
|
|
|
|
|
|
|
|
#include "UI_view2d.h"
|
|
|
|
|
2012-08-01 20:39:14 +00:00
|
|
|
#include "node_intern.h" /* own include */
|
2012-08-01 19:11:17 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
/* XXX Does some additional initialization on top of nodeAddNode
|
|
|
|
* Can be used with both custom and static nodes, if idname==NULL the static int type will be used instead.
|
|
|
|
*/
|
|
|
|
bNode *node_add_node(const bContext *C, const char *idname, int type, float locx, float locy)
|
2012-08-01 19:11:17 +00:00
|
|
|
{
|
2013-03-18 16:34:57 +00:00
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
|
|
|
Main *bmain = CTX_data_main(C);
|
|
|
|
bNode *node = NULL;
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
node_deselect_all(snode);
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
if (idname)
|
|
|
|
node = nodeAddNode(C, snode->edittree, idname);
|
|
|
|
else
|
|
|
|
node = nodeAddStaticNode(C, snode->edittree, type);
|
|
|
|
BLI_assert(node && node->typeinfo);
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
/* generics */
|
2013-03-18 16:34:57 +00:00
|
|
|
node->locx = locx;
|
2013-03-26 14:33:53 +00:00
|
|
|
node->locy = locy + 60.0f; /* arbitrary... so its visible, (0,0) is top of node */
|
2014-04-01 11:34:00 +11:00
|
|
|
nodeSetSelected(node, true);
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
node->locx = locx;
|
|
|
|
node->locy = locy + 60.0f;
|
|
|
|
|
2013-05-07 15:28:42 +00:00
|
|
|
ntreeUpdateTree(bmain, snode->edittree);
|
2013-03-18 16:34:57 +00:00
|
|
|
ED_node_set_active(bmain, snode->edittree, node);
|
|
|
|
|
|
|
|
snode_update(snode, node);
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
if (snode->nodetree->type == NTREE_TEXTURE) {
|
|
|
|
ntreeTexCheckCyclics(snode->edittree);
|
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ********************** Add reroute operator ***************** */
|
2014-01-22 02:48:11 +11:00
|
|
|
static bool add_reroute_intersect_check(bNodeLink *link, float mcoords[][2], int tot, float result[2])
|
2012-08-01 19:11:17 +00:00
|
|
|
{
|
|
|
|
float coord_array[NODE_LINK_RESOL + 1][2];
|
|
|
|
int i, b;
|
|
|
|
|
|
|
|
if (node_link_bezier_points(NULL, NULL, link, coord_array, NODE_LINK_RESOL)) {
|
|
|
|
|
|
|
|
for (i = 0; i < tot - 1; i++)
|
|
|
|
for (b = 0; b < NODE_LINK_RESOL; b++)
|
2015-11-13 07:25:36 +11:00
|
|
|
if (isect_seg_seg_v2(mcoords[i], mcoords[i + 1], coord_array[b], coord_array[b + 1]) > 0) {
|
2012-08-01 19:11:17 +00:00
|
|
|
result[0] = (mcoords[i][0] + mcoords[i + 1][0]) / 2.0f;
|
|
|
|
result[1] = (mcoords[i][1] + mcoords[i + 1][1]) / 2.0f;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-04 11:57:49 +00:00
|
|
|
typedef struct bNodeSocketLink {
|
|
|
|
struct bNodeSocketLink *next, *prev;
|
|
|
|
|
|
|
|
struct bNodeSocket *sock;
|
|
|
|
struct bNodeLink *link;
|
|
|
|
float point[2];
|
|
|
|
} bNodeSocketLink;
|
|
|
|
|
2013-05-08 12:54:33 +00:00
|
|
|
static bNodeSocketLink *add_reroute_insert_socket_link(ListBase *lb, bNodeSocket *sock, bNodeLink *link, const float point[2])
|
2012-11-04 11:57:49 +00:00
|
|
|
{
|
|
|
|
bNodeSocketLink *socklink, *prev;
|
|
|
|
|
|
|
|
socklink = MEM_callocN(sizeof(bNodeSocketLink), "socket link");
|
|
|
|
socklink->sock = sock;
|
|
|
|
socklink->link = link;
|
|
|
|
copy_v2_v2(socklink->point, point);
|
|
|
|
|
|
|
|
for (prev = lb->last; prev; prev = prev->prev) {
|
|
|
|
if (prev->sock == sock)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BLI_insertlinkafter(lb, prev, socklink);
|
|
|
|
return socklink;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bNodeSocketLink *add_reroute_do_socket_section(bContext *C, bNodeSocketLink *socklink, int in_out)
|
|
|
|
{
|
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
|
|
|
bNodeTree *ntree = snode->edittree;
|
|
|
|
bNode *reroute_node = NULL;
|
|
|
|
bNodeSocket *cursock = socklink->sock;
|
|
|
|
float insert_point[2];
|
|
|
|
int num_links;
|
|
|
|
|
|
|
|
zero_v2(insert_point);
|
|
|
|
num_links = 0;
|
|
|
|
|
|
|
|
while (socklink && socklink->sock == cursock) {
|
|
|
|
if (!(socklink->link->flag & NODE_LINK_TEST)) {
|
|
|
|
socklink->link->flag |= NODE_LINK_TEST;
|
|
|
|
|
|
|
|
/* create the reroute node for this cursock */
|
|
|
|
if (!reroute_node) {
|
2013-03-18 16:34:57 +00:00
|
|
|
reroute_node = nodeAddStaticNode(C, ntree, NODE_REROUTE);
|
2012-11-04 11:57:49 +00:00
|
|
|
|
|
|
|
/* add a single link to/from the reroute node to replace multiple links */
|
|
|
|
if (in_out == SOCK_OUT) {
|
|
|
|
nodeAddLink(ntree, socklink->link->fromnode, socklink->link->fromsock, reroute_node, reroute_node->inputs.first);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nodeAddLink(ntree, reroute_node, reroute_node->outputs.first, socklink->link->tonode, socklink->link->tosock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert the reroute node into the link */
|
|
|
|
if (in_out == SOCK_OUT) {
|
|
|
|
socklink->link->fromnode = reroute_node;
|
|
|
|
socklink->link->fromsock = reroute_node->outputs.first;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
socklink->link->tonode = reroute_node;
|
|
|
|
socklink->link->tosock = reroute_node->inputs.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_v2_v2(insert_point, socklink->point);
|
2012-12-14 04:38:52 +00:00
|
|
|
num_links++;
|
2012-11-04 11:57:49 +00:00
|
|
|
}
|
|
|
|
socklink = socklink->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_links > 0) {
|
|
|
|
/* average cut point from shared links */
|
|
|
|
mul_v2_fl(insert_point, 1.0f / num_links);
|
|
|
|
|
2014-01-17 15:58:28 +01:00
|
|
|
reroute_node->locx = insert_point[0] / UI_DPI_FAC;
|
|
|
|
reroute_node->locy = insert_point[1] / UI_DPI_FAC;
|
2012-11-04 11:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return socklink;
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
static int add_reroute_exec(bContext *C, wmOperator *op)
|
|
|
|
{
|
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
|
|
|
ARegion *ar = CTX_wm_region(C);
|
2012-11-04 11:57:49 +00:00
|
|
|
bNodeTree *ntree = snode->edittree;
|
2012-08-01 19:11:17 +00:00
|
|
|
float mcoords[256][2];
|
|
|
|
int i = 0;
|
2012-11-04 11:57:49 +00:00
|
|
|
|
|
|
|
/* Get the cut path */
|
2013-08-24 10:05:29 +00:00
|
|
|
RNA_BEGIN (op->ptr, itemptr, "path")
|
2012-08-01 19:11:17 +00:00
|
|
|
{
|
|
|
|
float loc[2];
|
|
|
|
|
|
|
|
RNA_float_get_array(&itemptr, "loc", loc);
|
|
|
|
UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1],
|
|
|
|
&mcoords[i][0], &mcoords[i][1]);
|
|
|
|
i++;
|
|
|
|
if (i >= 256) break;
|
|
|
|
}
|
|
|
|
RNA_END;
|
|
|
|
|
|
|
|
if (i > 1) {
|
2012-11-04 11:57:49 +00:00
|
|
|
ListBase output_links, input_links;
|
2012-08-01 19:11:17 +00:00
|
|
|
bNodeLink *link;
|
2012-11-04 11:57:49 +00:00
|
|
|
bNodeSocketLink *socklink;
|
|
|
|
float insert_point[2];
|
|
|
|
|
|
|
|
/* always first */
|
2014-11-18 15:51:31 +01:00
|
|
|
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
|
2012-11-04 11:57:49 +00:00
|
|
|
|
|
|
|
node_deselect_all(snode);
|
|
|
|
|
|
|
|
/* Find cut links and sort them by sockets */
|
2014-02-08 06:07:10 +11:00
|
|
|
BLI_listbase_clear(&output_links);
|
|
|
|
BLI_listbase_clear(&input_links);
|
|
|
|
|
2012-11-04 11:57:49 +00:00
|
|
|
for (link = ntree->links.first; link; link = link->next) {
|
2013-03-18 16:34:57 +00:00
|
|
|
if (nodeLinkIsHidden(link))
|
|
|
|
continue;
|
2012-11-04 11:57:49 +00:00
|
|
|
if (add_reroute_intersect_check(link, mcoords, i, insert_point)) {
|
|
|
|
add_reroute_insert_socket_link(&output_links, link->fromsock, link, insert_point);
|
|
|
|
add_reroute_insert_socket_link(&input_links, link->tosock, link, insert_point);
|
|
|
|
|
|
|
|
/* Clear flag */
|
|
|
|
link->flag &= ~NODE_LINK_TEST;
|
2012-08-01 19:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-04 11:57:49 +00:00
|
|
|
|
|
|
|
/* Create reroute nodes for intersected links.
|
|
|
|
* Only one reroute if links share the same input/output socket.
|
|
|
|
*/
|
|
|
|
socklink = output_links.first;
|
|
|
|
while (socklink) {
|
|
|
|
socklink = add_reroute_do_socket_section(C, socklink, SOCK_OUT);
|
|
|
|
}
|
|
|
|
socklink = input_links.first;
|
|
|
|
while (socklink) {
|
|
|
|
socklink = add_reroute_do_socket_section(C, socklink, SOCK_IN);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_freelistN(&output_links);
|
|
|
|
BLI_freelistN(&input_links);
|
|
|
|
|
|
|
|
/* always last */
|
2013-05-07 15:28:42 +00:00
|
|
|
ntreeUpdateTree(CTX_data_main(C), ntree);
|
2012-11-04 11:57:49 +00:00
|
|
|
snode_notify(C, snode);
|
|
|
|
snode_dag_update(C, snode);
|
|
|
|
|
|
|
|
return OPERATOR_FINISHED;
|
2012-08-01 19:11:17 +00:00
|
|
|
}
|
2012-11-04 11:57:49 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NODE_OT_add_reroute(wmOperatorType *ot)
|
|
|
|
{
|
|
|
|
PropertyRNA *prop;
|
|
|
|
|
2013-01-27 07:23:58 +00:00
|
|
|
ot->name = "Add Reroute";
|
2012-08-01 19:11:17 +00:00
|
|
|
ot->idname = "NODE_OT_add_reroute";
|
2013-02-28 15:31:20 +00:00
|
|
|
ot->description = "Add a reroute node";
|
2012-08-01 19:11:17 +00:00
|
|
|
|
|
|
|
ot->invoke = WM_gesture_lines_invoke;
|
|
|
|
ot->modal = WM_gesture_lines_modal;
|
|
|
|
ot->exec = add_reroute_exec;
|
|
|
|
ot->cancel = WM_gesture_lines_cancel;
|
|
|
|
|
2013-06-05 19:06:33 +00:00
|
|
|
ot->poll = ED_operator_node_editable;
|
2012-08-01 19:11:17 +00:00
|
|
|
|
|
|
|
/* flags */
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
|
|
|
prop = RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
|
|
|
|
RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
|
|
|
|
/* internal */
|
|
|
|
RNA_def_int(ot->srna, "cursor", BC_CROSSCURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ****************** Add File Node Operator ******************* */
|
|
|
|
|
|
|
|
static int node_add_file_exec(bContext *C, wmOperator *op)
|
|
|
|
{
|
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
|
|
|
bNode *node;
|
2014-11-23 22:48:48 +01:00
|
|
|
Image *ima;
|
2013-03-18 18:25:05 +00:00
|
|
|
int type = 0;
|
2014-11-23 19:38:27 +01:00
|
|
|
|
2014-11-23 22:48:48 +01:00
|
|
|
ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
|
|
|
|
if (!ima) {
|
|
|
|
return OPERATOR_CANCELLED;
|
2012-08-01 19:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (snode->nodetree->type) {
|
|
|
|
case NTREE_SHADER:
|
2013-03-18 16:34:57 +00:00
|
|
|
type = SH_NODE_TEX_IMAGE;
|
2012-08-01 19:11:17 +00:00
|
|
|
break;
|
|
|
|
case NTREE_TEXTURE:
|
2013-03-18 16:34:57 +00:00
|
|
|
type = TEX_NODE_IMAGE;
|
2012-08-01 19:11:17 +00:00
|
|
|
break;
|
|
|
|
case NTREE_COMPOSIT:
|
2013-03-18 16:34:57 +00:00
|
|
|
type = CMP_NODE_IMAGE;
|
2012-08-01 19:11:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2014-11-18 15:51:31 +01:00
|
|
|
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
node = node_add_node(C, NULL, type, snode->cursor[0], snode->cursor[1]);
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
if (!node) {
|
|
|
|
BKE_report(op->reports, RPT_WARNING, "Could not add an image node");
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
node->id = (ID *)ima;
|
2014-12-23 10:47:27 +05:00
|
|
|
|
|
|
|
/* When adding new image file via drag-drop we need to load imbuf in order
|
|
|
|
* to get proper image source.
|
|
|
|
*/
|
|
|
|
if (RNA_struct_property_is_set(op->ptr, "filepath")) {
|
|
|
|
BKE_image_signal(ima, NULL, IMA_SIGNAL_RELOAD);
|
|
|
|
WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);
|
|
|
|
}
|
2012-12-04 08:10:53 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
snode_notify(C, snode);
|
|
|
|
snode_dag_update(C, snode);
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
}
|
|
|
|
|
2013-03-13 09:03:46 +00:00
|
|
|
static int node_add_file_invoke(bContext *C, wmOperator *op, const wmEvent *event)
|
2012-08-01 19:11:17 +00:00
|
|
|
{
|
|
|
|
ARegion *ar = CTX_wm_region(C);
|
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
/* convert mouse coordinates to v2d space */
|
|
|
|
UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
|
2012-08-08 17:37:02 +00:00
|
|
|
&snode->cursor[0], &snode->cursor[1]);
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
if (RNA_struct_property_is_set(op->ptr, "filepath") || RNA_struct_property_is_set(op->ptr, "name"))
|
|
|
|
return node_add_file_exec(C, op);
|
|
|
|
else
|
|
|
|
return WM_operator_filesel(C, op, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NODE_OT_add_file(wmOperatorType *ot)
|
|
|
|
{
|
|
|
|
/* identifiers */
|
|
|
|
ot->name = "Add File Node";
|
|
|
|
ot->description = "Add a file node to the current node editor";
|
|
|
|
ot->idname = "NODE_OT_add_file";
|
|
|
|
|
|
|
|
/* callbacks */
|
|
|
|
ot->exec = node_add_file_exec;
|
|
|
|
ot->invoke = node_add_file_invoke;
|
2013-06-05 19:06:33 +00:00
|
|
|
ot->poll = ED_operator_node_editable;
|
2012-08-01 19:11:17 +00:00
|
|
|
|
|
|
|
/* flags */
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
2016-02-07 22:56:20 +11:00
|
|
|
WM_operator_properties_filesel(
|
|
|
|
ot, FILE_TYPE_FOLDER | FILE_TYPE_IMAGE | FILE_TYPE_MOVIE, FILE_SPECIAL, FILE_OPENFILE,
|
|
|
|
WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
|
2012-08-01 19:11:17 +00:00
|
|
|
RNA_def_string(ot->srna, "name", "Image", MAX_ID_NAME - 2, "Name", "Datablock name to assign");
|
|
|
|
}
|
|
|
|
|
2013-07-22 19:24:39 +00:00
|
|
|
/* ****************** Add Mask Node Operator ******************* */
|
|
|
|
|
|
|
|
static int node_add_mask_poll(bContext *C)
|
|
|
|
{
|
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
|
|
|
|
|
|
|
return ED_operator_node_editable(C) && snode->nodetree->type == NTREE_COMPOSIT;
|
|
|
|
}
|
|
|
|
|
2013-09-05 13:03:03 +00:00
|
|
|
static int node_add_mask_exec(bContext *C, wmOperator *op)
|
2013-07-22 19:24:39 +00:00
|
|
|
{
|
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
|
|
|
bNode *node;
|
|
|
|
ID *mask = NULL;
|
|
|
|
|
|
|
|
/* check input variables */
|
|
|
|
char name[MAX_ID_NAME - 2];
|
|
|
|
RNA_string_get(op->ptr, "name", name);
|
|
|
|
mask = BKE_libblock_find_name(ID_MSK, name);
|
|
|
|
if (!mask) {
|
|
|
|
BKE_reportf(op->reports, RPT_ERROR, "Mask '%s' not found", name);
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
}
|
|
|
|
|
2014-11-18 15:51:31 +01:00
|
|
|
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
|
2013-07-22 19:24:39 +00:00
|
|
|
|
|
|
|
node = node_add_node(C, NULL, CMP_NODE_MASK, snode->cursor[0], snode->cursor[1]);
|
|
|
|
|
|
|
|
if (!node) {
|
2013-07-23 06:58:05 +00:00
|
|
|
BKE_report(op->reports, RPT_WARNING, "Could not add a mask node");
|
2013-07-22 19:24:39 +00:00
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->id = mask;
|
|
|
|
id_us_plus(mask);
|
|
|
|
|
|
|
|
snode_notify(C, snode);
|
|
|
|
snode_dag_update(C, snode);
|
|
|
|
|
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NODE_OT_add_mask(wmOperatorType *ot)
|
|
|
|
{
|
|
|
|
/* identifiers */
|
|
|
|
ot->name = "Add Mask Node";
|
|
|
|
ot->description = "Add a mask node to the current node editor";
|
|
|
|
ot->idname = "NODE_OT_add_mask";
|
|
|
|
|
|
|
|
/* callbacks */
|
2013-09-05 13:03:03 +00:00
|
|
|
ot->exec = node_add_mask_exec;
|
2013-07-22 19:24:39 +00:00
|
|
|
ot->poll = node_add_mask_poll;
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
|
|
|
RNA_def_string(ot->srna, "name", "Mask", MAX_ID_NAME - 2, "Name", "Datablock name to assign");
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
/********************** New node tree operator *********************/
|
|
|
|
|
|
|
|
static int new_node_tree_exec(bContext *C, wmOperator *op)
|
|
|
|
{
|
2013-03-18 18:25:05 +00:00
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
2013-03-18 16:34:57 +00:00
|
|
|
Main *bmain = CTX_data_main(C);
|
2012-08-01 19:11:17 +00:00
|
|
|
bNodeTree *ntree;
|
|
|
|
PointerRNA ptr, idptr;
|
|
|
|
PropertyRNA *prop;
|
2013-03-18 16:34:57 +00:00
|
|
|
const char *idname;
|
2015-01-01 23:26:03 +11:00
|
|
|
char treename_buf[MAX_ID_NAME - 2];
|
|
|
|
const char *treename;
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
if (RNA_struct_property_is_set(op->ptr, "type")) {
|
|
|
|
prop = RNA_struct_find_property(op->ptr, "type");
|
|
|
|
RNA_property_enum_identifier(C, op->ptr, prop, RNA_property_enum_get(op->ptr, prop), &idname);
|
|
|
|
}
|
|
|
|
else if (snode)
|
|
|
|
idname = snode->tree_idname;
|
2013-04-15 23:12:49 +00:00
|
|
|
else
|
|
|
|
return OPERATOR_CANCELLED;
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2013-03-26 14:33:53 +00:00
|
|
|
if (RNA_struct_property_is_set(op->ptr, "name")) {
|
2015-01-01 23:26:03 +11:00
|
|
|
RNA_string_get(op->ptr, "name", treename_buf);
|
|
|
|
treename = treename_buf;
|
2013-03-26 14:33:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-01-01 23:26:03 +11:00
|
|
|
treename = DATA_("NodeTree");
|
2013-03-26 14:33:53 +00:00
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
if (!ntreeTypeFind(idname)) {
|
|
|
|
BKE_reportf(op->reports, RPT_ERROR, "Node tree type %s undefined", idname);
|
2012-08-01 19:11:17 +00:00
|
|
|
return OPERATOR_CANCELLED;
|
2013-03-18 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ntree = ntreeAddTree(bmain, treename, idname);
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
/* hook into UI */
|
2014-11-09 21:20:40 +01:00
|
|
|
UI_context_active_but_prop_get_templateID(C, &ptr, &prop);
|
2012-08-01 19:11:17 +00:00
|
|
|
|
|
|
|
if (prop) {
|
|
|
|
/* RNA_property_pointer_set increases the user count,
|
|
|
|
* fixed here as the editor is the initial user.
|
|
|
|
*/
|
2015-11-09 19:47:10 +01:00
|
|
|
id_us_min(&ntree->id);
|
2013-04-12 15:42:16 +00:00
|
|
|
|
|
|
|
RNA_id_pointer_create(&ntree->id, &idptr);
|
|
|
|
RNA_property_pointer_set(&ptr, prop, idptr);
|
2012-08-01 19:11:17 +00:00
|
|
|
RNA_property_update(C, &ptr, prop);
|
|
|
|
}
|
|
|
|
else if (snode) {
|
|
|
|
snode->nodetree = ntree;
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
ED_node_tree_update(C);
|
2012-08-01 19:11:17 +00:00
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
}
|
|
|
|
|
2014-01-04 18:08:43 +11:00
|
|
|
static EnumPropertyItem *new_node_tree_type_itemf(bContext *UNUSED(C), PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
|
2013-03-18 16:34:57 +00:00
|
|
|
{
|
2014-01-04 18:08:43 +11:00
|
|
|
return rna_node_tree_type_itemf(NULL, NULL, r_free);
|
2013-03-18 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
void NODE_OT_new_node_tree(wmOperatorType *ot)
|
|
|
|
{
|
2013-03-18 16:34:57 +00:00
|
|
|
PropertyRNA *prop;
|
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
/* identifiers */
|
|
|
|
ot->name = "New Node Tree";
|
|
|
|
ot->idname = "NODE_OT_new_node_tree";
|
|
|
|
ot->description = "Create a new node tree";
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
/* api callbacks */
|
|
|
|
ot->exec = new_node_tree_exec;
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-08-01 19:11:17 +00:00
|
|
|
/* flags */
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
prop = RNA_def_enum(ot->srna, "type", DummyRNA_NULL_items, 0, "Tree Type", "");
|
|
|
|
RNA_def_enum_funcs(prop, new_node_tree_type_itemf);
|
2012-08-01 19:11:17 +00:00
|
|
|
RNA_def_string(ot->srna, "name", "NodeTree", MAX_ID_NAME - 2, "Name", "");
|
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
|