This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroomantic.py
1081 lines (866 loc) · 35.3 KB
/
roomantic.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 *****
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# ***** END GPL LICENSE BLOCK *****
from bpy_extras.io_utils import ImportHelper
import bmesh
import bpy
import math
import os
from copy import copy
import time
bl_info = {
"name": "ROOMantic",
"author": "hickv",
"version": (1, 0),
"blender": (3, 3, 0),
"location": "View3D > Tools > ROOMantic",
"description": "Toolbox for doom-style sector-based game level creation",
"warning": "WIP",
"wiki_url": "",
"category": "Object",
}
# FUNCS
class Point:
def __init__(self, x, y, z) -> None:
self.x = x
self.y = y
self.z = z
class Bounds:
def __init__(self) -> None:
self.min = None
self.max = None
def encapsulate(self, p: Point):
# min
if self.min == None:
self.min = Point(p.x, p.y, p.z)
else:
if p.x < self.min.x:
self.min.x = p.x
if p.y < self.min.y:
self.min.y = p.y
if p.z < self.min.z:
self.min.z = p.z
# max
if self.max == None:
self.max = Point(p.x, p.y, p.z)
else:
if p.x > self.max.x:
self.max.x = p.x
if p.y > self.max.y:
self.max.y = p.y
if p.z > self.max.z:
self.max.z = p.z
def expand(self, f):
self.min.x -= f
self.min.y -= f
self.min.z -= f
self.max.x += f
self.max.y += f
self.max.z += f
def intersect(self, other):
return (
self.min.x <= other.max.x and
self.max.x >= other.min.x and
self.min.y <= other.max.y and
self.max.y >= other.min.y and
self.min.z <= other.max.z and
self.max.z >= other.min.z)
def calculate_bounds_ws(mat, mesh, expand):
bounds = Bounds()
for v in mesh.vertices:
pointWS = mat @ v.co
bounds.encapsulate(Point(pointWS.x, pointWS.y, pointWS.z))
bounds.expand(expand)
return bounds
def _update_sector_solidify(self, context):
update_sector2d_solidify(context.active_object)
def update_sector2d_solidify(shape):
if shape.modifiers:
mod = shape.modifiers[0]
mod.thickness = shape.rmtc_ceiling_height - shape.rmtc_floor_height
mod.offset = 1 + shape.rmtc_floor_height / (mod.thickness / 2)
def is_shape(obj):
return obj.rmtc_shape_type != 'NONE'
def initialize_shape(shape):
if is_shape(shape):
shape.display_type = 'WIRE'
shape.rmtc_ceiling_height = 4
shape.rmtc_floor_height = 0
shape.rmtc_shape_auto_texture = True
shape.rmtc_floor_texture = ''
shape.rmtc_wall_texture = ''
shape.rmtc_ceiling_texture = ''
shape.rmtc_ceiling_texture_scale_offset = (1.0, 1.0, 0.0, 0.0)
shape.rmtc_wall_texture_scale_offset = (1.0, 1.0, 0.0, 0.0)
shape.rmtc_floor_texture_scale_offset = (1.0, 1.0, 0.0, 0.0)
shape.rmtc_ceiling_texture_rotation = 0
shape.rmtc_wall_texture_rotation = 0
shape.rmtc_floor_texture_rotation = 0
def _get_add_collection(name):
if name in bpy.data.collections:
return bpy.data.collections[name]
return bpy.data.collections.new(name=name)
def get_add_collection(scene, name):
col = _get_add_collection(name)
if name not in scene.collection.children:
scene.collection.children.link(col)
return col
def unlink_collections_all(obj):
for col in obj.users_collection:
col.objects.unlink(obj)
def link_collection_unique(obj, collection):
unlink_collections_all(obj)
collection.objects.link(obj)
def get_modifier(obj, type):
for mod in obj.modifiers:
if mod.type == type:
return mod
return None
def add_modifier(obj, type):
return obj.modifiers.new(name=type, type=type)
def get_add_modifier(obj, type):
mod = get_modifier(obj, type)
if mod is None:
mod = add_modifier(obj, type)
return mod
def set_material_slots_size(obj, size):
while len(obj.material_slots) < size:
obj.data.materials.append(None)
while len(obj.material_slots) > size:
obj.data.materials.pop()
def remove_not_used():
for obj in bpy.data.objects:
if obj.users == 0:
bpy.data.objects.remove(obj, do_unlink=True)
for mesh in bpy.data.meshes:
if mesh.users == 0:
bpy.data.meshes.remove(mesh, do_unlink=True)
# for material in bpy.data.materials:
# if material.users == 0:
# bpy.data.materials.remove(material, do_unlink=True)
def update_sector2d(shape):
# get add update solidify
solidify = get_add_modifier(shape, 'SOLIDIFY')
solidify.use_even_offset = True
solidify.use_quality_normals = True
solidify.use_even_offset = True
solidify.material_offset = 1
solidify.material_offset_rim = 2
update_sector2d_solidify(shape)
# add delete materials
set_material_slots_size(shape, 3)
# update update
if bpy.data.materials.find(shape.rmtc_ceiling_texture) != -1:
shape.material_slots[0].material = bpy.data.materials[shape.rmtc_ceiling_texture]
if bpy.data.materials.find(shape.rmtc_floor_texture) != -1:
shape.material_slots[1].material = bpy.data.materials[shape.rmtc_floor_texture]
if bpy.data.materials.find(shape.rmtc_wall_texture) != -1:
shape.material_slots[2].material = bpy.data.materials[shape.rmtc_wall_texture]
def update_shape_precision(shape):
shape.location.x = round(
shape.location.x, bpy.context.scene.rmtc_precision)
shape.location.y = round(
shape.location.y, bpy.context.scene.rmtc_precision)
shape.location.z = round(
shape.location.z, bpy.context.scene.rmtc_precision)
for v in shape.data.vertices:
v.co.x = round(v.co.x, bpy.context.scene.rmtc_precision)
v.co.y = round(v.co.y, bpy.context.scene.rmtc_precision)
v.co.z = round(v.co.z, bpy.context.scene.rmtc_precision)
def update_shape(shape):
if shape:
shape.display_type = 'WIRE'
update_shape_precision(shape)
if shape.rmtc_shape_type == 'SECTOR2D':
update_sector2d(shape)
def get_shapes(collections):
shapes = []
for col in collections:
for obj in col.all_objects:
if obj.rmtc_shape_type != None:
if obj.rmtc_shape_type != 'NONE':
if obj not in shapes:
shapes.append(obj)
return shapes
def copy_materials(source, target):
set_material_slots_size(target, max(
len(target.data.materials), len(source.data.materials)))
for i in range(0, len(source.data.materials)):
target.data.materials[i] = source.data.materials[i]
def copy_transforms(source, target):
target.location = source.location
target.scale = source.scale
target.rotation_euler = source.rotation_euler
def create_remove_material():
newMat = bpy.data.materials.new('REMOVE')
newMat.name = 'REMOVE'
newMat.diffuse_color = [1, 0, 1, 1]
newMat.use_fake_user = True
return newMat
def eval_shape(shape, namePrefix):
dg = bpy.context.evaluated_depsgraph_get()
evalObj = shape.evaluated_get(dg)
mesh = bpy.data.meshes.new_from_object(evalObj)
mesh.use_auto_smooth = shape.data.use_auto_smooth
mesh.auto_smooth_angle = shape.data.auto_smooth_angle
roomName = namePrefix + shape.name
room = None
if roomName in bpy.data.objects:
room = bpy.data.objects[roomName]
room.data = mesh
else:
room = bpy.data.objects.new(roomName, mesh)
copy_transforms(shape, room)
update_shape_precision(room)
return room
def create_mesh_obj(name):
mesh = bpy.data.meshes.new(name)
obj = bpy.data.objects.new(name, mesh)
return obj
def make_shape_boolean(shape):
set_material_slots_size(shape, 1)
shape.data.materials[0] = bpy.data.materials[bpy.context.scene.rmtc_remove_material]
def apply_csg(target, boolean, operation):
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = target
target.select_set(True)
mod = target.modifiers.new(name='boolean', type='BOOLEAN')
mod.object = boolean
mod.solver = 'EXACT'
mod.operation = operation
bpy.ops.object.modifier_apply(modifier='boolean')
def apply_remove_material(shape):
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = shape
shape.select_set(True)
if bpy.context.scene.rmtc_remove_material != "":
remove = False
for i, m in enumerate(shape.material_slots):
if bpy.context.scene.rmtc_remove_material == m.name:
shape.active_material_index = i
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.material_slot_select()
bpy.ops.mesh.delete(type='FACE')
bpy.ops.object.editmode_toggle()
bpy.ops.object.material_slot_remove()
def flip_normals(shape):
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = shape
shape.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.flip_normals()
bpy.ops.object.mode_set(mode='OBJECT')
def translate(val, t):
return val + t
def scale(val, s):
return val * s
def rotate2D(uv, degrees):
radians = math.radians(degrees)
newUV = copy(uv)
newUV.x = uv.x*math.cos(radians) - uv.y*math.sin(radians)
newUV.y = uv.x*math.sin(radians) + uv.y*math.cos(radians)
return newUV
def apply_auto_texture(shape, eval):
mesh = eval.data
objectLocation = shape.location
objectScale = shape.scale
bm = bmesh.new()
bm.from_mesh(mesh)
uv_layer = bm.loops.layers.uv.verify()
for f in bm.faces:
nX = f.normal.x
nY = f.normal.y
nZ = f.normal.z
if nX < 0:
nX = nX * -1
if nY < 0:
nY = nY * -1
if nZ < 0:
nZ = nZ * -1
faceNormalLargest = nX
faceDirection = "x"
if faceNormalLargest < nY:
faceNormalLargest = nY
faceDirection = "y"
if faceNormalLargest < nZ:
faceNormalLargest = nZ
faceDirection = "z"
if faceDirection == "x":
if f.normal.x < 0:
faceDirection = "-x"
if faceDirection == "y":
if f.normal.y < 0:
faceDirection = "-y"
if faceDirection == "z":
if f.normal.z < 0:
faceDirection = "-z"
for l in f.loops:
luv = l[uv_layer]
if faceDirection == "x":
luv.uv.x = ((l.vert.co.y * objectScale[1]) + objectLocation[1])
luv.uv.y = ((l.vert.co.z * objectScale[2]) + objectLocation[2])
luv.uv = rotate2D(luv.uv, shape.rmtc_wall_texture_rotation)
luv.uv.x = translate(scale(
luv.uv.x, shape.rmtc_wall_texture_scale_offset[0]), shape.rmtc_wall_texture_scale_offset[2])
luv.uv.y = translate(scale(
luv.uv.y, shape.rmtc_wall_texture_scale_offset[1]), shape.rmtc_wall_texture_scale_offset[3])
if faceDirection == "-x":
luv.uv.x = ((l.vert.co.y * objectScale[1]) + objectLocation[1])
luv.uv.y = ((l.vert.co.z * objectScale[2]) + objectLocation[2])
luv.uv = rotate2D(luv.uv, shape.rmtc_wall_texture_rotation)
luv.uv.x = translate(scale(
luv.uv.x, shape.rmtc_wall_texture_scale_offset[0]), shape.rmtc_wall_texture_scale_offset[2])
luv.uv.y = translate(scale(
luv.uv.y, shape.rmtc_wall_texture_scale_offset[1]), shape.rmtc_wall_texture_scale_offset[3])
if faceDirection == "y":
luv.uv.x = ((l.vert.co.x * objectScale[0]) + objectLocation[0])
luv.uv.y = ((l.vert.co.z * objectScale[2]) + objectLocation[2])
luv.uv = rotate2D(luv.uv, shape.rmtc_wall_texture_rotation)
luv.uv.x = translate(scale(
luv.uv.x, shape.rmtc_wall_texture_scale_offset[0]), shape.rmtc_wall_texture_scale_offset[2])
luv.uv.y = translate(scale(
luv.uv.y, shape.rmtc_wall_texture_scale_offset[1]), shape.rmtc_wall_texture_scale_offset[3])
if faceDirection == "-y":
luv.uv.x = ((l.vert.co.x * objectScale[0]) + objectLocation[0])
luv.uv.y = ((l.vert.co.z * objectScale[2]) + objectLocation[2])
luv.uv = rotate2D(luv.uv, shape.rmtc_wall_texture_rotation)
luv.uv.x = translate(scale(
luv.uv.x, shape.rmtc_wall_texture_scale_offset[0]), shape.rmtc_wall_texture_scale_offset[2])
luv.uv.y = translate(scale(
luv.uv.y, shape.rmtc_wall_texture_scale_offset[1]), shape.rmtc_wall_texture_scale_offset[3])
if faceDirection == "z":
luv.uv.x = ((l.vert.co.x * objectScale[0]) + objectLocation[0])
luv.uv.y = ((l.vert.co.y * objectScale[1]) + objectLocation[1])
luv.uv = rotate2D(luv.uv, shape.rmtc_floor_texture_rotation)
luv.uv.x = translate(scale(
luv.uv.x, shape.rmtc_floor_texture_scale_offset[0]), shape.rmtc_floor_texture_scale_offset[2])
luv.uv.y = translate(scale(
luv.uv.y, shape.rmtc_floor_texture_scale_offset[1]), shape.rmtc_floor_texture_scale_offset[3])
if faceDirection == "-z":
luv.uv.x = ((l.vert.co.x * objectScale[0]) + objectLocation[0])
luv.uv.y = ((l.vert.co.y * objectScale[1]) + objectLocation[1])
luv.uv = rotate2D(luv.uv, shape.rmtc_ceiling_texture_rotation)
luv.uv.x = translate(scale(
luv.uv.x, shape.rmtc_ceiling_texture_scale_offset[0]), shape.rmtc_ceiling_texture_scale_offset[2])
luv.uv.y = translate(scale(
luv.uv.y, shape.rmtc_ceiling_texture_scale_offset[1]), shape.rmtc_ceiling_texture_scale_offset[3])
bm.to_mesh(mesh)
bm.free()
eval.data = mesh
def apply_triangulate(shape):
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = shape
shape.select_set(True)
mod = shape.modifiers.new(name='triangulate', type='TRIANGULATE')
bpy.ops.object.modifier_apply(modifier='triangulate')
def apply_split_faces(shape):
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = shape
shape.select_set(True)
# FUNCS
# DATA
bpy.types.Scene.rmtc_precision = bpy.props.IntProperty(
name="Precision",
default=3,
min=0,
max=6,
description='Controls the rounding level of vertex precisions. A level of 1 would round 1.234 to 1.2 and a level of 2 would round to 1.23'
)
bpy.types.Scene.rmtc_remove_material = bpy.props.StringProperty(
name="Remove Material",
description="Material used as flag for removing geometry"
)
bpy.types.Object.rmtc_shape_type = bpy.props.EnumProperty(
items=[
("SECTOR2D", "Sector2D", "is a 2D sector"),
("SECTOR3D", "Sector3D", "is a 3D sector"),
("BRUSH", "Brush", "is a shape"),
("NONE", "None", "none"),
],
name="Shape Type",
default='NONE'
)
bpy.types.Object.rmtc_ceiling_height = bpy.props.FloatProperty(
name="Ceiling Height",
default=4,
step=10,
precision=3,
update=_update_sector_solidify
)
bpy.types.Object.rmtc_floor_height = bpy.props.FloatProperty(
name="Floor Height",
default=0,
step=10,
precision=3,
update=_update_sector_solidify
)
bpy.types.Object.rmtc_shape_auto_texture = bpy.props.BoolProperty(
name="Shape Auto Texture",
default=True,
description='Auto Texture on or off'
)
bpy.types.Object.rmtc_floor_texture = bpy.props.StringProperty(
name="Floor Texture",
)
bpy.types.Object.rmtc_wall_texture = bpy.props.StringProperty(
name="Wall Texture",
)
bpy.types.Object.rmtc_ceiling_texture = bpy.props.StringProperty(
name="Ceiling Texture",
)
bpy.types.Object.rmtc_ceiling_texture_scale_offset = bpy.props.FloatVectorProperty(
name="Ceiling Texture Scale Offset",
default=(1, 1, 0, 0),
min=0,
step=10,
precision=3,
size=4
)
bpy.types.Object.rmtc_wall_texture_scale_offset = bpy.props.FloatVectorProperty(
name="Wall Texture Scale Offset",
default=(1, 1, 0, 0),
min=0,
step=10,
precision=3,
size=4
)
bpy.types.Object.rmtc_floor_texture_scale_offset = bpy.props.FloatVectorProperty(
name="Floor Texture Scale Offset",
default=(1, 1, 0, 0),
min=0,
step=10,
precision=3,
size=4
)
bpy.types.Object.rmtc_ceiling_texture_rotation = bpy.props.FloatProperty(
name="Ceiling Texture Rotation",
default=0,
min=0,
step=10,
precision=3,
)
bpy.types.Object.rmtc_wall_texture_rotation = bpy.props.FloatProperty(
name="Wall Texture Rotation",
default=0,
min=0,
step=10,
precision=3,
)
bpy.types.Object.rmtc_floor_texture_rotation = bpy.props.FloatProperty(
name="Floor Texture Rotation",
default=0,
min=0,
step=10,
precision=3,
)
# DATA
# CLASSES
class ROOManticPanel(bpy.types.Panel):
bl_label = "ROOMantic"
bl_space_type = "VIEW_3D"
bl_region_type = 'UI'
bl_category = 'ROOMantic'
def draw(self, context):
obj = context.active_object
scene = bpy.context.scene
layout = self.layout
# base
col = layout.column(align=True)
col.label(icon="WORLD", text="Map Settings")
col.prop(scene, "rmtc_precision")
col.prop_search(scene, "rmtc_remove_material", bpy.data, "materials")
col = layout.column(align=True)
col.operator("scene.rmtc_build", text="Build All",
icon="MOD_BUILD").selected_only = False
col.operator("scene.rmtc_build", text="Build Selected",
icon="MOD_BUILD").selected_only = True
# tools
col = layout.column(align=True)
col.label(icon="SNAP_PEEL_OBJECT", text="Tools")
col.operator("scene.rmtc_open_material",
text="Open Material", icon="TEXTURE")
if bpy.context.mode == 'EDIT_MESH':
col.operator("object.rmtc_rip_geometry", text="Rip To",
icon="UNLINKED").focus_to_rip = True
col.operator("object.rmtc_rip_geometry", text="Rip Stay",
icon="UNLINKED").focus_to_rip = False
else:
col.operator("scene.rmtc_new_geometry", text="New Sector2D",
icon="MESH_PLANE").shape_type = 'SECTOR2D'
col.operator("scene.rmtc_new_geometry", text="New Sector3D",
icon="CUBE").shape_type = 'SECTOR3D'
col.operator("scene.rmtc_new_geometry", text="New Brush",
icon="MESH_CUBE").shape_type = 'BRUSH'
# object
if obj != None:
col = layout.column(align=True)
col.label(icon="MOD_ARRAY", text="Shape Properties")
col.prop(obj, "rmtc_shape_type", text="Shape Type")
col.prop(obj, "rmtc_shape_auto_texture", text="Auto Texture")
if obj.rmtc_shape_auto_texture:
col = layout.row(align=True)
col.prop(obj, "rmtc_ceiling_texture_scale_offset")
col = layout.row(align=True)
col.prop(obj, "rmtc_wall_texture_scale_offset")
col = layout.row(align=True)
col.prop(obj, "rmtc_floor_texture_scale_offset")
col = layout.row(align=True)
col.prop(obj, "rmtc_ceiling_texture_rotation")
col = layout.row(align=True)
col.prop(obj, "rmtc_wall_texture_rotation")
col = layout.row(align=True)
col.prop(obj, "rmtc_floor_texture_rotation")
if obj.rmtc_shape_type == 'SECTOR2D':
col = layout.column(align=True)
col.label(icon="MOD_ARRAY", text="Sector Properties")
col.prop(obj, "rmtc_ceiling_height")
col.prop(obj, "rmtc_floor_height")
# layout.separator()
col = layout.column(align=True)
col.prop_search(obj, "rmtc_ceiling_texture", bpy.data,
"materials", icon="MATERIAL", text="Ceiling")
col.prop_search(obj, "rmtc_wall_texture", bpy.data,
"materials", icon="MATERIAL", text="Wall")
col.prop_search(obj, "rmtc_floor_texture", bpy.data,
"materials", icon="MATERIAL", text="Floor")
class ROOManticBuild(bpy.types.Operator):
bl_idname = "scene.rmtc_build"
bl_label = "Build"
selected_only: bpy.props.BoolProperty(name="selected_only", default=False)
def execute(self, context):
# cache selected
activeObj = context.active_object
selectedObjs = []
for obj in context.selected_objects:
selectedObjs.append(obj)
# save context
wasEditMode = False
if context.mode == 'EDIT_MESH':
bpy.ops.object.mode_set(mode='OBJECT')
wasEditMode = True
# The new algo works to achieve this goal: each shape must be its own separate object
# So we can no longer rely on a global level_geometry object that gets booleaned around by each shape
# we now need to treat each shape separately
print("\nroomantic: build start")
start = time.time()
# cleanup
remove_not_used()
# make sure remove_materials is present
if context.scene.rmtc_remove_material == '' or context.scene.rmtc_remove_material is None or context.scene.rmtc_remove_material not in bpy.data.materials:
context.scene.rmtc_remove_material = create_remove_material().name
# get collection
levelCollection = get_add_collection(context.scene, 'ROOMantic_LEVEL')
levelCollection.hide_select = False
shapeCollection = get_add_collection(context.scene, 'ROOMantic_SHAPES')
shapeCollection.hide_select = False
# clear collections
for obj in levelCollection.objects:
# unlink only if selected
if self.selected_only:
fullName = obj.name.split('rmtc_')
if len(fullName) == 2:
shapeName = fullName[1]
for sel in selectedObjs:
if sel.name == shapeName:
unlink_collections_all(obj)
continue
else:
unlink_collections_all(obj)
# if brush inside this collection, then link it to scene
if obj.rmtc_shape_type != 'NONE':
link_collection_unique(obj, context.scene.collection)
end = time.time()
print(
"roomantic: cleanup collections - {:.2g} sec.".format(end - start))
start = time.time()
# look for shapes
shapes = get_shapes(
[context.scene.collection, levelCollection, shapeCollection])
# optimizations
hasBrushes = False
# loop on shapes
for shape in shapes:
# if has no geom then remove
if len(shape.data.vertices) < 3:
shapes.remove(shape)
continue
# mark hasbrushes flag
if shape.rmtc_shape_type == 'BRUSH':
hasBrushes = True
# update + link shapes
update_shape(shape)
link_collection_unique(shape, shapeCollection)
end = time.time()
print("roomantic: setting up shapes - {:.2g} sec.".format(end - start))
start = time.time()
# cache data: shape-boolean + brush-boolean + bounds (they are just remove_material blobs)
shapeIntersections = {}
shapeBooleans = {}
shapeBounds = {}
sectorBoolean = create_mesh_obj(
'sectorBoolean') if hasBrushes else None
if hasBrushes:
link_collection_unique(sectorBoolean, context.scene.collection)
for shape in shapes:
if self.selected_only and shape not in selectedObjs:
continue
# init intersections
shapeIntersections[shape] = []
# shapeBool
shapeBoolean = eval_shape(shape, 'boolean_')
make_shape_boolean(shapeBoolean)
shapeBooleans[shape] = shapeBoolean
# bounds
shapeBounds[shape] = calculate_bounds_ws(
shape.matrix_world, shapeBoolean.data, 0.1)
# brushBool
if hasBrushes:
if shape.rmtc_shape_type != 'BRUSH':
apply_csg(sectorBoolean, shapeBoolean, 'UNION')
end = time.time()
print(
"roomantic: caching shape data - {:.2g} sec.".format(end - start))
start = time.time()
# shape intersect map
for shape0 in shapes:
if self.selected_only and shape0 not in selectedObjs:
continue
# loop for each other shape
for shape1 in shapes:
if shape0 == shape1:
continue
# if shape is already intersecting then just ignore
if shape1 in shapeIntersections[shape0]:
continue
# check if valid
if shape0 not in shapeBounds or shape1 not in shapeBounds:
continue
# check intersection and add both ways
if shapeBounds[shape0].intersect(shapeBounds[shape1]):
# add other to current
shapeIntersections[shape0].append(shape1)
# add current to other
if shape0 not in shapeIntersections[shape1]:
shapeIntersections[shape1].append(shape0)
end = time.time()
print(
"roomantic: creatin shape intersection tables - {:.2g} sec.".format(end - start))
start = time.time()
# create/duplicate shapes to output
for shape0 in shapes:
if self.selected_only and shape0 not in selectedObjs:
continue
# eval shape
evaluatedShape = eval_shape(shape0, 'rmtc_')
evaluatedShape.display_type = 'TEXTURED'
copy_materials(shape0, evaluatedShape)
# add remove_material
removeMaterialIndex = len(evaluatedShape.data.materials)
set_material_slots_size(evaluatedShape, len(
evaluatedShape.data.materials) + 1)
evaluatedShape.data.materials[removeMaterialIndex] = bpy.data.materials[context.scene.rmtc_remove_material]
# link
link_collection_unique(evaluatedShape, levelCollection)
# apply csg
if shape0.rmtc_shape_type == 'BRUSH':
for shape1 in shapeIntersections[shape0]:
if shape1.rmtc_shape_type == 'BRUSH':
apply_csg(evaluatedShape,
shapeBooleans[shape1], 'UNION')
else:
apply_csg(evaluatedShape, sectorBoolean, 'INTERSECT')
else:
for shape1 in shapeIntersections[shape0]:
apply_csg(evaluatedShape, shapeBooleans[shape1], 'UNION')
flip_normals(evaluatedShape)
apply_remove_material(evaluatedShape)
apply_triangulate(evaluatedShape)
if shape0.rmtc_shape_type == 'SECTOR2D' or shape0.rmtc_shape_auto_texture:
apply_auto_texture(shape0, evaluatedShape)
end = time.time()
print("roomantic: performing csg - {:.2g} sec.".format(end - start))
start = time.time()
# mark unselectable
levelCollection.hide_select = True
# restore context
bpy.ops.object.select_all(action='DESELECT')
if activeObj != None:
bpy.context.view_layer.objects.active = activeObj
activeObj.select_set(True)
for obj in selectedObjs:
if obj != None:
obj.select_set(True)
if wasEditMode:
bpy.ops.object.mode_set(mode='EDIT')
# unlink sectorBoolean
if hasBrushes:
unlink_collections_all(sectorBoolean)
end = time.time()
print("roomantic: final cleanup - {:.2g} sec.".format(end - start))
return {"FINISHED"}
class ROOManticNewGeometry(bpy.types.Operator):
bl_idname = "scene.rmtc_new_geometry"
bl_label = "New Geometry"
shape_type: bpy.props.StringProperty(name="shape_type", default='NONE')
def execute(self, context):
bpy.ops.object.select_all(action='DESELECT')
if self.shape_type == 'SECTOR2D':
bpy.ops.mesh.primitive_plane_add(size=2)
else:
bpy.ops.mesh.primitive_cube_add(size=2)
shape = bpy.context.active_object
shape.name = self.shape_type
shape.data.name = self.shape_type
shape.rmtc_shape_type = self.shape_type
initialize_shape(shape)
update_shape(shape)
return {"FINISHED"}
class ROOManticOpenMaterial(bpy.types.Operator, ImportHelper):
bl_idname = "scene.rmtc_open_material"
bl_label = "Open Material"
filter_glob: bpy.props.StringProperty(
default='*.jpg;*.jpeg;*.png;*.tif;*.tiff;*.bmp',
options={'HIDDEN'}
)
files: bpy.props.CollectionProperty(
type=bpy.types.OperatorFileListElement,
options={'HIDDEN', 'SKIP_SAVE'},
)
def execute(self, context):
directory, fileNameExtension = os.path.split(self.filepath)
# do it for all selected files/images
for f in self.files:
fileName, fileExtension = os.path.splitext(f.name)
# new material or find it
newMaterialName = fileName
newMaterial = bpy.data.materials.get(newMaterialName)
if newMaterial == None:
newMaterial = bpy.data.materials.new(newMaterialName)
newMaterial.use_fake_user = True
newMaterial.use_nodes = True
newMaterial.preview_render_type = 'FLAT'
# We clear it as we'll define it completely
newMaterial.node_tree.links.clear()
newMaterial.node_tree.nodes.clear()
# create nodes
bsdfNode = newMaterial.node_tree.nodes.new(
'ShaderNodeBsdfPrincipled')
outputNode = newMaterial.node_tree.nodes.new(
'ShaderNodeOutputMaterial')
texImageNode = newMaterial.node_tree.nodes.new(
'ShaderNodeTexImage')
texImageNode.name = fileName
texImageNode.image = bpy.data.images.load(
directory + "\\" + fileName + fileExtension, check_existing=True)
# create node links
newMaterial.node_tree.links.new(
bsdfNode.outputs['BSDF'], outputNode.inputs['Surface'])
newMaterial.node_tree.links.new(
bsdfNode.inputs['Base Color'], texImageNode.outputs['Color'])
# some params
bsdfNode.inputs['Roughness'].default_value = 0
bsdfNode.inputs['Specular'].default_value = 0
return {"FINISHED"}
class ROOManticRipGeometry(bpy.types.Operator):
bl_idname = "object.rmtc_rip_geometry"
bl_label = "Rip Geometry"
focus_to_rip: bpy.props.BoolProperty(
name="active_to_riped", default=False)
def execute(self, context):
activeObj = context.active_object
activeObjBM = bmesh.from_edit_mesh(activeObj.data)
# https://blender.stackexchange.com/questions/179667/split-off-bmesh-selected-faces
activeObjBM.verts.ensure_lookup_table()
activeObjBM.edges.ensure_lookup_table()
activeObjBM.faces.ensure_lookup_table()
selectedFaces = [x for x in activeObjBM.faces if x.select]
# early out
if len(selectedFaces) == 0:
activeObjBM.free()
return {"CANCELLED"}
ripedObjBM = bmesh.new()
pyVerts = []
pyFaces = []
# rip-copy faces
for f in selectedFaces:
currentFaceIndices = []
for v in f.verts:
if v not in pyVerts:
pyVerts.append(v)
currentFaceIndices.append(pyVerts.index(v))