-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.py
441 lines (348 loc) · 12.3 KB
/
ops.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
import bpy
import os
import json
from . import functions as func
from .render import RenderBatch
from .metadata import ExportMetadata
from bpy_extras.io_utils import ImportHelper
class Dummy(bpy.types.Operator):
bl_idname = "nftgen.dummy"
bl_label = ""
bl_description = ""
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
self.report({'INFO'}, "Dummy")
return {'FINISHED'}
class AddTrait(bpy.types.Operator):
bl_idname = "nftgen.add_trait"
bl_label = "Add"
bl_description = "Add new trait"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
props = func.get_props()
traits = func.get_traits()
tokens = func.get_tokens()
# if tokens:
# func.existing_tokens_msg(self)
# return {'CANCELLED'}
new_trait = traits.add()
new_trait.name = func.generate_random_id()
idx = traits.find(new_trait.name)
# print(idx)
props.active_trait_id = idx
props.traits_updated = func.traits_updated()
return {'FINISHED'}
class RemoveTrait(bpy.types.Operator):
bl_idname = "nftgen.remove_trait"
bl_label = "Remove"
bl_description = "Remove active trait"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
traits = func.get_traits()
return bool(traits)
def execute(self, context):
props = func.get_props()
traits = func.get_traits()
# tokens = func.get_tokens()
# if tokens:
# func.existing_tokens_msg(self)
# return {'CANCELLED'}
active_trait_index = props.active_trait_id
active_trait = traits[active_trait_index]
func.remove_trait_values(active_trait)
traits.remove(active_trait_index)
props.active_trait_id = max(0, active_trait_index - 1)
# print(active_trait_index)
props.traits_updated = func.traits_updated()
return {'FINISHED'}
class AddTraitValue(bpy.types.Operator):
bl_idname = "nftgen.add_trait_value"
bl_label = "Add"
bl_description = "Add new choice for active trait"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
props = func.get_props()
traits = func.get_traits()
trait_values = func.get_traits_values()
active_trait_index = props.active_trait_id
# tokens = func.get_tokens()
# if tokens:
# func.existing_tokens_msg(self)
# return {'CANCELLED'}
new_trait_value = trait_values.add()
new_trait_value.name = func.generate_random_id()
new_trait_value.trait_id = traits[active_trait_index].name
trait_values = [tv for tv in trait_values]
props.active_trait_value_id = trait_values.index(new_trait_value)
return {'FINISHED'}
class RemoveTraitValue(bpy.types.Operator):
bl_idname = "nftgen.remove_trait_value"
bl_label = "Remove"
bl_description = "Remove active choice"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
props = func.get_props()
active_trait_index = props.active_trait_id
traits = func.get_traits()
try:
active_trait = traits[active_trait_index]
traits_values = func.get_traits_values()
except IndexError:
return False
return bool([tv for tv in traits_values if tv.trait_id == active_trait.name])
def execute(self, context):
props = func.get_props()
traits_values = func.get_traits_values()
active_trait_value_id = props.active_trait_value_id
active_trait_value = traits_values[active_trait_value_id]
candidate_tv = func.get_candidate_tv(active_trait_value)
# print(f"Next tv= {candidate_tv.metadata_name}")
# tokens = func.get_tokens()
# if tokens:
# func.existing_tokens_msg(self)
# return {'CANCELLED'}
traits_values.remove(active_trait_value_id)
if candidate_tv:
traits_values = func.get_traits_values()
props.active_trait_value_id = traits_values.find(candidate_tv)
else:
props.active_trait_value_id = 0
return {'FINISHED'}
class ClearTraitValues(bpy.types.Operator):
bl_idname = "nftgen.clear_trait_value"
bl_label = "Clear"
bl_description = "Clear All Choices for Active Trait"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
props = func.get_props()
active_trait_index = props.active_trait_id
traits = func.get_traits()
func.remove_trait_values(traits[active_trait_index])
# props.active_trait_value_id = 0
return {'FINISHED'}
class ClearTokens(bpy.types.Operator):
bl_idname = "nftgen.clear_tokens"
bl_label = "Clear Tokens"
bl_description = "Clear All Generated Tokens"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
tokens = func.get_tokens()
return bool(tokens)
def execute(self, context):
tokens = func.get_tokens()
tokens.clear()
# clear active token collection props
active_token_props = func.get_active_token_props()
active_token_props.clear()
# go back to the generate panel
props = func.get_props()
props.mode = '0'
return {'FINISHED'}
class ShuffleTokens(bpy.types.Operator):
bl_idname = "nftgen.shuffle_tokens"
bl_label = "Shuffle Tokens"
bl_description = "Reorder the tokens collection randomly"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
tokens = func.get_tokens()
return bool(tokens)
def execute(self, context):
tokens = func.get_tokens()
func.randomize_tokens_order(tokens)
# clear active token collection props
active_token_props = func.get_active_token_props()
active_token_props.clear()
# go back to the first token
props = func.get_props()
props.active_token_id = 0
return {'FINISHED'}
class AddRule(bpy.types.Operator):
bl_idname = "nftgen.add_rule"
bl_label = "Add"
bl_description = "Add new rule"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
traits = func.get_traits()
return bool(traits)
def execute(self, context):
props = func.get_props()
rules = func.get_rules()
new_rule = rules.add()
new_rule.name = func.generate_random_id()
idx = rules.find(new_rule.name)
props.active_rule_id = idx
return {'FINISHED'}
class RemoveRule(bpy.types.Operator):
bl_idname = "nftgen.remove_rule"
bl_label = "Remove"
bl_description = "Remove selected rule"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
rules = func.get_rules()
return bool(rules)
def execute(self, context):
props = func.get_props()
rules = func.get_rules()
idx = props.active_rule_id
rules.remove(idx)
props.active_rule_id = max(0, idx - 1)
return {'FINISHED'}
class UpRule(bpy.types.Operator):
bl_idname = "nftgen.up_rule"
bl_label = "Up"
bl_description = "Move selected rule up"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
rules = func.get_rules()
props = func.get_props()
return bool(rules) and props.active_rule_id > 0
def execute(self, context):
props = func.get_props()
rules = func.get_rules()
rules.move(props.active_rule_id, props.active_rule_id - 1)
props.active_rule_id -= 1
return {'FINISHED'}
class DownRule(bpy.types.Operator):
bl_idname = "nftgen.down_rule"
bl_label = "Down"
bl_description = "Move selected rule down"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
rules = func.get_rules()
props = func.get_props()
return bool(rules) and props.active_rule_id < len(rules) - 1
def execute(self, context):
props = func.get_props()
rules = func.get_rules()
rules.move(props.active_rule_id, props.active_rule_id + 1)
props.active_rule_id += 1
return {'FINISHED'}
class OpenOutputDir(bpy.types.Operator):
bl_idname = "nftgen.open_output_dir"
bl_label = "Open Output Folder"
bl_description = "Open Output Folder in File Explorer"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
# check if output folder exists
props = func.get_props()
output_dir = bpy.path.abspath(props.output_dir)
return os.path.exists(output_dir)
def execute(self, context):
props = func.get_props()
output_dir = bpy.path.abspath(props.output_dir)
os.startfile(output_dir)
return {'FINISHED'}
class UpTrait(bpy.types.Operator):
bl_idname = "nftgen.up_trait"
bl_label = "Up"
bl_description = "Move active trait up"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
props = func.get_props()
traits = func.get_traits()
return bool(traits) and props.active_trait_id > 0
def execute(self, context):
props = func.get_props()
traits = func.get_traits()
# tokens = func.get_tokens()
# if tokens:
# func.existing_tokens_msg(self)
# return {'CANCELLED'}
traits.move(props.active_trait_id, props.active_trait_id - 1)
props.active_trait_id -= 1
# set prop.traits_order_updated
# to add warning in export panel in case traits order has been changed
# after tokens were generated
props.traits_updated = func.traits_updated()
return {'FINISHED'}
class DownTrait(bpy.types.Operator):
bl_idname = "nftgen.down_trait"
bl_label = "Down"
bl_description = "Move active trait down"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
traits = func.get_traits()
props = func.get_props()
return bool(traits) and props.active_trait_id < len(traits) - 1
def execute(self, context):
props = func.get_props()
traits = func.get_traits()
# tokens = func.get_tokens()
# if tokens:
# func.existing_tokens_msg(self)
# return {'CANCELLED'}
traits.move(props.active_trait_id, props.active_trait_id + 1)
props.active_trait_id += 1
props.traits_updated = func.traits_updated()
return {'FINISHED'}
class LoadImages(bpy.types.Operator, ImportHelper):
bl_idname = "nftgen.load_images"
bl_label = "Load Images"
bl_description = "Load images from your desk to blender"
bl_options = {'UNDO'}
# add images file type filter
files: bpy.props.CollectionProperty(
type=bpy.types.OperatorFileListElement,
options={'HIDDEN', 'SKIP_SAVE'},
)
directory: bpy.props.StringProperty(
subtype='DIR_PATH',
)
@classmethod
def poll(cls, context):
return True
def execute(self, context):
for f in self.files:
filepath = os.path.join(self.directory, f.name)
# load image
bpy.data.images.load(filepath, check_existing=True)
return {'FINISHED'}
classes = (
Dummy,
AddTrait,
RemoveTrait,
AddTraitValue,
RemoveTraitValue,
ClearTraitValues,
RenderBatch,
ExportMetadata,
ClearTokens,
ShuffleTokens,
AddRule,
RemoveRule,
UpRule,
DownRule,
OpenOutputDir,
UpTrait,
DownTrait,
LoadImages
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)