-
Notifications
You must be signed in to change notification settings - Fork 40
/
planks.py
1222 lines (1060 loc) · 41.5 KB
/
planks.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# Floor Generator, a Blender addon
# (c) 2013 - 2019 Michel J. Anders (varkenvarken)
#
# 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 #####
bl_info = {
"name": "Floor Generator",
"author": "Michel Anders (varkenvarken) with contributions from Alain, Floric and Lell. The idea to add patterns is based on Cedric Brandin's (clarkx) parquet addon",
"version": (0, 0, 201907310916),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh",
"description": "Adds a mesh representing floor boards (planks)",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Add Mesh"}
from random import random as rand, seed, uniform as randuni, randrange
from math import pi as PI, sqrt, radians
from copy import deepcopy
from itertools import zip_longest
import bpy
import bmesh
from bpy.props import FloatProperty, IntProperty, BoolProperty, EnumProperty, StringProperty
from mathutils import Vector, Euler
D180 = radians(180)
D90 = radians(90)
D45 = radians(45)
W2 = sqrt(2)
# Vector.rotate() does NOT return anything, contrary to what the docs say
# docs are now fixed (https://projects.blender.org/tracker/index.php?func=detail&aid=36518&group_id=9&atid=498)
# but unfortunately no rotated() function was added
def rotate(v, r):
v2 = deepcopy(v)
v2.rotate(r)
return v2
def rotatep(v, r, p):
v2 = v - p
v2.rotate(r)
return v2 + p
def vcenter(verts):
return sum(verts,Vector())/len(verts)
available_meshes = [(' None ','None',"")]
def availableMeshes(self, context):
available_meshes.clear()
for ob in bpy.data.objects:
if ob.type == 'MESH' and ob.name != context.active_object.name:
name = ob.name[:]
available_meshes.append((name, name, ""))
if(len(available_meshes)==0):
available_meshes.append((' None ','None',"There appear to be no mesh objects in this scene"))
return available_meshes
def swap(c, i, j):
p1 = deepcopy(c[i])
p2 = deepcopy(c[j])
c[i] = p2
c[j] = p1
def swapx(c, i):
p1 = c[i]
p2 = c[i+1]
x1min = min(v.x for v in p1)
x1max = max(v.x for v in p1)
x2min = min(v.x for v in p2)
x2max = max(v.x for v in p2)
dx1 = Vector((x2min - x1min,0,0))
dx2 = Vector((x2max - x1max,0,0))
c[i+1] = [v - dx1 for v in p2]
c[i] = [v + dx2 for v in p1]
def getMaterialList(obj):
materials = []
for slot in obj.material_slots:
materials.append((slot.link, slot.name, slot.material.name, slot.material.use_fake_user))
slot.material.use_fake_user = True # we remove the mesh so any linked data is invalidated. setting a fake user will keep our material 'live'
return materials
def rebuildMaterialList(obj, lst):
for slot, (link, slotname, materialname, use_fake_user) in enumerate(lst):
bpy.ops.object.material_slot_add()
obj.material_slots[slot].link = link
obj.material_slots[slot].material = bpy.data.materials[materialname]
obj.material_slots[slot].material.use_fake_user = use_fake_user
def assignRandomMaterial(slot_n):
bpy.ops.object.mode_set(mode = 'EDIT') # Go to edit mode to create bmesh
obj = bpy.context.object
bm = bmesh.from_edit_mesh(obj.data) # Create bmesh object from object mesh
for face in bm.faces: # Iterate over all of the object's faces
face.material_index = randrange(slot_n) # Assign random material to face
obj.data.update() # Update the mesh from the bmesh data
bpy.ops.object.mode_set(mode = 'OBJECT') # Return to object mode
def plank(start, end, left, right, longgap, shortgap, rot=None):
ll = Vector((start, left, 0))
lr = Vector((start, right - longgap, 0))
ul = Vector((end - shortgap, right - longgap, 0))
ur = Vector((end - shortgap, left, 0))
if rot:
midpoint = Vector(((start + end)/2.0, (left + right)/ 2.0, 0))
ll = rotate((ll - midpoint), rot) + midpoint
lr = rotate((lr - midpoint), rot) + midpoint
ul = rotate((ul - midpoint), rot) + midpoint
ur = rotate((ur - midpoint), rot) + midpoint
verts = (ll, lr, ul, ur)
return verts
def planklw(length, width, rot=None):
ll = Vector((0, 0, 0))
lr = Vector((length, 0, 0))
ul = Vector((length, width, 0))
ur = Vector((0, width, 0))
if rot:
ll = rotate(ll, rot)
lr = rotate(lr, rot)
ul = rotate(ul, rot)
ur = rotate(ur, rot)
verts = (ll, lr, ul, ur)
return verts
def planks(n, m,
length, lengthvar,
width, widthvar,
longgap, shortgap,
offset, randomoffset, minoffset,
nseed,
randrotx, randroty, randrotz,
originx, originy):
#n=Number of planks, m=Floor Length, length = Planklength
verts = []
faces = []
uvs = []
seed(nseed)
widthoffset = 0
s = 0
e = offset
c = offset # Offset per row
ws = 0
p = 0
while p < n:
p += 1
uvs.append([])
w = width + randuni(0, widthvar)
we = ws + w
if randomoffset:
e = randuni(4 * shortgap + (offset if minoffset else 0.0), length) # we don't like negative plank lengths
while (m - e) > (4 * shortgap + (offset if minoffset else 0.0)):
ll = len(verts)
rot = Euler((randrotx * randuni(-1, 1), randroty * randuni(-1, 1), randrotz * randuni(-1, 1)), 'XYZ')
pverts = plank(s - originx, e - originx, ws - originy, we - originy, longgap, shortgap, rot)
verts.extend(pverts)
uvs[-1].append(deepcopy(pverts))
faces.append((ll, ll + 3, ll + 2, ll + 1))
s = e
e += length + randuni(0, lengthvar)
ll = len(verts)
rot = Euler((randrotx * randuni(-1, 1), randroty * randuni(-1, 1), randrotz * randuni(-1, 1)), 'XYZ')
pverts = plank(s - originx, m - originx, ws - originy, we - originy, longgap, shortgap, rot)
verts.extend(pverts)
uvs[-1].append(deepcopy(pverts))
faces.append((ll, ll + 3, ll + 2, ll + 1))
s = 0
#e = e - m
if c <= (length):
c = c + offset
if c > (length):
c = c - length
e = c
ws = we
# randomly swap uvs of planks. Note: we only swap within one set of planks because different sets can have different widths.
nplanks = len(uvs[-1])
if nplanks < 2 : continue
for pp in range(nplanks//2): # // to make sure it stays an int
i = randrange(nplanks-1)
swapx(uvs[-1],i)
fuvs = [uv for col in uvs for plank in col for uv in plank]
return verts, faces, fuvs
def herringbone(rows, cols, planklength, plankwidth, longgap, shortgap, nseed, randrotx, randroty, randrotz, originx, originy):
verts = []
faces = []
uvs = []
seed(nseed)
ll=0
longside = (planklength-shortgap)/sqrt(2.0)
shortside = (plankwidth-longgap)/sqrt(2.0)
vstep = Vector((0,plankwidth * sqrt(2.0),0))
hstepl = Vector((planklength * sqrt(2.0),0,0))
hstep = Vector((planklength/sqrt(2.0)-(plankwidth-longgap)/sqrt(2.0),planklength/sqrt(2.0)+(plankwidth-longgap)/sqrt(2.0),0))
dy = Vector((0,-planklength/sqrt(2.0),0))
pu = [Vector((0,0,0)),Vector((longside,0,0)),Vector((longside,shortside,0)),Vector((0,shortside,0))]
pv = [Vector((0,0,0)),Vector((longside,longside,0)),Vector((longside-shortside,longside+shortside,0)),Vector((-shortside,shortside,0))]
rot = Euler((0,0,-PI/2),"XYZ")
pvm = [rotate(v, rot)+hstep for v in pv]
midpointpv = sum(pv,Vector())/4.0
midpointpvm = sum(pvm,Vector())/4.0
o = Vector((-originx, -originy, 0))
midpointpvo = midpointpv - o
midpointpvmo = midpointpvm - o
for col in range(cols):
for row in range(rows):
# CLEANUP: this could be shorter: for P in pv,pvm
rot = Euler((randrotx * randuni(-1, 1), randroty * randuni(-1, 1), randrotz * randuni(-1, 1)), 'XYZ')
pvo = [ v + o for v in pv]
pverts = [rotate(v - midpointpvo, rot) + midpointpvo + row * vstep + col * hstepl + dy for v in pvo]
verts.extend(deepcopy(pverts))
uvs.append([v + Vector((col*2*longside,row*shortside,0)) for v in pu])
faces.append((ll, ll + 1, ll + 2, ll + 3))
ll = len(verts)
rot = Euler((randrotx * randuni(-1, 1), randroty * randuni(-1, 1), randrotz * randuni(-1, 1)), 'XYZ')
pvmo = [ v + o for v in pvm]
pverts = [rotate(v - midpointpvmo, rot) + midpointpvmo + row * vstep + col * hstepl + dy for v in pvmo]
verts.extend(deepcopy(pverts))
uvs.append([v + Vector(((1+col*2)*longside,row*shortside,0)) for v in pu])
faces.append((ll, ll + 1, ll + 2, ll + 3))
ll = len(verts)
for i in range(len(uvs)):
pp1 = randrange(len(uvs))
pp2 = randrange(len(uvs))
swap(uvs,pp1,pp2)
fuvs = [v for p in uvs for v in p]
return verts, faces, fuvs
def square(rows, cols, planklength, n, border, longgap, shortgap, nseed, randrotx, randroty, randrotz, originx, originy):
verts = []
verts2 = []
faces = []
faces2 = []
uvs = []
uvs2 = []
seed(nseed)
ll=0
ll2=0
net_planklength = planklength - 2.0 * border
plankwidth = net_planklength/n
longside = (net_planklength-shortgap)
shortside = (plankwidth-longgap)
stepv = Vector((0,planklength ,0))
steph = Vector((planklength,0 ,0))
nstepv = Vector((0,plankwidth ,0))
nsteph = Vector((plankwidth,0 ,0))
pv = [Vector((0,0,0)),Vector((longside,0,0)),Vector((longside,shortside,0)),Vector((0,shortside,0))]
rot = Euler((0,0,-PI/2),"XYZ")
pvm = [rotate(v, rot) + Vector((0,planklength - border,0)) for v in pv]
midpointpv = sum(pv,Vector())/4.0
midpointpvm = sum(pvm,Vector())/4.0
offseth = Vector((border, border, 0))
offsetv = Vector((border, 0, 0))
bw = border - shortgap
b1 = [(0,longgap/2.0,0),(0,planklength - longgap/2.0,0),(bw,planklength - longgap/2.0 - border,0),(bw,longgap/2.0 + border,0)]
b1 = [Vector(v) for v in b1]
d = Vector((planklength/2.0, planklength/2.0, 0))
rot = Euler((0,0,- PI/2),"XYZ")
b2 = [rotate(v-d,rot)+d for v in b1]
rot = Euler((0,0,- PI ),"XYZ")
b3 = [rotate(v-d,rot)+d for v in b1]
rot = Euler((0,0,-3*PI/2),"XYZ")
b4 = [rotate(v-d,rot)+d for v in b1]
o = Vector((-originx, -originy, 0))
# CLEANUP: duplicate code, suboptimal loop nesting and a lot of repeated calculations
# note that the uv map we create here is always aligned in the same direction even though planks alternate. This matches the saw direction in real life
for col in range(cols):
for row in range(rows):
# add the regular planks
for p in range(n):
rot = Euler((randrotx * randuni(-1, 1), randroty * randuni(-1, 1), randrotz * randuni(-1, 1)), 'XYZ')
if (col ^ row) %2 == 1:
pverts = [rotate(v - midpointpv, rot) + midpointpv + row * stepv + col * steph + nstepv * p + offseth + o for v in pv]
uverts = [v + row * stepv + col * steph + nstepv * p for v in pv]
else:
pverts = [rotate(v - midpointpv, rot) + midpointpv + row * stepv + col * steph + nsteph * p + offsetv + o for v in pvm]
uverts = [v + row * stepv + col * steph + nstepv * p for v in pv]
verts.extend(deepcopy(pverts))
uvs.append(deepcopy(uverts))
faces.append((ll, ll + 1, ll + 2, ll + 3))
ll = len(verts)
# add the border planks
if bw > 0.001:
for vl in b1,b2,b3,b4:
rot = Euler((randrotx * randuni(-1, 1), randroty * randuni(-1, 1), randrotz * randuni(-1, 1)), 'XYZ')
midpointvl = sum(vl,Vector())/4.0
verts2.extend([rotate(v - midpointvl, rot) + midpointvl + row * stepv + col * steph + o for v in vl])
uvs2.append(deepcopy([v + row * stepv + col * steph for v in b1])) # again, always the unrotated uvs to match the saw direction
faces2.append((ll2, ll2 + 3, ll2 + 2, ll2 + 1))
ll2 = len(verts2)
for i in range(len(uvs)):
pp1 = randrange(len(uvs))
pp2 = randrange(len(uvs))
swap(uvs,pp1,pp2)
for i in range(len(uvs2)):
pp1 = randrange(len(uvs2))
pp2 = randrange(len(uvs2))
swap(uvs2,pp1,pp2)
fuvs = [v for p in uvs for v in p]
fuvs2 = [v for p in uvs2 for v in p]
return verts + verts2, faces + [(f[0]+ll,f[1]+ll,f[2]+ll,f[3]+ll) for f in faces2], fuvs + fuvs2
def shortside(vert):
"""return true if length of 2 out of 3 connected vertices is equal to the min length of the connected edges"""
n = 0
el = [e.calc_length() for e in vert.link_edges]
mel = min(el)
for e in el:
if abs(e - mel) < 1e-4 :
n += 1
return n == 2
def versaille(rows, cols, planklength, plankwidth,longgap=0, shortgap=0, randrotx=0, randroty=0, randrotz=0, originx=0, originy=0, switch=False):
o = Vector((-originx, -originy, 0)) * planklength
# (8*w+w/W2)*W2 + w = 8*w*W2+w = (8*W2+1)*w = 1
w = 1.0 / (8*W2+2)
#w1 = 1 - w
q = w/W2
#k = w*4*W2-w
#s = (k - w)/2
#d = ((s+2*w)/W2)/2
#S = s/W2
sg = shortgap
s2 = sg/W2
lg = longgap
dd=-q if switch else 0
planks1 = (
# rectangles
(0,[(0+sg,0,0), (w*5-sg,0,0), (w*5-sg,w,0), (0+sg,w,0)]),
(0,[(6*w+sg,0,0), (w*11-sg,0,0), (w*11-sg,w,0), (6*w+sg,w,0)]),
(90,[(5*w,-2*w+sg,0), (w*6,-2*w+sg,0), (w*6,3*w-sg,0), (5*w,3*w-sg,0)]),
(0,[(3*w+sg,3*w,0), (w*8-sg,3*w,0), (w*8-sg,w*4,0), (3*w+sg,w*4,0)]),
(0,[(3*w+sg,-3*w,0), (w*8-sg,-3*w,0), (w*8-sg,w*-2,0), (3*w+sg,w*-2,0)]),
(90,[(5*w,4*w+sg,0),(6*w,4*w+sg,0),(6*w,6*w-sg,0),(5*w,6*w-sg,0)]),
(90,[(5*w,-3*w-sg,0),(5*w,-5*w+sg,0),(6*w,-5*w+sg,0),(6*w,-3*w-sg,0)]),
# squares
(0,[(0+sg,w+sg,0), (w*2-sg,w+sg,0), (w*2-sg,w*3-sg,0), (0+sg,w*3-sg,0)]),
(0,[(3*w+sg,w+sg,0), (w*5-sg,w+sg,0), (w*5-sg,w*3-sg,0), (3*w+sg,w*3-sg,0)]),
(0,[(6*w+sg,w+sg,0), (w*8-sg,w+sg,0), (w*8-sg,w*3-sg,0), (6*w+sg,w*3-sg,0)]),
(0,[(9*w+sg,w+sg,0), (w*11-sg,w+sg,0), (w*11-sg,w*3-sg,0), (9*w+sg,w*3-sg,0)]),
(0,[(0+sg,-2*w+sg,0), (w*2-sg,-2*w+sg,0), (w*2-sg,0-sg,0), (0+sg,0-sg,0)]),
(0,[(3*w+sg,-2*w+sg,0), (w*5-sg,-2*w+sg,0), (w*5-sg,0-sg,0), (3*w+sg,0-sg,0)]),
(0,[(6*w+sg,-2*w+sg,0), (w*8-sg,-2*w+sg,0), (w*8-sg,0-sg,0), (6*w+sg,0-sg,0)]),
(0,[(9*w+sg,-2*w+sg,0), (w*11-sg,-2*w+sg,0), (w*11-sg,0-sg,0), (9*w+sg,0-sg,0)]),
(0,[(3*w+sg,4*w+sg,0),(5*w-sg,4*w+sg,0),(5*w-sg,6*w-sg,0),(3*w+sg,6*w-sg,0)]),
(0,[(6*w+sg,4*w+sg,0),(8*w-sg,4*w+sg,0),(8*w-sg,6*w-sg,0),(6*w+sg,6*w-sg,0)]),
(0,[(3*w+sg,-5*w+sg,0),(5*w-sg,-5*w+sg,0),(5*w-sg,-3*w-sg,0),(3*w+sg,-3*w-sg,0)]),
(0,[(6*w+sg,-5*w+sg,0),(8*w-sg,-5*w+sg,0),(8*w-sg,-3*w-sg,0),(6*w+sg,-3*w-sg,0)]),
# pointed
(0,[(0+sg,3*w,0),(2*w-sg,3*w,0),(2*w-sg,4*w,0),(w+sg,4*w,0)]),
#left
(0,[(w+sg,4*w,0),(2*w-sg,4*w,0),(2*w-sg,5*w-sg*2,0)]),
(0,[(9*w+sg,3*w,0),(11*w-sg,3*w,0),(10*w-sg,4*w,0),(9*w+sg,4*w,0)]),
#top
(0,[(9*w+sg,4*w,0),(10*w-sg,4*w,0),(9*w+sg,5*w-sg*2,0)]),
(0,[(0+sg,-2*w,0),(w+sg,-3*w,0),(2*w-sg,-3*w,0),(2*w-sg,-2*w,0)]),
#bottom
(0,[(1*w+sg,-3*w,0),(2*w-sg,-4*w+sg+sg,0),(2*w-sg,-3*w,0)]),
(0,[(9*w+sg,-3*w,0),(10*w-sg,-3*w,0),(11*w-sg,-2*w,0),(9*w+sg,-2*w,0)]),
#right
(0,[(9*w+sg,-3*w,0),(9*w+sg,-4*w+sg*2,0),(10*w-sg,-3*w,0)]),
# long pointed
(90,[(2*w,0-sg,0),(2*w,-4*w+sg,0),(3*w,-5*w+sg,0),(3*w,0-sg,0)]),
(90,[(8*w,0-sg,0),(8*w,-5*w+sg,0),(9*w,-4*w+sg,0),(9*w,0-sg,0)]),
(90,[(2*w,w+sg,0),(3*w,w+sg,0),(3*w,6*w-sg,0),(2*w,5*w-sg,0)]),
(90,[(8*w,w+sg,0),(9*w,w+sg,0),(9*w,5*w-sg,0),(8*w,6*w-sg,0)]),
# corner planks
(90,[(0,-2*w+sg,0),(0,3*w-sg,0),(-1*w,2*w-sg,0),(-1*w,-1*w+sg,0)]),
(90,[(11*w,-2*w+sg,0),(12*w,-1*w+sg,0),(12*w,2*w-sg,0),(11*w,3*w-sg,0)]),
(0,[(3*w+sg,-5*w,0),(4*w+sg,-6*w,0),(7*w-sg,-6*w,0),(8*w-sg,-5*w,0)]),
(0,[(3*w+sg,6*w,0),(8*w-sg,6*w,0),(7*w-sg,7*w,0),(4*w+sg,7*w,0)]),
# corner triangles
(90,[(-w-s2,-w+s2*2,0),(-w-s2,2*w-s2*2,0),(-2.5*w+s2,0.5*w,0)]),
(90,[(12*w+s2,2*w-s2*2,0),(12*w+s2,-w+s2*2,0),(13.5*w-s2,0.5*w,0)]),
(0,[(4*w+s2*2,7*w+s2,0),(7*w-s2*2,7*w+s2,0),(5.5*w,8.5*w-s2,0)]),
(0,[(4*w+s2*2,-6*w-s2,0),(5.5*w,-7.5*w+s2,0),(7*w-s2*2,-6*w-s2,0)]),
# border planks
# bottom
(45,[(-2.5*w-q+q+dd+lg,0.5*w+q-q-dd-lg,0),(-2.5*w-2*q+q+dd+lg+lg,0.5*w-q-dd+lg-lg,0),(5.5*w-q+lg-lg,-7.5*w-q+lg+lg,0),(5.5*w-lg,-7.5*w+lg,0)]),
# right
(135,[(5.5*w-q+lg,-7.5*w-q+lg,0),(5.5*w+lg-lg,-7.5*w-2*q+lg+lg,0),(13.5*w+2*q+dd-lg-lg,0.5*w+dd-lg+lg,0),(13.5*w+q+dd-lg,0.5*w+q+dd-lg,0)]),
#top
(45,[(13.5*w-dd-lg,0.5*w+dd+lg,0),(13.5*w+q-dd-lg-lg,0.5*w+q+dd-lg+lg,0),(5.5*w+q-lg+lg,8.5*w+q-lg-lg,0),(5.5*w+lg,8.5*w-lg,0)]),
#left
(135,[(-2.5*w-q-dd+lg,0.5*w-q-dd+lg,0),(5.5*w+q-lg,8.5*w+q-lg,0),(5.5*w-lg+lg,8.5*w+2*q-lg-lg,0),(-2.5*w-q-q-dd+lg+lg,0.5*w+q-q-dd+lg-lg,0)])
)
verts = []
faces = []
uvs = []
left = 0
center = Vector((5.5*w,0.5*w,0))*planklength
delta = Vector((w, -10*q, 0)) * planklength
for col in range(cols):
start = 0
for row in range(rows):
origin = Vector((start, left, 0))
for uvrot,p in planks1:
ll = len(verts)
rot = Euler((randrotx * randuni(-1, 1), randroty * randuni(-1, 1), randrotz * randuni(-1, 1)), 'XYZ')
# randomly rotate the plank a little bit around its own center
pverts = [rotate(Vector(v)*planklength, rot) for v in p]
pverts = [origin + delta + o + rotatep(v, Euler((0,0,radians(45)),'XYZ'), center) for v in pverts]
verts.extend(pverts)
midpoint = vcenter(pverts)
#if uvrot > 0:
# print(uvrot)
# print([v - midpoint for v in pverts])
# print([rotatep(v, Euler((0,0,radians(uvrot)),'XYZ'), midpoint) - midpoint for v in pverts])
# print()
uvs.append([rotatep(v, Euler((0,0,radians(uvrot)),'XYZ'), midpoint) for v in pverts])
faces.append((ll, ll + 3, ll + 2, ll + 1) if len(pverts)==4 else (ll, ll + 2, ll + 1))
start += planklength
left += planklength
fuvs = [v for p in uvs for v in p]
return verts, faces, fuvs
def updateMesh(self, context):
o = context.object
material_list = getMaterialList(o)
if o.pattern == 'Regular':
nplanks = (o.width + o.originy) / o.plankwidth
verts, faces, uvs = planks(nplanks, o.length + o.originx,
o.planklength, o.planklengthvar,
o.plankwidth, o.plankwidthvar,
o.longgap, o.shortgap,
o.offset, o.randomoffset, o.minoffset,
o.randomseed,
o.randrotx, o.randroty, o.randrotz,
o.originx, o.originy)
elif o.pattern == 'Herringbone':
# note that there is a lot of extra length and width here to make sure that we create a pattern w.o. gaps at the edges
v = o.plankwidth * sqrt(2.0)
w = o.planklength * sqrt(2.0)
nplanks = int((o.width+o.planklength + o.originy*2) / v)+1
nplanksc = int((o.length + o.originx*2) / w)+1
verts, faces, uvs = herringbone(nplanks, nplanksc,
o.planklength, o.plankwidth,
o.longgap, o.shortgap,
o.randomseed,
o.randrotx, o.randroty, o.randrotz,
o.originx, o.originy)
elif o.pattern == 'Square':
rows = int((o.width + o.originy)/ o.planklength)+1
cols = int((o.length + o.originx)/ o.planklength)+1
verts, faces, uvs = square(rows, cols, o.planklength, o.nsquare, o.border, o.longgap, o.shortgap, o.randomseed,
o.randrotx, o.randroty, o.randrotz,
o.originx, o.originy)
elif o.pattern == 'Versaille':
rows = int((o.width + o.originy)/ o.planklength)+2
cols = int((o.length + o.originx)/ o.planklength)+2
verts, faces, uvs = versaille(rows, cols,
o.planklength, o.plankwidth,
o.longgap, o.shortgap,
o.randrotx, o.randroty, o.randrotz,
o.originx, o.originy,
o.borderswitch)
# create mesh &link object to scene
emesh = o.data
mesh = bpy.data.meshes.new(name='Planks')
mesh.from_pydata(verts, [], faces)
mesh.update(calc_edges=True)
# more than one object can refer to the same emesh
for i in bpy.data.objects:
if i.data == emesh:
i.data = mesh
name = emesh.name
emesh.user_clear() # this way the old mesh is marked as used by noone and not saved on exit
bpy.data.meshes.remove(emesh)
mesh.name = name
if bpy.context.mode != 'EDIT_MESH':
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
bpy.ops.object.shade_smooth()
# add uv-coords and per face random vertex colors
rot = Euler((0,0,o.uvrotation))
mesh.uv_layers.new()
uv_layer = mesh.uv_layers.active.data
vertex_colors = mesh.vertex_colors.new().data
offset = Vector()
# note that the uvs that are returned are shuffled
for poly in mesh.polygons:
color = [rand(), rand(), rand(), 1.0]
if o.randomuv == 'Random':
offset = Vector((rand(), rand(), 0))
if o.randomuv == 'Restricted':
offset = Vector((rand()*2-1, rand()*2-1, 0))
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
co = offset + mesh.vertices[mesh.loops[loop_index].vertex_index].co
if co.x > o.length or co.x < 0:
offset[0] = 0
if co.y > o.width or co.y < 0:
offset[1] = 0
elif o.randomuv == 'Packed':
x = []
y = []
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
x.append(uvs[mesh.loops[loop_index].vertex_index].x)
y.append(uvs[mesh.loops[loop_index].vertex_index].y)
offset = Vector((-min(x), -min(y), 0))
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
if o.randomuv == 'Shuffle':
coords = uvs[mesh.loops[loop_index].vertex_index]
elif o.randomuv in ('Random', 'Restricted'):
coords = mesh.vertices[mesh.loops[loop_index].vertex_index].co + offset
elif o.randomuv == 'Packed':
coords = uvs[mesh.loops[loop_index].vertex_index] + offset
else:
coords = mesh.vertices[mesh.loops[loop_index].vertex_index].co
coords = Vector(coords) # copy
coords.x *= o.uvscalex
coords.y *= o.uvscaley
coords.rotate(rot)
uv_layer[loop_index].uv = coords.xy
vertex_colors[loop_index].color = color
# subdivide mesh and warp it
warped = o.hollowlong > 0 or o.hollowshort > 0 or o.twist > 0
if warped:
bm = bmesh.new()
bm.from_mesh(mesh)
# calculate hollowness for each face
dshortmap = {}
dlongmap = {}
for face in bm.faces:
dshort = o.hollowshort * rand()
dlong = o.hollowlong * rand()
for v in face.verts:
dshortmap[v.index] = dshort
dlongmap[v.index] = dlong
bm.to_mesh(mesh)
bm.free()
# at this point all new geometry is selected and subdivide works in all selection modes
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.subdivide() # bmesh subdivide doesn't work for me ...
bpy.ops.object.editmode_toggle()
bm = bmesh.new()
bm.from_mesh(mesh)
for v in bm.verts:
if o.twist and len(v.link_edges) == 4: # vertex in the middle of the plank
dtwist = o.twist * randuni(-1, 1)
for e in v.link_edges:
v2 = e.other_vert(v) # the vertices on the side of the plank
if shortside(v2):
for e2 in v2.link_edges:
v3 = e2.other_vert(v2)
if len(v3.link_edges) == 2:
v3.co.z += dtwist
dtwist = -dtwist # one corner up, the other corner down
elif len(v.link_edges) == 3: # vertex in the middle of a side of the plank
for e in v.link_edges:
v2 = e.other_vert(v)
if len(v2.link_edges) == 2: # hollowness values are stored with the all original corner vertices
dshort = dshortmap[v2.index]
dlong = dlongmap[v2.index]
break
if shortside(v):
v.co.z -= dlong
else:
v.co.z -= dshort
creases = bm.edges.layers.crease.new()
for edge in bm.edges:
edge[creases] = 1
for vert in edge.verts:
if len(vert.link_edges) == 4:
edge[creases] = 0
break
bm.to_mesh(mesh)
bm.free()
# remove all modifiers to make sure the boolean will be last & only modifier
n = len(o.modifiers)
while n > 0:
n -= 1
bpy.ops.object.modifier_remove(modifier=o.modifiers[-1].name)
# add thickness
bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(o.data)
# extrude to given thickness
ret=bmesh.ops.extrude_face_region(bm,geom=bm.faces[:]) # all planks are separate faces, except when subdivided by random twist or hollowness
if warped: # we have a extra subdivision
Z = Vector((0,0,1))
for el in ret['geom']:
if isinstance(el, bmesh.types.BMVert) and len(el.link_edges) == 4 and el.normal.dot(Z) > 0.99 : # we look start at the vertex in the middle of the 4 faces but only on the top
d = Vector((0,0,o.thickness + rand() * o.randomthickness))
verts = set(v for f in el.link_faces for v in f.verts) # some vertices are shared, this way we make them unique
bmesh.ops.translate(bm, vec=d, verts=list(verts))
else:
for el in ret['geom']:
if isinstance(el, bmesh.types.BMFace):
d = Vector((0,0,o.thickness + rand() * o.randomthickness))
bmesh.ops.translate(bm, vec=d, verts=el.verts)
# trim excess flooring
ret = bmesh.ops.bisect_plane(bm, geom=bm.verts[:]+bm.edges[:]+bm.faces[:], plane_co=(o.length,0,0), plane_no=(1,0,0), clear_outer=True)
ret = bmesh.ops.bisect_plane(bm, geom=bm.verts[:]+bm.edges[:]+bm.faces[:], plane_co=(0,0,0), plane_no=(-1,0,0), clear_outer=True)
ret = bmesh.ops.bisect_plane(bm, geom=bm.verts[:]+bm.edges[:]+bm.faces[:], plane_co=(0,o.width,0), plane_no=(0,1,0), clear_outer=True)
ret = bmesh.ops.bisect_plane(bm, geom=bm.verts[:]+bm.edges[:]+bm.faces[:], plane_co=(0,0,0), plane_no=(0,-1,0), clear_outer=True)
# fill in holes caused by the trimming
open_edges = [e for e in bm.edges if len(e.link_faces)==1]
bmesh.ops.edgeloop_fill(bm, edges=open_edges, mat_nr=0, use_smooth=False)
creases = bm.edges.layers.crease.active
if creases is not None:
for edge in open_edges:
edge[creases] = 1
bmesh.update_edit_mesh(o.data)
bpy.ops.object.mode_set(mode='OBJECT')
# intersect with a floorplan. Note the floorplan must be 2D (all z-coords must be identical) and a closed polygon.
if self.usefloorplan and self.floorplan != ' None ':
# make the floorplan the only active an selected object
bpy.ops.object.select_all(action='DESELECT')
context.scene.objects.active = bpy.data.objects[self.floorplan]
bpy.data.objects[self.floorplan].select = True
# duplicate the selected geometry into a separate object
me = context.scene.objects.active.data
selected_faces = [p.index for p in me.polygons if p.select]
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.duplicate()
bpy.ops.mesh.separate()
bpy.ops.object.editmode_toggle()
me = context.scene.objects.active.data
for i in selected_faces:
me.polygons[i].select = True
# now there will be two selected objects
# the one with the new name will be the copy
for ob in context.selected_objects:
if ob.name != self.floorplan:
fpob = ob
# make that copy active and selected
for ob in context.selected_objects:
ob.select = False
fpob.select = True
context.scene.objects.active = fpob
# add thickness
# let normals of select faces point in same direction
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.object.editmode_toggle()
# add solidify modifier
# NOTE: for some reason bpy.ops.object.modifier_add doesn't work here
# even though fpob at this point is verifyable the active and selected object ...
mod = fpob.modifiers.new(name='Solidify', type='SOLIDIFY')
mod.offset = 1.0 # in the direction of the normals
mod.thickness = 2000 # very thick
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Solidify")
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.object.editmode_toggle()
fpob.location -= Vector((0,0,1000)) # actually this should be in the negative direction of the normals not just plain downward...
# at this point the floorplan object is the active and selected object
if True:
# make the floorboards active and selected
for ob in context.selected_objects:
ob.select = False
context.scene.objects.active = o
o.select = True
# add-and-apply a boolean modifier to get the intersection with the floorplan copy
bpy.ops.object.modifier_add(type='BOOLEAN') # default is intersect
o.modifiers[-1].object = fpob
if True:
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")
# delete the copy
bpy.ops.object.select_all(action='DESELECT')
context.scene.objects.active = fpob
fpob.select = True
bpy.ops.object.delete()
# make the floorboards active and selected
context.scene.objects.active = o
o.select = True
if self.modify:
mods = o.modifiers
if len(mods) == 0: # always true
bpy.ops.object.modifier_add(type='BEVEL')
#bpy.ops.object.modifier_add(type='EDGE_SPLIT')
mods = o.modifiers
mods[0].show_expanded = False
#mods[1].show_expanded = False
mods[0].width = self.bevel
mods[0].segments = 2
mods[0].limit_method = 'ANGLE'
mods[0].angle_limit = (85/90.0)*PI/2
if warped and not ('SUBSURF' in [m.type for m in mods]):
bpy.ops.object.modifier_add(type='SUBSURF')
mods[-1].show_expanded = False
mods[-1].levels = 2
if not warped and ('SUBSURF' in [m.type for m in mods]):
bpy.ops.object.modifier_remove(modifier='Subsurf')
if self.preservemats and len(material_list)>0:
rebuildMaterialList(o, material_list)
assignRandomMaterial(len(material_list))
bpy.types.Object.reg = StringProperty(default='FloorBoards')
bpy.types.Object.length = FloatProperty(name="Length",
description="Length (X) of the floor in Blender units",
default=4,
soft_min=0.5,
soft_max=40.0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.width = FloatProperty(name="Width",
description="Width (Y) of the floor in Blender units",
default=4,
soft_min=0.5,
soft_max=40.0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.planklength = FloatProperty(name="Length",
description="Length of a single plank",
default=2,
soft_min=0.5,
soft_max=40.0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.planklengthvar = FloatProperty(name="Var",
description="Max Length variation of single planks",
default=0.2,
min=0,
soft_max=40.0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.plankwidth = FloatProperty(name="Width",
description="Width of a single plank",
default=0.18,
soft_min=0.05,
soft_max=40.0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.plankwidthvar = FloatProperty(name="Var",
description="Max Width variation of single planks",
default=0,
min=0,
soft_max=4.0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.longgap = FloatProperty(name="Long Gap",
description="Gap between the long edges of the planks",
default=0.002,
min=0,
soft_max=0.01,
step=0.01,
precision=4,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.shortgap = FloatProperty(name="Short Gap",
description="Gap between the short edges of the planks",
default=0.0005,
min=0,
soft_max=0.01,
step=0.01,
precision=4,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.thickness = FloatProperty(name="Thickness",
description="Thickness of the planks",
default=0.018,
soft_max=0.1,
soft_min=0.008,
step=0.1,
precision=3,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.bevel = FloatProperty(name="Bevel",
description="Bevel width planks",
default=0.001,
min=0,
soft_max=0.01,
step=0.01,
precision=4,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.offset = FloatProperty(name="Offset",
description="Offset per row in Blender Units",
default=0.4,
min=0,
soft_max=2,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.randomoffset = BoolProperty(name="Offset random",
description="Uses random values for offset",
default=True,
update=updateMesh)
bpy.types.Object.minoffset = BoolProperty(name="Minimum offset",
description="Use Offset value as a minimum when using random values for offset",
default=False,
update=updateMesh)
bpy.types.Object.randomseed = IntProperty(name="Random Seed",
description="The seed governing random generation",
default=0,
min=0,
update=updateMesh)
bpy.types.Object.nsquare = IntProperty(name="Planks per Square",
description="Number of planks in each square tile",
default=4,
min=1,
update=updateMesh)
bpy.types.Object.border = FloatProperty(name="Border",
description="Width of border",
default=0,
soft_max=0.1,
min=0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.originx = FloatProperty(name="OriginX",
description="X offset of the whole pattern",
default=0,
soft_max=1,
min=0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.originy = FloatProperty(name="OriginY",
description="Y offset of the whole pattern",
default=0,
soft_max=1,
min=0,
subtype='DISTANCE',
unit='LENGTH',
update=updateMesh)
bpy.types.Object.randomuv = EnumProperty(name="UV randomization",
description="Randomization mode for the uv-offset of individual planks",
items = [('None','None','Plain mapping from top view'),
('Random','Random','Add a random offset to the plain uv map of individual planks'),
('Restricted','Restricted','Add a random offset to the plain map but keep individual planks withing the orginal border'),
('Shuffle','Shuffle','Exchange uvmaps of simlilar planks'),
('Packed','Packed','Overlap all uvs of individual planks while maintaining their proportions')],
update=updateMesh)
bpy.types.Object.modify = BoolProperty(name="Add modifiers",
description="Add bevel and solidify modifiers to the planks",
default=True,
update=updateMesh)
bpy.types.Object.preservemats = BoolProperty(name="Keep materials",
description="Keep any materials assigned to the mesh",
default=True,
update=updateMesh)
bpy.types.Object.randrotx = FloatProperty(name="X Rotation",
description="Random rotation of individual planks around x-axis",
default=0,
min=0,
soft_max=0.01,
step=(0.02 / 180) * PI,
precision=4,
subtype='ANGLE',
unit='ROTATION',
update=updateMesh)
bpy.types.Object.randroty = FloatProperty(name="Y Rotation",
description="Random rotation of individual planks around y-axis",
default=0,
min=0,
soft_max=0.01,
step=(0.02 / 180) * PI,
precision=4,
subtype='ANGLE',
unit='ROTATION',
update=updateMesh)
bpy.types.Object.randrotz = FloatProperty(name="Z Rotation",
description="Random rotation of individual planks around z-axis",
default=0,
min=0,
soft_max=0.01,
step=(0.02 / 180) * PI,
precision=4,
subtype='ANGLE',
unit='ROTATION',
update=updateMesh)
bpy.types.Object.randomthickness = FloatProperty(name="Thickness",