-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
319 lines (254 loc) · 10.3 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
# _____ _ _ _____ _
# | __|_____ ___ ___| |_|_|___ ___ | __| |_ ___ ___ ___
# |__ | | . | | '_| | | . | |__ | | -_| -_| . |
# |_____|_|_|_|___|_|_|_,_|_|_|_|_ |_____|_____|_|_|___|___| _|
# |___|_____| |_|
# ===== Proudly presents =====
#
# ██╗ ██╗███████╗ ██████╗
# ██║ ██║██╔════╝██╔════╝
# ██║ █╗ ██║█████╗ ██║
# ██║███╗██║██╔══╝ ██║
# ╚███╔███╔╝██║ ╚██████╗ For blender
# ╚══╝╚══╝ ╚═╝ ╚═════╝
# next up WEIGHTS
import json
import os
from pickle import TRUE
import bpy
from . import database, databaseManagment, utils, wavefunction
# TODO create rotated variations of a single tile
bl_info = {
"name": "BlenderWFC",
"author": "Smonking_Sheep",
"description": "",
"blender": (3, 2, 1),
"version": (0, 0, 3),
"location": "",
"warning": "",
"category": "Generic"
}
class helloWorld(bpy.types.Operator):
bl_idname = "object.hello_world"
bl_label = "Print Hello World"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
return {'FINISHED'}
# creates the interface for the wfc
class runWfcPanel(bpy.types.Panel):
# usefull guide to make blender panels :
# https://medium.com/geekculture/creating-a-custom-panel-with-blenders-python-api-b9602d890663
"""create a panel"""
bl_label = "run wfc"
bl_idname = "VIEW3D_PT_runWfc"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Wavefunction"
def draw(self, context):
layout = self.layout
scene = context.scene
layout.prop(scene.wfc, 'size')
layout.operator('wfc.run', text='Run wave function')
class CreateWfcDatabasePanel(bpy.types.Panel):
"""create a panel"""
bl_label = "Create database"
bl_idname = "VIEW3D_PT_createDatabase"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Wavefunction"
def draw(self, context):
layout = self.layout
scene = context.scene
layout.operator('wfc.create_database', text='Create database')
class databaseManagmentPanel(bpy.types.Panel):
"""create a panel"""
bl_label = "Database Managment"
bl_idname = "VIEW3D_PT_wfcDatabaseManagment"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Wavefunction"
def draw(self, context):
layout = self.layout
scene = context.scene
if context.scene.wfc.DbManagment == True:
layout.operator('wfc.enable_disable_dbm',
text='disable database Managment')
else:
layout.operator('wfc.enable_disable_dbm',
text='enable database Managment')
if context.scene.wfc.DbManagment == True:
if context.scene.wfc.current_edited_tile != '':
layout.label(text=context.scene.wfc.current_edited_tile)
row = layout.row(align=True)
row.operator('wfc.display_previous_tile', text='<< previous tile')
row.operator('wfc.display_next_tile', text='next tile >>')
layout.operator('wfc.update_tile', text='Update tile')
class ToolsPanel(bpy.types.Panel):
"""create a panel"""
bl_label = "Tools"
bl_idname = "VIEW3D_PT_wfcTools"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Wavefunction"
def draw(self, context):
layout = self.layout
scene = context.scene
layout.label(text='Round mesh vertex coordinates : ')
layout.prop(scene.wfc, 'tools_decimalLenght')
layout.operator('wfc.clean_meshes', text='Clean meshes')
# creates the database from the selected meshes and saves it into a file
class CreateAndSaveDatabase(bpy.types.Operator):
"""Create and save the WFC in a json file""" # Use this as a tooltip for menu items and buttons.
bl_idname = "wfc.create_database" # Unique identifier for buttons and menu items to reference.
bl_label = "Create database" # Display name in the interface.
bl_options = {'REGISTER'}
# execute() is called when running the operator.
def execute(self, context):
d = database.databaseMaker()
db = d.create_database()
db = d.sortDatabaseKeys(db)
utils.save_database(db)
return {'FINISHED'}
# TODO : save the file within the blender file
# TODO : option to use objects in a collection as tiles for the database
# TODO : option to load and save the database to external file
class runWaveFunction(bpy.types.Operator):
"""runs and displays the wave function"""
bl_idname = "wfc.run"
bl_label = "run the wavefunction"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# read the parameters
size = context.scene.wfc.size
# read the database
dir = os.path.dirname(bpy.data.filepath)
databaseFile = open(f'{dir}/database.json', 'r')
print(f'opening : {dir}/database.json \n\n')
database = json.loads(databaseFile.read())
# create the wfc object
wave = wavefunction.waveFunction(database, size)
# initiate it at random location and tile
wave.set_start()
step = 0
while True:
if len(wave.tilesToUpdate) == 0:
coord = wave.consolidate_entropy()
if coord == False:
break
else:
wave.set_cell(coord)
wave.update_adjacent(coord)
else:
wave.propagate()
# print(tilesToUpdate)
step += 1
if step % 50 == 0:
wave.display_status(step)
pass
print('\n**** CREATING GRID VIZ ****')
# print(wave.cellGrid)
utils.preview_grid(size, wave.cellGrid)
return {'FINISHED'}
class enableDisableDbM(bpy.types.Operator):
bl_idname = "wfc.enable_disable_dbm"
bl_label = "enables or disables the db managment"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if context.scene.wfc.DbManagment == True:
databaseManagment.removeCurrentlyDisplayedTile()
databaseManagment.disableDbManagment()
context.scene.wfc.DbManagment = False
context.scene.wfc.current_edited_tile = ''
else:
databaseManagment.enableDbManagment()
context.scene.wfc.current_edited_tile = databaseManagment.displayTile(
tileIndex=0)
context.scene.wfc.DbManagment = True
return {'FINISHED'}
class displayNextTile(bpy.types.Operator):
bl_idname = "wfc.display_next_tile"
bl_label = 'changes the current displayed tile in db managment'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# remove the displayed tile
# TODO check if they were created from the db managment
databaseManagment.removeCurrentlyDisplayedTile()
context.scene.wfc.current_edited_tile = databaseManagment.displayTile(
tileIndex=1)
return {'FINISHED'}
class displayPreviousTile(bpy.types.Operator):
bl_idname = "wfc.display_previous_tile"
bl_label = 'changes the current displayed tile in db managment'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# remove the displayed tile
# TODO check if they were created from the db managment
databaseManagment.removeCurrentlyDisplayedTile()
context.scene.wfc.current_edited_tile = databaseManagment.displayTile(
tileIndex=-1)
return {'FINISHED'}
class updateTile(bpy.types.Operator):
bl_idname = "wfc.update_tile"
bl_label = 'update the object available tiles with the ones present'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
updatedTileValues = databaseManagment.neighbouringTile()
databaseManagment.updateDb(updatedTileValues)
databaseManagment.removeCurrentlyDisplayedTile()
databaseManagment.displayTile(tileIndex=0)
return {'FINISHED'}
class cleanMeshes(bpy.types.Operator):
"""round the vertices of every selected meshes"""
bl_idname = "wfc.clean_meshes"
bl_label = "clean meshes"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
utils.clean_meshes(context.scene.wfc.tools_decimalLenght)
return {'FINISHED'}
# TODO move all propreties into this class
class wfcPropertiesGroup(bpy.types.PropertyGroup):
#testint = bpy.props.IntProperty(name="testint",description="",default=1,min=1,)
current_edited_tile: bpy.props.StringProperty(name='current_edited_tile')
tools_decimalLenght: bpy.props.IntProperty(
name='decimal lenght', min=0, max=10, soft_min=1, soft_max=4)
size: bpy.props.IntVectorProperty(
name='size', default=(5, 5, 5), min=1, soft_max=20)
DbManagment: bpy.props.BoolProperty(name='DbManagment', default=False)
class wfcObjectPropertiesGroup(bpy.types.PropertyGroup):
tile_type: bpy.props.StringProperty(name='tile_type')
tile_name: bpy.props.StringProperty(name='tile_name')
CLASSES = [
# property group
wfcPropertiesGroup,
wfcObjectPropertiesGroup,
# operators
CreateAndSaveDatabase,
runWaveFunction,
cleanMeshes,
enableDisableDbM,
displayNextTile,
displayPreviousTile,
updateTile,
# panels
CreateWfcDatabasePanel,
runWfcPanel,
databaseManagmentPanel,
ToolsPanel,
]
def register():
print(f'**** Registring {len(CLASSES)} class')
for c in CLASSES:
bpy.utils.register_class(c)
print('**** Done')
# register the property group
bpy.types.Scene.wfc = bpy.props.PointerProperty(type=wfcPropertiesGroup)
bpy.types.Object.wfc_object = bpy.props.PointerProperty(
type=wfcObjectPropertiesGroup)
def unregister():
print(f'**** Unregistring {len(CLASSES)} class')
for c in CLASSES:
bpy.utils.unregister_class(c)
print('**** Done')
# delete the proterty group
del bpy.types.Scene.wfc
del bpy.types.Object.wfc_object