-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutfitcreator.py
291 lines (218 loc) · 8.91 KB
/
outfitcreator.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
import bpy
import re
import os
import random
#TODO:
#refactor hardcoded values
#refactor nested foor loops
top_items = {
'CASUAL': 'Wolf3D_Top_Casual',
'CYBERPUNK': 'Wolf3D_Top_Cyberpunk',
'OFFICE': 'Wolf3D_Top_Office'
}
bottom_items = {
'CASUAL': 'Wolf3D_Bottom_Casual',
'CYBERPUNK': 'Wolf3D_Bottom_Cyberpunk',
'OFFICE': 'Wolf3D_Bottom_Office'
}
footwear_items = {
'CASUAL': 'Wolf3D_Footwear_Casual',
'CYBERPUNK': 'Wolf3D_Footwear_Cyberpunk',
'OFFICE': 'Wolf3D_Footwear_Office'
}
class Enums(bpy.types.PropertyGroup):
name_string : bpy.props.StringProperty(name= "Name Your Outfit")
enum_top : bpy.props.EnumProperty(
name= "Top Style",
description= "sample text",
items= [('CASUAL', "Casual", ""),
('CYBERPUNK', "Cyberpunk", ""),
('OFFICE', "Office", "")
]
)
enum_bottom : bpy.props.EnumProperty(
name= "Bottom Style",
description= "sample text",
items= [('CASUAL', "Casual", ""),
('CYBERPUNK', "Cyberpunk", ""),
('OFFICE', "Office", "")
]
)
enum_footwear : bpy.props.EnumProperty(
name= "Footwear Style",
description= "sample text",
items= [('CASUAL', "Casual", ""),
('CYBERPUNK', "Cyberpunk", ""),
('OFFICE', "Office", "")
]
)
enum_export_format : bpy.props.EnumProperty(
name= "Export Format",
description="",
items= [('obj',"OBJ",""),
('fbx',"FBX",""),
('gltf',"GLTF",""),
('glb',"GLB","")]
)
class OutfitCreatorPanel(bpy.types.Panel):
bl_idname = "outfitcreator"
bl_label = "Full-body Outfit Creator"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Tool"
def draw(self, context):
layout = self.layout
scene = context.scene
mytool = scene.my_tool
layout.prop(mytool, "name_string")
layout.label(text="Please use a blank space to separate words.")
layout.prop(mytool, "enum_top")
layout.prop(mytool, "enum_bottom")
layout.prop(mytool, "enum_footwear")
layout.operator("outfitcreator.operator")
layout.operator("generator.operator")
layout.label(text="Please pick a format before exporting")
layout.prop(mytool, "enum_export_format")
layout.operator("exporter.operator")
layout.operator("batch.operator")
class OutfitCreatorOperator(bpy.types.Operator):
bl_label = "Compose Outfit"
bl_idname = "outfitcreator.operator"
bl_description = "Composes an outfit from the chosen pieces"
for item in bpy.data.objects:
if item.name.startswith('Armature'):
item.hide_set(True)
def execute(self, context):
scene = context.scene
mytool = scene.my_tool
# showing the chosen pieces
for k, v in top_items.items():
bpy.data.objects[v].hide_set(k != mytool.enum_top)
for k, v in bottom_items.items():
bpy.data.objects[v].hide_set(k != mytool.enum_bottom)
for k, v in footwear_items.items():
bpy.data.objects[v].hide_set(k != mytool.enum_footwear)
return {'FINISHED'}
class GeneratorOperator(bpy.types.Operator):
bl_idname = "generator.operator"
bl_label = "Generate Random"
bl_description = "Generates a random combination of all pieces"
@classmethod
def poll(cls):
return True
# hiding all pieces
def execute(self):
items = [list(top_items.values()), list(bottom_items.values()), list(footwear_items.values())]
for item in bpy.data.objects:
if item.name.startswith('Wolf3D'):
item.hide_set(True)
for item in items:
bpy.data.objects[random.choice(item)].hide_set(False)
return {"FINISHED"}
def export(format, filename):
visible=[ob for ob in bpy.context.view_layer.objects if ob.visible_get()]
for v in visible:
v.select_set( state = True, view_layer = None)
arm = bpy.data.objects['FullBody_Armature']
arm.hide_set(False)
arm.select_set( state = True, view_layer = None)
body = bpy.data.objects['Wolf3D_Body']
body.hide_set(False)
body.select_set( state = True, view_layer = None)
filepath = bpy.data.filepath
directory = os.path.dirname(filepath)
exportpath = os.path.join(directory, "{}.{}".format(filename, format))
if format == 'fbx':
getattr(bpy.ops.export_scene, format)(filepath=exportpath, object_types={'ARMATURE','MESH'}, use_selection=True)
elif format == 'obj':
getattr(bpy.ops.export_scene, format)(filepath=exportpath, use_selection=True)
elif format == 'glb' :
#uses the same function as gltf
getattr(bpy.ops.export_scene, "gltf")(filepath=exportpath, export_format='GLB', use_selection=True)
elif format == 'gltf':
getattr(bpy.ops.export_scene, format)(filepath=exportpath, export_format='GLTF_SEPARATE', use_selection=True)
return exportpath
class ExporterOperator(bpy.types.Operator):
bl_idname = "exporter.operator"
bl_label = "Export Outfit"
bl_description = "Exports a file of the chosen format in the .blend file location"
@classmethod
def poll(cls):
return True
def execute(self, context):
scene = context.scene
mytool = scene.my_tool
def enforce_naming(string):
bad_chars = [",","&","*","^","."]
for char in bad_chars:
if char in string:
self.report({'ERROR'}, "Name contains irregular characters, please review")
chunks = re.split(" ",string)
new_name = "_".join(map(lambda chunk: chunk.upper(), chunks))
return new_name
filename = enforce_naming(mytool.name_string)
if mytool.name_string == "":
self.report({'ERROR'}, "Please, name your outfit")
return {"FINISHED"}
filename = enforce_naming(mytool.name_string)
exportpath = export(context, mytool.enum_export_format, filename)
self.report({'INFO'}, "Export successful at location: {}".format(exportpath))
return {"FINISHED"}
class BatchOperator(bpy.types.Operator):
bl_idname = "batch.operator"
bl_label = "Batch Generate and Export"
bl_description = ""
@classmethod
def poll(cls):
return True
def execute(self, context):
scene = context.scene
mytool = scene.my_tool
combinations = []
counter = 0
for i in range(10):
for v in top_items.values():
bpy.data.objects[v].hide_set(True)
for v in bottom_items.values():
bpy.data.objects[v].hide_set(True)
for v in footwear_items.values():
bpy.data.objects[v].hide_set(True)
for i in top_items.values():
for j in bottom_items.values():
for k in footwear_items.values():
combinations.append((i, j, k))
combination = combinations.pop(random.randrange(len(combinations)))
for el in combination:
bpy.data.objects[el].hide_set(False)
filename = mytool.name_string
exportpath = export(context, mytool.enum_export_format, filename + 'ver-{}'.format(counter))
export(context, mytool.enum_export_format, filename)
for el in combination:
bpy.data.objects[el].hide_set(True)
counter += 1
self.report({'INFO'},''.join(exportpath))
return {"FINISHED"}
def register():
bpy.utils.register_class(Enums)
bpy.utils.register_class(OutfitCreatorPanel)
bpy.utils.register_class(OutfitCreatorOperator)
bpy.utils.register_class(ExporterOperator)
bpy.utils.register_class(GeneratorOperator)
bpy.utils.register_class(BatchOperator)
bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=Enums)
def unregister():
bpy.utils.unregister_class(Enums)
bpy.utils.unregister_class(OutfitCreatorPanel)
bpy.utils.unregister_class(OutfitCreatorOperator)
bpy.utils.unregister_class(ExporterOperator)
bpy.utils.register_class(GeneratorOperator)
bpy.utils.unregister_class(BatchOperator)
del bpy.types.Scene.my_tool
#TODO: loading from folder or JSON
# def load_assets():
# for path in import_paths:
# abspath = os.path.realpath(path)
# bpy.ops.import_scene.obj(abspath)
if __name__ == "__main__":
register()
# load_assets()