From dd56dc0f4b078cd5bc9f7f23ce4dcad48b5639d1 Mon Sep 17 00:00:00 2001 From: DolphinDream Date: Tue, 7 Nov 2017 07:07:07 -0500 Subject: [PATCH 1/2] Add color in/out MK1 nodes to use the new SvColorSocket type Note: Further cleanup could have the ColorOut node socket use the default prop (use_prop) instead of the "unit_color", though in that case this node will have to rely on the SvColorSocket default color values for the prop. --- index.md | 2 + nodes/vector/color_in_mk1.py | 151 ++++++++++++++++++++++++++++++++++ nodes/vector/color_out_mk1.py | 104 +++++++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 nodes/vector/color_in_mk1.py create mode 100644 nodes/vector/color_out_mk1.py diff --git a/index.md b/index.md index e4b2069fb..31be257bc 100644 --- a/index.md +++ b/index.md @@ -276,6 +276,8 @@ --- SvColorsInNode SvColorsOutNode + SvColorsInNodeMK1 + SvColorsOutNodeMK1 --- SvMatrixNormalNode SvMatrixTrackToNode diff --git a/nodes/vector/color_in_mk1.py b/nodes/vector/color_in_mk1.py new file mode 100644 index 000000000..0c83fa826 --- /dev/null +++ b/nodes/vector/color_in_mk1.py @@ -0,0 +1,151 @@ +# ##### 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. +# +# ##### END GPL LICENSE BLOCK ##### + +import colorsys +import bpy +from bpy.props import FloatProperty, BoolProperty + +from sverchok.node_tree import SverchCustomTreeNode, StringsSocket +from sverchok.data_structure import updateNode, fullList +from sverchok.utils.sv_itertools import sv_zip_longest + +# pylint: disable=w0141 + + +def fprop_generator(**altprops): + # min can be overwritten by passing in min=some_value into the altprops dict + default_dict_vals = dict(update=updateNode, precision=3, min=0.0, max=1.0) + default_dict_vals.update(**altprops) + return FloatProperty(**default_dict_vals) + + +class SvColorsInNodeMK1(bpy.types.Node, SverchCustomTreeNode): + ''' rgb(a) ---> color /// Generator for Color data''' + bl_idname = 'SvColorsInNodeMK1' + bl_label = 'Color in MK1' + sv_icon = 'SV_COMBINE_IN' + + def psuedo_update(self, context): + for idx, socket in enumerate(self.selected_mode): + self.inputs[idx].name = socket + self.inputs[idx].prop_name = socket.lower() + '_' + updateNode(self, context) + + use_alpha = BoolProperty(default=False, update=updateNode) + + r_ = fprop_generator(name='R', description='Red (0..1)') + g_ = fprop_generator(name='G', description='Green (0..1)') + b_ = fprop_generator(name='B', description='Blue (0..1)') + a_ = fprop_generator(name='A', description='Alpha (0..1) - opacity', default=1.0) + + h_ = fprop_generator(name='H', description='Hue (0..1)') + s_ = fprop_generator(name='S', description='Saturation (0..1) - different for hsv and hsl') + l_ = fprop_generator(name='L', description='Lightness / Brightness (0..1)') + v_ = fprop_generator(name='V', description='Value / Brightness (0..1)') + + mode_options = [ + ("RGB", "RGB", "", 0), + ("HSV", "HSV", "", 1), + ("HSL", "HSL", "", 2), + ] + + selected_mode = bpy.props.EnumProperty( + default="RGB", description="offers color spaces", + items=mode_options, update=psuedo_update + ) + + def draw_buttons(self, context, layout): + layout.prop(self, 'selected_mode', expand=True) + layout.prop(self, 'use_alpha') + + def sv_init(self, context): + self.width = 110 + inew = self.inputs.new + inew('StringsSocket', "R").prop_name = 'r_' + inew('StringsSocket', "G").prop_name = 'g_' + inew('StringsSocket', "B").prop_name = 'b_' + inew('StringsSocket', "A").prop_name = 'a_' + onew = self.outputs.new + onew('SvColorSocket', "Colors") + + def process(self): + """ + colorsys.rgb_to_yiq(r, g, b) + colorsys.yiq_to_rgb(y, i, q) + colorsys.rgb_to_hls(r, g, b) + colorsys.hls_to_rgb(h, l, s) + colorsys.rgb_to_hsv(r, g, b) + colorsys.hsv_to_rgb(h, s, v) + """ + + if not self.outputs['Colors'].is_linked: + return + inputs = self.inputs + + i0 = inputs[0].sv_get() + i1 = inputs[1].sv_get() + i2 = inputs[2].sv_get() + i3 = inputs[3].sv_get() + + series_vec = [] + max_obj = max(map(len, (i0, i1, i2, i3))) + fullList(i0, max_obj) + fullList(i1, max_obj) + fullList(i2, max_obj) + fullList(i3, max_obj) + for i in range(max_obj): + + max_v = max(map(len, (i0[i], i1[i], i2[i], i3[i]))) + fullList(i0[i], max_v) + fullList(i1[i], max_v) + fullList(i2[i], max_v) + fullList(i3[i], max_v) + + if self.selected_mode == 'RGB': + if self.use_alpha: + series_vec.append(list(zip(i0[i], i1[i], i2[i], i3[i]))) + else: + series_vec.append(list(zip(i0[i], i1[i], i2[i]))) + else: + if self.selected_mode == 'HSV': + convert = colorsys.hsv_to_rgb + elif self.selected_mode == 'HSL': + convert = colorsys.hls_to_rgb + + # not sure if the python hsl function is simply named wrong but accepts + # the params in the right order.. or they need to be supplied i0[i] i2[i] i1[i] + # colordata = [list(convert(c0, c1, c2)) + [c3] for c0, c1, c2, c3 in zip(i0[i], i1[i], i2[i], i3[i])] + colordata = [] + for c0, c1, c2, c3 in zip(i0[i], i1[i], i2[i], i3[i]): + colorv = list(convert(c0, c1, c2)) + if self.use_alpha: + colordata.append([colorv[0], colorv[1], colorv[2], c3]) + else: + colordata.append(colorv) + + series_vec.append(colordata) + + self.outputs['Colors'].sv_set(series_vec) + + +def register(): + bpy.utils.register_class(SvColorsInNodeMK1) + + +def unregister(): + bpy.utils.unregister_class(SvColorsInNodeMK1) diff --git a/nodes/vector/color_out_mk1.py b/nodes/vector/color_out_mk1.py new file mode 100644 index 000000000..f622cce00 --- /dev/null +++ b/nodes/vector/color_out_mk1.py @@ -0,0 +1,104 @@ +# ##### 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. +# +# ##### END GPL LICENSE BLOCK ##### + +import colorsys +import bpy +from bpy.props import FloatProperty, BoolProperty, FloatVectorProperty +from mathutils import Color + +from sverchok.node_tree import SverchCustomTreeNode, StringsSocket +from sverchok.data_structure import updateNode, fullList, dataCorrect +from sverchok.utils.sv_itertools import sv_zip_longest + +# pylint: disable=w0141 + + +class SvColorsOutNodeMK1(bpy.types.Node, SverchCustomTreeNode): + ''' color ---> rgb(a) /// Generator for Color data''' + bl_idname = 'SvColorsOutNodeMK1' + bl_label = 'Color Out MK1' + sv_icon = 'SV_COMBINE_OUT' + + def psuedo_update(self, context): + for idx, socket in enumerate(self.selected_mode): + self.outputs[idx].name = socket + updateNode(self, context) + + unit_color = FloatVectorProperty( + update=updateNode, name='', default=(.3, .3, .2, 1.0), + size=4, min=0.0, max=1.0, subtype='COLOR' + ) + + use_alpha = BoolProperty(default=False, update=updateNode) + + def sv_init(self, context): + self.width = 110 + inew = self.inputs.new + inew('SvColorSocket', "Colors").prop_name = "unit_color" + onew = self.outputs.new + onew('StringsSocket', "R") + onew('StringsSocket', "G") + onew('StringsSocket', "B") + onew('StringsSocket', "A") + + def draw_buttons(self, context, layout): + layout.prop(self, 'use_alpha') + + def process(self): + """ + colorsys.rgb_to_yiq(r, g, b) + colorsys.yiq_to_rgb(y, i, q) + colorsys.rgb_to_hls(r, g, b) + colorsys.hls_to_rgb(h, l, s) + colorsys.rgb_to_hsv(r, g, b) + colorsys.hsv_to_rgb(h, s, v) + """ + + color_input = self.inputs['Colors'] + if color_input.is_linked: + abc = self.inputs['Colors'].sv_get() + data = dataCorrect(abc) + else: + data = [[self.unit_color[:]]] + + A, B, C, D = [], [], [], [] + if self.use_alpha: + for obj in data: + a_, b_, c_, d_ = (list(x) for x in zip(*obj)) + A.append(a_) + B.append(b_) + C.append(c_) + D.append(d_) + for i, socket in enumerate(self.outputs): + self.outputs[socket.name].sv_set([A, B, C, D][i]) + else: + for obj in data: + a_, b_, c_ = (list(x) for x in zip(*obj)) + A.append(a_) + B.append(b_) + C.append(c_) + for i, socket in enumerate(self.outputs[:3]): + self.outputs[socket.name].sv_set([A, B, C][i]) + + +def register(): + bpy.utils.register_class(SvColorsOutNodeMK1) + + +def unregister(): + bpy.utils.unregister_class(SvColorsOutNodeMK1) -- GitLab From fdfda3a1945d8f8b037c457ab06ba0e43b1e2e70 Mon Sep 17 00:00:00 2001 From: DolphinDream Date: Fri, 8 Dec 2017 20:25:20 -0500 Subject: [PATCH 2/2] Moved previous color in/out nodes to old nodes --- index.md | 2 -- {nodes/vector => old_nodes}/color_in.py | 0 {nodes/vector => old_nodes}/color_out.py | 0 3 files changed, 2 deletions(-) rename {nodes/vector => old_nodes}/color_in.py (100%) rename {nodes/vector => old_nodes}/color_out.py (100%) diff --git a/index.md b/index.md index 31be257bc..d3e186750 100644 --- a/index.md +++ b/index.md @@ -274,8 +274,6 @@ SvFormulaShapeNode SvHeavyTriangulateNode --- - SvColorsInNode - SvColorsOutNode SvColorsInNodeMK1 SvColorsOutNodeMK1 --- diff --git a/nodes/vector/color_in.py b/old_nodes/color_in.py similarity index 100% rename from nodes/vector/color_in.py rename to old_nodes/color_in.py diff --git a/nodes/vector/color_out.py b/old_nodes/color_out.py similarity index 100% rename from nodes/vector/color_out.py rename to old_nodes/color_out.py -- GitLab