-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
356 lines (325 loc) · 15.8 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
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
bl_info = { "name": "Blue Fang BFB format (Zoo Tycoon 2)",
"author": "HENDRIX",
"blender": (2, 74, 0),
"location": "File > Import-Export",
"description": "Import-Export models, skeletons and animations, batch-process models to add LODs. Experimental map & particle support.",
"warning": "",
"wiki_url": "https://github.com/HENDRIX-ZT2/bfb-blender",
"support": 'COMMUNITY',
"tracker_url": "https://github.com/HENDRIX-ZT2/bfb-blender/issues/new",
"category": "Import-Export"}
import bpy
from bpy.props import StringProperty, FloatProperty, BoolProperty, IntProperty, CollectionProperty
from bpy_extras.io_utils import ImportHelper, ExportHelper
from bpy_extras.object_utils import AddObjectHelper
import bpy.utils.previews
preview_collection = bpy.utils.previews.new()
class AddCapsule(bpy.types.Operator, AddObjectHelper):
"""Create a new BFB Capsule Collider"""
bl_idname = "mesh.add_bfb_capsule_collider"
bl_label = "BFB Capsule Collider"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
from . import common_bfb
import mathutils
start = mathutils.Vector((0,0,0))
end = mathutils.Vector((1,0,0))
r = 1
ob = common_bfb.create_capsule("capsule", start, end, r)
for pa in bpy.context.scene.objects:
print(type(pa.data))
if type(pa.data) == bpy.types.Armature:
ob.parent = pa
ob.parent_type = 'BONE'
for name in ("Bip01 Spine1", "Bip01 Spine", "Bip01 Pelvis", "Bip01"):
try:
ob.parent_bone = name
bone = pa.data.bones[name]
ob.location.y = -bone.length
break
except: pass
break
return {'FINISHED'}
class AddSphere(bpy.types.Operator, AddObjectHelper):
"""Create a new BFB Sphere Collider"""
bl_idname = "mesh.add_bfb_sphere_collider"
bl_label = "BFB Sphere Collider"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
from . import common_bfb
import mathutils
x = 0
y = 0
z = 1
r = 1
pa = None
col = common_bfb.create_sphere("sphere", x, y, z, r)
for ob in bpy.context.scene.objects:
if ob.name.startswith("paint_"):
pa = ob
break
if not pa:
pa = common_bfb.create_empty(None,"paint_cover_grassland",mathutils.Matrix())
col.parent = pa
return {'FINISHED'}
class AddBox(bpy.types.Operator, AddObjectHelper):
"""Create a new BFB Box Collider"""
bl_idname = "mesh.add_bfb_box_collider"
bl_label = "BFB Box Collider"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
from . import common_bfb
import mathutils
x = 1
y = 1
z = 1
pa = None
col = common_bfb.create_bounding_box("orientedbox", mathutils.Matrix(), x, y, z)
for ob in bpy.context.scene.objects:
if ob.name.startswith("footprint"):
pa = ob
break
if not pa:
pa = common_bfb.create_empty(None,"footprint",mathutils.Matrix())
col.parent = pa
return {'FINISHED'}
class ImportBF(bpy.types.Operator, ImportHelper):
"""Import from BF file format (.bf)"""
bl_idname = "import_scene.bluefang_bf"
bl_label = 'Import BF'
bl_options = {'UNDO'}
filename_ext = ".bf"
filter_glob = StringProperty(default="*.bf", options={'HIDDEN'})
files = CollectionProperty(type=bpy.types.PropertyGroup)
set_fps = BoolProperty(name="Adjust FPS", description="Set the scene to 30 frames per second to conform with the BFs.", default=True)
def execute(self, context):
from . import import_bf
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob"))
return import_bf.load(self, context, **keywords)
class ImportPSYS(bpy.types.Operator, ImportHelper):
"""Import from PSYS file format (.psys)"""
bl_idname = "import_scene.bluefang_psys"
bl_label = 'Import PSYS'
bl_options = {'UNDO'}
filename_ext = ".psys"
filter_glob = StringProperty(default="*.psys", options={'HIDDEN'})
def execute(self, context):
from . import import_psys
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob"))
return import_psys.load(self, context, **keywords)
class ImportBFB(bpy.types.Operator, ImportHelper):
"""Import from BFB file format (.bfb)"""
bl_idname = "import_scene.bluefang_bfb"
bl_label = 'Import BFB'
bl_options = {'UNDO'}
filename_ext = ".bfb"
filter_glob = StringProperty(default="*.bfb", options={'HIDDEN'})
use_custom_normals = BoolProperty(name="Use BFB Normals", description="Preserves the original shading of a BFB.", default=False)
mirror_mesh = BoolProperty(name="Mirror Rigged Meshes", description="Mirrors models with a skeleton. Careful, sometimes bones don't match!", default=False)
def execute(self, context):
from . import import_bfb
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob"))
errors = import_bfb.load(self, context, **keywords)
for error in errors:
self.report({"ERROR"}, error)
return {'FINISHED'}
class ImportDAT(bpy.types.Operator, ImportHelper):
"""Import from DAT map format (.dat)"""
bl_idname = "import_scene.bluefang_dat"
bl_label = 'Import DAT'
bl_options = {'UNDO'}
filename_ext = ".dat"
filter_glob = StringProperty(default="*.dat", options={'HIDDEN'})
def execute(self, context):
from . import import_dat
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob"))
errors = import_dat.load(self, context, **keywords)
for error in errors:
self.report({"ERROR"}, error)
return {'FINISHED'}
class ExportDAT(bpy.types.Operator, ExportHelper):
"""Export to DAT map format (.dat)"""
bl_idname = "export_scene.bluefang_dat"
bl_label = 'Export DAT'
filename_ext = ".dat"
filter_glob = StringProperty(default="*.dat", options={'HIDDEN'})
def execute(self, context):
from . import export_dat
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob", "check_existing"))
errors = export_dat.save(self, context, **keywords)
for error in errors:
self.report({"ERROR"}, error)
return {'FINISHED'}
class ExportBFB(bpy.types.Operator, ExportHelper):
"""Export to BFB file format (.bfb)"""
bl_idname = "export_scene.bluefang_bfb"
bl_label = 'Export BFB'
filename_ext = ".bfb"
filter_glob = StringProperty(default="*.bfb", options={'HIDDEN'})
try:
from . import common_bfb
cfg = common_bfb.load_config()
author = cfg["author"]
except:
author="somebody"
export_materials = BoolProperty(name="Export Materials", description="Should BFMAT materials be exported? Beware, they might not be identical to the existing material!", default=True)
author_name = StringProperty(name="Author", description="A signature included in the BFB file.", default=author)
fix_root_bones = BoolProperty(name="Fix Root Bones", description="Deletes surplus root bones automatically.", default=False)
create_lods = BoolProperty(name="Create LODs", description="Adds Levels of Detail - overwrites existing LODs!", default=True)
numlods = IntProperty( name="Number of LODs",
description="Number of Levels Of Detail, including the original",
min=1, max=5,
default=2,)
rate = IntProperty( name="Detail Decrease Rate",
description="The higher, the faster the detail will decrease: ratio = 1 /(LODX + Rate)",
min=1, max=5,
default=2,)
def execute(self, context):
from . import export_bfb
try: common_bfb.update_config("author", self.author_name)
except: pass
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob", "check_existing"))
errors = export_bfb.save(self, context, **keywords)
for error in errors:
self.report({"ERROR"}, error)
return {'FINISHED'}
class ExportBF(bpy.types.Operator, ExportHelper):
"""Export to BF file format (.bf)"""
bl_idname = "export_scene.bluefang_bf"
bl_label = 'Export BF'
filename_ext = ".bf"
filter_glob = StringProperty(default="*.bf", options={'HIDDEN'})
bake_actions = BoolProperty(name="Fix Tangents, Bake Actions and Clean Results", description="Smoothes tangents between actions, creates armature without constraints and baked actions for armatures and actions marked with *.", default=True)
error = FloatProperty(name="Max Cleaning Error", description="Adaptive Error - the more children a bone has, the less error it gets. The larger the error value, the smaller the file size, but the more error you get.", precision=3, step=1, soft_min=0.0, min=0.0, default=0.05)
exp_power = FloatProperty(name="Error Exponent", description="This influences how fast the error increases along the bone chain. Use larger values for a steeper falloff", precision=3, step=1, soft_min=0.0, min=0.0, default=1.0)
#TODO: replace these settings with the more transparent curve UI
#https://blender.stackexchange.com/questions/61618/add-a-custom-curve-mapping-property-for-an-add-on
def execute(self, context):
from . import export_bf
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob", "check_existing"))
errors = export_bf.save(self, context, **keywords)
for error in errors:
self.report({"ERROR"}, error)
return {'FINISHED'}
class BatchBFB(bpy.types.Operator, ImportHelper):
"""Batch process BFB and NIF files. Converts all NIFs to BFBs, and adds LODs to all BFBs."""
bl_idname = "import_scene.bluefang_bfb_batch"
bl_label = 'Batch Process'
bl_options = {'UNDO'}
filename_ext = ".bfb"
filter_glob = StringProperty(default="*.bfb;*.nif", options={'HIDDEN'})
files = CollectionProperty(type=bpy.types.PropertyGroup)
numlods = IntProperty( name="Number of LODs",
description="Number of Levels Of Detail, including the original",
min=1, max=5,
default=2,)
rate = IntProperty( name="Detail Decrease Rate",
description="The higher, the faster the detail will decrease: ratio = 1 /(LODX + Rate)",
min=1, max=5,
default=2,)
def execute(self, context):
from . import batch_bfb
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob"))
return batch_bfb.process(self, context, **keywords)
class ToggleIKLink(bpy.types.Operator):
"""Toggle the IK link between limbs etc. and the root. Useful for animations, if you want to switch between treadmill and standard motion for easier workflow."""
bl_idname = "bone.toggle_ik_link"
bl_label = "Toggle IK link from Bip01 root"
bl_options = {'REGISTER', 'UNDO'}
root_name = StringProperty(name="Root bone name", description="The name of the root bone.", default="Bip01")
ik_names = StringProperty(name="Limb IK bone names", description="The names of the bones to (un)link.", default="*IKH.L,*IKH.R,*IKF.L,*IKF.R")
def execute(self, context):
from . import bone_tools
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob", "view_align"))
return bone_tools.toggle_link_ik_controllers(self, context, **keywords)
class ReorientBone(bpy.types.Operator, AddObjectHelper):
"""Reorient a bone while maintaining its global space animation. Useful if Bip01 is causing trouble."""
bl_idname = "bone.reorient_bone"
bl_label = "Reorient bone"
bl_options = {'REGISTER', 'UNDO'}
# bone_name = StringProperty(name="Bone name", description="The bone you want to rotate.", default="Bip01")
fixed_items = bpy.props.EnumProperty(items= (('0', 'Worldspace', 'Keep the worldspace rotation intact.'),
('1', 'Bonespace', 'Correct against the changed restpose.')),
name = "Anim Mode")
def execute(self, context):
from . import bone_tools
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob", "view_align"))
return bone_tools.reorient_bone(self, context, **keywords)
class AddCorrectionBone(bpy.types.Operator, AddObjectHelper):
"""Add a correction bone, which will allow you to rearrange the skeleton without breaking existing animations."""
bl_idname = "bone.add_correction_bone"
bl_label = "Add Correction bone"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
from . import bone_tools
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob", "view_align"))
return bone_tools.add_correction_bone(self, context, **keywords)
class AddFXWind(bpy.types.Operator):
"""Add a vertex group for BFB wind effect."""
bl_idname = "object.fx_wind_add"
bl_label = "Add FX Wind Weights"
bl_options = {'REGISTER', 'UNDO'}
fixed_items = bpy.props.EnumProperty(items= (('0', 'Height', 'Use height coordinate of the mesh.'),
('1', 'Mesh Center Radius', 'Use distance to the mesh center.'),
('2', 'Object Origin Radius', 'Use distance to the mesh origin.'),
('3', 'Z Axis Distance', 'Use distance to the Z axis.')),
name = "Wind source")
wmin = FloatProperty(
name="Minimal weight",
description="Minimal weight used in the gradient",
min=0.0, max=1.0,
default=0.0, )
wmax = FloatProperty(
name="Maximal weight",
description="Maximal weight used in the gradient",
min=0.001, max=1.0,
default=0.5, )
def execute(self, context):
from . import fx_wind
fx_wind.add_fx_wind(self.fixed_items, self.wmin, self.wmax)
return {'FINISHED'}
# Add to a menu
def menu_func_export(self, context):
self.layout.operator(ExportBFB.bl_idname, text="Blue Fang Model (.bfb)", icon_value=preview_collection["zt2.png"].icon_id)
self.layout.operator(ExportDAT.bl_idname, text="Blue Fang Map (.dat)", icon_value=preview_collection["zt2.png"].icon_id)
self.layout.operator(ExportBF.bl_idname, text="Blue Fang Animation (.bf)", icon_value=preview_collection["zt2.png"].icon_id)
def menu_func_import(self, context):
self.layout.operator(ImportBFB.bl_idname, text="Blue Fang Model (.bfb)", icon_value=preview_collection["zt2.png"].icon_id)
self.layout.operator(ImportDAT.bl_idname, text="Blue Fang Map (.dat)", icon_value=preview_collection["zt2.png"].icon_id)
self.layout.operator(ImportBF.bl_idname, text="Blue Fang Animation (.bf)", icon_value=preview_collection["zt2.png"].icon_id)
self.layout.operator(ImportPSYS.bl_idname, text="Blue Fang Particles (.psys)", icon_value=preview_collection["zt2.png"].icon_id)
self.layout.operator(BatchBFB.bl_idname, text="BFB and NIF Batch Processing (.bfb, .nif)", icon_value=preview_collection["zt2.png"].icon_id)
def menu_func_add_objects(self, context):
self.layout.operator(AddCapsule.bl_idname, icon_value=preview_collection["bfb_capsule.png"].icon_id)
self.layout.operator(AddSphere.bl_idname, icon_value=preview_collection["bfb_sphere.png"].icon_id)
self.layout.operator(AddBox.bl_idname, icon_value=preview_collection["bfb_box.png"].icon_id)
def menu_func_armature(self, context):
self.layout.operator(AddCorrectionBone.bl_idname, text="Add Correction Bone", icon_value=preview_collection["zt2.png"].icon_id)
self.layout.operator(ReorientBone.bl_idname, text="Reorient Bone", icon_value=preview_collection["zt2.png"].icon_id)
self.layout.operator(ToggleIKLink.bl_idname, text="Toggle IK Link", icon_value=preview_collection["zt2.png"].icon_id)
def menu_func_weights(self, context):
self.layout.operator(AddFXWind.bl_idname, icon_value=preview_collection["zt2.png"].icon_id)
def register():
import os
icons_dir = os.path.join(os.path.dirname(__file__), "icons")
for icon_name_ext in os.listdir(icons_dir):
icon_name = os.path.basename(icon_name_ext)
preview_collection.load(icon_name, os.path.join(os.path.join(os.path.dirname(__file__), "icons"), icon_name_ext), 'IMAGE')
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_import.append(menu_func_import)
bpy.types.INFO_MT_file_export.append(menu_func_export)
bpy.types.INFO_MT_mesh_add.append(menu_func_add_objects)
bpy.types.VIEW3D_PT_tools_armatureedit.append(menu_func_armature)
bpy.types.VIEW3D_PT_tools_weightpaint.append(menu_func_weights)
bpy.types.VIEW3D_PT_tools_object.append(menu_func_weights)
def unregister():
bpy.utils.previews.remove(preview_collection)
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_import.remove(menu_func_import)
bpy.types.INFO_MT_file_export.remove(menu_func_export)
bpy.types.INFO_MT_mesh_add.remove(menu_func_add_objects)
bpy.types.VIEW3D_PT_tools_armatureedit.remove(menu_func_armature)
bpy.types.VIEW3D_PT_tools_weightpaint.remove(menu_func_weights)
bpy.types.VIEW3D_PT_tools_object.remove(menu_func_weights)
if __name__ == "__main__":
register()