-
Notifications
You must be signed in to change notification settings - Fork 16
/
library.py
289 lines (241 loc) · 10.1 KB
/
library.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
#
# Copyright (C) 2020-2022 Michael Vigovsky
import os, logging
import bpy # pylint: disable=import-error
from bpy_extras.wm_utils.progress_report import ProgressReport # pylint: disable=import-error, no-name-in-module
from . import common, prefs
from .lib import morpher, materials, morphs, utils
from .lib.charlib import library, empty_char
logger = logging.getLogger(__name__)
class OpReloadLib(bpy.types.Operator):
bl_idname = "charmorph.reload_library"
bl_label = "Reload library"
bl_description = "Reload character library"
def execute(self, _): # pylint: disable=no-self-use
library.load()
common.manager.recreate_charmorphs()
return {"FINISHED"}
def _import_moprhs(wm, obj, char):
ui = wm.charmorph_ui
if not ui.use_sk:
ui.import_morphs = False
ui.import_expressions = False
steps = int(ui.import_morphs) + int(ui.import_expressions)
if not steps:
return None
storage = morphs.MorphStorage(char)
importer = morphs.MorphImporter(storage, obj)
with ProgressReport(wm) as progress:
progress.enter_substeps(steps, "Importing shape keys...")
if ui.import_morphs:
importer.import_morphs(progress)
# ??? without extra empty step, progress resets to 0% after loadging morphs. TODO: investigate
progress.step()
progress.step("Morphs imported")
if ui.import_expressions:
importer.import_expressions(progress)
progress.step("Expressions imported")
progress.leave_substeps("Shape keys done")
return storage
class OpImport(bpy.types.Operator):
bl_idname = "charmorph.import_char"
bl_label = "Import character"
bl_description = "Import character"
bl_options = {"UNDO"}
@classmethod
def poll(cls, context):
return context.mode == "OBJECT"
def execute(self, context):
ui = context.window_manager.charmorph_ui
if not ui.base_model:
self.report({'ERROR'}, "Please select base model")
return {"CANCELLED"}
char = library.chars[ui.base_model]
if ui.alt_topo != "<Base>" and char.faces is None:
ui.alt_topo = "<Base>"
return {"CANCELLED"}
if ui.alt_topo == "<Custom>":
if not ui.alt_topo_obj or ui.alt_topo_obj.type != "MESH":
self.report({'ERROR'}, "Please select correct custom alternative topology object")
return {"CANCELLED"}
orig_mesh = ui.alt_topo_obj.data
mesh = orig_mesh.copy()
mesh.name = char.name
# TODO: cleanup shape keys
mesh["cm_alt_topo"] = orig_mesh
obj = bpy.data.objects.new(char.name, mesh)
context.collection.objects.link(obj)
storage = None
else:
obj = utils.import_obj(char.blend_file(), char.char_obj)
if obj is None:
self.report({'ERROR'}, "Import failed")
return {"CANCELLED"}
storage = _import_moprhs(context.window_manager, obj, char)
if not ui.import_morphs and os.path.isdir(char.path("morphs")):
obj.data["cm_morpher"] = "ext"
materials.init_materials(obj, char)
obj.location = context.scene.cursor.location
if ui.import_cursor_z:
obj.rotation_mode = "XYZ"
obj.rotation_euler = (0, 0, context.scene.cursor.rotation_euler[2])
obj.data["charmorph_template"] = ui.base_model
if (ui.use_sk or char.np_basis is None) and (not obj.data.shape_keys or not obj.data.shape_keys.key_blocks):
obj.shape_key_add(name="Basis", from_mix=False)
m = morpher.get(obj, storage, common.undo_handler)
common.manager.update_morpher(m)
context.view_layer.objects.active = obj
ui.fitting_char = obj
asset_list = []
def add_assets(lst):
asset_list.extend((char.assets[name] for name in lst))
add_assets(char.default_assets)
if not prefs.is_adult_mode():
add_assets(char.underwear)
m.fitter.fit_import(asset_list)
m.update()
return {"FINISHED"}
def char_default_tex_set(char):
if not char:
return "/"
if not char.default_tex_set:
return char.texture_sets[0]
return char.default_tex_set
def update_base_model(ui, _):
ui.tex_set = char_default_tex_set(library.chars.get(ui.base_model))
class UIProps:
base_model: bpy.props.EnumProperty(
name="Base",
items=lambda _ui, _: [(name, char.title, char.description) for name, char in library.chars.items()],
update=update_base_model,
description="Choose a base model")
material_mode: bpy.props.EnumProperty(
name="Materials",
default="TS",
description="Share materials between different Charmorph characters or not",
items=[
("NS", "Non-Shared", "Use unique material for each character"),
("TS", "Shared textures only", "Use same texture for all characters"),
("MS", "Shared", "Use same materials for all characters")]
)
# TODO: copy materials from custom object
material_local: bpy.props.BoolProperty(
name="Use local materials", default=True,
description="Use local copies of materials for faster loading")
tex_set: bpy.props.EnumProperty(
name="Texture set",
description="Select texture set for the character",
items=lambda ui, _: [
(name, "<Default>" if name == "/" else name, "")
for name in library.chars.get(ui.base_model, empty_char).texture_sets
],
)
tex_downscale: bpy.props.EnumProperty(
name="Downscale textures",
description="Downscale large textures to avoid memory overflows",
default="UL",
items=[("1K", "1K", ""), ("2K", "2K", ""), ("4K", "4K", ""), ("UL", "No limit", "")]
)
import_cursor_z: bpy.props.BoolProperty(
name="Use Z cursor rotation", default=True,
description="Take 3D cursor Z rotation into account when creating the character")
use_sk: bpy.props.BoolProperty(
name="Use shape keys for morphing", default=False,
description="Use shape keys during morphing"
"(should be on if you plan to resume morphing later, maybe with other versions of CharMorph)")
import_morphs: bpy.props.BoolProperty(
name="Import morphing shape keys", default=False,
description="Import and morph character using shape keys")
import_expressions: bpy.props.BoolProperty(
name="Import expression shape keys", default=False,
description="Import and morph character using shape keys")
alt_topo: bpy.props.EnumProperty(
name="Alt topo",
default="<Base>",
description="Select alternative topology to use",
items=[
("<Base>", "<Base>", "Use base character topology"),
("<Custom>", "<Custom>", "Use custom local object as alt topo")]
)
alt_topo_obj: bpy.props.PointerProperty(
name="Custom alt topo",
type=bpy.types.Object,
description="Select custom object to use as alternative topology",
poll=utils.visible_mesh_poll)
class CHARMORPH_PT_Library(bpy.types.Panel):
bl_label = "Character library"
bl_parent_id = "VIEW3D_PT_CharMorph"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_order = 1
@classmethod
def poll(cls, context):
return context.mode == "OBJECT"
def draw(self, context):
l = self.layout
ui = context.window_manager.charmorph_ui
l.operator('charmorph.reload_library')
l.separator()
if library.dirpath == "":
l.label(text="Data dir is not found. Importing is not available.")
return
if not library.chars:
l.label(text=f"No characters found at {library.dirpath}. Nothing to import.")
return
l.prop(ui, "base_model")
char = library.chars.get(ui.base_model)
if char:
r = l.row()
c = r.column()
c.alignment = "RIGHT"
c.ui_units_x = 2.5
c.label(text="Author:")
c.label(text="License:")
c = r.column()
c.label(text=char.author)
c.label(text=char.license)
l.prop(ui, "material_mode")
l.prop(ui, "material_local")
l.prop(ui, "tex_set")
l.prop(ui, "tex_downscale")
l.prop(ui, "import_cursor_z")
c = l.column()
c.prop(ui, "use_sk")
c = c.column()
c.enabled = ui.use_sk and ui.alt_topo == "<Base>"
c.prop(ui, "import_morphs")
c.prop(ui, "import_expressions")
c = l.column()
c.enabled = bool(char and char.basis and char.has_faces)
c.prop(ui, "alt_topo")
if ui.alt_topo == "<Custom>":
c.prop(ui, "alt_topo_obj")
l.operator('charmorph.import_char', icon='ARMATURE_DATA')
l.alignment = "CENTER"
c = l.column(align=True)
if prefs.is_adult_mode():
labels = ["Adult mode is on", "The character will be naked"]
else:
labels = ["Adult mode is off", "Default underwear or censors will be added"]
for text in labels:
r = c.row()
r.alignment = "CENTER"
r.label(text=text)
classes = [OpReloadLib, OpImport, CHARMORPH_PT_Library]