-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathnoise_mk2.py
202 lines (160 loc) · 7.02 KB
/
noise_mk2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# ##### 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 bpy
from bpy.props import EnumProperty, IntProperty, BoolProperty
from mathutils import noise
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode
from sverchok.utils.sv_noise_utils import noise_options, PERLIN_ORIGINAL, noise_numpy_types
import numpy as np
def numpy_noise(vecs, out, out_mode, seed, noise_function, smooth, output_numpy):
if out_mode == 'VECTOR':
obj = np.array(vecs)
r_noise = np.stack((
noise_function(obj, seed, smooth),
noise_function(obj, seed + 1, smooth),
noise_function(obj, seed + 2, smooth)
)).T
out.append((2 * r_noise - 1) if output_numpy else (2 * r_noise - 1).tolist())
else:
if output_numpy:
out.append(noise_function(np.array(vecs), seed, smooth))
else:
out.append(noise_function(np.array(vecs), seed, smooth).tolist())
def mathulis_noise(vecs, out, out_mode, noise_type, noise_function, output_numpy):
if out_mode == 'VECTOR':
if output_numpy:
out.append(np.array([noise_function(v, noise_basis=noise_type)[:] for v in vecs]))
else:
out.append([noise_function(v, noise_basis=noise_type)[:] for v in vecs])
else:
vecs = np.array([noise_function(v, noise_basis=noise_type)[:] for v in vecs])
vecs -= [0, 0, 1]
noise_output = np.linalg.norm(vecs, axis=1)*0.5
out.append(noise_output if output_numpy else noise_output.tolist())
avail_noise = [(t[0], t[0].title().replace('_', ' '), t[0].title(), '', t[1]) for t in noise_options]
for idx, new_type in enumerate(noise_numpy_types.keys()):
avail_noise.append((new_type, new_type.title().replace('_', ' '), new_type.title(), '', 100 + idx))
class SvNoiseNodeMK2(SverchCustomTreeNode, bpy.types.Node):
"""
Triggers: Vector Noise
Tooltip: Affect input verts with a noise function.
A short description for reader of node code
"""
bl_idname = 'SvNoiseNodeMK2'
bl_label = 'Vector Noise'
bl_icon = 'FORCE_TURBULENCE'
sv_icon = 'SV_VECTOR_NOISE'
replacement_nodes = [('SvNoiseNodeMK3', None, None)]
def changeMode(self, context):
outputs = self.outputs
if self.out_mode == 'SCALAR':
if 'Noise S' not in outputs:
outputs[0].replace_socket('SvStringsSocket', 'Noise S')
return
if self.out_mode == 'VECTOR':
if 'Noise V' not in outputs:
outputs[0].replace_socket('SvVerticesSocket', 'Noise V')
return
updateNode(self, context)
out_modes = [
('SCALAR', 'Scalar', 'Scalar output', '', 1),
('VECTOR', 'Vector', 'Vector output', '', 2)]
out_mode: EnumProperty(
items=out_modes,
default='VECTOR',
description='Output type',
update=changeMode)
noise_type: EnumProperty(
items=avail_noise,
default=PERLIN_ORIGINAL,
description="Noise type",
update=updateNode)
seed: IntProperty(default=0, name='Seed', update=updateNode)
smooth: BoolProperty(
name='Smooth',
description='Smooth noise',
default=True, update=updateNode)
interpolate: BoolProperty(
name='Interpolate',
description='Interpolate gradients',
default=True, update=updateNode)
output_numpy: BoolProperty(
name='Output NumPy',
description='Output NumPy arrays',
default=False, update=updateNode)
def sv_init(self, context):
self.inputs.new('SvVerticesSocket', 'Vertices')
self.inputs.new('SvStringsSocket', 'Seed').prop_name = 'seed'
self.outputs.new('SvVerticesSocket', 'Noise V')
def draw_buttons(self, context, layout):
layout.prop(self, 'out_mode', expand=True)
layout.prop(self, 'noise_type', text="Type")
if self.noise_type in noise_numpy_types.keys():
row = layout.row(align=True)
row.prop(self, 'smooth', toggle=True)
row.prop(self, 'interpolate', toggle=True)
def draw_buttons_ext(self, ctx, layout):
self.draw_buttons(ctx, layout)
layout.prop(self, "output_numpy", toggle=False)
def rclick_menu(self, context, layout):
layout.prop_menu_enum(self, "out_mode")
layout.prop_menu_enum(self, "noise_type")
if self.noise_type in noise_numpy_types.keys():
layout.prop(self, 'smooth', toggle=True)
layout.prop(self, 'interpolate', toggle=True)
layout.prop(self, "output_numpy", toggle=True)
def process(self):
inputs, outputs = self.inputs, self.outputs
if not (outputs[0].is_linked and inputs[0].is_linked):
return
out = []
verts = inputs['Vertices'].sv_get(deepcopy=False)
seeds = inputs['Seed'].sv_get()[0]
max_len = max(map(len, (seeds, verts)))
noise_type = self.noise_type
out_mode = self.out_mode
output_numpy = self.output_numpy
if noise_type in noise_numpy_types.keys():
noise_function = noise_numpy_types[noise_type][self.interpolate]
smooth = self.smooth
for i in range(max_len):
seed = seeds[min(i, len(seeds)-1)]
obj_id = min(i, len(verts)-1)
numpy_noise(verts[obj_id], out, out_mode, seed, noise_function, smooth, output_numpy)
else:
noise_function = noise.noise_vector
for i in range(max_len):
seed = seeds[min(i, len(seeds)-1)]
obj_id = min(i, len(verts)-1)
# 0 unsets the seed and generates unreproducable output based on system time
seed_val = int(round(seed)) or 140230
noise.seed_set(seed_val)
mathulis_noise(verts[obj_id], out, out_mode, noise_type, noise_function, output_numpy)
outputs[0].sv_set(out)
def draw_label(self):
if self.hide:
if not self.inputs['Seed'].is_linked:
seed = ' + ({0})'.format(str(int(self.seed)))
else:
seed = ' + seed(s)'
return self.noise_type.title() + seed
else:
return self.label or self.name
classes = [SvNoiseNodeMK2]
register, unregister = bpy.utils.register_classes_factory(classes)