-
Notifications
You must be signed in to change notification settings - Fork 4
/
Blendpeaks.py
1214 lines (985 loc) · 38.8 KB
/
Blendpeaks.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Blendpeaks by oormi creations
# A free and open source add-on for Blender
# Creates mountain peaks with a procedural material.
# http://oormi.in
bl_info = {
"name": "Blendpeaks",
"description": "Creates Mountain Peaks",
"author": "Oormi Creations",
"version": (0, 1, 5),
"blender": (2, 80, 0),
"location": "3D View > Blendpeaks",
"warning": "", # used for warning icon and text in addons panel
"wiki_url": "https://github.com/oormicreations/Blendpeaks",
"tracker_url": "https://github.com/oormicreations/Blendpeaks/issues",
"category": "Object"
}
import bpy
from mathutils import *
from bpy import context
import random
from bpy.props import (StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
FloatVectorProperty,
EnumProperty,
PointerProperty,
)
from bpy.types import (Panel,
Menu,
Operator,
PropertyGroup,
)
def ShowMessageBox(message = "", title = "BlendPeak Says...", icon = 'INFO'):
def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)
def creatematerial(sstool):
mat = bpy.data.materials.new(name="BlendpeaksMat")
mat.use_nodes = True
matnodes = mat.node_tree.nodes
matnodes['Material Output'].location = Vector((1400,220))
matnodes['Principled BSDF'].location = Vector((800,1220))
bpy.context.scene.cycles.feature_set = 'EXPERIMENTAL'
mat.cycles.displacement_method = 'BOTH'
#displacement
coodpeak = matnodes.new('ShaderNodeTexCoord')
gradpeak = matnodes.new('ShaderNodeTexGradient')
mappeak = matnodes.new('ShaderNodeMapping')
ramppeak = matnodes.new('ShaderNodeValToRGB')
coodpeak.location = Vector((-1700,50))
gradpeak.location = Vector((-1500,50))
mappeak.location = Vector((-1300,50))
ramppeak.location = Vector((-1100,50))
gradpeak.gradient_type = 'SPHERICAL'
mappeak.vector_type = 'POINT'
ramppeak.color_ramp.elements[0].position = 0.75
ramppeak.color_ramp.interpolation = 'EASE'
matlinks = mat.node_tree.links
matlinks.new(coodpeak.outputs[3], gradpeak.inputs[0])
matlinks.new(gradpeak.outputs[0], mappeak.inputs[0])
matlinks.new(mappeak.outputs[0], ramppeak.inputs[0])
coodmask = matnodes.new('ShaderNodeTexCoord')
gradmask = matnodes.new('ShaderNodeTexGradient')
mapmask = matnodes.new('ShaderNodeMapping')
rampmask = matnodes.new('ShaderNodeValToRGB')
coodmask.location = Vector((-1700,500))
gradmask.location = Vector((-1500,500))
mapmask.location = Vector((-1300,500))
rampmask.location = Vector((-1100,500))
gradmask.gradient_type = 'SPHERICAL'
mapmask.vector_type = 'POINT'
mapmask.inputs[1].default_value[0] = -0.2
rampmask.color_ramp.interpolation = 'EASE'
matlinks.new(coodmask.outputs[3], gradmask.inputs[0])
matlinks.new(gradmask.outputs[0], mapmask.inputs[0])
matlinks.new(mapmask.outputs[0], rampmask.inputs[0])
#erosion
noiseero = matnodes.new('ShaderNodeTexNoise')
rampero = matnodes.new('ShaderNodeValToRGB')
mathero = matnodes.new('ShaderNodeMath')
noiseero.name = "Eroscale"
rampero.name = "Ero"
noiseero.location = Vector((-237, 597))
rampero.location = Vector((-26, 620))
mathero.location = Vector((340, 375))
noiseero.inputs[2].default_value = sstool.p_eroscale/10.0
noiseero.inputs[3].default_value = 16
rampero.color_ramp.elements[0].position = sstool.p_ero/100 #0.47
rampero.color_ramp.elements[1].position = 1 - (sstool.p_ero/100) #0.58
mathero.operation = 'MULTIPLY'
matlinks.new(noiseero.outputs[0], rampero.inputs[0])
matlinks.new(rampero.outputs[0], mathero.inputs[0])
matlinks.new(rampmask.outputs[0], mathero.inputs[1])
#seed
ncoodseed = matnodes.new('ShaderNodeTexCoord')
nmapseed = matnodes.new('ShaderNodeMapping')
vcoodseed = matnodes.new('ShaderNodeTexCoord')
vmapseed = matnodes.new('ShaderNodeMapping')
nmapseed.name = "NSeed"
vmapseed.name = "VSeed"
ncoodseed.location = Vector((-1300,-600))
nmapseed.location = Vector((-1100,-600))
vcoodseed.location = Vector((-600,-600))
vmapseed.location = Vector((-400,-600))
nmapseed.inputs[1].default_value[0] = sstool.p_seed/10
vmapseed.inputs[1].default_value[0] = sstool.p_seed/10
matlinks.new(ncoodseed.outputs[0], nmapseed.inputs[0])
matlinks.new(vcoodseed.outputs[0], vmapseed.inputs[0])
#noise
noisegross = matnodes.new('ShaderNodeTexNoise')
noisefine = matnodes.new('ShaderNodeTexNoise')
mix1 = matnodes.new('ShaderNodeMixRGB')
mix2 = matnodes.new('ShaderNodeMixRGB')
noisegross.name = "Gross"
noisefine.name = "Fine"
noisegross.location = Vector((-600,-90))
noisefine.location = Vector((-600,-300))
mix1.location = Vector((-350,170))
mix2.location = Vector((-150,170))
noisegross.noise_dimensions = '3D'
noisegross.inputs[2].default_value = sstool.p_gross/10
noisegross.inputs[3].default_value = 0
noisefine.noise_dimensions = '2D'
noisefine.inputs[2].default_value = sstool.p_fine/10
noisefine.inputs[3].default_value = 16
mix2.blend_type = 'SOFT_LIGHT'
mix2.inputs[0].default_value = 1
matlinks.new(rampmask.outputs[0], mix1.inputs[0])
matlinks.new(ramppeak.outputs[0], mix1.inputs[1])
matlinks.new(noisegross.outputs[0], mix1.inputs[2])
matlinks.new(mix1.outputs[0], mix2.inputs[1])
matlinks.new(noisefine.outputs[0], mix2.inputs[2])
matlinks.new(nmapseed.outputs[0], noisegross.inputs[0])
vnoisegross = matnodes.new('ShaderNodeTexVoronoi')
vnoisefine = matnodes.new('ShaderNodeTexVoronoi')
math1 = matnodes.new('ShaderNodeMath')
math2 = matnodes.new('ShaderNodeMath')
vnoisegross.name = "RidgeGross"
vnoisefine.name = "RidgeFine"
vnoisegross.location = Vector((-70,-140))
vnoisefine.location = Vector((-70,-440))
math1.location = Vector((140,-140))
math2.location = Vector((140,-440))
vnoisegross.inputs[2].default_value = sstool.p_rgross/10
vnoisefine.inputs[2].default_value = sstool.p_rfine/10
math1.operation = 'MULTIPLY'
math2.operation = 'MULTIPLY'
math1.inputs[1].default_value = 2
math2.inputs[1].default_value = 0.5
matlinks.new(vnoisegross.outputs[0], math1.inputs[0])
matlinks.new(vnoisefine.outputs[0], math2.inputs[0])
matlinks.new(vmapseed.outputs[0], vnoisegross.inputs[0])
mix3 = matnodes.new('ShaderNodeMixRGB')
math3 = matnodes.new('ShaderNodeMath')
math4 = matnodes.new('ShaderNodeMath')
mix3.location = Vector((400,-140))
math3.location = Vector((400,140))
math4.location = Vector((640,-140))
math3.operation = 'MULTIPLY'
math4.operation = 'MULTIPLY'
math3.inputs[1].default_value = 1.7
math4.inputs[1].default_value = 1.2
matlinks.new(mix2.outputs[0], math3.inputs[0])
matlinks.new(mix3.outputs[0], math4.inputs[0])
matlinks.new(math1.outputs[0], mix3.inputs[1])
matlinks.new(math2.outputs[0], mix3.inputs[2])
mix4 = matnodes.new('ShaderNodeMixRGB')
mix4.name='finalmix'
disp = matnodes.new('ShaderNodeDisplacement')
mix4.location = Vector((900,220))
disp.location = Vector((1100,220))
mix4.blend_type = 'ADD'
disp.space = 'OBJECT'
disp.inputs[1].default_value = 0
disp.inputs[2].default_value = sstool.p_height/10.0 #0.4
#matlinks.new(rampmask.outputs[0], mix4.inputs[0])
matlinks.new(mathero.outputs[0], mix4.inputs[0])
matlinks.new(math3.outputs[0], mix4.inputs[1])
matlinks.new(math4.outputs[0], mix4.inputs[2])
matlinks.new(mix4.outputs[0], disp.inputs[0])
matlinks.new(disp.outputs[0], matnodes['Material Output'].inputs[2])
#colors
ccood = matnodes.new('ShaderNodeTexCoord')
csepxyz = matnodes.new('ShaderNodeSeparateXYZ')
cramp = matnodes.new('ShaderNodeValToRGB')
cinv = matnodes.new('ShaderNodeInvert')
cnoise = matnodes.new('ShaderNodeTexNoise')
cbump = matnodes.new('ShaderNodeBump')
cramp.name = "Snow"
ccood.location = Vector((-640,1060))
csepxyz.location = Vector((-440,1060))
cramp.location = Vector((-240,1060))
cinv.location = Vector((340,1060))
cnoise.location = Vector((340,940))
cbump.location = Vector((540,940))
cinv.inputs[0].default_value = 1
cnoise.inputs[2].default_value = 4.6
cnoise.inputs[3].default_value = 16
cbump.inputs[0].default_value = 0.483
cbump.inputs[1].default_value = 1
cramp.color_ramp.interpolation = 'LINEAR'
cramp.color_ramp.elements.new(0.432)
cramp.color_ramp.elements.new(0.618)
cramp.color_ramp.elements.new(0.682)
cramp.color_ramp.elements[0].position = sstool.p_rock/100
cramp.color_ramp.elements[1].position = 0.05 + sstool.p_snow/100
cramp.color_ramp.elements[2].position = 0.10 + sstool.p_snow/100
cramp.color_ramp.elements[3].position = 0.15 + sstool.p_snow/100
cramp.color_ramp.elements[0].color = sstool.p_rockcolor
cramp.color_ramp.elements[1].color = sstool.p_snowcolor1
cramp.color_ramp.elements[2].color = sstool.p_snowcolor2
cramp.color_ramp.elements[3].color = sstool.p_grasscolor2
cramp.color_ramp.elements[4].color = sstool.p_grasscolor1
matlinks.new(ccood.outputs[1], csepxyz.inputs[0])
matlinks.new(csepxyz.outputs[2], cramp.inputs[0])
matlinks.new(cramp.outputs[0], cinv.inputs[1])
matlinks.new(cnoise.outputs[0], cbump.inputs[2])
matlinks.new(cinv.outputs[0], matnodes['Principled BSDF'].inputs[7])
matlinks.new(cramp.outputs[0], matnodes['Principled BSDF'].inputs[0])
if bpy.app.version[1] > 90:
matlinks.new(cbump.outputs[0], matnodes['Principled BSDF'].inputs[20])
else:
matlinks.new(cbump.outputs[0], matnodes['Principled BSDF'].inputs[19])
matlinks.new(matnodes['Principled BSDF'].outputs[0], matnodes['Material Output'].inputs[0])
return mat
##############################################################################################################
def createpeak(sstool):
bpy.ops.mesh.primitive_plane_add(size=sstool.p_sz)
bpy.ops.object.shade_smooth()
bpy.context.object.name = "Blendpeak" + str(sstool.p_count)
peakname = bpy.context.object.name #because name can change if exists already
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.subdivide(number_cuts=sstool.p_divs)
bpy.ops.object.editmode_toggle()
peak = bpy.data.objects.get(peakname)
bpy.ops.object.material_slot_add()
peak.data.materials[0] = creatematerial(sstool)
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.space_data.shading.type = 'RENDERED'
sstool.p_count += 1
return {'FINISHED'}
## Real time updates
def on_update_height(self, context):
bpy.context.object.data.materials[0].node_tree.nodes["Displacement"].inputs[2].default_value = context.scene.bp_tool.p_height/10.0
def on_update_seed(self, context):
bpy.context.object.data.materials[0].node_tree.nodes["NSeed"].inputs[1].default_value[0] = context.scene.bp_tool.p_seed/10.0
bpy.context.object.data.materials[0].node_tree.nodes["VSeed"].inputs[1].default_value[1] = context.scene.bp_tool.p_seed/10.0
def on_update_gross(self, context):
bpy.context.object.data.materials[0].node_tree.nodes["Gross"].inputs[2].default_value = context.scene.bp_tool.p_gross/10.0
def on_update_fine(self, context):
bpy.context.object.data.materials[0].node_tree.nodes["Fine"].inputs[2].default_value = context.scene.bp_tool.p_fine/10.0
def on_update_rgross(self, context):
bpy.context.object.data.materials[0].node_tree.nodes["RidgeGross"].inputs[2].default_value = context.scene.bp_tool.p_rgross/10.0
def on_update_rfine(self, context):
bpy.context.object.data.materials[0].node_tree.nodes["RidgeFine"].inputs[2].default_value = context.scene.bp_tool.p_rfine/10.0
def on_update_ero(self, context):
rampero = bpy.context.object.data.materials[0].node_tree.nodes["Ero"]
val = context.scene.bp_tool.p_ero/100.0
rampero.color_ramp.elements[0].position = val
rampero.color_ramp.elements[1].position = 1-val
def on_update_eroscale(self, context):
bpy.context.object.data.materials[0].node_tree.nodes["Eroscale"].inputs[2].default_value = context.scene.bp_tool.p_eroscale/10.0
def on_update_snow(self, context):
cramp = bpy.context.object.data.materials[0].node_tree.nodes["Snow"]
snow = context.scene.bp_tool.p_snow
rock = context.scene.bp_tool.p_rock
cramp.color_ramp.elements[0].position = rock/100
cramp.color_ramp.elements[1].position = 0.05 + snow/100
cramp.color_ramp.elements[2].position = 0.10 + snow/100
cramp.color_ramp.elements[3].position = 0.15 + snow/100
def on_update_rock(self, context):
cramp = bpy.context.object.data.materials[0].node_tree.nodes["Snow"]
rock = context.scene.bp_tool.p_rock
cramp.color_ramp.elements[0].position = rock/100
def on_update_scale(self, context):
peak = bpy.context.view_layer.objects.active
if peak == None:
return
pscale = context.scene.bp_tool.p_scale
peak.scale = Vector((pscale, pscale, pscale))
prock = context.scene.bp_tool.p_rock
if pscale>=1.0:
context.scene.bp_tool.p_rock = 50.0/pscale
context.scene.bp_tool.p_snow = 50.0/pscale
else:
context.scene.bp_tool.p_rock = 50.0*pscale
context.scene.bp_tool.p_snow = 50.0*pscale
def on_update_colors(self, context):
cramp = bpy.context.object.data.materials[0].node_tree.nodes["Snow"]
cramp.color_ramp.elements[4].color = context.scene.bp_tool.p_grasscolor1
cramp.color_ramp.elements[3].color = context.scene.bp_tool.p_grasscolor2
cramp.color_ramp.elements[2].color = context.scene.bp_tool.p_snowcolor1
cramp.color_ramp.elements[1].color = context.scene.bp_tool.p_snowcolor2
cramp.color_ramp.elements[0].color = context.scene.bp_tool.p_rockcolor
def on_update_colors_rock(self, context):
cramp = bpy.context.object.data.materials[0].node_tree.nodes["ColorRamp"]
cramp.color_ramp.elements[1].color = context.scene.bp_tool.p_rockcolor1
cramp.color_ramp.elements[0].color = context.scene.bp_tool.p_rockcolor2
def on_update_rockparam(self, context):
t = context.scene.bp_tool
n = bpy.context.object.data.materials[0].node_tree
bpy.context.object.scale[2] = t.p_rockht
n.nodes["Mapping"].inputs[1].default_value[0] = t.p_rockshape
n.nodes["Noise Texture.001"].inputs[2].default_value = t.p_rockshapescale/10.0
n.nodes["Displacement"].inputs[2].default_value = 20 - (t.p_rocksmooth/10.0)
n.nodes["Mix"].inputs[0].default_value = t.p_rockfine/100
n.nodes["Noise Texture"].inputs[2].default_value = t.p_rockfinescale
n.nodes["Mapping"].inputs[3].default_value[2] = t.p_rocklava
def randomizeall(sstool):
sstool.p_height = random.randrange(30, 60)/10
sstool.p_seed = random.randrange(0, 100)
sstool.p_gross = random.randrange(0, 50)
sstool.p_fine = random.randrange(0, 200)
sstool.p_rgross = random.randrange(0, 50)
sstool.p_rfine = random.randrange(0, 200)
sstool.p_snow = random.randrange(0, 70)
sstool.p_rock = random.randrange(0, 70)
def bakepeak(sstool):
peakobj = bpy.context.view_layer.objects.active
if peakobj == None:
return 0
mat = peakobj.active_material
if mat == None:
return 0
mapsz = sstool.p_bakesz
imgname = peakobj.name + "_Height_Bake"
bpy.data.images.new(name=imgname, width = mapsz, height = mapsz, float_buffer=True)
img = bpy.data.images[imgname]
img.filepath = '//' + imgname + ".exr"
n_image = len(bpy.data.images) - 1
img.source = 'GENERATED'
img.generated_type = 'BLANK'
img.use_generated_float = True
matnodes = mat.node_tree.nodes
matlinks = mat.node_tree.links
cmath = matnodes.new('ShaderNodeMath')
cmath.operation = 'DIVIDE'
cmath.inputs[1].default_value = 2
cmath.location = 1100,440
matlinks.new(matnodes['finalmix'].outputs[0], cmath.inputs[0])
timage = matnodes.new('ShaderNodeTexImage')
timage.location = 1100,-140
timage.image = bpy.data.images[imgname]
matlinks.new(cmath.outputs[0], matnodes['Material Output'].inputs[0])
#remove disp link if present
if len(matnodes['Material Output'].inputs[2].links) > 0:
link = matnodes['Material Output'].inputs[2].links[0]
mat.node_tree.links.remove(link)
timage.select = True
matnodes.active = timage
bpy.ops.object.bake(type='COMBINED')
img.file_format = 'OPEN_EXR'
print(img.filepath)
if img.filepath is None:
return 1
img.save()
#switch to Eevee
if sstool.p_toeevee:
bpy.context.scene.render.engine = 'BLENDER_EEVEE'
bpy.context.space_data.shading.type = 'SOLID'
bpy.ops.texture.new()
l = len(bpy.data.textures)
disptex = bpy.data.textures[l-1]
disptex.image = bpy.data.images[imgname]
bpy.ops.object.modifier_add(type='DISPLACE')
mod = bpy.context.view_layer.objects.active.modifiers[0]
mod.texture = disptex
mod.mid_level = 0
return 2
def createrock(sstool):
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=sstool.p_rockdiv, radius=sstool.p_rocksz, enter_editmode=False, location=(0, 0, 0))
bpy.ops.object.shade_smooth()
bpy.context.object.name = "Blendrock" + str(sstool.p_rcount)
rockname = bpy.context.object.name #because name can change if exists already
#height
bpy.context.object.scale[2] = sstool.p_rockht
mat = bpy.data.materials.new(name="NodeToScriptMat")
mat.use_nodes = True
matnodes = mat.node_tree.nodes
matlinks = mat.node_tree.links
bpy.context.scene.cycles.feature_set = 'EXPERIMENTAL'
mat.cycles.displacement_method = 'BOTH'
for n in matnodes:
matnodes.remove(n)
NoiseTexture = matnodes.new('ShaderNodeTexNoise')
NoiseTexture001 = matnodes.new('ShaderNodeTexNoise')
BrightContrast = matnodes.new('ShaderNodeBrightContrast')
Math001 = matnodes.new('ShaderNodeMath')
ColorRamp = matnodes.new('ShaderNodeValToRGB')
Bump = matnodes.new('ShaderNodeBump')
Mapping = matnodes.new('ShaderNodeMapping')
PrincipledBSDF = matnodes.new('ShaderNodeBsdfPrincipled')
Mix = matnodes.new('ShaderNodeMixRGB')
Displacement = matnodes.new('ShaderNodeDisplacement')
MaterialOutput = matnodes.new('ShaderNodeOutputMaterial')
TextureCoordinate = matnodes.new('ShaderNodeTexCoord')
NoiseTexture.location = Vector ((-113.9401, -15.4704))
NoiseTexture001.location = Vector ((-207.2210, 250.1717))
BrightContrast.location = Vector ((63.7708, -21.8994))
Math001.location = Vector ((236.7996, -13.1770))
ColorRamp.location = Vector ((73.8341, 607.8826))
Bump.location = Vector ((94.9198, 351.3014))
Mapping.location = Vector ((-394.1164, 253.8673))
PrincipledBSDF.location = Vector ((428.0041, 730.4140))
Mix.location = Vector ((424.8583, 109.6794))
Displacement.location = Vector ((648.7039, 112.1413))
MaterialOutput.location = Vector ((862.9899, 431.3890))
TextureCoordinate.location = Vector ((-574.0769, 250.8262))
#Links
matlinks.new(Mapping.outputs['Vector'], NoiseTexture001.inputs['Vector'])
matlinks.new(TextureCoordinate.outputs['Generated'], Mapping.inputs['Vector'])
matlinks.new(Bump.outputs['Normal'], PrincipledBSDF.inputs['Normal'])
matlinks.new(Displacement.outputs['Displacement'], MaterialOutput.inputs['Displacement'])
matlinks.new(NoiseTexture001.outputs['Color'], Mix.inputs['Color1'])
matlinks.new(Mix.outputs['Color'], Displacement.inputs['Normal'])
matlinks.new(NoiseTexture.outputs['Fac'], BrightContrast.inputs['Color'])
matlinks.new(ColorRamp.outputs['Color'], PrincipledBSDF.inputs['Base Color'])
matlinks.new(NoiseTexture001.outputs['Fac'], Bump.inputs['Height'])
matlinks.new(NoiseTexture001.outputs['Color'], ColorRamp.inputs['Fac'])
matlinks.new(PrincipledBSDF.outputs['BSDF'], MaterialOutput.inputs['Surface'])
matlinks.new(Math001.outputs['Value'], Mix.inputs['Color2'])
matlinks.new(BrightContrast.outputs['Color'], Math001.inputs[0])
#Values
#shape
Mapping.inputs['Location'].default_value = Vector((sstool.p_rockshape, 0.0000, 0.0000))
Mapping.inputs['Rotation'].default_value = [0.0,0.0,0.0]
Mapping.inputs['Scale'].default_value = Vector((1.0000, 1.0000, 1.0000))
#shape scale
NoiseTexture001.inputs['Scale'].default_value = sstool.p_rockshapescale/10
NoiseTexture001.inputs['Detail'].default_value = 16.0
NoiseTexture001.inputs['Distortion'].default_value = 0.0
#smoothness
Displacement.inputs['Height'].default_value = 1.0
Displacement.inputs['Midlevel'].default_value = 0.0
Displacement.inputs['Scale'].default_value = 20 - (sstool.p_rocksmooth/10.0)
Displacement.inputs['Normal'].default_value = [0.0,0.0,0.0]
#details
Mix.inputs['Fac'].default_value = sstool.p_rockfine/100
#details scale
NoiseTexture.inputs['Scale'].default_value = sstool.p_rockfinescale
NoiseTexture.inputs['Detail'].default_value = 16.0
NoiseTexture.inputs['Distortion'].default_value = 0.0
BrightContrast.inputs['Contrast'].default_value = 70
#lava
Mapping.inputs['Scale'].default_value = Vector((1.0000, 1.0000, sstool.p_rocklava))
ColorRamp.inputs['Fac'].default_value = 0.5
Bump.inputs['Strength'].default_value = 0.55
Bump.inputs['Distance'].default_value = 2.0
PrincipledBSDF.inputs['Roughness'].default_value = 0.65
Math001.inputs[0].default_value = 0.5
Math001.inputs[1].default_value = 1
Math001.use_clamp = True
Math001.operation = 'MULTIPLY'
ColorRamp.color_ramp.elements[0].color = sstool.p_rockcolor2 #(0.012596523389220238,0.009892581030726433,0.007905444130301476,1.0)
ColorRamp.color_ramp.elements[0].position = 0.0
ColorRamp.color_ramp.elements[1].color = sstool.p_rockcolor1 #(0.03287532180547714,0.03428680822253227,0.02379169873893261,1.0)
ColorRamp.color_ramp.elements[1].position = 0.6
rock = bpy.data.objects.get(rockname)
bpy.ops.object.material_slot_add()
rock.data.materials[0] = mat
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.space_data.shading.type = 'RENDERED'
sstool.p_rcount += 1
return 1
#----------------------------------------------------------------------------------------
# Operators
#----------------------------------------------------------------------------------------
class CRD_OT_CResetPeakDefaults(bpy.types.Operator):
bl_idname = "resetpeak.defaults"
bl_label = "Reset Defaults"
bl_description = "Reset Defaults."
def execute(self, context):
scene = context.scene
sstool = scene.bp_tool
sstool.p_divs = 100
sstool.p_sz = 2
sstool.p_height = 4
sstool.p_seed = 10
sstool.p_gross = 25
sstool.p_fine = 80
sstool.p_rgross = 75
sstool.p_rfine = 120
sstool.p_ero = 30
sstool.p_eroscale = 30
sstool.p_snow = 50
sstool.p_rock = 50
sstool.p_rand = False
sstool.p_scale = 1
sstool.p_rockht = 0.45
sstool.p_rocksz = 1.0
sstool.p_rockshape = 0
sstool.p_rockshapescale = 15
sstool.p_rocksmooth = 185
sstool.p_rockfine = 12.5
sstool.p_rockfinescale = 10
sstool.p_rocklava = 0
sstool.p_grasscolor1 = (0.048438, 0.040584, 0.007456, 1.0)
sstool.p_grasscolor2 = (0.015930, 0.042520, 0.022400, 1.0)
sstool.p_snowcolor1 = (0.5,0.5,0.45,1.0)
sstool.p_snowcolor2 = (0.9,0.9,0.9,1.0)
sstool.p_rockcolor = (0.008550, 0.006840, 0.006090, 1.0)
sstool.p_rockcolor1 = (0.003213, 0.005759, 0.015993, 1)
sstool.p_rockcolor2 = (0.147313, 0.075701, 0.040408, 1)
if bpy.context.object is not None:
try:
cramp = bpy.context.object.data.materials[0].node_tree.nodes["Snow"]
cramp.color_ramp.elements[0].color = (0.008550, 0.006840, 0.006090, 1.0)
cramp.color_ramp.elements[1].color = (0.5,0.5,0.45,1.0)
cramp.color_ramp.elements[2].color = (0.9,0.9,0.9,1.0)
cramp.color_ramp.elements[3].color = (0.015930, 0.042520, 0.022400, 1.0)
cramp.color_ramp.elements[4].color = (0.048438, 0.040584, 0.007456, 1.0)
except:
pass
sstool.p_res = "Reset to defaults, except Divisions"
return{'FINISHED'}
class CCP_OT_CCreatePeak(bpy.types.Operator):
bl_idname = "create.peak"
bl_label = "Create Peak"
bl_description = "Create Mountain Peak."
def execute(self, context):
scene = context.scene
sstool = scene.bp_tool
#prevents changing last peak
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = None
if sstool.p_rand:
randomizeall(sstool)
createpeak(sstool)
sstool.p_res = "Peak created !"
return{'FINISHED'}
class CBP_OT_CBakePeak(bpy.types.Operator):
bl_idname = "bake.peak"
bl_label = "Bake Peak"
bl_description = "Bake the height map."
def execute(self, context):
scene = context.scene
sstool = scene.bp_tool
res = bakepeak(sstool)
if res==0 :
ShowMessageBox("Please select a peak!")
if res==1 :
sstool.p_res = "Baked image could not be saved!"
ShowMessageBox("Baked image could not be saved! Save it from Image Editor.")
else:
sstool.p_res = "Height Map Created and Saved!"
return{'FINISHED'}
class CCR_OT_CCreateRock(bpy.types.Operator):
bl_idname = "create.rock"
bl_label = "Create Rock"
bl_description = "Create a rock."
def execute(self, context):
scene = context.scene
sstool = scene.bp_tool
res = createrock(sstool)
if res==0 :
ShowMessageBox("Error: Could not create a rock!")
else:
sstool.p_res = "Rock created!"
return{'FINISHED'}
#---------------------------------------------------------------------------------------------------------------------------
# Panels
#---------------------------------------------------------------------------------------------------------------------------
class OBJECT_PT_PeakPanel(bpy.types.Panel):
bl_label = "Blendpeaks"
bl_idname = "OBJECT_PT_PEAK_Panel"
bl_category = "Blendpeaks"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = "objectmode"
def draw(self, context):
layout = self.layout
scene = context.scene
sstool = scene.bp_tool
layout.prop(sstool, "p_divs")
#layout.prop(sstool, "p_sz")
layout.prop(sstool, "p_rand")
layout.operator("create.peak", text = "Create Peak", icon='HEART')
# layout.prop(sstool, "p_adjust")
layout.prop(sstool, "p_height")
layout.prop(sstool, "p_seed")
layout.prop(sstool, "p_gross")
layout.prop(sstool, "p_fine")
layout.prop(sstool, "p_rgross")
layout.prop(sstool, "p_rfine")
layout.prop(sstool, "p_ero")
layout.prop(sstool, "p_eroscale")
layout.prop(sstool, "p_snow")
layout.prop(sstool, "p_rock")
layout.prop(sstool, "p_scale")
row = layout.row(align=True)
row.prop(sstool, "p_grasscolor1")
row.prop(sstool, "p_grasscolor2")
row.prop(sstool, "p_snowcolor1")
row.prop(sstool, "p_snowcolor2")
row.prop(sstool, "p_rockcolor")
class OBJECT_PT_MiscPeakPanel(bpy.types.Panel):
bl_label = "Misc"
bl_idname = "OBJECT_PT_MISCP_Panel"
bl_category = "Blendpeaks"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = "objectmode"
def draw(self, context):
layout = self.layout
scene = context.scene
sstool = scene.bp_tool
layout.label(text = sstool.p_res)
layout.operator("resetpeak.defaults", text = "Reset Defaults", icon='X')
layout.operator("wm.url_open", text="Help | Source | Updates", icon='QUESTION').url = "https://github.com/oormicreations/Blendpeaks"
layout.label(text = sstool.p_about)
class OBJECT_PT_BakePanel(bpy.types.Panel):
bl_label = "Bake"
bl_idname = "OBJECT_PT_BAKE_Panel"
bl_category = "Blendpeaks"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = "objectmode"
def draw(self, context):
layout = self.layout
scene = context.scene
sstool = scene.bp_tool
#layout.label(text = sstool.p_res)
layout.prop(sstool, "p_bakesz")
layout.prop(sstool, "p_toeevee")
layout.operator("bake.peak", text = "Bake Height Map", icon='OUTLINER_OB_IMAGE')
class OBJECT_PT_RockPanel(bpy.types.Panel):
bl_label = "Rocks"
bl_idname = "OBJECT_PT_ROCK_Panel"
bl_category = "Blendpeaks"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = "objectmode"
def draw(self, context):
layout = self.layout
scene = context.scene
sstool = scene.bp_tool
layout.prop(sstool, "p_rockdiv")
layout.prop(sstool, "p_rocksz")
layout.prop(sstool, "p_rockht")
layout.prop(sstool, "p_rockshape")
layout.prop(sstool, "p_rockshapescale")
layout.prop(sstool, "p_rocksmooth")
layout.prop(sstool, "p_rockfine")
layout.prop(sstool, "p_rockfinescale")
layout.prop(sstool, "p_rocklava")
#layout.prop(sstool, "p_rockcrack")
#layout.prop(sstool, "p_rockcrackscale")
#layout.prop(sstool, "p_rockcrackmix")
row = layout.row(align=True)
row.prop(sstool, "p_rockcolor1")
row.prop(sstool, "p_rockcolor2")
layout.operator("create.rock", text = "Create Rock", icon='MOD_FLUID')
#---------------------------------------------------------------------------------------------------
# Properties
#---------------------------------------------------------------------------------------------------
class CCProperties(PropertyGroup):
p_count: IntProperty(
name = "Count",
description = "Index for naming, internal use only",
default = 1,
)
p_rcount: IntProperty(
name = "Rock Count",
description = "Index for naming, internal use only",
default = 1,
)
p_divs: IntProperty(
name = "Divisions",
description = "Number of Subdivisions, density of mesh",
default = 200,
min=1,
max=2000
)
p_sz: FloatProperty(
name = "Size",
description = "Expanse of the landscape",
default = 2,
min=0.01,
max=10000
)
p_scale: FloatProperty(
name = "Scale",
description = "Scales textures proportionately",
default = 1,
min=0.01,
max=10,
update=on_update_scale
)
p_height: FloatProperty(
name = "Height",
description = "Height of the peak",
default = 4.0,
min=-100,
max=1000,
update=on_update_height
)
p_seed: FloatProperty(
name = "Seed",
description = "Randomized shape of the peak",
default = 10.0,
min=0,
max=10000,
update=on_update_seed
)
p_gross: FloatProperty(
name = "Shape",
description = "Gross shape of the peak",
default = 25,
min=0,
max=10000,
update=on_update_gross
)
p_fine: FloatProperty(
name = "Shape Details",
description = "Terrain finer details",
default = 80,
min=0,
max=10000,
update=on_update_fine
)
p_rgross: FloatProperty(
name = "Ridges",
description = "Sharp edges or ridges",
default = 75,
min=0,
max=10000,
update=on_update_rgross
)
p_rfine: FloatProperty(
name = "Ridges Detail",
description = "Finer ridges",
default = 120,
min=0,
max=10000,
update=on_update_rfine
)
p_ero: FloatProperty(
name = "Erosion",
description = "Erodes the terrain",
default = 30,
min=0,
max=100,
update=on_update_ero
)
p_eroscale: FloatProperty(
name = "Erosion Scale",
description = "Scale of erosion",
default = 30,
min=0,
max=1000,
update=on_update_eroscale
)
p_snow: FloatProperty(
name = "Snow",
description = "Amount of snow",
default = 50,
min=0,
max=100,
update=on_update_snow
)
p_rock: FloatProperty(
name = "Rock",
description = "Amount of rock",
default = 50,
min=0,
max=100,
update=on_update_rock
)
p_rand: BoolProperty(
name = "Randomize",
description = "Ignores the settings and creates a random peak",
default = False
)
# p_adjust: BoolProperty(
# name = "Adjust",
# description = "Adjust the settings of an existing peak",
# default = False
# )
p_res: StringProperty(
name = "Result",
description = "NA",
default = "Ready..."
)
p_about: StringProperty(
name = "About",
description = "NA",
default = "Oormi Creations | http://oormi.in"
)
p_bakesz: IntProperty(
name = "Size",
description = "Size in pixels of the image.",