-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path__init__.py
318 lines (269 loc) · 9.26 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
import math
import os
import sys
bl_info = {
"name": "BCGA",
"author": "Vladimir Elistratov <[email protected]>",
"version": (1, 0, 0),
"blender": (2, 80, 0),
"location": "View3D > Tool Shelf",
"description": "BCGA: Computer Generated Architecture for Blender",
"warning": "",
"wiki_url": "https://github.com/vvoovv/bcga/wiki",
"tracker_url": "https://github.com/vvoovv/bcga/issues",
"support": "COMMUNITY",
"category": "BCGA",
}
numFloatParams = 200
numColorParams = 50
for path in sys.path:
if "bpro" in path:
path = None
break
if path:
# we need to add path to bpro package
sys.path.append(os.path.dirname(__file__))
import bpy
import bpro
from pro import context as proContext
from pro.base import ParamFloat, ParamColor
from bpro.bl_util import create_rectangle, align_view, first_edge_ymin
def getRuleFile(ruleFile, operator):
"""
Returns full path to a BCGA script or None if it does not exist.
"""
if len(ruleFile)>1 and ruleFile[:2]=="//":
ruleFile = ruleFile[2:]
ruleFile = os.path.join(os.path.dirname(bpy.data.filepath), ruleFile)
if not os.path.isfile(ruleFile):
operator.report({"ERROR"}, "The BCGA script '%s' not found" % ruleFile)
ruleFile = None
return ruleFile
bpy.types.Scene.bcgaScript = bpy.props.StringProperty(
name = "Script",
description = "Path to a BCGA script",
)
bpy.types.Scene.bakingBcgaScript = bpy.props.StringProperty(
name = "Low poly script",
description = "Path to a BCGA script with a low poly model",
)
class CustomFloatProperty(bpy.types.PropertyGroup):
"""A bpy.types.PropertyGroup descendant for bpy.props.CollectionProperty"""
value: bpy.props.FloatProperty(name="")
class CustomColorProperty(bpy.types.PropertyGroup):
"""A bpy.types.PropertyGroup descendant for bpy.props.CollectionProperty"""
value: bpy.props.FloatVectorProperty(name="", subtype='COLOR', min=0.0, max=1.0)
class ProMainPanel(bpy.types.Panel):
bl_label = "Main"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
#bl_context = "objectmode"
bl_category = "BCGA"
def draw(self, context):
scene = context.scene
layout = self.layout
layout.row().operator_menu_enum("object.footprint_set", "size", text="Footprint")
layout.separator()
layout.row().prop_search(scene, "bcgaScript", bpy.data, "texts")
layout.row().operator("object.apply_pro_script")
class BakingPanel(bpy.types.Panel):
bl_label = "Baking"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "BCGA"
bl_options = {"DEFAULT_CLOSED"}
def draw(self, context):
scene = context.scene
layout = self.layout
layout.row().prop_search(scene, "bakingBcgaScript", bpy.data, "texts")
self.layout.operator("object.bake_pro_model")
class FirstEdgePanel(bpy.types.Panel):
bl_label = "First edge"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "BCGA"
bl_options = {"DEFAULT_CLOSED"}
def draw(self, context):
self.layout.operator("object.first_edge_ymin")
class Pro(bpy.types.Operator):
bl_idname = "object.apply_pro_script"
bl_label = "Apply"
bl_options = {"REGISTER", "UNDO"}
collectionFloat: bpy.props.CollectionProperty(type=CustomFloatProperty)
collectionColor: bpy.props.CollectionProperty(type=CustomColorProperty)
initialized = False
def initialize(self):
if self.initialized:
return
for _ in range(numFloatParams):
self.collectionFloat.add()
for _ in range(numColorParams):
self.collectionColor.add()
self.initialized = True
def invoke(self, context, event):
self.initialize()
proContext.blenderContext = context
ruleFile = bpy.data.texts[context.scene.bcgaScript].filepath
if ruleFile:
# append the directory of the ruleFile to sys.path
ruleFileDirectory = os.path.dirname(os.path.realpath(os.path.expanduser(ruleFile)))
if ruleFileDirectory not in sys.path:
sys.path.append(ruleFileDirectory)
module,params = bpro.apply(ruleFile)
#align_view(context.object)
self.module = module
self.params = params
numFloats = 0
numColors = 0
# for each entry in self.params create a new item in self.collection
for param in self.params:
param = param[1]
if isinstance(param, ParamFloat):
collectionItem = self.collectionFloat[numFloats]
numFloats += 1
elif isinstance(param, ParamColor):
collectionItem = self.collectionColor[numColors]
numColors += 1
collectionItem.value = param.getValue()
param.collectionItem = collectionItem
return {"FINISHED"}
def execute(self, context):
proContext.blenderContext = context
for param in self.params:
param = param[1]
param.setValue(getattr(param.collectionItem, "value"))
bpro.apply(self.module)
#align_view(context.object)
return {"FINISHED"}
def draw(self, context):
layout = self.layout
if hasattr(self, "params"):
# self.params is a list of tuples: (paramName, instanceofParamClass)
for param in self.params:
paramName = param[0]
row = layout.split()
row.label(text=paramName + ":")
row.prop(param[1].collectionItem, "value")
class Bake(bpy.types.Operator):
bl_idname = "object.bake_pro_model"
bl_label = "Bake"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
return context.scene.render.engine == "CYCLES"
def execute(self, context):
proContext.blenderContext = context
bpy.ops.object.select_all(action="DESELECT")
# remember the original object, it will be used for low poly model
lowPolyObject = context.object
lowPolyObject.select_set(True)
bpy.ops.object.duplicate()
highPolyObject = context.object
# high poly model
ruleFile = bpy.data.texts[context.scene.bcgaScript].filepath
if ruleFile:
highPolyParams = bpro.apply(ruleFile)[1]
# convert highPolyParams to a dict paramName->instanceofParamClass
highPolyParams = dict(highPolyParams)
# low poly model
context.view_layer.objects.active = lowPolyObject
ruleFile = bpy.data.texts[context.scene.bakingBcgaScript].filepath
if ruleFile:
name = lowPolyObject.name
module = bpro.getModule(ruleFile)
lowPolyParams = bpro.getParams(module)
# Apply highPolyParams to lowPolyParams
# Normally lowPolyParams is a subset of highPolyParams
for paramName,param in lowPolyParams:
if paramName in highPolyParams:
param.setValue(highPolyParams[paramName].getValue())
bpro.apply(module)
# unwrap the low poly model
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.uv.smart_project()
# prepare settings for baking
bpy.ops.object.mode_set(mode="OBJECT")
highPolyObject.select_set(True)
bpy.context.scene.cycles.bake_type = "DIFFUSE"
bpy.context.scene.cycles.use_bake_selected_to_active = True
# create a new image with default settings for baking
image = bpy.data.images.new(name=name, width=512, height=512)
# finally perform baking
bpy.ops.object.bake_image()
# delete the high poly object and its mesh
context.view_layer.objects.active = highPolyObject
mesh = highPolyObject.data
bpy.ops.object.delete()
bpy.data.meshes.remove(mesh)
context.view_layer.objects.active = lowPolyObject
# assign the baked texture to the low poly object
blenderTexture = bpy.data.textures.new(name, type = "IMAGE")
blenderTexture.image = image
blenderTexture.use_alpha = True
material = bpy.data.materials.new(name)
# textureSlot = material.texture_slots.add()
# textureSlot.texture = blenderTexture
# textureSlot.texture_coords = "UV"
# textureSlot.uv_layer = "bcga"
lowPolyObject.data.materials.append(material)
return {"FINISHED"}
class FootprintSet(bpy.types.Operator):
bl_idname = "object.footprint_set"
bl_label = "BCGA footprint"
bl_description = "Set a building footprint for BCGA"
bl_options = {"REGISTER", "UNDO"}
size: bpy.props.EnumProperty(
items = [
("35x15", "rectangle 35x15", "35x15"),
("20x10", "rectangle 20x10", "20x10"),
("10x10", "rectangle 10x10", "10x10")
]
)
def execute(self, context):
lightOffset = 20
lightHeight = 20
scene = context.scene
# delete active object if it is a mesh
active = context.object
if active and active.type=="MESH":
bpy.ops.object.delete()
# getting width and height of the footprint
w, h = [float(i) for i in self.size.split("x")]
# add lights
rx = math.atan((h+lightOffset)/lightHeight)
rz = math.atan((w+lightOffset)/(h+lightOffset))
def lamp_add(x, y, rx, rz):
bpy.ops.object.light_add(
type="SUN",
location=((x,y,lightHeight)),
rotation=(rx, 0, rz)
)
context.active_object.data.energy = 0.5
lamp_add(w+lightOffset, h+lightOffset, -rx, -rz)
lamp_add(-w-lightOffset, h+lightOffset, -rx, rz)
lamp_add(-w-lightOffset, -h-lightOffset, rx, -rz)
lamp_add(w+lightOffset, -h-lightOffset, rx, rz)
create_rectangle(context, w, h)
align_view(context.object)
return {"FINISHED"}
class FirstEdgeYmin(bpy.types.Operator):
bl_idname = "object.first_edge_ymin"
bl_label = "Contains min Y"
bl_description = "The first edge contains the vertex with minimal Y coordinate and has a longer length"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
first_edge_ymin(context)
return {"FINISHED"}
classes = (
CustomColorProperty,
CustomFloatProperty,
FirstEdgeYmin,
FootprintSet,
Bake,
Pro,
ProMainPanel,
BakingPanel,
FirstEdgePanel
)
register, unregister = bpy.utils.register_classes_factory(classes)