forked from s-leger/BlenderParametricObject
-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
1142 lines (958 loc) · 41.4 KB
/
__init__.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
# -*- coding:utf-8 -*-
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# ----------------------------------------------------------
# Blender Parametric object skeleton
# Author: Stephen Leger (s-leger)
# ----------------------------------------------------------
bl_info = {
'name': 'Floor',
'description': 'Floor parametric object',
'author': 's-leger, Jacob Morris',
'license': 'GPL',
'version': (1, 0, 0),
'blender': (2, 7, 8),
'location': 'View3D > Tools > Sample',
'warning': '',
'wiki_url': 'https://github.com/BlendingJake/Parametric-Flooring-for-Blender-3D/wiki',
'tracker_url': 'https://github.com/BlendingJake/Parametric-Flooring-for-Blender-3D/issues',
'link': 'hhttps://github.com/BlendingJake/Parametric-Flooring-for-Blender-3D',
'support': 'COMMUNITY',
'category': '3D View'
}
import bpy
from bpy.types import Operator, PropertyGroup, Mesh, Panel
from bpy.props import FloatProperty, CollectionProperty, BoolProperty, IntProperty, EnumProperty
from mathutils import Vector
from random import uniform
from math import radians, cos, sin, atan
from .bmesh_utils import BmeshEdit as BmeshHelper
from .simple_manipulator import Manipulable
import bmesh
# ------------------------------------------------------------------
# Constants
# ------------------------------------------------------------------
FOOT = 0.3048 # 1 foot in meters
INCH = 0.0254 # 1 inch in meters
EQUAL, NOT_EQUAL, LESS_EQUAL, GREATER_EQUAL, LESS, GREATER = [i for i in range(6)]
SLOP = 0.001 # amount of wiggle room in rough_comp
# ------------------------------------------------------------------
# Define property class to store object parameters and update mesh
# ------------------------------------------------------------------
def update(self, context):
if self.auto_update:
self.update(context)
class archipack_floor(Manipulable, PropertyGroup):
# keep track of data
vs, fs = [], [] # vertices and faces
ms, us = [], [] # mat ids and uvs
uv_factor = 1 # uv scale factor
auto_update = BoolProperty(
name="Auto Update Mesh", default=True, update=update,
description="Automatically update the mesh whenever a parameter is changed"
)
# pattern
pattern = EnumProperty(
name='Floor Pattern', items=(("boards", "Boards", ""), ("square_parquet", "Square Parquet", ""),
("herringbone_parquet", "Herringbone Parquet", ""),
("herringbone", "Herringbone", ""), ("regular_tile", "Regular Tile", ""),
("hopscotch", "Hopscotch", ""), ("stepping_stone", "Stepping Stone", ""),
("hexagon", "Hexagon", ""), ("windmill", "Windmill", "")),
default="boards", update=update
)
# overall length and width
width = FloatProperty( # x
name='Width',
min=2*FOOT, soft_max=100*FOOT,
default=20*FOOT, precision=2,
description='Width', update=update,
subtype="DISTANCE"
)
length = FloatProperty( # y
name='Length',
min=2*FOOT, soft_max=100*FOOT,
default=8*FOOT, precision=2,
description='Length', update=update,
subtype="DISTANCE"
)
# generic spacing
spacing = FloatProperty(
name='Spacing', unit='LENGTH', min=0, soft_max=1 * INCH,
default=0.125 * INCH, precision=2, update=update,
description='The amount of space between boards or tiles in both directions'
)
# general thickness
thickness = FloatProperty( # z
name='Thickness',
min=0.25*INCH, soft_max=2*INCH,
default=1*INCH, precision=2,
description='Thickness', update=update,
subtype="DISTANCE"
)
vary_thickness = BoolProperty(
name='Vary Thickness', update=update, default=False,
description='Vary board thickness?'
)
thickness_variance = FloatProperty(
name='Thickness Variance', min=0, max=100, subtype='PERCENTAGE',
default=25, update=update, precision=2,
description='How much board thickness can vary by'
)
# board width, variance, and spacing
board_width = FloatProperty(
name='Board Width', unit='LENGTH', min=2*INCH,
soft_max=2*FOOT, default=6*INCH, update=update,
description='The width of the boards', precision=2
)
vary_width = BoolProperty(
name='Vary Width', default=False,
description='Vary board width?', update=update
)
width_variance = FloatProperty(
name='Width Variance', subtype='PERCENTAGE',
min=1, max=100, default=50, description='How much board width can vary by',
precision=2, update=update
)
width_spacing = FloatProperty(
name='Width Spacing', unit='LENGTH', min=0, soft_max=1*INCH,
default=0.125*INCH, precision=2, update=update,
description='The amount of space between boards in the width direction'
)
# board length
board_length = FloatProperty(
name='Board Length', unit='LENGTH', min=2*FOOT,
soft_max=100*FOOT, default=8*FOOT, update=update,
description='The length of the boards', precision=2
)
short_board_length = FloatProperty(
name='Board Length', unit='LENGTH', min=6*INCH,
soft_max=4*FOOT, default=2*FOOT, update=update,
description='The length of the boards', precision=2
)
vary_length = BoolProperty(
name='Vary Length', default=False,
description='Vary board length?', update=update
)
length_variance = FloatProperty(
name='Length Variance', subtype='PERCENTAGE',
min=1, max=100, default=50, description='How much board length can vary by',
precision=2, update=update
)
max_boards = IntProperty(
name='Max Boards', min=1, soft_max=10, default=2,
update=update, description='Max number of boards in one row'
)
length_spacing = FloatProperty(
name='Length Spacing', unit='LENGTH', min=0, soft_max=1*INCH,
default=0.125*INCH, precision=2, update=update,
description='The amount of space between boards in the length direction'
)
# parquet specific
boards_in_group = IntProperty(
name='Boards in Group', min=1, soft_max=10, default=4,
update=update, description='Number of boards in a group'
)
# tile specific
tile_width = FloatProperty(
name='Tile Width', min=2*INCH, soft_max=2*FOOT, default=1*FOOT,
update=update, precision=2, description='Width of the tiles', unit='LENGTH',
)
tile_length = FloatProperty(
name='Tile Length', min=2*INCH, soft_max=2*FOOT, default=8*INCH,
update=update, precision=2, description='Length of the tiles', unit='LENGTH',
)
# grout
add_grout = BoolProperty(
name='Add Grout', default=False, description='Add grout', update=update
)
mortar_depth = FloatProperty(
name='Mortar Depth', min=0, soft_max=1*INCH, default=0.25*INCH,
update=update, precision=2, unit='LENGTH', step=0.005,
description='The depth of the mortar from the surface of the tile'
)
# regular tile
random_offset = BoolProperty(
name='Random Offset', update=update, default=False,
description='Random amount of offset for each row of tiles'
)
offset = FloatProperty(
name='Offset', update=update, min=0, max=100, default=0,
precision=2, description='How much to offset each row of tiles'
)
offset_variance = FloatProperty(
name='Offset Variance', update=update, min=0.001, max=100, default=50,
precision=2, description='How much to vary the offset each row of tiles'
)
# UV stuff
random_uvs = BoolProperty(
name='Random UV\'s', update=update, default=True, description='Random UV positions for the faces'
)
# bevel
bevel = BoolProperty(
name='Bevel', update=update, default=False, description='Bevel upper faces'
)
bevel_amount = FloatProperty(
name='Bevel Amount', update=update, unit='LENGTH', min=0.0001, max=0.005, default=0.001,
description='Bevel amount', precision=2, step=0.0005
)
@staticmethod
def append_all(v_list, add):
for i in add:
v_list.append(i)
@staticmethod
def create_uv_seams(bm):
handled = set()
for edge in bm.edges:
if edge.verts[0].co.z == 0 and edge.verts[1].co.z == 0: # bottom
# make sure both vertices on the edge haven't been handled, this forces one edge to not be made a seam
# leaving the bottom face still attached
if not (edge.verts[0].index in handled and edge.verts[1].index in handled):
edge.seam = True
handled.add(edge.verts[0].index)
handled.add(edge.verts[1].index)
elif edge.verts[0].co.z != edge.verts[1].co.z: # not horizontal, so they are vertical seams
edge.seam = True
@staticmethod
def rotate_point(point, pivot, angle, units="DEGREES"):
if units == "DEGREES":
angle = radians(angle)
x, y = point[0] - pivot[0], point[1] - pivot[1]
new_x = (x * cos(angle)) - (y * sin(angle))
new_y = (x * sin(angle)) - (y * cos(angle))
return new_x + pivot[0], new_y + pivot[1]
# ---------------------------------------------------
# Patterns
# ---------------------------------------------------
def regular_tile(self):
"""
____ ____ ____
| || || | Regular tile, rows can be offset, either manually or randomly
|____||____||____|
____ ____ ____
| || || |
|____||____||____|
"""
off = False
o = 1 / (100 / self.offset) if self.offset != 0 else 0
cur_y = 0.0
while cur_y < self.length:
cur_x = 0.0
tl2 = self.tile_length
if cur_y < self.length < cur_y + self.tile_length:
tl2 = self.length - cur_y
while cur_x < self.width:
tw2 = self.tile_width
if cur_x < self.width < cur_x + self.tile_width:
tw2 = self.width - cur_x
elif cur_x == 0.0 and off and not self.random_offset:
tw2 = self.tile_width * o
elif cur_x == 0.0 and self.random_offset:
v = self.tile_width * self.offset_variance * 0.0049
tw2 = uniform((self.tile_width / 2) - v, (self.tile_width / 2) + v)
self.add_plane(cur_x, cur_y, tw2, tl2)
cur_x += tw2 + self.spacing
cur_y += tl2 + self.spacing
off = not off
def hopscotch(self):
"""
____ _ Large tile, plus small one on top right corner
| ||_|
|____| ____ _ But shifted up so next large one is right below previous small one
| ||_|
|____|
"""
cur_y = 0
sp = self.spacing
# movement variables
row = 0
tw = self.tile_width
tl = self.tile_length
s_tw = (tw - sp) / 2 # small tile width
s_tl = (tl - sp) / 2 # small tile length
pre_y = cur_y
while cur_y < self.length or (row == 2 and cur_y - s_tl - sp < self.length):
cur_x = 0
step_back = True
if row == 1: # row start indented slightly
cur_x = s_tw + sp
while cur_x < self.width:
if row == 0 or row == 1:
# adjust for if there is a need to cut off the bottom of the tile
if cur_y < 0:
self.add_plane(cur_x, 0, tw, tl + cur_y) # large one
else:
self.add_plane(cur_x, cur_y, tw, tl) # large one
self.add_plane(cur_x + tw + sp, cur_y + s_tl + sp, s_tw, s_tl) # small one
if step_back:
cur_x += tw + sp
cur_y -= s_tl + sp
else:
cur_x += tw + s_tw + 2*sp
cur_y += s_tl + sp
step_back = not step_back
else:
if cur_x == 0: # half width for starting position
self.add_plane(cur_x, cur_y, s_tw, tl) # large one
# small one on right
self.add_plane(cur_x + s_tw + sp, cur_y + s_tl + sp, s_tw, s_tl)
# small one on bottom
self.add_plane(cur_x, cur_y - sp - s_tl, s_tw, s_tl)
cur_x += (2 * s_tw) + tw + (3 * sp)
else:
self.add_plane(cur_x, cur_y, tw, tl) # large one
# small one on right
self.add_plane(cur_x + tw + sp, cur_y + s_tl + sp, s_tw, s_tl)
cur_x += (2 * tw) + (3*sp) + s_tw
if row == 0 or row == 2:
cur_y = pre_y + tl + sp
else:
cur_y = pre_y + s_tl + sp
pre_y = cur_y
row = (row + 1) % 3 # keep wrapping rows
def stepping_stone(self):
"""
____ __ ____
| ||__|| | Row of large one, then two small ones stacked beside it
| | __ | |
|____||__||____|
__ __ __ __
|__||__||__||__| Row of smalls
"""
sp = self.spacing
cur_y = 0.0
row = 0
tw = self.tile_width
tl = self.tile_length
s_tw = (tw - sp) / 2
s_tl = (tl - sp) / 2
while cur_y < self.length:
cur_x = 0
while cur_x < self.width:
if row == 0: # large one then two small ones stacked beside it
self.add_plane(cur_x, cur_y, tw, tl)
self.add_plane(cur_x + tw + sp, cur_y, s_tw, s_tl,)
self.add_plane(cur_x + tw + sp, cur_y + s_tl + sp, s_tw, s_tl)
cur_x += tw + s_tw + (2 * sp)
else: # row of small ones
self.add_plane(cur_x, cur_y, s_tw, s_tl)
self.add_plane(cur_x + s_tw + sp, cur_y, s_tw, s_tl)
cur_x += tw + sp
if row == 0:
cur_y += tl + sp
else:
cur_y += s_tl + sp
row = (row + 1) % 2
def hexagon(self):
"""
__ Hexagon tiles
/ \
\___/
"""
sp = self.spacing
width = self.tile_width
dia = (width / 2) / cos(radians(30))
# top of current, half way up next, vertical spacing component
vertical_spacing = dia * (1 + sin(radians(30))) + (sp * sin(radians(60))) # center of one row to next row
base_points = [self.rotate_point((dia, 0), (0, 0), ang + 30) for ang in range(0, 360, 60)]
cur_y = 0
offset = False
while cur_y - width / 2 < self.length: # place tile as long as bottom is still within bounds
if offset:
cur_x = width / 2
else:
cur_x = -sp / 2
while cur_x - width / 2 < self.width: # place tile as long as left is still within bounds
p = len(self.vs)
for pt in base_points:
self.vs.append((pt[0] + cur_x, pt[1] + cur_y, 0))
self.fs.append([p] + [i for i in range(len(self.vs) - 1, p, -1)])
cur_x += width + sp
cur_y += vertical_spacing
offset = not offset
def windmill(self):
"""
__ ____
| ||____| This also has a square one in the middle, totaling 5 tiles per pattern
|__| __
____ | |
|____||__|
"""
sp = self.spacing
tw = self.tile_width
tl = self.tile_length
s_tw = (tw - sp) / 2
s_tl = (tl - sp) / 2
cur_y = 0
while cur_y < self.length:
cur_x = 0
while cur_x < self.width:
self.add_plane(cur_x, cur_y, tw, s_tl) # bottom
self.add_plane(cur_x + tw + sp, cur_y, s_tw, tl) # right
self.add_plane(cur_x + s_tw + sp, cur_y + tl + sp, tw, s_tl) # top
self.add_plane(cur_x, cur_y + s_tl + sp, s_tw, tl) # left
self.add_plane(cur_x + s_tw + sp, cur_y + s_tl + sp, s_tw, s_tl) # center
cur_x += tw + s_tw + (2*sp)
cur_y += tl + s_tl + (2*sp)
def boards(self):
"""
||| Typical wood boards
|||
"""
cur_x = 0.0
bw, bl = self.board_width, self.board_length
while cur_x < self.width:
if self.vary_width:
v = bw * (self.width_variance / 100) * 0.99
bw2 = uniform(bw - v, bw + v)
else:
bw2 = bw
if bw2 + cur_x > self.width:
bw2 = self.width - cur_x
cur_y = 0.0
counter = 1
while cur_y < self.length:
bl2 = bl
if self.vary_length:
v = bl * (self.length_variance / 100) * 0.99
bl2 = uniform(bl - v, bl + v)
if (counter >= self.max_boards and self.vary_length) or cur_y + bl2 > self.length:
bl2 = self.length - cur_y
self.add_plane(cur_x, cur_y, bw2, bl2)
cur_y += bl2 + self.length_spacing
counter += 1
cur_x += bw2 + self.width_spacing
def square_parquet(self):
"""
||--||-- Alternating groups oriented either horizontally, or forwards and backwards.
||--||-- self.spacing is used because it is the same spacing for width and length
--||--|| Board width is calculated using number of boards and the length.
--||--||
"""
cur_x = 0.0
start_orient_length = True
# figure board width
bl = self.short_board_length
bw = (bl - (self.boards_in_group - 1) * self.spacing) / self.boards_in_group
while cur_x < self.width:
cur_y = 0.0
orient_length = start_orient_length
while cur_y < self.length:
if orient_length:
start_x = cur_x
for i in range(self.boards_in_group):
if cur_x < self.width and cur_y < self.length:
self.add_plane(cur_x, cur_y, bw, bl)
cur_x += bw + self.spacing
cur_x = start_x
cur_y += bl + self.spacing
else:
for i in range(self.boards_in_group):
if cur_x < self.width and cur_y < self.length:
self.add_plane(cur_x, cur_y, bl, bw)
cur_y += bw + self.spacing
orient_length = not orient_length
start_orient_length = not start_orient_length
cur_x += bl + self.spacing
def herringbone(self):
"""
Boards are at 45 degree angle, in chevron pattern, ends are angled
"""
width_dif = self.board_width / cos(radians(45))
x_dif = self.short_board_length * cos(radians(45))
y_dif = self.short_board_length * sin(radians(45))
total_y_dif = width_dif + y_dif
sp_dif = self.spacing / cos(radians(45))
cur_y = -y_dif
while cur_y < self.length:
cur_x = 0
while cur_x < self.width:
# left side
p = len(self.vs)
self.append_all(
self.vs,
[(cur_x, cur_y, 0), (cur_x + x_dif, cur_y + y_dif, 0),
(cur_x + x_dif, cur_y + total_y_dif, 0), (cur_x, cur_y + width_dif, 0)]
)
self.fs.append([p + 3, p + 2, p + 1, p])
cur_x += x_dif + self.spacing
# right side
if cur_x < self.width:
p = len(self.vs)
self.append_all(
self.vs,
[(cur_x, cur_y + y_dif, 0), (cur_x + x_dif, cur_y, 0),
(cur_x + x_dif, cur_y + width_dif, 0), (cur_x, cur_y + total_y_dif, 0)]
)
self.fs.append([p + 3, p + 2, p + 1, p])
cur_x += x_dif + self.spacing
cur_y += width_dif + sp_dif # adjust spacing amount for 45 degree angle
def herringbone_parquet(self):
"""
Boards are at 45 degree angle, in chevron pattern, ends are square, not angled
"""
x_dif = self.short_board_length * cos(radians(45))
y_dif = self.short_board_length * sin(radians(45))
y_dif_45 = self.board_width * cos(radians(45))
x_dif_45 = self.board_width * sin(radians(45))
total_y_dif = y_dif + y_dif_45
sp_dif = (self.spacing / cos(radians(45))) / 2 # divide by two since it is used for both x and y
width_dif = self.board_width / cos(radians(45))
cur_y = -y_dif
while cur_y - y_dif_45 < self.length: # continue as long as bottom left corner is still good
cur_x = 0
pre_y = cur_y
while cur_x - x_dif_45 < self.width: # continue as long as top left corner is still good
# left side
p = len(self.vs)
self.append_all(
self.vs,
[(cur_x, cur_y, 0), (cur_x + x_dif, cur_y + y_dif, 0),
(cur_x + x_dif - x_dif_45, cur_y + total_y_dif, 0), (cur_x - x_dif_45, cur_y + y_dif_45, 0)]
)
self.fs.append([p + 3, p + 2, p + 1, p])
cur_x += x_dif - x_dif_45 + sp_dif
cur_y += y_dif - y_dif_45 - sp_dif
if cur_x < self.width:
p = len(self.vs)
self.append_all(
self.vs,
[(cur_x, cur_y, 0), (cur_x + x_dif, cur_y - y_dif, 0),
(cur_x + x_dif + x_dif_45, cur_y - y_dif + y_dif_45, 0),
(cur_x + x_dif_45, cur_y + y_dif_45, 0)]
)
self.fs.append([p + 3, p + 2, p + 1, p])
cur_x += x_dif + x_dif_45 + sp_dif
cur_y -= y_dif - y_dif_45 - sp_dif
else: # we didn't place the right board, so step ahead far enough the the while loop for x breaks
cur_x = self.width + x_dif_45
cur_y = pre_y + width_dif + (2*sp_dif)
# --------------------------------------------------
# Non-pattern functions
# --------------------------------------------------
def add_plane(self, x, y, w, l, clip=True):
"""
Adds vertices and faces for a place, clip to outer boundaries if clip is True
:param x: start x position
:param y: start y position
:param w: width (in x direction)
:param l: length (in y direction)
:param clip: trim back plane to be within length and width
"""
# if starting point is greater than bounds, don't even bother
if clip and (x >= self.width or y >= self.length):
return
if clip and x + w > self.width:
w = self.width - x
if clip and y + l > self.length:
l = self.length - y
p = len(self.vs)
self.append_all(self.vs, [(x, y, 0), (x + w, y, 0), (x + w, y + l, 0), (x, y + l, 0)])
self.fs.append([p + 3, p + 2, p + 1, p])
def add_manipulator(self, name, pt1, pt2, pt3):
m = self.manipulators.add()
m.prop1_name = name
m.set_pts([pt1, pt2, pt3])
def confirm_materials(self, obj):
mats = len(obj.data.materials)
if mats == 0: # add main material
mat = bpy.data.materials.new(obj.name + "_floor")
obj.data.materials.append(mat)
if (mats == 0 or mats == 1) and self.add_grout: # add grout
mat = bpy.data.materials.new(obj.name + "_grout")
obj.data.materials.append(mat)
if mats == 2 and not self.add_grout: # remove grout
obj.data.materials.pop(1, update_data=True)
def generate_pattern(self):
# clear data before refreshing it
self.vs, self.fs, self.ms, self.us = [], [], [], []
self.uv_factor = 1 / max(self.width, self.length) # automatically scale to keep within reasonable bounds
if self.pattern == "boards":
self.boards()
elif self.pattern == "square_parquet":
self.square_parquet()
elif self.pattern == "herringbone":
self.herringbone()
elif self.pattern == "herringbone_parquet":
self.herringbone_parquet()
elif self.pattern == "regular_tile":
self.regular_tile()
elif self.pattern == "hopscotch":
self.hopscotch()
elif self.pattern == "stepping_stone":
self.stepping_stone()
elif self.pattern == "hexagon":
self.hexagon()
elif self.pattern == "windmill":
self.windmill()
def update_manipulators(self):
self.manipulators.clear() # clear every time, add new ones
self.add_manipulator("length", (0, 0, 0), (0, self.length, 0), (-0.4, 0, 0))
self.add_manipulator("width", (0, 0, 0), (self.width, 0, 0), (0.4, 0, 0))
z = self.thickness
if self.pattern == "boards":
self.add_manipulator("board_length", (0, 0, z), (0, self.board_length, z), (0.1, 0, z))
self.add_manipulator("board_width", (0, 0, z), (self.board_width, 0, z), (-0.2, 0, z))
elif self.pattern == "square_parquet":
self.add_manipulator("short_board_length", (0, 0, z), (0, self.short_board_length, z), (-0.2, 0, z))
elif self.pattern in ("herringbone", "herringbone_parquet"):
dia = self.short_board_length * cos(radians(45))
dia2 = self.board_width * cos(radians(45))
self.add_manipulator("short_board_length", (0, 0, z), (dia, dia, z), (0, 0, z))
self.add_manipulator("board_width", (dia, 0, z), (dia - dia2, dia2, z), (0, 0, z))
else:
tl = self.tile_length
tw = self.tile_width
if self.pattern in ("regular_tile", "hopscotch", "stepping_stone"):
self.add_manipulator("tile_width", (0, tl, z), (tw, tl, z), (0, 0, z))
self.add_manipulator("tile_length", (0, 0, z), (0, tl, z), (0, 0, z))
elif self.pattern == "hexagon":
self.add_manipulator("tile_width", (tw / 2 + self.spacing, 0, z), (tw * 1.5 + self.spacing, 0, z),
(0, 0, 0))
elif self.pattern == "windmill":
self.add_manipulator("tile_width", (0, 0, z), (tw, 0, 0), (0, 0, z))
self.add_manipulator("tile_length", (0, tl / 2 + self.spacing, z), (0, tl * 1.5 + self.spacing, z),
(0, 0, z))
@property
def verts(self):
return self.vs
@property
def faces(self):
return self.fs
@property
def uvs(self):
return self.us
@property
def matids(self):
return self.ms
def update(self, context):
old = context.active_object
o, props = ARCHIPACK_PT_floor.params(old)
if props != self:
return
o.select = True
context.scene.objects.active = o
self.generate_pattern() # update vertices and faces
self.confirm_materials(o) # update materials
BmeshHelper.buildmesh(context, o, self.verts, self.faces)
# needs bisected?
bisect = self.pattern in ('hexagon', 'herringbone', 'herringbone_parquet')
for mod in o.modifiers:
if mod.type == 'BOOLEAN':
bisect = False
if bisect:
BmeshHelper.bissect(context, o, Vector((0, 0, 0)), Vector((0, -1, 0))) # bottom
BmeshHelper.bissect(context, o, Vector((0, self.length, 0)), Vector((0, 1, 0))) # top
BmeshHelper.bissect(context, o, Vector((0, 0, 0)), Vector((-1, 0, 0))) # left
BmeshHelper.bissect(context, o, Vector((self.width, 0, 0)), Vector((1, 0, 0))) # right
# create bmesh to edit
bm = bmesh.new()
bm.from_mesh(o.data)
bm.verts.ensure_lookup_table()
# extrude
bmesh.ops.solidify(bm, geom=bm.faces, thickness=1)
bm.verts.ensure_lookup_table()
bm.faces.ensure_lookup_table()
# thickness
faces = []
for face in bm.faces:
if face.verts[0].co.z > 0.9: # the thickness above is one, allow for some rounding error
# calculate thickness
if self.vary_thickness:
off = 1 / (100 / self.thickness_variance) if self.thickness_variance != 0 else 0
v = off * self.thickness
z = uniform(self.thickness - v, self.thickness + v)
else:
z = self.thickness
# apply thickness to every vertex in the face
for vertex in face.verts:
bm.verts[vertex.index].co.z = z
faces.append(face)
# bevel if needed
if self.bevel:
geometry = []
for face in faces:
self.append_all(geometry, face.edges)
self.append_all(geometry, face.verts)
bmesh.ops.bevel(bm, geom=geometry, offset=self.bevel_amount, segments=1, profile=0.5)
# add material id's before adding grout
for face in bm.faces:
face.material_index = 0
# grout
if self.add_grout:
# add cube then adjust vertex positions
grout_geometry = bmesh.ops.create_cube(bm, size=1.0)
z = self.thickness - self.mortar_depth
for vertex in grout_geometry['verts']:
# x
if vertex.co.x > 0:
vertex.co.x = self.width
elif vertex.co.x < 0:
vertex.co.x = 0
# y
if vertex.co.y > 0:
vertex.co.y = self.length
elif vertex.co.y < 0:
vertex.co.y = 0
# z
if vertex.co.z > 0:
vertex.co.z = z
elif vertex.co.z < 0:
vertex.co.z = 0
# material id for grout
for face in vertex.link_faces:
face.material_index = 1
# create seams
self.create_uv_seams(bm)
# free bmesh
bm.to_mesh(o.data)
bm.free()
# unwrap since mesh has now been updated
if context.mode != "EDIT_MESH":
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.unwrap()
bpy.ops.mesh.select_all(action='DESELECT')
if context.mode == "EDIT_MESH":
bpy.ops.object.editmode_toggle()
# update manipulators
self.update_manipulators()
# restore context
old.select = True
context.scene.objects.active = old
# ------------------------------------------------------------------
# Define panel class to show object parameters in ui panel (N)
# ------------------------------------------------------------------
class ARCHIPACK_PT_floor(Panel):
bl_idname = "ARCHIPACK_PT_floor"
bl_label = "Floor"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Floor'
def draw(self, context):
layout = self.layout
o = context.object
o, props = ARCHIPACK_PT_floor.params(o)
if props is None:
return
# manipulate
layout.operator("archipack.floor_manipulate")
layout.separator()
layout.prop(props, 'pattern')
layout.separator()
# overall measurements
layout.prop(props, 'width')
layout.prop(props, 'length')
# thickness
layout.separator()
layout.prop(props, 'thickness')
layout.prop(props, 'vary_thickness', icon='RNDCURVE')
if props.vary_thickness:
layout.prop(props, 'thickness_variance')
layout.separator()
if props.pattern == 'boards':
layout.prop(props, 'board_length')
layout.prop(props, 'vary_length', icon='RNDCURVE')
if props.vary_length:
layout.prop(props, 'length_variance')
layout.prop(props, 'max_boards')
layout.separator()
# width
layout.prop(props, 'board_width')
# vary width
layout.prop(props, 'vary_width', icon='RNDCURVE')
if props.vary_width:
layout.prop(props, 'width_variance')
layout.separator()
layout.prop(props, 'length_spacing')
layout.prop(props, 'width_spacing')
layout.separator()
elif props.pattern in ('square_parquet', 'herringbone_parquet', 'herringbone'):
layout.prop(props, 'short_board_length')
if props.pattern != "square_parquet":
layout.prop(props, "board_width")
layout.prop(props, "spacing")
if props.pattern == 'square_parquet':
layout.prop(props, 'boards_in_group')
elif props.pattern in ('regular_tile', 'hopscotch', 'stepping_stone', 'hexagon', 'windmill'):
# width and length and mortar
if props.pattern != "hexagon":
layout.prop(props, "tile_length")
layout.prop(props, "tile_width")
layout.prop(props, "mortar_depth")
layout.separator()
if props.pattern == "regular":
layout.prop(props, "random_offset", icon="RNDCURVE")
if props.random_offset:
layout.prop(props, "offset_variance")
else:
layout.prop(props, "offset")
# grout
layout.separator()
layout.prop(props, 'add_grout', icon='MESH_GRID')
if props.add_grout:
layout.prop(props, 'mortar_depth')
# bevel
layout.separator()
layout.prop(props, 'bevel', icon='MOD_BEVEL')
if props.bevel:
layout.prop(props, 'bevel_amount')
# uv
# updating
layout.separator()
layout.prop(props, 'auto_update', icon='FILE_REFRESH')
if not props.auto_update:
layout.operator('archipack.floor_update')
@classmethod
def params(cls, o):
if cls.filter(o):
if 'archipack_floor' in o.data:
return o, o.data.archipack_floor[0]
return o, None
@classmethod
def filter(cls, o):
try:
return o.data is not None and bool('archipack_floor' in o.data)
except:
return False
@classmethod
def poll(cls, context):
o = context.object
if o is None:
return False
return cls.filter(o)
# ------------------------------------------------------------------
# Define operator class to create object
# ------------------------------------------------------------------
class ARCHIPACK_OT_floor(Operator):
bl_idname = "archipack.floor"