io_scene_gltf2: quiet error running when context.space is None
[blender-addons.git] / ant_landscape / add_mesh_ant_landscape.py
blobe12561bc9ec68afc1437e891d1de17c17cbe309d
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Another Noise Tool - Add Landscape
4 # Jimmy Hazevoet
6 # import modules
7 import bpy
8 from bpy.props import (
9 BoolProperty,
10 EnumProperty,
11 FloatProperty,
12 IntProperty,
13 StringProperty,
14 FloatVectorProperty,
17 from .ant_functions import (
18 grid_gen,
19 sphere_gen,
20 create_mesh_object,
21 store_properties,
22 draw_ant_refresh,
23 draw_ant_main,
24 draw_ant_noise,
25 draw_ant_displace,
26 draw_ant_water,
29 from ant_landscape import ant_noise
31 # ------------------------------------------------------------
32 # Add landscape
35 class AntAddLandscape(bpy.types.Operator):
36 bl_idname = "mesh.landscape_add"
37 bl_label = "Another Noise Tool - Landscape"
38 bl_description = "Add landscape mesh"
39 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
41 ant_terrain_name: StringProperty(
42 name="Name",
43 default="Landscape"
45 land_material: StringProperty(
46 name='Material',
47 default="",
48 description="Terrain material"
50 water_material: StringProperty(
51 name='Material',
52 default="",
53 description="Water plane material"
55 texture_block: StringProperty(
56 name="Texture",
57 default=""
59 at_cursor: BoolProperty(
60 name="Cursor",
61 default=True,
62 description="Place at cursor location",
64 smooth_mesh: BoolProperty(
65 name="Smooth",
66 default=True,
67 description="Shade smooth"
69 tri_face: BoolProperty(
70 name="Triangulate",
71 default=False,
72 description="Triangulate faces"
74 sphere_mesh: BoolProperty(
75 name="Sphere",
76 default=False,
77 description="Generate uv sphere - remove doubles when ready"
79 subdivision_x: IntProperty(
80 name="Subdivisions X",
81 default=128,
82 min=4,
83 max=6400,
84 description="Mesh X subdivisions"
86 subdivision_y: IntProperty(
87 default=128,
88 name="Subdivisions Y",
89 min=4,
90 max=6400,
91 description="Mesh Y subdivisions"
93 mesh_size: FloatProperty(
94 default=2.0,
95 name="Mesh Size",
96 min=0.01,
97 max=100000.0,
98 description="Mesh size"
100 mesh_size_x: FloatProperty(
101 default=2.0,
102 name="Mesh Size X",
103 min=0.01,
104 description="Mesh x size"
106 mesh_size_y: FloatProperty(
107 name="Mesh Size Y",
108 default=2.0,
109 min=0.01,
110 description="Mesh y size"
113 random_seed: IntProperty(
114 name="Random Seed",
115 default=0,
116 min=0,
117 description="Randomize noise origin"
119 noise_offset_x: FloatProperty(
120 name="Offset X",
121 default=0.0,
122 description="Noise X Offset"
124 noise_offset_y: FloatProperty(
125 name="Offset Y",
126 default=0.0,
127 description="Noise Y Offset"
129 noise_offset_z: FloatProperty(
130 name="Offset Z",
131 default=0.0,
132 description="Noise Z Offset"
134 noise_size_x: FloatProperty(
135 default=1.0,
136 name="Size X",
137 min=0.01,
138 max=1000.0,
139 description="Noise x size"
141 noise_size_y: FloatProperty(
142 name="Size Y",
143 default=1.0,
144 min=0.01,
145 max=1000.0,
146 description="Noise y size"
148 noise_size_z: FloatProperty(
149 name="Size Z",
150 default=1.0,
151 min=0.01,
152 max=1000.0,
153 description="Noise Z size"
155 noise_size: FloatProperty(
156 name="Noise Size",
157 default=1.0,
158 min=0.01,
159 max=1000.0,
160 description="Noise size"
162 noise_type: EnumProperty(
163 name="Noise Type",
164 default='hetero_terrain',
165 description="Noise type",
166 items=[
167 ('multi_fractal', "Multi Fractal", "Blender: Multi Fractal algorithm", 0),
168 ('ridged_multi_fractal', "Ridged MFractal", "Blender: Ridged Multi Fractal", 1),
169 ('hybrid_multi_fractal', "Hybrid MFractal", "Blender: Hybrid Multi Fractal", 2),
170 ('hetero_terrain', "Hetero Terrain", "Blender: Hetero Terrain", 3),
171 ('fractal', "fBm Fractal", "Blender: fBm - Fractional Browninian motion", 4),
172 ('turbulence_vector', "Turbulence", "Blender: Turbulence Vector", 5),
173 ('variable_lacunarity', "Distorted Noise", "Blender: Distorted Noise", 6),
174 ('marble_noise', "Marble", "A.N.T.: Marble Noise", 7),
175 ('shattered_hterrain', "Shattered hTerrain", "A.N.T.: Shattered hTerrain", 8),
176 ('strata_hterrain', "Strata hTerrain", "A.N.T: Strata hTerrain", 9),
177 ('ant_turbulence', "Another Noise", "A.N.T: Turbulence variation", 10),
178 ('vl_noise_turbulence', "vlNoise turbulence", "A.N.T: Real vlNoise turbulence", 11),
179 ('vl_hTerrain', "vlNoise hTerrain", "A.N.T: vlNoise hTerrain", 12),
180 ('distorted_heteroTerrain', "Distorted hTerrain", "A.N.T distorted hTerrain", 13),
181 ('double_multiFractal', "Double MultiFractal", "A.N.T: double multiFractal", 14),
182 ('rocks_noise', "Noise Rocks", "A.N.T: turbulence variation", 15),
183 ('slick_rock', "Slick Rock", "A.N.T: slick rock", 16),
184 ('planet_noise', "Planet Noise", "Planet Noise by: Farsthary", 17),
185 ('blender_texture', "Blender Texture - Texture Nodes", "Blender texture data block", 18)]
187 basis_type: EnumProperty(
188 name="Noise Basis",
189 default=ant_noise.noise_basis_default,
190 description="Noise basis algorithms",
191 items=ant_noise.noise_basis
193 vl_basis_type: EnumProperty(
194 name="vlNoise Basis",
195 default=ant_noise.noise_basis_default,
196 description="VLNoise basis algorithms",
197 items=ant_noise.noise_basis
199 distortion: FloatProperty(
200 name="Distortion",
201 default=1.0,
202 min=0.01,
203 max=100.0,
204 description="Distortion amount"
206 hard_noise: EnumProperty(
207 name="Soft Hard",
208 default="0",
209 description="Soft Noise, Hard noise",
210 items=[
211 ("0", "Soft", "Soft Noise", 0),
212 ("1", "Hard", "Hard noise", 1)]
214 noise_depth: IntProperty(
215 name="Depth",
216 default=8,
217 min=0,
218 max=16,
219 description="Noise Depth - number of frequencies in the fBm"
221 amplitude: FloatProperty(
222 name="Amp",
223 default=0.5,
224 min=0.01,
225 max=1.0,
226 description="Amplitude"
228 frequency: FloatProperty(
229 name="Freq",
230 default=2.0,
231 min=0.01,
232 max=5.0,
233 description="Frequency"
235 dimension: FloatProperty(
236 name="Dimension",
237 default=1.0,
238 min=0.01,
239 max=2.0,
240 description="H - fractal dimension of the roughest areas"
242 lacunarity: FloatProperty(
243 name="Lacunarity",
244 min=0.01,
245 max=6.0,
246 default=2.0,
247 description="Lacunarity - gap between successive frequencies"
249 offset: FloatProperty(
250 name="Offset",
251 default=1.0,
252 min=0.01,
253 max=6.0,
254 description="Offset - raises the terrain from sea level"
256 gain: FloatProperty(
257 name="Gain",
258 default=1.0,
259 min=0.01,
260 max=6.0,
261 description="Gain - scale factor"
263 marble_bias: EnumProperty(
264 name="Bias",
265 default="0",
266 description="Marble bias",
267 items=[
268 ("0", "Sin", "Sin", 0),
269 ("1", "Cos", "Cos", 1),
270 ("2", "Tri", "Tri", 2),
271 ("3", "Saw", "Saw", 3)]
273 marble_sharp: EnumProperty(
274 name="Sharp",
275 default="0",
276 description="Marble sharpness",
277 items=[
278 ("0", "Soft", "Soft", 0),
279 ("1", "Sharp", "Sharp", 1),
280 ("2", "Sharper", "Sharper", 2),
281 ("3", "Soft inv.", "Soft", 3),
282 ("4", "Sharp inv.", "Sharp", 4),
283 ("5", "Sharper inv.", "Sharper", 5)]
285 marble_shape: EnumProperty(
286 name="Shape",
287 default="0",
288 description="Marble shape",
289 items=[
290 ("0", "Default", "Default", 0),
291 ("1", "Ring", "Ring", 1),
292 ("2", "Swirl", "Swirl", 2),
293 ("3", "Bump", "Bump", 3),
294 ("4", "Wave", "Wave", 4),
295 ("5", "Z", "Z", 5),
296 ("6", "Y", "Y", 6),
297 ("7", "X", "X", 7)]
299 height: FloatProperty(
300 name="Height",
301 default=0.5,
302 min=-10000.0,
303 max=10000.0,
304 description="Noise intensity scale"
306 height_invert: BoolProperty(
307 name="Invert",
308 default=False,
309 description="Height invert",
311 height_offset: FloatProperty(
312 name="Offset",
313 default=0.0,
314 min=-10000.0,
315 max=10000.0,
316 description="Height offset"
318 fx_mixfactor: FloatProperty(
319 name="Mix Factor",
320 default=0.0,
321 min=-1.0,
322 max=1.0,
323 description="Effect mix factor: -1.0 = Noise, +1.0 = Effect"
325 fx_mix_mode: EnumProperty(
326 name="Effect Mix",
327 default="0",
328 description="Effect mix mode",
329 items=[
330 ("0", "Mix", "Mix", 0),
331 ("1", "Add", "Add", 1),
332 ("2", "Sub", "Subtract", 2),
333 ("3", "Mul", "Multiply", 3),
334 ("4", "Abs", "Absolute", 4),
335 ("5", "Scr", "Screen", 5),
336 ("6", "Mod", "Modulo", 6),
337 ("7", "Min", "Minimum", 7),
338 ("8", "Max", "Maximum", 8)
341 fx_type: EnumProperty(
342 name="Effect Type",
343 default="0",
344 description="Effect type",
345 items=[
346 ("0", "None", "No effect", 0),
347 ("1", "Gradient", "Gradient", 1),
348 ("2", "Waves", "Waves - Bumps", 2),
349 ("3", "Zigzag", "Zigzag", 3),
350 ("4", "Wavy", "Wavy", 4),
351 ("5", "Bump", "Bump", 5),
352 ("6", "Dots", "Dots", 6),
353 ("7", "Rings", "Rings", 7),
354 ("8", "Spiral", "Spiral", 8),
355 ("9", "Square", "Square", 9),
356 ("10", "Blocks", "Blocks", 10),
357 ("11", "Grid", "Grid", 11),
358 ("12", "Tech", "Tech", 12),
359 ("13", "Crackle", "Crackle", 13),
360 ("14", "Cracks", "Cracks", 14),
361 ("15", "Rock", "Rock noise", 15),
362 ("16", "Lunar", "Craters", 16),
363 ("17", "Cosine", "Cosine", 17),
364 ("18", "Spikey", "Spikey", 18),
365 ("19", "Stone", "Stone", 19),
366 ("20", "Flat Turb", "Flat turbulence", 20),
367 ("21", "Flat Voronoi", "Flat voronoi", 21)
370 fx_bias: EnumProperty(
371 name="Effect Bias",
372 default="0",
373 description="Effect bias type",
374 items=[
375 ("0", "Sin", "Sin", 0),
376 ("1", "Cos", "Cos", 1),
377 ("2", "Tri", "Tri", 2),
378 ("3", "Saw", "Saw", 3),
379 ("4", "None", "None", 4)]
381 fx_turb: FloatProperty(
382 name="Distortion",
383 default=0.0,
384 min=0.0,
385 max=1000.0,
386 description="Effect turbulence distortion"
388 fx_depth: IntProperty(
389 name="Depth",
390 default=0,
391 min=0,
392 max=16,
393 description="Effect depth - number of frequencies"
395 fx_amplitude: FloatProperty(
396 name="Amp",
397 default=0.5,
398 min=0.01,
399 max=1.0,
400 description="Amplitude"
402 fx_frequency: FloatProperty(
403 name="Freq",
404 default=2.0,
405 min=0.01,
406 max=5.0,
407 description="Frequency"
409 fx_size: FloatProperty(
410 name="Effect Size",
411 default=1.0,
412 min=0.01,
413 max=1000.0,
414 description="Effect size"
416 fx_loc_x: FloatProperty(
417 name="Offset X",
418 default=0.0,
419 description="Effect x offset"
421 fx_loc_y: FloatProperty(
422 name="Offset Y",
423 default=0.0,
424 description="Effect y offset"
426 fx_height: FloatProperty(
427 name="Intensity",
428 default=1.0,
429 min=-1000.0,
430 max=1000.0,
431 description="Effect intensity scale"
433 fx_invert: BoolProperty(
434 name="Invert",
435 default=False,
436 description="Effect invert"
438 fx_offset: FloatProperty(
439 name="Offset",
440 default=0.0,
441 min=-1000.0,
442 max=1000.0,
443 description="Effect height offset"
446 edge_falloff: EnumProperty(
447 name="Falloff",
448 default="3",
449 description="Flatten edges",
450 items=[
451 ("0", "None", "None", 0),
452 ("1", "Y", "Y Falloff", 1),
453 ("2", "X", "X Falloff", 2),
454 ("3", "X Y", "X Y Falloff", 3)]
456 falloff_x: FloatProperty(
457 name="Falloff X",
458 default=4.0,
459 min=0.1,
460 max=100.0,
461 description="Falloff x scale"
463 falloff_y: FloatProperty(
464 name="Falloff Y",
465 default=4.0,
466 min=0.1,
467 max=100.0,
468 description="Falloff y scale"
470 edge_level: FloatProperty(
471 name="Edge Level",
472 default=0.0,
473 min=-10000.0,
474 max=10000.0,
475 description="Edge level, sealevel offset"
477 maximum: FloatProperty(
478 name="Maximum",
479 default=1.0,
480 min=-10000.0,
481 max=10000.0,
482 description="Maximum, flattens terrain at plateau level"
484 minimum: FloatProperty(
485 name="Minimum",
486 default=-1.0,
487 min=-10000.0,
488 max=10000.0,
489 description="Minimum, flattens terrain at seabed level"
491 vert_group: StringProperty(
492 name="Vertex Group",
493 default=""
495 strata: FloatProperty(
496 name="Amount",
497 default=5.0,
498 min=0.01,
499 max=1000.0,
500 description="Strata layers / terraces"
502 strata_type: EnumProperty(
503 name="Strata",
504 default="0",
505 description="Strata types",
506 items=[
507 ("0", "None", "No strata", 0),
508 ("1", "Smooth", "Smooth transitions", 1),
509 ("2", "Sharp Sub", "Sharp subtract transitions", 2),
510 ("3", "Sharp Add", "Sharp add transitions", 3),
511 ("4", "Quantize", "Quantize", 4),
512 ("5", "Quantize Mix", "Quantize mixed", 5)]
514 water_plane: BoolProperty(
515 name="Water Plane",
516 default=False,
517 description="Add water plane"
519 water_level: FloatProperty(
520 name="Level",
521 default=0.01,
522 min=-10000.0,
523 max=10000.0,
524 description="Water level"
526 remove_double: BoolProperty(
527 name="Remove Doubles",
528 default=False,
529 description="Remove doubles"
531 show_main_settings: BoolProperty(
532 name="Main Settings",
533 default=True,
534 description="Show settings"
536 show_noise_settings: BoolProperty(
537 name="Noise Settings",
538 default=True,
539 description="Show noise settings"
541 show_displace_settings: BoolProperty(
542 name="Displace Settings",
543 default=True,
544 description="Show displace settings"
546 refresh: BoolProperty(
547 name="Refresh",
548 default=False,
549 description="Refresh"
551 auto_refresh: BoolProperty(
552 name="Auto",
553 default=True,
554 description="Automatic refresh"
557 @classmethod
558 def poll(self, context):
559 ob = context.object
560 if ob is not None:
561 if ob.mode == 'EDIT':
562 return False
563 return True
565 def draw(self, context):
566 draw_ant_refresh(self, context)
567 draw_ant_main(self, context, generate=True)
568 draw_ant_noise(self, context)
569 draw_ant_displace(self, context, generate=True)
570 draw_ant_water(self, context)
572 def invoke(self, context, event):
573 self.refresh = True
574 return self.execute(context)
576 def execute(self, context):
577 if not self.refresh:
578 return {'PASS_THROUGH'}
580 # turn off 'Enter Edit Mode'
581 use_enter_edit_mode = bpy.context.preferences.edit.use_enter_edit_mode
582 bpy.context.preferences.edit.use_enter_edit_mode = False
584 # deselect all objects when in object mode
585 if bpy.ops.object.select_all.poll():
586 bpy.ops.object.select_all(action='DESELECT')
588 # Properties
589 ant_props = [
590 self.ant_terrain_name,
591 self.at_cursor,
592 self.smooth_mesh,
593 self.tri_face,
594 self.sphere_mesh,
595 self.land_material,
596 self.water_material,
597 self.texture_block,
598 self.subdivision_x,
599 self.subdivision_y,
600 self.mesh_size_x,
601 self.mesh_size_y,
602 self.mesh_size,
603 self.random_seed,
604 self.noise_offset_x,
605 self.noise_offset_y,
606 self.noise_offset_z,
607 self.noise_size_x,
608 self.noise_size_y,
609 self.noise_size_z,
610 self.noise_size,
611 self.noise_type,
612 self.basis_type,
613 self.vl_basis_type,
614 self.distortion,
615 self.hard_noise,
616 self.noise_depth,
617 self.amplitude,
618 self.frequency,
619 self.dimension,
620 self.lacunarity,
621 self.offset,
622 self.gain,
623 self.marble_bias,
624 self.marble_sharp,
625 self.marble_shape,
626 self.height,
627 self.height_invert,
628 self.height_offset,
629 self.maximum,
630 self.minimum,
631 self.edge_falloff,
632 self.edge_level,
633 self.falloff_x,
634 self.falloff_y,
635 self.strata_type,
636 self.strata,
637 self.water_plane,
638 self.water_level,
639 self.vert_group,
640 self.remove_double,
641 self.fx_mixfactor,
642 self.fx_mix_mode,
643 self.fx_type,
644 self.fx_bias,
645 self.fx_turb,
646 self.fx_depth,
647 self.fx_frequency,
648 self.fx_amplitude,
649 self.fx_size,
650 self.fx_loc_x,
651 self.fx_loc_y,
652 self.fx_height,
653 self.fx_offset,
654 self.fx_invert
657 scene = context.scene
658 vl = context.view_layer
660 # Main function, create landscape mesh object
661 if self.ant_terrain_name != "":
662 new_name = self.ant_terrain_name
663 else:
664 new_name = "Landscape"
666 if self.sphere_mesh:
667 # sphere
668 verts, faces = sphere_gen(
669 self.subdivision_y,
670 self.subdivision_x,
671 self.tri_face,
672 self.mesh_size,
673 ant_props,
674 False,
675 0.0,
677 new_ob = create_mesh_object(context, verts, [], faces, new_name)
678 if self.remove_double:
679 new_ob.select_set(True)
680 bpy.ops.object.mode_set(mode='EDIT')
681 bpy.ops.mesh.remove_doubles(threshold=0.0001, use_unselected=False)
682 bpy.ops.object.mode_set(mode='OBJECT')
683 else:
684 # grid
685 verts, faces = grid_gen(
686 self.subdivision_x,
687 self.subdivision_y,
688 self.tri_face,
689 self.mesh_size_x,
690 self.mesh_size_y,
691 ant_props,
692 False,
693 0.0,
695 new_ob = create_mesh_object(context, verts, [], faces, new_name)
697 new_ob.select_set(True)
699 if self.smooth_mesh:
700 bpy.ops.object.shade_smooth()
702 if not self.at_cursor:
703 new_ob.location = (0.0, 0.0, 0.0)
705 # Landscape Material
706 if self.land_material != "" and self.land_material in bpy.data.materials:
707 mat = bpy.data.materials[self.land_material]
708 bpy.context.object.data.materials.append(mat)
710 # Water plane
711 if self.water_plane:
712 if self.sphere_mesh:
713 # sphere
714 verts, faces = sphere_gen(
715 self.subdivision_y,
716 self.subdivision_x,
717 self.tri_face,
718 self.mesh_size,
719 ant_props,
720 self.water_plane,
721 self.water_level,
723 wobj = create_mesh_object(context, verts, [], faces, new_name + "_plane")
724 if self.remove_double:
725 wobj.select_set(True)
726 bpy.ops.object.mode_set(mode='EDIT')
727 bpy.ops.mesh.remove_doubles(threshold=0.0001, use_unselected=False)
728 bpy.ops.object.mode_set(mode='OBJECT')
729 else:
730 # grid
731 verts, faces = grid_gen(
734 self.tri_face,
735 self.mesh_size_x,
736 self.mesh_size_y,
737 ant_props,
738 self.water_plane,
739 self.water_level,
741 wobj = create_mesh_object(context, verts, [], faces, new_name + "_plane")
743 wobj.select_set(True)
745 if self.smooth_mesh:
746 bpy.ops.object.shade_smooth()
748 if not self.at_cursor:
749 wobj.location = (0.0, 0.0, 0.0)
751 # Water Material
752 if self.water_material != "" and self.water_material in bpy.data.materials:
753 mat = bpy.data.materials[self.water_material]
754 bpy.context.object.data.materials.append(mat)
756 # select landscape and make active
757 new_ob.select_set(True)
758 vl.objects.active = new_ob
760 new_ob = store_properties(self, new_ob)
762 if self.auto_refresh is False:
763 self.refresh = False
765 # restore pre operator state
766 bpy.context.preferences.edit.use_enter_edit_mode = use_enter_edit_mode
768 return {'FINISHED'}