-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClonkPort.py
More file actions
1218 lines (939 loc) · 47.6 KB
/
ClonkPort.py
File metadata and controls
1218 lines (939 loc) · 47.6 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# --------------------------
# Clonk Port
# 11.02.2022
# --------------------------
# Robin Hohnsbeen (Ryou)
from operator import mod
import bpy
from bpy.props import StringProperty, BoolProperty, IntProperty
import math
import mathutils
import glob # for wildcard directory search
from bpy_extras.io_utils import ImportHelper
from bpy_extras.io_utils import ExportHelper
from pathlib import Path
import os
from . import AnimPort
from . import MeshPort
from . import MetaData
from . import PathUtilities
from . import IniPort
from . import SpritesheetMaker
script_file = os.path.realpath(__file__)
AddonDir = os.path.dirname(script_file)
found_meshes = []
found_actions = []
found_actionlists = []
last_search_path = ""
def get_res_multiplier():
return bpy.context.scene.render.resolution_percentage / 100.0
def content_glob_search(path):
global found_meshes
global found_actions
global found_actionlists
found_meshes += glob.glob(os.path.join(path, "*.mesh*"), recursive=False)
found_actions += glob.glob(os.path.join(path, "*.anim*"), recursive=False)
found_actionlists += glob.glob(os.path.join(path, "*.act"), recursive=False)
def collect_clonk_content_files(path):
global last_search_path
if last_search_path == path:
return
global found_meshes
global found_actions
global found_actionlists
found_meshes.clear()
found_actions.clear()
found_actionlists.clear()
path = str(path)
# Note: This does not use recursion, because it might use an inordinate amount of time on large directories.
content_glob_search(path)
searching_path = os.path.join(path, "**")
content_glob_search(searching_path)
print("Looking for Data.." + path)
def _ImportExtraMesh(meshname, meshfiles, reuse_materials=True):
if bpy.data.objects.find(meshname) > -1:
return [bpy.data.objects[meshname]]
for meshfilespath in meshfiles:
meshfile = Path(meshfilespath)
if meshfile.stem == meshname:
clonk_objects = MeshPort.import_mesh(meshfilespath, reuse_materials=reuse_materials)
new_objects = reuse_rigs_and_parent_objects(clonk_objects)
return new_objects
return []
def _ImportToolsIfAnyLegacy(action_entry, animdata, meshfiles, reuse_materials=True):
tool1 = []
tool2 = []
if animdata.get("Tool1"):
tool1 = _ImportExtraMesh(animdata["Tool1"], meshfiles, reuse_materials=reuse_materials)
if animdata.get("Tool2"):
tool2 = _ImportExtraMesh(animdata["Tool2"], meshfiles, reuse_materials=reuse_materials)
if action_entry == None:
return
if len(tool1) > 0 or len(tool2) > 0:
if len(tool1) + len(tool2) > 1:
new_collection = bpy.data.collections.new(
name=animdata["Action"].name + "_tools")
for tool in tool1:
new_collection.objects.link(tool)
for tool in tool2:
new_collection.objects.link(tool)
bpy.context.scene.collection.children.link(new_collection)
action_entry.additional_object_enum = "2_Collection"
action_entry.additional_collection = new_collection
elif len(tool1) > 0:
action_entry.additional_object_enum = "1_Object"
action_entry.additional_object = tool1[0]
elif len(tool2) > 0:
action_entry.additional_object_enum = "1_Object"
action_entry.additional_object = tool2[0]
def LoadAction(path, animation_target, force_import_action=False, import_tools=True, reuse_materials=True):
if ".animblend" in path or ".anim.blend" in path:
with bpy.data.libraries.load(path) as (data_from, data_to):
data_to.scenes = data_from.scenes
for imported_entry in data_to.scenes[0].animlist:
new_entry = bpy.context.scene.animlist.add()
for key, value in imported_entry.items():
new_entry[key] = value
tool_objects, collection = MetaData.get_action_entry_tools(new_entry)
# These objects get implicitly imported when they are referenced inside an anim blend file.
if import_tools:
if collection and collection.name not in bpy.context.view_layer.layer_collection.collection.children:
bpy.context.view_layer.layer_collection.collection.children.link(collection)
elif len(tool_objects) == 1 and tool_objects[0].name not in bpy.context.view_layer.layer_collection.collection.objects:
bpy.context.view_layer.layer_collection.collection.objects.link(tool_objects[0])
if reuse_materials:
MetaData.replace_duplicate_materials(tool_objects)
reuse_rigs_and_parent_objects(tool_objects)
# Remove them again
else:
for tool in tool_objects:
if tool:
if tool.type == "MESH":
for material_slot in tool.material_slots:
if material_slot.material:
bpy.data.materials.remove(material_slot.material)
bpy.data.objects.remove(tool)
if collection:
bpy.data.collections.remove(collection)
bpy.data.scenes.remove(data_to.scenes[0])
return None
# Legacy action load
else:
anim_data = AnimPort.LoadActionLegacy(path, animation_target, force_import_action)
# Update old action names
current_action_name = anim_data["Action"].name
if current_action_name.lower() in MetaData.action_map:
anim_data["Action"].name = MetaData.action_map[current_action_name.lower()]
return anim_data
def get_animfilemap(animfiles):
animfilemap = {}
for animfilepath in animfiles:
animpath = Path(animfilepath)
# Prioritize blend files
if animpath.stem in animfilemap and "blend" not in animpath.suffix:
pass
else:
animfilemap[animpath.stem] = animfilepath
return animfilemap
def import_actions_multi(action_names, animfiles, meshfiles, target, create_entry, import_tools, reuse_materials=True):
print("Looking in " + str(len(animfiles)) + " animfiles")
animfilemap = get_animfilemap(animfiles)
animations_not_found = []
animations_found = 0
for action in action_names:
# Look for updated file or name change.
if action.lower() in MetaData.action_map:
name_replacement = MetaData.action_map[action.lower()]
if animfilemap.get(name_replacement) != None:
action = name_replacement
if animfilemap.get(action) != None:
anim_data = LoadAction(animfilemap[action], target, import_tools=import_tools, reuse_materials=reuse_materials)
animations_found += 1
if anim_data: # Legacy import
new_entry = None
if create_entry:
new_entry = MetaData.MakeActionEntry(anim_data)
if import_tools:
_ImportToolsIfAnyLegacy(new_entry, anim_data, meshfiles, reuse_materials=reuse_materials)
else:
animations_not_found.append(action)
if animations_found == 0:
return "WARNING", "No actions could be found."
if len(animations_not_found) > 0:
missing_actions = ""
for animation in animations_not_found:
missing_actions += animation + ", "
return "WARNING", f"Imported {animations_found} actions. Omitted: {missing_actions}"
else:
return "INFO", "Imported all actions from file."
def ImportActList(path, animfiles, meshfiles, target, create_entry, import_tools, reuse_materials=True):
print("Read act " + path)
file = open(path, "r")
lines = file.readlines()
is_reading_actions = False
action_names = []
for line in lines:
line = line.replace("\n", "")
if line == "[Actions]":
is_reading_actions = True
continue
if is_reading_actions == False:
continue
action_names.append(line)
file.close()
message_type, message = import_actions_multi(action_names, animfiles, meshfiles, target, create_entry, import_tools, reuse_materials)
return message_type, message
# ActMap.txt
def ImportActMap(path, animfiles, meshfiles, target, create_entry, import_tools, reuse_materials=True):
print("Read actmap " + path)
file = open(path, "r")
actmap, messagetype, message = IniPort.Read(path)
if messagetype == "ERROR":
return messagetype, message
animations_not_found = []
action_names = []
for section in actmap:
action_names.append(section["Name"])
file.close()
message_type, message = import_actions_multi(action_names, animfiles, meshfiles, target, create_entry, import_tools, reuse_materials)
return message_type, message
def AppendRenderClonkSetup():
global AddonDir
dir = Path(AddonDir)
bpy.ops.wm.append(
filepath="RenderClonk.blend",
directory=str(Path.joinpath(dir, "RenderClonk.blend", "Collection")),
filename="RenderClonk"
)
def GetOrAppendOverlayMaterials():
global AddonDir
dir = Path(AddonDir)
if bpy.data.materials.find("Overlay") == -1:
bpy.ops.wm.append(
filepath="RenderClonk.blend",
directory=str(Path.joinpath(dir, "RenderClonk.blend", "Material")),
filename="Overlay"
)
if bpy.data.materials.find("Holdout") == -1:
bpy.ops.wm.append(
filepath="RenderClonk.blend",
directory=str(Path.joinpath(dir, "RenderClonk.blend", "Material")),
filename="Holdout"
)
if bpy.data.materials.find("Fill") == -1:
bpy.ops.wm.append(
filepath="RenderClonk.blend",
directory=str(Path.joinpath(dir, "RenderClonk.blend", "Material")),
filename="Fill"
)
return bpy.data.materials["Overlay"], bpy.data.materials["Holdout"], bpy.data.materials["Fill"]
def GetOrAppendClonkRig(ReuseOld=True):
rig_index = bpy.data.objects.find("ClonkRig")
if rig_index == -1 or ReuseOld == False:
global AddonDir
dir = Path(AddonDir)
bpy.ops.wm.append(
filepath="RenderClonk.blend",
directory=str(Path.joinpath(
dir, "RenderClonk.blend", "Collection")),
filename="ClonkRig"
)
add_anim_target(bpy.data.objects['ClonkRig'])
for child in bpy.data.objects['ClonkRig'].children:
if child.type == "CAMERA":
bpy.context.scene.camera = child
return bpy.data.objects['ClonkRig']
def GetOrAppendCamSetup(ReuseOld=True):
collection_index = bpy.data.collections.find("CamSetup")
if collection_index == -1 or ReuseOld == False:
global AddonDir
dir = Path(AddonDir)
bpy.ops.wm.append(
filepath="RenderClonk.blend",
directory=str(Path.joinpath(
dir, "RenderClonk.blend", "Collection")),
filename="CamSetup"
)
return bpy.data.collections['CamSetup']
def are_armatures_equal(first, second) -> bool:
if first is None or second is None:
print("Can't compare armatures. One of them is None!")
if len(first.data.bones) != len(second.data.bones):
return False
maching_names = 0
for bone_first in first.data.bones:
for bone_second in second.data.bones:
if bone_first.name == bone_second.name:
maching_names += 1
break
return maching_names == len(first.data.bones)
def get_armature_modifier(in_object):
for modifier in in_object.modifiers:
if modifier.type == "ARMATURE":
return modifier
def parent_objects_to_rig(in_objects, rig):
for clonk_object in in_objects:
if clonk_object is None:
continue
if clonk_object.type == "ARMATURE":
continue
if clonk_object.parent and clonk_object.parent.type != "ARMATURE":
continue # Ignore objects that are parented to other objects. So we don't destroy normal object parenting.
clonk_object.parent = rig
if clonk_object.parent_type == "BONE":
if clonk_object.parent_bone is None:
clonk_object.parent_type = "OBJECT"
continue
if clonk_object.parent_bone not in rig.data.bones:
print(f"Bone {clonk_object.parent_bone} not available in {rig.name}. Transform of {clonk_object.name} may be incorrect.")
continue
parent_bone = rig.data.bones[clonk_object.parent_bone]
# Relative Parenting: inverse matrix depends on the world position of the rig
if parent_bone.use_relative_parent:
clonk_object.matrix_parent_inverse = rig.matrix_world.inverted()
# Normal Parenting: inverse matrix depends on the world position of the bone
else:
translation_matrix = mathutils.Matrix.Translation(parent_bone.tail - parent_bone.head)
clonk_object.matrix_parent_inverse = (rig.matrix_world @ translation_matrix @ parent_bone.matrix_local).inverted()
continue
clonk_object.matrix_parent_inverse = rig.matrix_world.inverted()
armature_modifier = get_armature_modifier(clonk_object)
if armature_modifier is None:
armature_modifier = clonk_object.modifiers.new(
name="ClonkRig", type="ARMATURE")
armature_modifier.object = rig
def get_anim_target_armatures():
armatures = []
for anim_target in MetaData.get_anim_targets():
if anim_target is None:
continue
if anim_target.type == "ARMATURE":
armatures.append(anim_target)
return armatures
def add_anim_target(in_object):
if in_object is None:
return
found_anim_target = None
if bpy.context.scene.anim_target_enum == "1_Object":
if bpy.context.scene.anim_target is None:
bpy.context.scene.anim_target = in_object
return
else:
found_anim_target = bpy.context.scene.anim_target
anim_target_collection = bpy.context.scene.anim_target_collection
if anim_target_collection is None:
anim_target_collection = bpy.data.collections.new("AnimTargets")
bpy.context.scene.anim_target_collection = anim_target_collection
bpy.context.view_layer.layer_collection.collection.children.link(anim_target_collection)
if found_anim_target is not None:
if found_anim_target.name not in anim_target_collection.objects:
anim_target_collection.objects.link(found_anim_target)
if in_object.name not in anim_target_collection.objects:
anim_target_collection.objects.link(in_object)
if bpy.context.scene.anim_target_enum == "1_Object":
bpy.context.scene.anim_target_enum = "2_Collection"
def reuse_rigs_and_parent_objects(in_objects):
# On import there is the possibility to import armatures as well. There could even be several armatures that are linked to individual objects.
# Furthermore, we usually want wo reuse the rigs we have, since the imported rig might be identical to the clonk rig (or other rigs in the scene already)
# So we compare the imported rigs with the ones available and then decide what rigs to keep.
clonk_rig = GetOrAppendClonkRig(ReuseOld=True)
objects_without_rig = []
armatures_to_object = {}
# Sort objects by armature
# Find out what objects aren't attached to armatures
for clonk_object in in_objects:
if clonk_object is None:
continue
if clonk_object.type == "ARMATURE":
continue
armature_modifier = get_armature_modifier(clonk_object)
if (armature_modifier and armature_modifier.object) or (clonk_object.parent and clonk_object.parent.type == "ARMATURE" and clonk_object.parent_type == "BONE"):
parent_armature = None
if armature_modifier:
parent_armature = armature_modifier.object
else:
parent_armature = clonk_object.parent
if parent_armature in armatures_to_object:
armatures_to_object[parent_armature].append(clonk_object)
else:
armatures_to_object[parent_armature] = [clonk_object]
else:
objects_without_rig.append(clonk_object)
unused_armatures = set()
for imported_armature, objects_with_armature in armatures_to_object.items():
if imported_armature is None:
continue
anim_targets = get_anim_target_armatures()
if len(anim_targets) > 0:
found_matching_anim_target = False
for anim_target in anim_targets:
if anim_target.name == imported_armature.name:
# If several actions get imported at once, the tool might have already been attached to the default Clonk Rig. So we end here.
print(f"{objects_with_armature} has/have already been parented to {anim_target.name}. So we won't parent it/them again.")
found_matching_anim_target = True
break
if are_armatures_equal(anim_target, imported_armature):
parent_objects_to_rig(objects_with_armature, anim_target)
unused_armatures.add(imported_armature)
found_matching_anim_target = True
print(f"Armature {imported_armature.name} is equal to {anim_target.name}. Removing {imported_armature.name} armature.")
break
if found_matching_anim_target == False:
add_anim_target(imported_armature)
for unused_armature in unused_armatures:
if unused_armature in in_objects:
in_objects.remove(unused_armature)
bpy.data.objects.remove(unused_armature)
parent_objects_to_rig(objects_without_rig, clonk_rig)
return in_objects
class OT_MeshFilebrowser(bpy.types.Operator, ImportHelper):
bl_idname = "mesh.open_filebrowser"
bl_label = "Import Object (.mesh*)"
bl_options = {'UNDO'}
filter_glob: StringProperty(default="*.mesh*", options={"HIDDEN"})
parent_to_existing_rigs: BoolProperty(name="Parent to existing rigs", default=True,
description="This will parent the objects to existing (matching) rigs (anim targets) and apply an Armature Modifier (if necessary)")
reuse_materials: BoolProperty(name="Reuse materials", default=True,
description="Decide whether to search for existing materials and replace imported ones.")
def execute(self, context):
print(self.filepath)
if ".mesh" in self.filepath:
if self.parent_to_existing_rigs:
collection: bpy.types.Collection = None
if bpy.context.scene.always_rendered_objects != None:
collection = bpy.context.scene.always_rendered_objects
clonk_objects = MeshPort.import_mesh(self.filepath, collection, reuse_materials=self.reuse_materials)
clonk_objects = reuse_rigs_and_parent_objects(clonk_objects)
else:
clonk_objects = MeshPort.import_mesh(self.filepath, reuse_materials=self.reuse_materials)
else:
print(self.filepath + " is no Clonk mesh!")
context.scene.lastfilepath = self.filepath
return {'FINISHED'}
def GetSelectedObjects(context):
active_object = None
out_objects = []
if context.active_object is not None and context.active_object.type != "ARMATURE":
active_object = context.active_object
for selected_object in context.selected_objects:
if selected_object.type != "ARMATURE":
out_objects.append(selected_object)
return out_objects, active_object
def export_objects(filepath, selected_objects):
modular_filepath = Path(filepath)
file_name = modular_filepath.stem
export_scene = bpy.data.scenes.new(name=f"Export_{file_name}")
export_collection = bpy.data.collections.new(name=file_name)
try:
export_scene.view_layers[0].layer_collection.collection.children.link(export_collection)
for selected_object in selected_objects:
export_collection.objects.link(selected_object)
if selected_object.animation_data is not None:
selected_object.animation_data.action = None # We don't want to export other animations
export_collection.asset_mark()
data_blocks = set()
data_blocks.add(export_scene)
bpy.data.libraries.write(filepath, data_blocks, fake_user=True)
except BaseException as Err:
return Err
finally:
bpy.data.collections.remove(export_collection)
bpy.data.scenes.remove(export_scene)
class OT_MeshExport(bpy.types.Operator):
bl_idname = "mesh.export"
bl_label = "Export Object (.meshblend)"
@classmethod
def poll(cls, context):
selected_objects, active_object = GetSelectedObjects(context)
return active_object is not None
def execute(self, context):
preferences = context.preferences
addon_prefs = preferences.addons[__package__].preferences
selected_objects, active_object = GetSelectedObjects(context)
if addon_prefs.use_quick_export:
objects_path = os.path.join(addon_prefs.content_folder, "Meshes")
if os.path.exists(objects_path) == False:
os.mkdir(objects_path)
export_path = os.path.join(objects_path, f"{active_object.name}.meshblend")
Err = export_objects(export_path, selected_objects)
if Err is not None:
self.report({"ERROR"}, f"{Err}")
return {"CANCELLED"}
self.report(
{"INFO"}, f"Exported mesh \'{active_object.name}\' successfully to \'{objects_path}\'")
else:
if context.scene.lastfilepath is None or context.scene.lastfilepath == "":
context.scene.lastfilepath = addon_prefs.content_folder
export_path = os.path.join(context.scene.lastfilepath, f"{active_object.name}.meshblend")
bpy.ops.object.open_exportfilebrowser("INVOKE_DEFAULT", filepath=export_path)
return {'FINISHED'}
class OT_ExportObjectFilebrowser(bpy.types.Operator, ExportHelper):
bl_idname = "object.open_exportfilebrowser"
bl_label = "Export Object(s)"
filter_glob: StringProperty(default="*.mesh*", options={"HIDDEN"})
filename_ext: StringProperty(default=".meshblend", options={"HIDDEN"})
def execute(self, context):
print(self.filepath)
modular_filepath = Path(self.filepath)
if len(modular_filepath.suffix) == 0:
self.filepath += ".meshblend"
selected_objects, active_object = GetSelectedObjects(context)
Err = export_objects(self.filepath, selected_objects)
if Err is not None:
self.report({"ERROR"}, f"{Err}")
return {"CANCELLED"}
self.report({"INFO"}, f"Exported mesh \'{active_object.name}\' successfully to \'{self.filepath}\'")
return {'FINISHED'}
class OT_AnimFilebrowser(bpy.types.Operator, ImportHelper):
bl_idname = "anim.open_filebrowser"
bl_label = "Import Action (.anim*)"
bl_options = {'UNDO'}
filter_glob: StringProperty(default="*.anim*", options={"HIDDEN"})
force_import_action: BoolProperty(name="Force action import", default=False,
description="Import action although there is an action with the same name in blender", options={"HIDDEN"})
import_tools: BoolProperty(name="Import Tool Objects", default=True,
description="Import tool objects if the action references any")
reuse_materials_on_tools: BoolProperty(name="Reuse tool materials", default=True,
description="Decide whether to search for existing materials and replace imported ones.")
def execute(self, context):
print(self.filepath)
if ".anim" in self.filepath:
clonk_rig = GetOrAppendClonkRig(True)
if clonk_rig == None:
self.report({"ERROR"}, f"ClonkRig not found.")
print("ClonkRig not found")
return {"CANCELLED"}
anim_data = LoadAction(
self.filepath, clonk_rig, self.force_import_action, import_tools=self.import_tools, reuse_materials=self.reuse_materials_on_tools)
if anim_data: # Legacy import
new_entry = MetaData.MakeActionEntry(anim_data)
if self.import_tools:
parent_path = Path(self.filepath).parents[1]
collect_clonk_content_files(parent_path)
global found_meshes
_ImportToolsIfAnyLegacy(new_entry, anim_data, found_meshes, reuse_materials=self.reuse_materials_on_tools)
if anim_data.get("ERROR"):
self.report({"ERROR"}, f"" + anim_data["ERROR"])
return {"CANCELLED"}
else:
print(self.filepath + " is no Animation!")
context.scene.lastfilepath = self.filepath
return {'FINISHED'}
def export_action(filepath, context, export_all_enabled=False):
modular_filepath = Path(filepath)
filename = modular_filepath.stem
export_scene = bpy.data.scenes.new(name=f"Export_{filename}")
export_collection = bpy.data.collections.new(name=filename)
try:
for anim_target in MetaData.get_anim_targets():
if anim_target:
if anim_target.animation_data:
anim_target.animation_data.action = None # We don't want to export other animations
export_scene.view_layers[0].layer_collection.collection.children.link(
export_collection)
exported_actions = False
if export_all_enabled:
for anim_entry in context.scene.animlist:
if anim_entry.is_used:
new_entry = export_scene.animlist.add()
for key, value in anim_entry.items():
new_entry[key] = value
exported_actions = True
else:
new_entry = export_scene.animlist.add()
selected_entry = context.scene.action_meta_data_index
for key, value in context.scene.animlist[selected_entry].items():
new_entry[key] = value
exported_actions = True
if exported_actions:
data_blocks = set()
data_blocks.add(export_scene)
bpy.data.libraries.write(filepath, data_blocks, fake_user=True)
else:
print("No actions exported, since none were enabled.")
except BaseException as Err:
return Err
finally:
bpy.data.collections.remove(export_collection)
bpy.data.scenes.remove(export_scene)
class OT_AnimExport(bpy.types.Operator):
bl_idname = "anim.export"
bl_label = "Export action (.animblend)"
@classmethod
def poll(cls, context):
action_name = MetaData.GetActionNameFromIndex(
bpy.context.scene.action_meta_data_index)
if action_name == "":
return False
if bpy.context.scene.animlist[bpy.context.scene.action_meta_data_index].is_used == False:
return False
return True
def execute(self, context):
preferences = context.preferences
addon_prefs = preferences.addons[__package__].preferences
action_name = MetaData.GetActionNameFromIndex(
context.scene.action_meta_data_index)
if action_name == "":
self.report({"ERROR"}, "No valid action entry selected.")
return {"CANCELLED"}
if addon_prefs.use_quick_export:
actions_path = os.path.join(addon_prefs.content_folder, "Actions")
if os.path.exists(actions_path) == False:
os.mkdir(actions_path)
export_path = os.path.join(actions_path, f"{action_name}.animblend")
# TODO: Multi export for quick export (Would need more UI and checks)
Err = export_action(export_path, context)
if Err is not None:
self.report({"ERROR"}, f"{Err}")
return {"CANCELLED"}
self.report({"INFO"}, f"Exported action \'{action_name}\' successfully to \'{self.filepath}\'")
else:
if context.scene.lastfilepath is None or context.scene.lastfilepath == "":
context.scene.lastfilepath = addon_prefs.content_folder
export_path = os.path.join(context.scene.lastfilepath, f"{action_name}.animblend")
bpy.ops.anim.open_exportfilebrowser("INVOKE_DEFAULT", filepath=export_path)
return {'FINISHED'}
class OT_ExportAnimFilebrowser(bpy.types.Operator, ExportHelper):
bl_idname = "anim.open_exportfilebrowser"
bl_label = "Export Action"
filter_glob: StringProperty(default="*.anim*", options={"HIDDEN"})
filename_ext: StringProperty(default=".animblend", options={"HIDDEN"})
export_all_enabled: BoolProperty(name="Export all enabled actions", default=False,
description="Exports multiple actionsin one file")
def execute(self, context):
print(self.filepath)
modular_filepath = Path(self.filepath)
if len(modular_filepath.suffix) == 0:
self.filepath += ".animblend"
Err = export_action(self.filepath, context, export_all_enabled=self.export_all_enabled)
if Err is not None:
self.report({"ERROR"}, f"{Err}")
return {"CANCELLED"}
if self.export_all_enabled:
action_count = 0
for anim_entry in context.scene.animlist:
if anim_entry.is_used:
action_count += 1
self.report({"INFO"}, f"Exported {action_count} action(s) successfully to \'{self.filepath}\'")
else:
action_name = MetaData.GetActionNameFromIndex(context.scene.action_meta_data_index)
self.report({"INFO"}, f"Exported action \'{action_name}\' successfully to \'{self.filepath}\'")
return {'FINISHED'}
class OT_PictureFilebrowser(bpy.types.Operator, ImportHelper):
bl_idname = "picture.open_filebrowser"
bl_label = "Load image"
filter_glob: StringProperty(default="*.png")
load_overlay_image: BoolProperty(
name="Image To Load", default=False, description="")
def execute(self, context):
parent_path = Path(self.filepath).parents[1]
print(self.filepath)
extension = Path(self.filepath).suffix
if extension == ".png":
global AddonDir
try:
scene = bpy.context.scene
loaded_image = bpy.data.images.load(self.filepath)
anim_entry = scene.animlist[scene.action_meta_data_index]
if self.load_overlay_image:
anim_entry.image_for_picture_overlay = loaded_image
else:
anim_entry.image_for_picture_combined = loaded_image
for region in context.area.regions:
if region.type == "UI":
region.tag_redraw()
except BaseException as Err:
self.report({"ERROR"}, f"{Err}")
return {"CANCELLED"}
else:
self.report({"ERROR"}, f"{self.filepath} is no png!")
return {"CANCELLED"}
context.scene.lastfilepath = self.filepath
return {'FINISHED'}
class OT_ActListFilebrowser(bpy.types.Operator, ImportHelper):
bl_idname = "act.open_filebrowser"
bl_label = "Import Actionlist (.act)"
bl_options = {'UNDO'}
filter_glob: StringProperty(default="*.act", options={"HIDDEN"})
import_tool_mesh: BoolProperty(name="Import Tool Meshes", default=True,
description="Import tool meshes if the actions reference any")
reuse_materials_on_tools: BoolProperty(name="Reuse tool materials", default=True,
description="Decide whether to search for existing materials and replace imported ones.")
def execute(self, context):
parent_path = Path(self.filepath).parents[1]
collect_clonk_content_files(parent_path)
print(self.filepath)
extension = Path(self.filepath).suffix
if extension == ".act":
global found_actions
global found_meshes
clonk_rig = GetOrAppendClonkRig()
bpy.context.scene.anim_target = clonk_rig
if bpy.data.collections.find("ClonkRig") == -1:
raise AssertionError("No Collection named ClonkRig found.")
bpy.context.scene.always_rendered_objects = bpy.data.collections["ClonkRig"]
reporttype, message = ImportActList(self.filepath, found_actions, found_meshes,
bpy.context.scene.anim_target, True, self.import_tool_mesh, reuse_materials=self.reuse_materials_on_tools)
self.report({reporttype}, "%s" % (message))
else:
print(self.filepath + " is no Actionlist!")
context.scene.lastfilepath = self.filepath
return {"FINISHED"}
class OT_ActMapFilebrowser(bpy.types.Operator, ImportHelper):
bl_idname = "actmap.open_filebrowser"
bl_label = "Import ActMap.txt"
bl_options = {'UNDO'}
filter_glob: StringProperty(default="*.txt", options={"HIDDEN"})
import_tool_mesh: BoolProperty(name="Import Tool Meshes", default=True,
description="Import tool meshes if the actions reference any")
reuse_materials_on_tools: BoolProperty(name="Reuse tool materials", default=True,
description="Decide whether to search for existing materials and replace imported ones.")
def execute(self, context):
extension = Path(self.filepath).name
if "actmap" in extension.lower():
parent_path = Path(self.filepath).parents[1]
collect_clonk_content_files(parent_path)
print(self.filepath)
global found_actions
clonk_rig = GetOrAppendClonkRig()
bpy.context.scene.anim_target = clonk_rig
if bpy.data.collections.find("ClonkRig") == -1:
raise AssertionError("No Collection named ClonkRig found.")
bpy.context.scene.always_rendered_objects = bpy.data.collections["ClonkRig"]
if len(found_actions) == 0:
self.report(
{"ERROR"}, "Your ActMap.txt needs to be in the same folder (or neighboring folders) as your .anim files.")
return {"CANCELLED"}
reporttype, message = ImportActMap(self.filepath, found_actions, found_meshes,
bpy.context.scene.anim_target, True, self.import_tool_mesh, reuse_materials=self.reuse_materials_on_tools)
self.report({reporttype}, f"{message}")
else:
print(self.filepath + " is no ActMap!")
self.report(
{"ERROR"}, "Could not load file. Make sure the file you tried to load is an actmap.")
context.scene.lastfilepath = self.filepath
return {"FINISHED"}
def DoesActmapExist():
path = os.path.join(PathUtilities.GetOutputPath(), "ActMap.txt")
return os.path.exists(path)
def DoesDefCoreExist():
path = os.path.join(PathUtilities.GetOutputPath(), "DefCore.txt")
return os.path.exists(path)
def PrintActmap(path, remove_unused_sections=False):
valid_action_entries = MetaData.GetValidActionEntries()
messagetype, message = MetaData.CheckIfActionListIsValid(
valid_action_entries)
if messagetype == "ERROR":
return messagetype, message
sheet_width, sheet_height, sprite_strips = SpritesheetMaker.GetSpritesheetInfo(
valid_action_entries)
actmap_path = os.path.join(path, "ActMap.txt")
# Get old actmap data
file_content = []
if os.path.exists(actmap_path):
if PathUtilities.CanReadFile(actmap_path) == False or PathUtilities.CanWriteFile(actmap_path) == False:
return "ERROR", "Need read/write permissions at output path. Aborted."
file_content, messagetype, message = IniPort.Read(actmap_path)
if messagetype == "ERROR":
return messagetype, message
else:
print("No old Actmap.txt found. Creating new..")
if os.path.exists(path) == False:
os.mkdir(path)
# What section in the file is listed explicitly in the addon?
remaining_action_entries = valid_action_entries.copy()
remaining_file_content = file_content.copy()
section_descriptions = []
for action_entry in valid_action_entries:
for content_section in file_content:
if content_section["Name"] == MetaData.GetActionName(action_entry):
section_descriptions.append(
{"Action": action_entry, "FullCopy": True, "Section": content_section})
remaining_action_entries.remove(action_entry)
remaining_file_content.remove(content_section)
break
# What file section is related to what action but not explicitly listed in the addon?
omitted_sections = remaining_file_content.copy()
copied_descriptions = section_descriptions.copy()
for section_index, section_description in enumerate(section_descriptions):
for content_section in remaining_file_content:
if content_section["Facet"] == section_description["Section"]["Facet"]:
new_description = {
"Action": section_description["Action"], "FullCopy": False, "Section": content_section}