Nodes: Add dynamic label support for Math Nodes
authorCharlie Jolly <charlie>
Tue, 11 Feb 2020 15:31:40 +0000 (15:31 +0000)
committerCharlie Jolly <mistajolly@gmail.com>
Tue, 11 Feb 2020 16:09:25 +0000 (16:09 +0000)
Reviewed By: brecht

Differential Revision: https://developer.blender.org/D6375

12 files changed:
release/scripts/startup/bl_ui/space_node.py
source/blender/blenkernel/BKE_node.h
source/blender/blenkernel/intern/node.c
source/blender/editors/space_node/node_draw.c
source/blender/makesdna/DNA_node_types.h
source/blender/makesrna/intern/rna_nodetree.c
source/blender/nodes/composite/nodes/node_composite_math.c
source/blender/nodes/intern/node_socket.c
source/blender/nodes/intern/node_util.c
source/blender/nodes/intern/node_util.h
source/blender/nodes/shader/nodes/node_shader_math.c
source/blender/nodes/texture/nodes/node_texture_math.c

index 2ce81ba8359ac668b390fde765683036ea4ea4c9..55ec8306e1616df8b2c7cef2935adccde8fece67 100644 (file)
@@ -530,7 +530,7 @@ class NODE_PT_active_node_properties(Panel):
             layout.label(text="Inputs:")
             for socket in value_inputs:
                 row = layout.row()
-                socket.draw(context, row, node, iface_(socket.name, socket.bl_rna.translation_context))
+                socket.draw(context, row, node, iface_(socket.label if socket.label else socket.name, socket.bl_rna.translation_context))
 
 
 class NODE_PT_texture_mapping(Panel):
index 1c479c92ef5d5a345bb73cb8072882b9cc88707d..278e8ec1d894c508d7f34a08f4ee63c3f3e200af 100644 (file)
@@ -740,6 +740,7 @@ void BKE_node_preview_set_pixel(
  * \{ */
 
 void nodeLabel(struct bNodeTree *ntree, struct bNode *node, char *label, int maxlen);
+const char *nodeSocketLabel(const struct bNodeSocket *sock);
 
 int nodeGroupPoll(struct bNodeTree *nodetree, struct bNodeTree *grouptree);
 
index 64897d05c96c57b4c197f2c7a3c1f475e9e09c30..2a8c9f2a40b23ebda95da517cd1ff775c5ce3ec4 100644 (file)
@@ -3574,6 +3574,12 @@ void nodeLabel(bNodeTree *ntree, bNode *node, char *label, int maxlen)
   }
 }
 
+/* Get node socket label if it is set */
+const char *nodeSocketLabel(const bNodeSocket *sock)
+{
+  return (sock->label[0] != '\0') ? sock->label : sock->name;
+}
+
 static void node_type_base_defaults(bNodeType *ntype)
 {
   /* default size values */
index 2202eee3a53afd1c2782ef5ba825b46afa39bece..755e2cc16189c00324ebc43e9621ee1d848bcd82 100644 (file)
@@ -354,6 +354,8 @@ void node_from_view(struct bNode *node, float x, float y, float *rx, float *ry)
   nodeFromView(node, x, y, rx, ry);
 }
 
+
+
 /* based on settings in node, sets drawing rect info. each redraw! */
 static void node_update_basis(const bContext *C, bNodeTree *ntree, bNode *node)
 {
@@ -409,8 +411,8 @@ static void node_update_basis(const bContext *C, bNodeTree *ntree, bNode *node)
     /* align output buttons to the right */
     row = uiLayoutRow(layout, 1);
     uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_RIGHT);
-
-    nsock->typeinfo->draw((bContext *)C, row, &sockptr, &nodeptr, IFACE_(nsock->name));
+    const char *socket_label = nodeSocketLabel(nsock);
+    nsock->typeinfo->draw((bContext *)C, row, &sockptr, &nodeptr, IFACE_(socket_label));
 
     UI_block_align_end(node->block);
     UI_block_layout_resolve(node->block, NULL, &buty);
