-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
305 lines (258 loc) · 9.12 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
bl_info = {
'name': 'PBR Model Importer',
'author': 'pjsamm',
'blender': (2, 93, 0),
'category': 'Import-Export',
}
if 'bpy' in locals():
print('Reloading pbr-models-import-export...')
import sys, importlib
for name in list(sys.modules):
if name.startswith('pbr-models-import-export'):
if '.' not in name:
print(' Reloaded .')
else:
print(' Reloaded', name[name.index('.'):])
importlib.reload(sys.modules[name])
import bpy
from bpy.types import (
Scene,
Panel,
Operator,
Armature,
Material,
Action
)
from bpy.props import *
from bpy_extras.io_utils import ImportHelper
from bpy_extras.io_utils import ExportHelper
from .importer import importer
from .exporter import exporter
## Material animations tab
anim_name = StringProperty(
name='',
description='The name of the new material animation to create.',
maxlen=1024,
)
class AddMatAnim(Operator):
bl_idname = 'wm.add_mat_anim'
bl_label = 'Create new animation'
def execute(self, context):
scene = context.scene
action = bpy.data.actions.new(scene.prop_anim_name)
action.id_root = 'NODETREE'
scene.prop_anim_name = ''
return {'FINISHED'}
class MatAnimPanel(Panel):
bl_idname = 'OBJECT_PT_mat_anims_panel'
bl_label = 'Animations'
bl_space_type = 'NODE_EDITOR'
bl_region_type = 'UI'
bl_category = 'PBR'
bl_context = 'objectmode'
@classmethod
def poll(self, context):
return context.object is not None and \
context.object.type == 'MESH'
def draw(self, context):
layout = self.layout
scene = context.scene
obj = context.object
for slot in obj.material_slots:
mat = slot.material
layout.prop_search(mat, f'prop_action',
bpy.data, 'actions', text=mat.name)
layout.separator()
layout.prop(scene, 'prop_anim_name')
layout.operator('wm.add_mat_anim')
### Animation selection tab
class PBRPanel(Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'PBR'
bl_context = 'objectmode'
bl_options = { 'DEFAULT_CLOSED' }
show_arma_prop = True
@classmethod
def poll(self, context):
return context.object is not None and \
context.object.type == 'ARMATURE'
def draw(self, context):
layout = self.layout
obj = context.object
arma = obj.data
if self.show_arma_prop:
layout.prop_search(arma, f'prop_{self.anim_id}',
bpy.data, 'actions', text='Arma.')
for child in obj.children:
if child.type != 'MESH':
continue
# doesn't account for meshes sharing materials
for slot in child.material_slots:
mat = slot.material
layout.prop_search(mat, f'prop_{self.anim_id}',
bpy.data, 'actions', text=mat.name)
class PBRPropertiesPanel(PBRPanel):
bl_options = set()
bl_label = 'Animations'
bl_idname = 'OBJECT_PT_properties_panel'
def draw(self, context):
return
class IdleAnimPanel(PBRPanel):
bl_label = 'Idle'
bl_idname = 'OBJECT_PT_idle_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'idle'
class RunAnimPanel(PBRPanel):
bl_label = 'Run'
bl_idname = 'OBJECT_PT_run_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'run'
class DamageAnimPanel(PBRPanel):
bl_label = 'Damage'
bl_idname = 'OBJECT_PT_damage_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'damage'
class FaintAnimPanel(PBRPanel):
bl_label = 'Faint'
bl_idname = 'OBJECT_PT_faint_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'faint'
class PhysAnimPanel(PBRPanel):
bl_label = 'Phys'
bl_idname = 'OBJECT_PT_phys_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'move_phys'
class SpecAnimPanel(PBRPanel):
bl_label = 'Spec'
bl_idname = 'OBJECT_PT_spec_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'move_spec'
class BlinkAnimPanel(PBRPanel):
bl_label = 'Blink'
bl_idname = 'OBJECT_PT_blink_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'tx_wink'
show_arma_prop = False
class SleepAnimPanel(PBRPanel):
bl_label = 'Sleep'
bl_idname = 'OBJECT_PT_sleep_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'tx_sleep'
show_arma_prop = False
class WakeupAnimPanel(PBRPanel):
bl_label = 'Wake Up'
bl_idname = 'OBJECT_PT_wakeup_anim_panel'
bl_parent_id = 'OBJECT_PT_properties_panel'
anim_id = 'tx_wakeup'
show_arma_prop = False
subpanels = (
IdleAnimPanel,
RunAnimPanel,
DamageAnimPanel,
FaintAnimPanel,
PhysAnimPanel,
SpecAnimPanel,
BlinkAnimPanel,
SleepAnimPanel,
WakeupAnimPanel
)
### Import/export operators
class ImportModel(Operator, ImportHelper):
'''Import a model from Pokémon Battle Revolution'''
bl_idname = 'pbr.pbrimport'
bl_label = 'PBR Model (.sdr/.odr/.mdr)'
bl_options = {'REGISTER', 'UNDO'}
filter_glob: StringProperty(
default='*.sdr;*.mdr;*.odr',
options={'HIDDEN'}
)
use_default_pose: BoolProperty(
name='Import In Default Pose',
description="Some models' bind poses are pretty funky at " + \
"the moment. Enable\nthis to import models in " + \
"their default pose instead.",
default=True
)
join_meshes: BoolProperty(
name='Merge Objects',
description="Enable to merge objects into a single mesh " + \
"when importing. Meshes from different skin nodes " + \
"will remain separate.",
default=False
)
def execute(self, context):
importer.importSDR(context, self.filepath,
useDefaultPose=self.use_default_pose,
joinMeshes=self.join_meshes)
# set viewport shading to Material Preview in Layout view
view = [space for area in bpy.data.screens['Layout'].areas
for space in area.spaces if space.type == 'VIEW_3D'][0]
view.shading.type = 'MATERIAL'
return {'FINISHED'}
class ExportModel(Operator, ExportHelper):
'''Export a model for use in Pokémon Battle Revolution'''
bl_idname = 'pbr.pbrexport'
bl_label = 'PBR Model (.sdr)'
bl_options = {'REGISTER', 'UNDO'}
filename_ext = '.sdr'
filter_glob: bpy.props.StringProperty(
default='*.sdr',
options={'HIDDEN'}
)
def execute(self, context):
exporter.writeSDR(self, context)
self.report({'INFO'}, 'Export successful.')
return {'FINISHED'}
def menu_func_import(self, context):
self.layout.operator(ImportModel.bl_idname)
def menu_func_export(self, context):
self.layout.operator(ExportModel.bl_idname)
def poll_obj(self, object):
return object.id_root == 'OBJECT'
def poll_node(self, object):
return object.id_root == 'NODETREE'
def set_mat_action(self, context):
if not self.node_tree.animation_data:
self.node_tree.animation_data_create()
self.node_tree.animation_data.action = self.prop_action
def register():
from bpy.utils import register_class
# import/export operators
register_class(ImportModel)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
register_class(ExportModel)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
# material animations tab
register_class(AddMatAnim)
register_class(MatAnimPanel)
Material.prop_action = \
PointerProperty(type=Action, poll=poll_node, update=set_mat_action)
Scene.prop_anim_name = anim_name
# animations tab
register_class(PBRPropertiesPanel)
for cls in subpanels:
register_class(cls)
setattr(Armature, f'prop_{cls.anim_id}',
PointerProperty(type=Action, poll=poll_obj))
setattr(Material, f'prop_{cls.anim_id}',
PointerProperty(type=Action, poll=poll_node))
def unregister():
from bpy.utils import unregister_class
# import/export operators
unregister_class(ImportModel)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
unregister_class(ExportModel)
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
# material animations tab
unregister_class(MatAnimPanel)
del Material.prop_action
del Scene.prop_anim_name
# animations tab
unregister_class(PBRPropertiesPanel)
for cls in subpanels:
unregister_class(cls)
RemoveProperty(Armature, attr=f'prop_{cls.anim_id}')
RemoveProperty(Material, attr=f'prop_{cls.anim_id}')
if __name__ == '__main__':
register()