io_scene_gltf2: quiet error running when context.space is None
[blender-addons.git] / ant_landscape / mesh_ant_displace.py
blob132ca6fe660f14c2c18d6320c67e1f1fb81cc746
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Another Noise Tool - Mesh Displace
4 # Jimmy Hazevoet
6 # ------------------------------------------------------------
7 # import modules
8 import bpy
9 from bpy.props import (
10 BoolProperty,
11 EnumProperty,
12 FloatProperty,
13 IntProperty,
14 StringProperty,
15 FloatVectorProperty,
17 from .ant_functions import (
18 draw_ant_refresh,
19 draw_ant_main,
20 draw_ant_noise,
21 draw_ant_displace,
23 from .ant_noise import noise_gen
24 from ant_landscape import ant_noise
26 # ------------------------------------------------------------
27 # Do vert displacement
30 class AntMeshDisplace(bpy.types.Operator):
31 bl_idname = "mesh.ant_displace"
32 bl_label = "Another Noise Tool - Displace"
33 bl_description = "Displace mesh vertices"
34 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
36 ant_terrain_name: StringProperty(
37 name="Name",
38 default="Landscape"
40 land_material: StringProperty(
41 name='Material',
42 default="",
43 description="Terrain material"
45 water_material: StringProperty(
46 name='Material',
47 default="",
48 description="Water plane material"
50 texture_block: StringProperty(
51 name="Texture",
52 default=""
54 at_cursor: BoolProperty(
55 name="Cursor",
56 default=True,
57 description="Place at cursor location",
59 smooth_mesh: BoolProperty(
60 name="Smooth",
61 default=True,
62 description="Shade smooth"
64 tri_face: BoolProperty(
65 name="Triangulate",
66 default=False,
67 description="Triangulate faces"
69 sphere_mesh: BoolProperty(
70 name="Sphere",
71 default=False,
72 description="Generate uv sphere - remove doubles when ready"
74 subdivision_x: IntProperty(
75 name="Subdivisions X",
76 default=128,
77 min=4,
78 max=6400,
79 description="Mesh X subdivisions"
81 subdivision_y: IntProperty(
82 default=128,
83 name="Subdivisions Y",
84 min=4,
85 max=6400,
86 description="Mesh Y subdivisions"
88 mesh_size: FloatProperty(
89 default=2.0,
90 name="Mesh Size",
91 min=0.01,
92 max=100000.0,
93 description="Mesh size"
95 mesh_size_x: FloatProperty(
96 default=2.0,
97 name="Mesh Size X",
98 min=0.01,
99 description="Mesh x size"
101 mesh_size_y: FloatProperty(
102 name="Mesh Size Y",
103 default=2.0,
104 min=0.01,
105 description="Mesh y size"
108 random_seed: IntProperty(
109 name="Random Seed",
110 default=0,
111 min=0,
112 description="Randomize noise origin"
114 noise_offset_x: FloatProperty(
115 name="Offset X",
116 default=0.0,
117 description="Noise X Offset"
119 noise_offset_y: FloatProperty(
120 name="Offset Y",
121 default=0.0,
122 description="Noise Y Offset"
124 noise_offset_z: FloatProperty(
125 name="Offset Z",
126 default=0.0,
127 description="Noise Z Offset"
129 noise_size_x: FloatProperty(
130 default=1.0,
131 name="Size X",
132 min=0.01,
133 max=1000.0,
134 description="Noise x size"
136 noise_size_y: FloatProperty(
137 name="Size Y",
138 default=1.0,
139 min=0.01,
140 max=1000.0,
141 description="Noise y size"
143 noise_size_z: FloatProperty(
144 name="Size Z",
145 default=1.0,
146 min=0.01,
147 max=1000.0,
148 description="Noise Z size"
150 noise_size: FloatProperty(
151 name="Noise Size",
152 default=0.25,
153 min=0.01,
154 max=1000.0,
155 description="Noise size"
157 noise_type: EnumProperty(
158 name="Noise Type",
159 default='hetero_terrain',
160 description="Noise type",
161 items=[
162 ('multi_fractal', "Multi Fractal", "Blender: Multi Fractal algorithm", 0),
163 ('ridged_multi_fractal', "Ridged MFractal", "Blender: Ridged Multi Fractal", 1),
164 ('hybrid_multi_fractal', "Hybrid MFractal", "Blender: Hybrid Multi Fractal", 2),
165 ('hetero_terrain', "Hetero Terrain", "Blender: Hetero Terrain", 3),
166 ('fractal', "fBm Fractal", "Blender: fBm - Fractional Browninian motion", 4),
167 ('turbulence_vector', "Turbulence", "Blender: Turbulence Vector", 5),
168 ('variable_lacunarity', "Distorted Noise", "Blender: Distorted Noise", 6),
169 ('marble_noise', "Marble", "A.N.T.: Marble Noise", 7),
170 ('shattered_hterrain', "Shattered hTerrain", "A.N.T.: Shattered hTerrain", 8),
171 ('strata_hterrain', "Strata hTerrain", "A.N.T: Strata hTerrain", 9),
172 ('ant_turbulence', "Another Noise", "A.N.T: Turbulence variation", 10),
173 ('vl_noise_turbulence', "vlNoise turbulence", "A.N.T: Real vlNoise turbulence", 11),
174 ('vl_hTerrain', "vlNoise hTerrain", "A.N.T: vlNoise hTerrain", 12),
175 ('distorted_heteroTerrain', "Distorted hTerrain", "A.N.T distorted hTerrain", 13),
176 ('double_multiFractal', "Double MultiFractal", "A.N.T: double multiFractal", 14),
177 ('rocks_noise', "Noise Rocks", "A.N.T: turbulence variation", 15),
178 ('slick_rock', "Slick Rock", "A.N.T: slick rock", 16),
179 ('planet_noise', "Planet Noise", "Planet Noise by: Farsthary", 17),
180 ('blender_texture', "Blender Texture - Texture Nodes", "Blender texture data block", 18)]
182 basis_type: EnumProperty(
183 name="Noise Basis",
184 default=ant_noise.noise_basis_default,
185 description="Noise basis algorithms",
186 items=ant_noise.noise_basis
188 vl_basis_type: EnumProperty(
189 name="vlNoise Basis",
190 default=ant_noise.noise_basis_default,
191 description="VLNoise basis algorithms",
192 items=ant_noise.noise_basis
194 distortion: FloatProperty(
195 name="Distortion",
196 default=1.0,
197 min=0.01,
198 max=100.0,
199 description="Distortion amount"
201 hard_noise: EnumProperty(
202 name="Soft Hard",
203 default="0",
204 description="Soft Noise, Hard noise",
205 items=[
206 ("0", "Soft", "Soft Noise", 0),
207 ("1", "Hard", "Hard noise", 1)]
209 noise_depth: IntProperty(
210 name="Depth",
211 default=8,
212 min=0,
213 max=16,
214 description="Noise Depth - number of frequencies in the fBm"
216 amplitude: FloatProperty(
217 name="Amp",
218 default=0.5,
219 min=0.01,
220 max=1.0,
221 description="Amplitude"
223 frequency: FloatProperty(
224 name="Freq",
225 default=2.0,
226 min=0.01,
227 max=5.0,
228 description="Frequency"
230 dimension: FloatProperty(
231 name="Dimension",
232 default=1.0,
233 min=0.01,
234 max=2.0,
235 description="H - fractal dimension of the roughest areas"
237 lacunarity: FloatProperty(
238 name="Lacunarity",
239 min=0.01,
240 max=6.0,
241 default=2.0,
242 description="Lacunarity - gap between successive frequencies"
244 offset: FloatProperty(
245 name="Offset",
246 default=1.0,
247 min=0.01,
248 max=6.0,
249 description="Offset - raises the terrain from sea level"
251 gain: FloatProperty(
252 name="Gain",
253 default=1.0,
254 min=0.01,
255 max=6.0,
256 description="Gain - scale factor"
258 marble_bias: EnumProperty(
259 name="Bias",
260 default="0",
261 description="Marble bias",
262 items=[
263 ("0", "Sin", "Sin", 0),
264 ("1", "Cos", "Cos", 1),
265 ("2", "Tri", "Tri", 2),
266 ("3", "Saw", "Saw", 3)]
268 marble_sharp: EnumProperty(
269 name="Sharp",
270 default="0",
271 description="Marble sharpness",
272 items=[
273 ("0", "Soft", "Soft", 0),
274 ("1", "Sharp", "Sharp", 1),
275 ("2", "Sharper", "Sharper", 2),
276 ("3", "Soft inv.", "Soft", 3),
277 ("4", "Sharp inv.", "Sharp", 4),
278 ("5", "Sharper inv.", "Sharper", 5)]
280 marble_shape: EnumProperty(
281 name="Shape",
282 default="0",
283 description="Marble shape",
284 items=[
285 ("0", "Default", "Default", 0),
286 ("1", "Ring", "Ring", 1),
287 ("2", "Swirl", "Swirl", 2),
288 ("3", "Bump", "Bump", 3),
289 ("4", "Wave", "Wave", 4),
290 ("5", "Z", "Z", 5),
291 ("6", "Y", "Y", 6),
292 ("7", "X", "X", 7)]
294 height: FloatProperty(
295 name="Height",
296 default=0.25,
297 min=-10000.0,
298 max=10000.0,
299 description="Noise intensity scale"
301 height_invert: BoolProperty(
302 name="Invert",
303 default=False,
304 description="Height invert",
306 height_offset: FloatProperty(
307 name="Offset",
308 default=0.0,
309 min=-10000.0,
310 max=10000.0,
311 description="Height offset"
314 fx_mixfactor: FloatProperty(
315 name="Mix Factor",
316 default=0.0,
317 min=-1.0,
318 max=1.0,
319 description="Effect mix factor: -1.0 = Noise, +1.0 = Effect"
321 fx_mix_mode: EnumProperty(
322 name="Effect Mix",
323 default="0",
324 description="Effect mix mode",
325 items=[
326 ("0", "Mix", "Mix", 0),
327 ("1", "Add", "Add", 1),
328 ("2", "Sub", "Subtract", 2),
329 ("3", "Mul", "Multiply", 3),
330 ("4", "Abs", "Absolute", 4),
331 ("5", "Scr", "Screen", 5),
332 ("6", "Mod", "Modulo", 6),
333 ("7", "Min", "Minimum", 7),
334 ("8", "Max", "Maximum", 8)
337 fx_type: EnumProperty(
338 name="Effect Type",
339 default="0",
340 description="Effect type",
341 items=[
342 ("0", "None", "No effect", 0),
343 ("1", "Gradient", "Gradient", 1),
344 ("2", "Waves", "Waves - Bumps", 2),
345 ("3", "Zigzag", "Zigzag", 3),
346 ("4", "Wavy", "Wavy", 4),
347 ("5", "Bump", "Bump", 5),
348 ("6", "Dots", "Dots", 6),
349 ("7", "Rings", "Rings", 7),
350 ("8", "Spiral", "Spiral", 8),
351 ("9", "Square", "Square", 9),
352 ("10", "Blocks", "Blocks", 10),
353 ("11", "Grid", "Grid", 11),
354 ("12", "Tech", "Tech", 12),
355 ("13", "Crackle", "Crackle", 13),
356 ("14", "Cracks", "Cracks", 14),
357 ("15", "Rock", "Rock noise", 15),
358 ("16", "Lunar", "Craters", 16),
359 ("17", "Cosine", "Cosine", 17),
360 ("18", "Spikey", "Spikey", 18),
361 ("19", "Stone", "Stone", 19),
362 ("20", "Flat Turb", "Flat turbulence", 20),
363 ("21", "Flat Voronoi", "Flat voronoi", 21)
366 fx_bias: EnumProperty(
367 name="Effect Bias",
368 default="0",
369 description="Effect bias type",
370 items=[
371 ("0", "Sin", "Sin", 0),
372 ("1", "Cos", "Cos", 1),
373 ("2", "Tri", "Tri", 2),
374 ("3", "Saw", "Saw", 3),
375 ("4", "None", "None", 4)
378 fx_turb: FloatProperty(
379 name="Distortion",
380 default=0.0,
381 min=0.0,
382 max=1000.0,
383 description="Effect turbulence distortion"
385 fx_depth: IntProperty(
386 name="Depth",
387 default=0,
388 min=0,
389 max=16,
390 description="Effect depth - number of frequencies"
392 fx_amplitude: FloatProperty(
393 name="Amp",
394 default=0.5,
395 min=0.01,
396 max=1.0,
397 description="Amplitude"
399 fx_frequency: FloatProperty(
400 name="Freq",
401 default=2.0,
402 min=0.01,
403 max=5.0,
404 description="Frequency"
406 fx_size: FloatProperty(
407 name="Effect Size",
408 default=1.0,
409 min=0.01,
410 max=1000.0,
411 description="Effect size"
413 fx_loc_x: FloatProperty(
414 name="Offset X",
415 default=0.0,
416 description="Effect x offset"
418 fx_loc_y: FloatProperty(
419 name="Offset Y",
420 default=0.0,
421 description="Effect y offset"
423 fx_height: FloatProperty(
424 name="Intensity",
425 default=1.0,
426 min=-1000.0,
427 max=1000.0,
428 description="Effect intensity scale"
430 fx_invert: BoolProperty(
431 name="Invert",
432 default=False,
433 description="Effect invert"
435 fx_offset: FloatProperty(
436 name="Offset",
437 default=0.0,
438 min=-1000.0,
439 max=1000.0,
440 description="Effect height offset"
443 edge_falloff: EnumProperty(
444 name="Falloff",
445 default="0",
446 description="Flatten edges",
447 items=[
448 ("0", "None", "None", 0),
449 ("1", "Y", "Y Falloff", 1),
450 ("2", "X", "X Falloff", 2),
451 ("3", "X Y", "X Y Falloff", 3)]
453 falloff_x: FloatProperty(
454 name="Falloff X",
455 default=4.0,
456 min=0.1,
457 max=100.0,
458 description="Falloff x scale"
460 falloff_y: FloatProperty(
461 name="Falloff Y",
462 default=4.0,
463 min=0.1,
464 max=100.0,
465 description="Falloff y scale"
467 edge_level: FloatProperty(
468 name="Edge Level",
469 default=0.0,
470 min=-10000.0,
471 max=10000.0,
472 description="Edge level, sealevel offset"
474 maximum: FloatProperty(
475 name="Maximum",
476 default=1.0,
477 min=-10000.0,
478 max=10000.0,
479 description="Maximum, flattens terrain at plateau level"
481 minimum: FloatProperty(
482 name="Minimum",
483 default=-1.0,
484 min=-10000.0,
485 max=10000.0,
486 description="Minimum, flattens terrain at seabed level"
488 vert_group: StringProperty(
489 name="Vertex Group",
490 default=""
492 strata: FloatProperty(
493 name="Amount",
494 default=5.0,
495 min=0.01,
496 max=1000.0,
497 description="Strata layers / terraces"
499 strata_type: EnumProperty(
500 name="Strata",
501 default="0",
502 description="Strata types",
503 items=[
504 ("0", "None", "No strata", 0),
505 ("1", "Smooth", "Smooth transitions", 1),
506 ("2", "Sharp Sub", "Sharp subtract transitions", 2),
507 ("3", "Sharp Add", "Sharp add transitions", 3),
508 ("4", "Quantize", "Quantize", 4),
509 ("5", "Quantize Mix", "Quantize mixed", 5)]
511 water_plane: BoolProperty(
512 name="Water Plane",
513 default=False,
514 description="Add water plane"
516 water_level: FloatProperty(
517 name="Level",
518 default=0.01,
519 min=-10000.0,
520 max=10000.0,
521 description="Water level"
523 remove_double: BoolProperty(
524 name="Remove Doubles",
525 default=False,
526 description="Remove doubles"
528 direction: EnumProperty(
529 name="Direction",
530 default="NORMAL",
531 description="Displacement direction",
532 items=[
533 ("NORMAL", "Normal", "Displace along vertex normal direction", 0),
534 ("Z", "Z", "Displace in the Z direction", 1),
535 ("Y", "Y", "Displace in the Y direction", 2),
536 ("X", "X", "Displace in the X direction", 3)]
538 show_main_settings: BoolProperty(
539 name="Main Settings",
540 default=True,
541 description="Show settings"
543 show_noise_settings: BoolProperty(
544 name="Noise Settings",
545 default=True,
546 description="Show noise settings"
548 show_displace_settings: BoolProperty(
549 name="Displace Settings",
550 default=True,
551 description="Show terrain settings"
553 refresh: BoolProperty(
554 name="Refresh",
555 default=False,
556 description="Refresh"
558 auto_refresh: BoolProperty(
559 name="Auto",
560 default=False,
561 description="Automatic refresh"
564 def draw(self, context):
565 draw_ant_refresh(self, context)
566 draw_ant_noise(self, context, generate=False)
567 draw_ant_displace(self, context, generate=False)
569 @classmethod
570 def poll(cls, context):
571 ob = context.object
572 return (ob and ob.type == 'MESH')
574 def invoke(self, context, event):
575 self.refresh = True
576 return self.execute(context)
578 def execute(self, context):
579 if not self.refresh:
580 return {'PASS_THROUGH'}
582 ob = context.object
584 # Properties:
585 props = [
586 self.ant_terrain_name,
587 self.at_cursor,
588 self.smooth_mesh,
589 self.tri_face,
590 self.sphere_mesh,
591 self.land_material,
592 self.water_material,
593 self.texture_block,
594 self.subdivision_x,
595 self.subdivision_y,
596 self.mesh_size_x,
597 self.mesh_size_y,
598 self.mesh_size,
599 self.random_seed,
600 self.noise_offset_x,
601 self.noise_offset_y,
602 self.noise_offset_z,
603 self.noise_size_x,
604 self.noise_size_y,
605 self.noise_size_z,
606 self.noise_size,
607 self.noise_type,
608 self.basis_type,
609 self.vl_basis_type,
610 self.distortion,
611 self.hard_noise,
612 self.noise_depth,
613 self.amplitude,
614 self.frequency,
615 self.dimension,
616 self.lacunarity,
617 self.offset,
618 self.gain,
619 self.marble_bias,
620 self.marble_sharp,
621 self.marble_shape,
622 self.height,
623 self.height_invert,
624 self.height_offset,
625 self.maximum,
626 self.minimum,
627 self.edge_falloff,
628 self.edge_level,
629 self.falloff_x,
630 self.falloff_y,
631 self.strata_type,
632 self.strata,
633 self.water_plane,
634 self.water_level,
635 self.vert_group,
636 self.remove_double,
637 self.fx_mixfactor,
638 self.fx_mix_mode,
639 self.fx_type,
640 self.fx_bias,
641 self.fx_turb,
642 self.fx_depth,
643 self.fx_frequency,
644 self.fx_amplitude,
645 self.fx_size,
646 self.fx_loc_x,
647 self.fx_loc_y,
648 self.fx_height,
649 self.fx_offset,
650 self.fx_invert
653 # do displace
654 mesh = ob.data
656 if self.vert_group != "" and self.vert_group in ob.vertex_groups:
657 vertex_group = ob.vertex_groups[self.vert_group]
659 if vertex_group:
660 gi = vertex_group.index
661 if self.direction == "X":
662 for v in mesh.vertices:
663 for g in v.groups:
664 if g.group == gi:
665 v.co[0] += vertex_group.weight(v.index) * noise_gen(v.co, props)
667 if self.direction == "Y":
668 for v in mesh.vertices:
669 for g in v.groups:
670 if g.group == gi:
671 v.co[1] += vertex_group.weight(v.index) * noise_gen(v.co, props)
673 if self.direction == "Z":
674 for v in mesh.vertices:
675 for g in v.groups:
676 if g.group == gi:
677 v.co[2] += vertex_group.weight(v.index) * noise_gen(v.co, props)
679 else:
680 for v in mesh.vertices:
681 for g in v.groups:
682 if g.group == gi:
683 v.co += vertex_group.weight(v.index) * v.normal * noise_gen(v.co, props)
685 else:
686 if self.direction == "X":
687 for v in mesh.vertices:
688 v.co[0] += noise_gen(v.co, props)
690 elif self.direction == "Y":
691 for v in mesh.vertices:
692 v.co[1] += noise_gen(v.co, props)
694 elif self.direction == "Z":
695 for v in mesh.vertices:
696 v.co[2] += noise_gen(v.co, props)
698 else:
699 for v in mesh.vertices:
700 v.co += v.normal * noise_gen(v.co, props)
702 mesh.update()
704 if self.auto_refresh is False:
705 self.refresh = False
707 return {'FINISHED'}