@@ -535,7 +537,8 @@ static void node_update_basis(const bContext *C, bNodeTree *ntree, bNode *node)
 
     row = uiLayoutRow(layout, 1);
 
-    nsock->typeinfo->draw((bContext *)C, row, &sockptr, &nodeptr, IFACE_(nsock->name));
+    const char *socket_label = nodeSocketLabel(nsock);
+    nsock->typeinfo->draw((bContext *)C, row, &sockptr, &nodeptr, IFACE_(socket_label));
 
     UI_block_align_end(node->block);
     UI_block_layout_resolve(node->block, NULL, &buty);
index 22202717b0cfadee1dc3c6e06b1fdbc4d60d924b..559048ab8cf044ff7ebeb8e1e5699ada41e59843 100644 (file)
@@ -116,6 +116,9 @@ typedef struct bNodeSocket {
   char display_shape;
   char _pad[3];
 
+  /** Custom dynamic defined label, MAX_NAME. */
+  char label[64];
+
   /** Cached data from execution. */
   void *cache;
 
index 68e3e522dffc0f3712fee5fa1234433cbde45da0..180bf96dd90cb21728c64f28e6a07fbdeb0f6585 100644 (file)
@@ -7891,6 +7891,11 @@ static void rna_def_node_socket(BlenderRNA *brna)
   RNA_def_struct_name_property(srna, prop);
   RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_NodeSocket_update");
 
+  prop = RNA_def_property(srna, "label", PROP_STRING, PROP_NONE);
+  RNA_def_property_string_sdna(prop, NULL, "label");
+  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+  RNA_def_property_ui_text(prop, "Label", "Custom dynamic defined socket label");
+
   prop = RNA_def_property(srna, "identifier", PROP_STRING, PROP_NONE);
   RNA_def_property_string_sdna(prop, NULL, "identifier");
   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
index 741c0e488064655b8558015cd15f70725836d582..3efb650c6ebd05ab97c6f7e726bbd1b3108de1e8 100644 (file)
@@ -32,43 +32,6 @@ static bNodeSocketTemplate cmp_node_math_in[] = {
 
 static bNodeSocketTemplate cmp_node_math_out[] = {{SOCK_FLOAT, 0, N_("Value")}, {-1, 0, ""}};
 
-static void node_shader_update_math(bNodeTree *UNUSED(ntree), bNode *node)
-{
-  bNodeSocket *sock = BLI_findlink(&node->inputs, 1);
-  nodeSetSocketAvailability(sock,
-                            !ELEM(node->custom1,
-                                  NODE_MATH_SQRT,
-                                  NODE_MATH_SIGN,
-                                  NODE_MATH_CEIL,
-                                  NODE_MATH_SINE,
-                                  NODE_MATH_ROUND,
-                                  NODE_MATH_FLOOR,
-                                  NODE_MATH_COSINE,
-                                  NODE_MATH_ARCSINE,
-                                  NODE_MATH_TANGENT,
-                                  NODE_MATH_ABSOLUTE,
-                                  NODE_MATH_RADIANS,
-                                  NODE_MATH_DEGREES,
-                                  NODE_MATH_FRACTION,
-                                  NODE_MATH_ARCCOSINE,
-                                  NODE_MATH_ARCTANGENT) &&
-                                !ELEM(node->custom1,
-                                      NODE_MATH_INV_SQRT,
-                                      NODE_MATH_TRUNC,
-                                      NODE_MATH_EXPONENT,
-                                      NODE_MATH_COSH,
-                                      NODE_MATH_SINH,
-                                      NODE_MATH_TANH));
-  bNodeSocket *sock2 = BLI_findlink(&node->inputs, 2);
-  nodeSetSocketAvailability(sock2,
-                            ELEM(node->custom1,
-                                 NODE_MATH_COMPARE,
-                                 NODE_MATH_MULTIPLY_ADD,
-                                 NODE_MATH_WRAP,
-                                 NODE_MATH_SMOOTH_MIN,
-                                 NODE_MATH_SMOOTH_MAX));
-}
-
 void register_node_type_cmp_math(void)
 {
   static bNodeType ntype;
@@ -76,7 +39,7 @@ void register_node_type_cmp_math(void)
   cmp_node_type_base(&ntype, CMP_NODE_MATH, "Math", NODE_CLASS_CONVERTOR, 0);
   node_type_socket_templates(&ntype, cmp_node_math_in, cmp_node_math_out);
   node_type_label(&ntype, node_math_label);
-  node_type_update(&ntype, node_shader_update_math);
+  node_type_update(&ntype, node_math_update);
 
   nodeRegisterType(&ntype);
 }
index 3485bd41660362b42d23ed58c153c38809fddfec..13f9039379d8c1c67755f55f40d31eeaa0be78b5 100644 (file)
@@ -276,6 +276,11 @@ void node_socket_copy_default_value(bNodeSocket *to, const bNodeSocket *from)
   }
   node_socket_init_default_value(to);
 
+  /* use label instead of name if it has been set */
+  if (from->label[0] != '\0') {
+    BLI_strncpy(to->name, from->label, NODE_MAXSTR);
+  }
+
   switch (from->typeinfo->type) {
     case SOCK_FLOAT: {
       bNodeSocketValueFloat *toval = to->default_value;
index b16c814ade9e8a7aa73671698497aa1b2c9b5e92..7d029e7a400aafab0a148e8b69de81b66e496a37 100644 (file)
@@ -77,6 +77,102 @@ void *node_initexec_curves(bNodeExecContext *UNUSED(context),
   return NULL; /* unused return */
 }
 
+/**** Updates ****/
+
+void node_sock_label(bNodeSocket *sock, const char *name)
+{
+  BLI_strncpy(sock->label, name, MAX_NAME);
+}
+
+void node_math_update(bNodeTree *UNUSED(ntree), bNode *node)
+{
+  bNodeSocket *sock1 = BLI_findlink(&node->inputs, 0);
+  bNodeSocket *sock2 = BLI_findlink(&node->inputs, 1);
+  bNodeSocket *sock3 = BLI_findlink(&node->inputs, 2);
+  nodeSetSocketAvailability(sock2,
+                            !ELEM(node->custom1,
+                                  NODE_MATH_SQRT,
+                                  NODE_MATH_SIGN,
+                                  NODE_MATH_CEIL,
+                                  NODE_MATH_SINE,
+                                  NODE_MATH_ROUND,
+                                  NODE_MATH_FLOOR,
+                                  NODE_MATH_COSINE,
+                                  NODE_MATH_ARCSINE,
+                                  NODE_MATH_TANGENT,
+                                  NODE_MATH_ABSOLUTE,
+                                  NODE_MATH_RADIANS,
+                                  NODE_MATH_DEGREES,
+                                  NODE_MATH_FRACTION,
+                                  NODE_MATH_ARCCOSINE,
+                                  NODE_MATH_ARCTANGENT) &&
+                                !ELEM(node->custom1,
+                                      NODE_MATH_INV_SQRT,
+                                      NODE_MATH_TRUNC,
+                                      NODE_MATH_EXPONENT,
+                                      NODE_MATH_COSH,
+                                      NODE_MATH_SINH,
+                                      NODE_MATH_TANH));
+  nodeSetSocketAvailability(sock3,
+                            ELEM(node->custom1,
+                                 NODE_MATH_COMPARE,
+                                 NODE_MATH_MULTIPLY_ADD,
+                                 NODE_MATH_WRAP,
+                                 NODE_MATH_SMOOTH_MIN,
+                                 NODE_MATH_SMOOTH_MAX));
+
+  if (sock1->label[0] != '\0') {
+    sock1->label[0] = '\0';
+  }
+  if (sock2->label[0] != '\0') {
+    sock2->label[0] = '\0';
+  }
+  if (sock3->label[0] != '\0') {
+    sock3->label[0] = '\0';
+  }
+
+  switch (node->custom1) {
+    case NODE_MATH_WRAP:
+      node_sock_label(sock2, "Min");
+      node_sock_label(sock3, "Max");
+      break;
+    case NODE_MATH_MULTIPLY_ADD:
+      node_sock_label(sock2, "Multiplier");
+      node_sock_label(sock3, "Addend");
+      break;
+    case NODE_MATH_LESS_THAN:
+    case NODE_MATH_GREATER_THAN:
+      node_sock_label(sock2, "Threshold");
+      break;
+    case NODE_MATH_PINGPONG:
+      node_sock_label(sock2, "Scale");
+      break;
+    case NODE_MATH_SNAP:
+      node_sock_label(sock2, "Increment");
+      break;
+    case NODE_MATH_POWER:
+      node_sock_label(sock1, "Base");
+      node_sock_label(sock2, "Exponent");
+      break;
+    case NODE_MATH_LOGARITHM:
+      node_sock_label(sock2, "Base");
+      break;
+    case NODE_MATH_DEGREES:
+      node_sock_label(sock1, "Radians");
+      break;
+    case NODE_MATH_RADIANS:
+      node_sock_label(sock1, "Degrees");
+      break;
+    case NODE_MATH_COMPARE:
+      node_sock_label(sock3, "Epsilon");
+      break;
+    case NODE_MATH_SMOOTH_MAX:
+    case NODE_MATH_SMOOTH_MIN:
+      node_sock_label(sock3, "Distance");
+      break;
+  }
+}
+
 /**** Labels ****/
 
 void node_blend_label(bNodeTree *UNUSED(ntree), bNode *node, char *label, int maxlen)
index 49117b38abac9a3baed311b2a63c573055a84802..a7f09d24a7098fd9a773f8e170f707fe41a71169 100644 (file)
@@ -66,8 +66,11 @@ extern void *node_initexec_curves(struct bNodeExecContext *context,
                                   struct bNode *node,
                                   bNodeInstanceKey key);
 
-/**** Labels ****/
+/**** Updates ****/
+void node_sock_label(struct bNodeSocket *sock, const char *name);
+void node_math_update(struct bNodeTree *ntree, struct bNode *node);
 
+/**** Labels ****/
 void node_blend_label(struct bNodeTree *ntree, struct bNode *node, char *label, int maxlen);
 void node_image_label(struct bNodeTree *ntree, struct bNode *node, char *label, int maxlen);
 void node_math_label(struct bNodeTree *ntree, struct bNode *node, char *label, int maxlen);
index 5d9da7788ee6e6e12de697ed1131565b9e195d74..c2bb48068acf742dfe8a6dcddd984b103c5aed2c 100644 (file)
@@ -101,43 +101,6 @@ static int gpu_shader_math(GPUMaterial *mat,
   }
 }
 
-static void node_shader_update_math(bNodeTree *UNUSED(ntree), bNode *node)
-{
-  bNodeSocket *sock = BLI_findlink(&node->inputs, 1);
-  nodeSetSocketAvailability(sock,
-                            !ELEM(node->custom1,
-                                  NODE_MATH_SQRT,
-                                  NODE_MATH_SIGN,
-                                  NODE_MATH_CEIL,
-                                  NODE_MATH_SINE,
-                                  NODE_MATH_ROUND,
-                                  NODE_MATH_FLOOR,
-                                  NODE_MATH_COSINE,
-                                  NODE_MATH_ARCSINE,
-                                  NODE_MATH_TANGENT,
-                                  NODE_MATH_ABSOLUTE,
-                                  NODE_MATH_RADIANS,
-                                  NODE_MATH_DEGREES,
-                                  NODE_MATH_FRACTION,
-                                  NODE_MATH_ARCCOSINE,
-                                  NODE_MATH_ARCTANGENT) &&
-                                !ELEM(node->custom1,
-                                      NODE_MATH_INV_SQRT,
-                                      NODE_MATH_TRUNC,
-                                      NODE_MATH_EXPONENT,
-                                      NODE_MATH_COSH,
-                                      NODE_MATH_SINH,
-                                      NODE_MATH_TANH));
-  bNodeSocket *sock2 = BLI_findlink(&node->inputs, 2);
-  nodeSetSocketAvailability(sock2,
-                            ELEM(node->custom1,
-                                 NODE_MATH_COMPARE,
-                                 NODE_MATH_MULTIPLY_ADD,
-                                 NODE_MATH_WRAP,
-                                 NODE_MATH_SMOOTH_MIN,
-                                 NODE_MATH_SMOOTH_MAX));
-}
-
 void register_node_type_sh_math(void)
 {
   static bNodeType ntype;
@@ -146,7 +109,7 @@ void register_node_type_sh_math(void)
   node_type_socket_templates(&ntype, sh_node_math_in, sh_node_math_out);
   node_type_label(&ntype, node_math_label);
   node_type_gpu(&ntype, gpu_shader_math);
-  node_type_update(&ntype, node_shader_update_math);
+  node_type_update(&ntype, node_math_update);
 
   nodeRegisterType(&ntype);
 }
index 310300df2040c2c5636ed71f2fdd6f0189244147..8abe638216cb453b698c704aee265f06c696963d 100644 (file)
@@ -336,43 +336,6 @@ static void exec(void *data,
   tex_output(node, execdata, in, out[0], &valuefn, data);
 }
 
-static void node_shader_update_math(bNodeTree *UNUSED(ntree), bNode *node)
-{
-  bNodeSocket *sock = BLI_findlink(&node->inputs, 1);
-  nodeSetSocketAvailability(sock,
-                            !ELEM(node->custom1,
-                                  NODE_MATH_SQRT,
-                                  NODE_MATH_SIGN,
-                                  NODE_MATH_CEIL,
-                                  NODE_MATH_SINE,
-                                  NODE_MATH_ROUND,
-                                  NODE_MATH_FLOOR,
-                                  NODE_MATH_COSINE,
-                                  NODE_MATH_ARCSINE,
-                                  NODE_MATH_TANGENT,
-                                  NODE_MATH_ABSOLUTE,
-                                  NODE_MATH_RADIANS,
-                                  NODE_MATH_DEGREES,
-                                  NODE_MATH_FRACTION,
-                                  NODE_MATH_ARCCOSINE,
-                                  NODE_MATH_ARCTANGENT) &&
-                                !ELEM(node->custom1,
-                                      NODE_MATH_INV_SQRT,
-                                      NODE_MATH_TRUNC,
-                                      NODE_MATH_EXPONENT,
-                                      NODE_MATH_COSH,
-                                      NODE_MATH_SINH,
-                                      NODE_MATH_TANH));
-  bNodeSocket *sock2 = BLI_findlink(&node->inputs, 2);
-  nodeSetSocketAvailability(sock2,
-                            ELEM(node->custom1,
-                                 NODE_MATH_COMPARE,
-                                 NODE_MATH_MULTIPLY_ADD,
-                                 NODE_MATH_WRAP,
-                                 NODE_MATH_SMOOTH_MIN,
-                                 NODE_MATH_SMOOTH_MAX));
-}
-
 void register_node_type_tex_math(void)
 {
   static bNodeType ntype;
@@ -382,7 +345,7 @@ void register_node_type_tex_math(void)
   node_type_label(&ntype, node_math_label);
   node_type_storage(&ntype, "", NULL, NULL);
   node_type_exec(&ntype, NULL, NULL, exec);
-  node_type_update(&ntype, node_shader_update_math);
+  node_type_update(&ntype, node_math_update);
 
   nodeRegisterType(&ntype);
 }