13
13
14
14
stdout = io .StringIO ()
15
15
from dataset_config import *
16
- from blender_utils import gancio , get_min_max
16
+ from blender_utils import *
17
17
from shp2obj import Collection , deselect_all
18
18
19
19
20
+ class IdAssigner :
21
+ """
22
+ Class that assigns an instance id based on the module type.
23
+ """
24
+ def __init__ (self ):
25
+ self .mapping = {x : y for (x , y ) in zip (MODULES , range (2 , len (MODULES ) + 2 ))}
26
+
27
+ def make (self , name : str ) -> int :
28
+ """
29
+ Function that returns an id based on the module type. Numbering starts from
30
+ 1 since 0 is the building envelope.
31
+ :param name: name of the module, str
32
+ :return: id, int
33
+ """
34
+ assert name in MODULES , "Expected name to be in MODULES, got {}" .format (name )
35
+ return self .mapping [name ]
36
+
37
+
20
38
class Connector :
21
- def __init__ (self , module , volume , axis ):
39
+ def __init__ (self , module , volume , axis , side = 0 ):
22
40
self .module = module
23
41
self .axis = axis
42
+ self .side = side
24
43
self .volume = volume
25
44
self ._connect ()
26
45
27
46
def _connect (self ):
28
- gancio (self .volume , self .module , self .axis , 0 , 1 )
47
+ gancio (self .volume , self .module , self .axis , self . side , 1 )
29
48
30
49
31
50
class Module :
@@ -37,9 +56,11 @@ def __init__(self, name='generic', scale=None, mask=(1.0, 0.0, 0.0)):
37
56
or issubclass (mask .__class__ , np .ndarray ), "Expected mask to be an " \
38
57
"array or a tuple, got {}" .format (type (mask ))
39
58
assert len (mask ) == 3 , "Expected mask to have 3 colors, got {}" .format (len (mask ))
40
- self .mask = mask
59
+ # self.mask = mask
41
60
self .mesh = self ._create ()
42
61
self .parent = self ._nest ()
62
+ self ._assign_id ()
63
+ self ._triangulate ()
43
64
44
65
def __copy__ (self ):
45
66
m = self .__class__ (self .name , scale = self .scale )
@@ -54,12 +75,12 @@ def apply(self, material=None):
54
75
_material = MaterialFactory ().produce ()
55
76
self .mesh .active_material = _material .value
56
77
57
- def connect (self , volume , axis ):
58
- self ._connect (volume , axis )
78
+ def connect (self , volume , axis , side = 0 ):
79
+ self ._connect (volume , axis , side )
59
80
60
- def mask (self ):
61
- material = MaterialFactory ().produce ('mask' , color = self .mask )
62
- self .mesh .active_material = material .value
81
+ # def mask(self):
82
+ # material = MaterialFactory().produce('mask', color=self.mask)
83
+ # self.mesh.active_material = material.value
63
84
64
85
def position (self , position ):
65
86
assert isinstance (position , list ) or isinstance (position , tuple ) or \
@@ -77,8 +98,12 @@ def remove(self):
77
98
with redirect_stdout (stdout ), redirect_stderr (stdout ):
78
99
bpy .ops .object .delete ()
79
100
80
- def _connect (self , volume , axis ):
81
- self .connector = self .ModuleConnector (self , volume , axis )
101
+ def _assign_id (self ):
102
+ self .mesh ["inst_id" ] = IdAssigner ().make (self .name )
103
+ self .mesh .pass_index = IdAssigner ().make (self .name )
104
+
105
+ def _connect (self , volume , axis , side ):
106
+ self .connector = self .ModuleConnector (self , volume , axis , side )
82
107
83
108
def _create (self ):
84
109
# rule how connects to mesh
@@ -93,13 +118,20 @@ def _nest(self):
93
118
bpy .data .collections [self .name ].objects .link (bpy .data .objects [self .mesh .name ])
94
119
return bpy .data .collections [self .name ]
95
120
121
+ def _triangulate (self ):
122
+ deselect_all ()
123
+ if self .mesh :
124
+ select (self .mesh )
125
+ bpy .ops .object .modifier_add (type = 'TRIANGULATE' )
126
+ bpy .ops .object .modifier_apply ()
127
+
96
128
class ModuleConnector (Connector ):
97
- def __init__ (self , module , volume , axis ):
98
- Connector .__init__ (self , module , volume , axis )
129
+ def __init__ (self , module , volume , axis , side ):
130
+ Connector .__init__ (self , module , volume , axis , side )
99
131
100
132
101
133
class Window (Module ):
102
- def __init__ (self , name = 'window' , scale = (1.5 , 0.05 , 1.5 )):
134
+ def __init__ (self , name : str = 'window' , scale : tuple = (1.5 , 0.05 , 1.5 )):
103
135
Module .__init__ (self , name , scale )
104
136
105
137
def _create (self ):
@@ -109,13 +141,13 @@ def _create(self):
109
141
return bpy .context .selected_objects [0 ]
110
142
111
143
class ModuleConnector (Connector ):
112
- def __init__ (self , module , volume , axis ):
113
- Connector .__init__ (self , module , volume , axis )
144
+ def __init__ (self , module : Module , volume , axis : bool , side ):
145
+ Connector .__init__ (self , module , volume , axis , side )
114
146
115
147
def _connect (self ):
116
148
if self .axis == 0 :
117
- self .module .mesh .rotation_euler = math .radians (90 )
118
- gancio (self .volume , self .module , self .axis , 0 , 1 )
149
+ self .module .mesh .rotation_euler [ 2 ] = math .radians (90 )
150
+ gancio (self .volume , self .module , self .axis , self . side , 1 )
119
151
120
152
121
153
class ModuleFactory :
@@ -125,11 +157,10 @@ class ModuleFactory:
125
157
def __init__ (self ):
126
158
self .mapping = {'generic' : Module ,
127
159
'window' : Window }
128
- self .mapping = {x :y for x ,y in self .mapping .items () if x in MODULES or x == 'generic' }
160
+ self .mapping = {x : y for x , y in self .mapping .items () if x in MODULES or x == 'generic' }
129
161
self .mask_colors = list (range (len (self .mapping )))
130
- self ._mask_colors ()
131
162
132
- def produce (self , name ) :
163
+ def produce (self , name : str ) -> object :
133
164
"""
134
165
Function that produces a module based on its name.
135
166
:param name: name of the module to produce, str, should be in mapping
@@ -141,16 +172,6 @@ def produce(self, name):
141
172
else :
142
173
return self .mapping ['generic' ]()
143
174
144
- def _mask_colors (self ):
145
- if len (self .mask_colors ) <= 3 :
146
- for i in range (len (self .mask_colors )):
147
- self .mask_colors [i ] = [0.0 , 0.0 , 0.0 ]
148
- self .mask_colors [i ][i ] = 1.0
149
- else :
150
- # Distribute colors around the color circle
151
- return NotImplementedError
152
-
153
-
154
175
155
176
class ModuleApplier :
156
177
def __init__ (self , module_type ):
0 commit comments