-
Notifications
You must be signed in to change notification settings - Fork 9
/
NMSDK.py
940 lines (803 loc) · 35.1 KB
/
NMSDK.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
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
# stdlib imports
from math import radians
import os.path as op
# Blender imports
from bpy.props import (StringProperty, BoolProperty, EnumProperty, IntProperty)
import bpy
from bpy_extras.io_utils import ExportHelper, ImportHelper
from bpy.types import Operator, PropertyGroup
from mathutils import Matrix
# internal imports
from ModelImporter.import_scene import ImportScene
from ModelExporter.addon_script import Exporter
from ModelExporter.utils import get_all_actions_in_scene, get_all_actions
from utils.settings import read_settings, write_settings
from BlenderExtensions.UIWidgets import ShowMessageBox
def set_import_export_defaults(cls, context):
if hasattr(cls, 'settings_loaded'):
if not cls.settings_loaded:
default_settings = context.scene.nmsdk_default_settings
cls.export_directory = default_settings.export_directory
cls.group_name = default_settings.group_name
cls.settings_loaded = True
# If there is an imported node, then get some default info from it.
# TODO: This needs a bit of work...
if bpy.context.scene.get('scene_node') and cls.preserve_node_info:
scene_node = bpy.context.scene.get('scene_node')
scene_path = scene_node['imported_from']
export_dir = op.dirname(scene_path)
cls.export_directory, cls.group_name = op.split(export_dir)
cls.preserve_node_info = True
if hasattr(cls, 'scene_name'):
cls.scene_name = op.basename(scene_node['imported_from'])
# Operators to be used for the public API
# Import/Export operators
class ImportSceneOperator(Operator):
""" Import an entire scene into the current blender context."""
bl_idname = "nmsdk.import_scene"
bl_label = "Import NMS Scene file"
path: StringProperty(default="")
clear_scene: BoolProperty(
name='Clear scene',
description='Whether or not to clear the currently exiting scene in '
'blender.',
default=True,
)
draw_hulls: BoolProperty(
name='Draw bounded hulls',
description='Whether or not to draw the points that make up the '
'bounded hulls of the materials. This is only for research'
'/debugging, so can safely be left as False.',
default=False,
)
import_collisions: BoolProperty(
name='Import collisions',
description='Whether or not to import the collision objects.',
default=True,
)
show_collisions: BoolProperty(
name='Draw collisions',
description='Whether or not to draw the collision objects.',
default=False,
)
import_recursively: BoolProperty(
name='Import recursively',
description='Whether or not to import reference nodes automatically.\n'
'For large scenes with many referenced scenes it is better'
' to set this as False to avoid long wait times, and then '
'only import the scenes you want after it has loaded.',
default=True,
)
# Animation related properties
import_bones: BoolProperty(
name='Import bones',
description="Whether or not to import the models' bones",
default=False,
)
import_anims: BoolProperty(
name='Import animations',
description='Whether or not to import animations for this scene',
default=False,
)
max_anims: IntProperty(
name='Max loaded animations',
description='Maximum number of animations to load',
default=10,
soft_min=-1,
)
def execute(self, context):
keywords = self.as_keywords()
importer = ImportScene(self.path, parent_obj=None, ref_scenes=dict(),
settings=keywords)
importer.render_scene()
return importer.state
class ImportMeshOperator(Operator):
""" Import one or more individual meshes from a single scene into the
current blender context. """
bl_idname = "nmsdk.import_mesh"
bl_label = "Import NMS meshes"
path: StringProperty(default="")
mesh_id: StringProperty(default="")
def execute(self, context):
importer = ImportScene(self.path, parent_obj=None, ref_scenes=dict())
importer.render_mesh(str(self.mesh_id))
return importer.state
class ExportSceneOperator(Operator):
""" Export the current scene to a SCENE.MBIN file and associated geometry,
animation, entity and other files.
"""
bl_idname = "nmsdk.export_scene"
bl_label = "Export to NMS scene"
output_directory: StringProperty(
name="Output Directory",
description="The directory the exported data is to be placed in."
)
export_directory: StringProperty(
name="Export Directory",
description="The base path relative to the PCBANKS folder under which "
"all models will be exported.",
default="CUSTOMMODELS",
)
group_name: StringProperty(
name="Group Name",
description="Group name so that models that all belong in the same "
"folder are placed there (path becomes "
"group_name/scene_name).",
)
scene_name: StringProperty(
name="Scene Name",
description="Name of the scene to be exported.")
preserve_node_info: BoolProperty(
name="Preserve Node Info",
description="If the exported scene was originally imported, preserve "
"the details of any nodes that were in the original scene."
"\nNote that this will not currently export any geometry "
"data, so adding mesh collisions to an existing scene is "
"not currently possible.",
default=False,
)
AT_only: BoolProperty(
name="ActionTriggers Only",
description="If this box is ticked, all the action trigger data will "
"be exported directly to an ENTITY file in the specified "
"location with the project name. Anything else in the "
"project is ignored",
default=False,
)
no_vert_colours: BoolProperty(
name="Don't export vertex colours",
description="Ticking this box will force vertex colours to not be "
"exported. Use this if you have accidentally added vertex "
"colours to a mesh and don't know how to get rid of them.",
default=False,
)
no_convert: BoolProperty(
name="Don't convert files with MBINCompiler",
description="Ticking this will mean MBINCompiler will not be run on "
"the produced files. This will leave a mix of .mbin and "
".exml files. This is generally not recommended unless "
"you are trying to quickly test something as the .exml "
"files will be not formatted nicely.",
default=False,
)
idle_anim: StringProperty(
name="Idle animation name",
description="The name of the animation that is the idle animation.",
)
def execute(self, context):
set_import_export_defaults(self, context)
keywords = self.as_keywords()
keywords.pop('output_directory')
keywords.pop('export_directory')
keywords.pop('group_name')
keywords.pop('scene_name')
main_exporter = Exporter(self.output_directory, self.export_directory,
self.group_name, self.scene_name, keywords)
status = main_exporter.state
if status == {'FINISHED'}:
path = op.join(self.export_directory, self.group_name,
self.scene_name)
self.report({'INFO'}, f"Models Exported Successfully to {path}")
return status
# Operators to create various NMSDK objects
class CreateNMSDKScene(Operator):
"""Add the currently selected object to the NMSDK scene node. """
bl_idname = "nmsdk.create_root_scene"
bl_label = "Create empty NMSDK scene"
def execute(self, context):
empty_mesh = bpy.data.meshes.new('NMS_Scene')
empty_obj = bpy.data.objects.new('NMS_Scene', empty_mesh)
empty_obj.NMSNode_props.node_types = 'Reference'
# Set the empty object to have a 90 degree rotation around the x-axis
# to emulate the NMS coordinate system.
empty_obj.matrix_world = Matrix.Rotation(radians(90), 4, 'X')
bpy.context.scene.collection.objects.link(empty_obj)
bpy.context.view_layer.objects.active = empty_obj
bpy.ops.object.mode_set(mode='OBJECT')
return {'FINISHED'}
# Private operators for internal use
class _FixActionNames(Operator):
"""Fix any incorrect action names."""
bl_idname = "nmsdk._fix_action_names"
bl_label = "Fix any incorrect action names"
def _correct_name(self, action, obj_name):
""" Correct the name of an action if it needs to be... """
action_name = action.name.split('.')[0]
correct_name = '{0}.{1}'.format(action_name, obj_name)
if action.name != correct_name:
action.name = correct_name
def execute(self, context):
for obj in context.scene.objects:
obj_name = obj.name
for action in get_all_actions(obj):
self._correct_name(action[2], obj_name)
action[2].use_fake_user = True
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
class _FixOldFormat(Operator):
"""Change the type of node an object has"""
bl_idname = "nmsdk._fix_old_format"
bl_label = "Change NMS Node type"
def execute(self, context):
try:
context.scene.objects[
'NMS_SCENE'].NMSNode_props.node_types = 'Reference'
return {'FINISHED'}
except KeyError:
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
class _ImportReferencedScene(Operator):
"""Import a referenced scene into an existing node"""
bl_idname = "nmsdk._import_ref_scene"
bl_label = "Import referenced NMS scene file"
def execute(self, context):
obj = context.object
scene_path = obj.NMSReference_props.reference_path
if not scene_path:
# Can't import anything. Give up.
return {'FINISHED'}
if obj.children:
# It already has children. For now, do nothing...
return {'FINISHED'}
# If we get here, then the node has no children, and has a path to
# import. Try and do so...
# TODO: globalize the ref_scenes or the importer itself. This will
# allow the referenced scenes to be potentially taken from a previously
# imported scene.
PCBANKS_dir = context.scene.nmsdk_default_settings.PCBANKS_directory
full_path = op.join(PCBANKS_dir, scene_path)
importer = ImportScene(full_path, parent_obj=obj, ref_scenes=dict(),
settings={'clear_scene': False})
importer.render_scene()
return importer.state
class _ToggleCollisionVisibility(Operator):
"""Toggle whether the collision objects are visible or not"""
bl_idname = "nmsdk._toggle_collision_visibility"
bl_label = "Toggle collision visibility"
def execute(self, context):
nmsdk_settings = context.scene.nmsdk_settings
nmsdk_settings.toggle_collision_visibility()
# For every collision object in the scene, set its visibility to the
# value specified by the `show_collisions` button.
for obj in bpy.context.scene.objects:
if obj.NMSNode_props.node_types == 'Collision':
obj.hide_set(not nmsdk_settings.show_collisions)
return {'FINISHED'}
class _SaveDefaultSettings(Operator):
"""Save any default settings"""
bl_idname = "nmsdk._save_default_settings"
bl_label = "Save Settings"
def execute(self, context):
default_settings = context.scene.nmsdk_default_settings
default_settings.save()
# TODO: spawn a new thread which can display this then un-display it
# after some time...
# bpy.types.WorkSpace.status_text_set(text="Settings saved")
return {'FINISHED'}
class _GetPCBANKSFolder(Operator):
"""Select the PCBANKS folder location"""
# Code modified from https://blender.stackexchange.com/a/126596
bl_idname = "nmsdk._find_pcbanks"
bl_label = "Specify PCBANKS location"
# Define this to tell 'fileselect_add' that we want a directoy
directory: StringProperty(
name="PCBANKS path",
description="Location of the PCBANKS folder")
filter_folder: BoolProperty(default=True, options={'HIDDEN'})
def execute(self, context):
# Set the PCBANKS_directory value
context.scene.nmsdk_default_settings.PCBANKS_directory = self.directory
return {'FINISHED'}
def invoke(self, context, event):
# Open browser, take reference to 'self' read the path to selected
# file, put path in predetermined self fields.
# See:
# https://docs.blender.org/api/current/bpy.types.WindowManager.html#bpy.types.WindowManager.fileselect_add
self.directory = context.scene.nmsdk_default_settings.PCBANKS_directory
context.window_manager.fileselect_add(self)
# Tells Blender to hang on for the slow user input
return {'RUNNING_MODAL'}
class _RemovePCBANKSFolder(Operator):
"""Reset the PCBANKS folder location"""
bl_idname = "nmsdk._remove_pcbanks"
bl_label = "Remove PCBANKS location"
def execute(self, context):
# Set the PCBANKS_directory as blank
context.scene.nmsdk_default_settings.PCBANKS_directory = ""
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
class _GetMBINCompilerLocation(Operator):
"""Select the MBINCompiler executable location"""
# Code modified from https://blender.stackexchange.com/a/126596
bl_idname = "nmsdk._find_mbincompiler"
bl_label = "Specify MBINCompiler location"
filepath: StringProperty(
name="MBINCompiler Location",
description="Location of the MBINCompiler executable")
def execute(self, context):
# Set the PCBANKS_directory value
context.scene.nmsdk_default_settings.MBINCompiler_path = self.filepath
return {'FINISHED'}
def invoke(self, context, event):
# Open browser, take reference to 'self' read the path to selected
# file, put path in predetermined self fields.
# See:
# https://docs.blender.org/api/current/bpy.types.WindowManager.html#bpy.types.WindowManager.fileselect_add
self.directory = context.scene.nmsdk_default_settings.MBINCompiler_path
context.window_manager.fileselect_add(self)
# Tells Blender to hang on for the slow user input
return {'RUNNING_MODAL'}
class _RemoveMBINCompilerLocation(Operator):
"""Reset the MBINCompiler executable location"""
bl_idname = "nmsdk._remove_mbincompiler"
bl_label = "Remove MBINCompiler location"
def execute(self, context):
# Set the PCBANKS_directory as blank
context.scene.nmsdk_default_settings.MBINCompiler_path = ""
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
# Animation classes and functions
# TODO: move...
def get_loaded_anim_names(self, context):
try:
names = context.scene.nmsdk_anim_data.loadable_anim_data.keys()
# Only show the names of animations that haven't been loaded
return list(tuple([name] * 3) for name in names if name not in
context.scene.nmsdk_anim_data.loaded_anims)
except KeyError:
return [('None', 'None', 'None')]
def get_anim_names(self, context):
try:
names = context.scene.nmsdk_anim_data.loaded_anims
return list(tuple([name] * 3) for name in names)
except KeyError:
return [('None', 'None', 'None')]
def get_anim_names_not_none(self, context):
try:
# make a copy of the names just to be safe
names = list(context.scene.nmsdk_anim_data.loaded_anims)
if 'None' in names:
names.remove('None')
if names:
return list(tuple([name] * 3) for name in names)
return [('None', 'None', 'None')]
except KeyError:
return [('None', 'None', 'None')]
class AnimProperties(PropertyGroup):
anims_loaded: BoolProperty(
name='Animations loaded',
description='Whether the animations are loaded or not',
default=True)
has_bound_mesh: BoolProperty(
name='Has bound mesh',
description='Whether or not the mesh of the object is bound to bones',
default=False)
idle_anim: EnumProperty(
name='Idle animation',
description='Animation that is played idly',
items=get_anim_names_not_none)
# key: name of animation
# value: path to animation data
loadable_anim_data = dict()
# names of loaded animations. Instantiate with the default 'None'
loaded_anims = ['None']
# List of joint names
joints = list()
def reset(self):
""" Reset all the values back to their original ones. """
self.anims_loaded = False
self.loadable_anim_data.clear()
self.loaded_anims.clear()
self.loaded_anims.extend(['None'])
self.joints.clear()
class _RefreshAnimations(Operator):
"""Refresh the animation data"""
bl_idname = "nmsdk._refresh_anim_list"
bl_label = "Refresh Animation List"
def execute(self, context):
# Set the variables
actions = get_all_actions_in_scene(context.scene)
if len(actions) != 0:
for action in actions:
if action not in context.scene.nmsdk_anim_data.loaded_anims:
context.scene.nmsdk_anim_data.loaded_anims.append(action)
context.scene.nmsdk_anim_data.anims_loaded = True
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
class _LoadAnimation(Operator):
"""Load the selected animation data"""
bl_idname = "nmsdk._load_animation"
bl_label = "Load Animation"
loadable_anim_name: EnumProperty(
name='Available animations',
description='List of all available animations for the scene',
items=get_loaded_anim_names)
def execute(self, context):
# Set the variables
loadable_anim_names = context.scene.nmsdk_anim_data.loadable_anim_data
anim_name = self.loadable_anim_name
anim_data = loadable_anim_names.pop(anim_name)
bpy.ops.nmsdk.animation_handler(
anim_name=anim_name,
anim_path=anim_data['Filename'])
# Set the current animation as the one we just selected
bpy.ops.nmsdk._change_animation(anim_names=anim_name)
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
class _ChangeAnimation(Operator):
"""Change the currently selected animation"""
bl_idname = "nmsdk._change_animation"
bl_label = "Change Animation"
anim_names: EnumProperty(
name='Available animations',
description='List of all available animations for the scene',
items=get_anim_names)
def execute(self, context):
"""Set every node in the scene to have the appropriate action.
If the node is not animated in the current animation then set its
action to None.
"""
context.scene['curr_anim'] = self.anim_names
# If the selected animation is none, reset everything to base.
if self.anim_names == 'None':
for armature in bpy.data.armatures:
armature.pose_position = 'REST'
for obj in bpy.data.objects:
if obj.animation_data:
obj.animation_data.action = None
for track in obj.animation_data.nla_tracks:
track.mute = True
context.scene.frame_end = 0
return {'FINISHED'}
frame_count = 0
for armature in bpy.data.armatures:
armature.pose_position = 'POSE'
# Apply the action to each object
for obj in context.scene.objects:
action_name = '{0}.{1}'.format(self.anim_names, obj.name)
if action_name in bpy.data.actions:
obj.animation_data.action = bpy.data.actions[action_name]
frame_count = max(frame_count,
obj.animation_data.action.frame_range[1])
else:
# If the action doesn't exist, then the object isn't animated
if obj.animation_data is not None:
obj.animation_data.action = None
# Set the final frame count
context.scene.frame_end = frame_count
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
class _PlayAnimation(Operator):
"""Play the currently selected animation"""
bl_idname = "nmsdk._play_animation"
bl_label = "Play"
def execute(self, context):
bpy.ops.screen.animation_play()
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
class _PauseAnimation(Operator):
"""Pause the currently playing animation"""
bl_idname = "nmsdk._pause_animation"
bl_label = "Pause"
def execute(self, context):
bpy.ops.screen.animation_cancel(restore_frame=False)
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
class _StopAnimation(Operator):
"""Stop the currently selected animation"""
bl_idname = "nmsdk._stop_animation"
bl_label = "Stop"
def execute(self, context):
bpy.ops.screen.animation_cancel()
bpy.ops.screen.frame_jump(end=False)
return {'FINISHED'}
def invoke(self, context, event):
return self.execute(context)
# Settings classes
class NMSDKSettings(PropertyGroup):
show_collisions: BoolProperty(
name='Draw collisions',
description='Whether or not to draw the collision objects.',
default=False)
def toggle_collision_visibility(self):
""" Toggle the collision visibility state. """
self.show_collisions = not self.show_collisions
class NMSDKDefaultSettings(PropertyGroup):
default_settings = read_settings()
export_directory: StringProperty(
name="Export Directory",
description="The base path under which all models will be exported.",
default=default_settings.get('export_directory', ""))
group_name: StringProperty(
name="Group Name",
description="Group name so that models that all belong in the same "
"folder are placed there (path becomes group_name/name)",
default=default_settings.get('group_name', ""))
PCBANKS_directory: StringProperty(
name="PCBANKS directory",
description="Path to the PCBANKS folder",
default=default_settings.get('PCBANKS_directory', ""))
MBINCompiler_path: StringProperty(
name="MBINCompiler location",
description="Path to the Mbincompiler executable",
default=default_settings.get('MBINCompiler_path', ""))
def save(self):
""" Save the current settings. """
settings = {'export_directory': self.export_directory,
'group_name': self.group_name,
'PCBANKS_directory': self.PCBANKS_directory,
'MBINCompiler_path': self.MBINCompiler_path}
write_settings(settings)
# Operators to be added to the blender UI for various tasks
class NMS_Export_Operator(Operator, ExportHelper):
"""Export scene to NMS compatible files"""
# important since its how bpy.ops.import_test.some_data is constructed
bl_idname = "export_mesh.nms"
bl_label = "Export to NMS XML Format"
filepath: StringProperty(
name="File Path",
description="Filepath used for exporting the file",
maxlen=1024,
subtype='FILE_PATH',
)
export_directory: StringProperty(
name="Export Directory",
description="The base path relative to the PCBANKS folder under which "
"all models will be exported.",
default="CUSTOMMODELS",
)
group_name: StringProperty(
name="Group Name",
description="Group name so that models that all belong in the same "
"folder are placed there (path becomes group_name/name)",
)
preserve_node_info: BoolProperty(
name="Preserve Node Info",
description="If the exported scene was originally imported, preserve "
"the details of any nodes that were in the original scene."
"\nNote that this will not currently export any geometry "
"data, so adding mesh collisions to an existing scene is "
"not currently possible.",
default=False,
)
reexport_geometry: BoolProperty(
name="Re-export geometry data",
description="(BETA) Whether or not to re-export the geometry data for "
"the current scene. This is not guaranteed to be the same "
"as the source geometry, however it should be sufficiently"
" close.",
)
AT_only: BoolProperty(
name="ActionTriggers Only",
description="If this box is ticked, all the action trigger data will "
"be exported directly to an ENTITY file in the specified "
"location with the project name. Anything else in the "
"project is ignored",
default=False,
)
no_vert_colours: BoolProperty(
name="Don't export vertex colours",
description="Ticking this box will force vertex colours to not be "
"exported. Use this if you have accidentally added vertex "
"colours to a mesh and don't know how to get rid of them.",
default=False,
)
no_convert: BoolProperty(
name="Don't convert files with MBINCompiler",
description="Ticking this will mean MBINCompiler will not be run on "
"the produced files. This will leave a mix of .mbin and "
".exml files. This is generally not recommended unless "
"you are trying to quickly test something as the .exml "
"files will be not formatted nicely.",
default=False,
)
idle_anim: StringProperty(
name="Idle animation name",
description="The name of the animation that is the idle animation.",
)
export_anims: BoolProperty(
name="Export animations",
description="Whether to export animations",
)
use_shared_textures: BoolProperty(
name="Use shared textures location",
description="If True, then all exported textures will be placed in a "
"folder in the same location as the CUSTOMMODELS folder "
"called 'CUSTOMTEXTURES'. Otherwise the textures will be "
"located in a TEXTURES folder within the scene folder.",
default=False,
)
shared_texture_folder: StringProperty(
name="Shared texture folder",
description="The base path relative to the PCBANKS folder under which "
"all textures will be exported.",
default="CUSTOMTEXTURES",
)
overwrite_textures: BoolProperty(
name="Overwrite existing textures",
description="If selected then force the exporter to overwrite any "
"existing .DDS files\nthat are created during the export "
"process.",
default=False,
)
rename_textures: BoolProperty(
name="Rename textures based on material name",
description="If selected then all textures are renamed when moved/"
"converted\nto be in the form "
"{material name}.{texture type}.DDS\n"
"(Only for masks and normals)",
default=False,
)
# ExportHelper mixin class uses this.
filename_ext = ""
# Track whether some values have been read from the settings file to avoid
# constantly reading from them.
settings_loaded = False
def invoke(self, context, _event):
""" Override the default behavior so that we can provide a scene
file name automatically. """
if bpy.context.scene.get('scene_node'):
scene_node = bpy.context.scene.get('scene_node')
scene_name = op.basename(scene_node['imported_from'])
self.filepath = scene_name
return super().invoke(context, _event)
def draw(self, context):
set_import_export_defaults(self, context)
layout = self.layout
layout.prop(self, 'export_directory')
layout.prop(self, 'group_name')
layout.prop(self, 'preserve_node_info')
if self.preserve_node_info:
layout.prop(self, 'reexport_geometry')
layout.prop(self, 'AT_only')
layout.prop(self, 'no_vert_colours')
layout.prop(self, 'no_convert')
# Texture settings
textures_box = layout.box()
textures_box.label(text='Materials')
textures_box.prop(self, 'use_shared_textures')
if self.use_shared_textures:
textures_box.prop(self, 'shared_texture_folder')
textures_box.prop(self, 'overwrite_textures')
textures_box.prop(self, 'rename_textures')
# Animation settings
animations_box = layout.box()
animations_box.label(text='Animations')
animations_box.prop(self, 'export_anims')
if self.export_anims:
animations_box.prop(self, 'idle_anim')
def execute(self, context):
keywords = self.as_keywords()
# Split the filepath provided as the final part is the name of the file
export_path, scene_name = op.split(self.filepath)
keywords.pop('export_directory')
keywords.pop('group_name')
if not bpy.context.scene.nmsdk_default_settings.MBINCompiler_path:
ShowMessageBox("No MBINCompiler specified or found", "Error",
'ERROR')
print("[ERROR]: No MBINCompiler specified or found")
return {'CANCELLED'}
main_exporter = Exporter(export_path, self.export_directory,
self.group_name, scene_name, keywords)
status = main_exporter.state
if main_exporter.warnings:
invalid_lights = main_exporter.warnings.get('light_is_mesh', [])
invalid_lights_msg = ('The following lights are meshes: '
f'{", ".join(invalid_lights)}')
self.report({'WARNING'}, invalid_lights_msg)
print(invalid_lights_msg)
if status == {'FINISHED'}:
self.report({'INFO'}, "Models Exported Successfully")
return status
class NMS_Import_Operator(Operator, ImportHelper):
"""Import NMS Scene files"""
# important since its how bpy.ops.import_test.some_data is constructed
bl_idname = "import_mesh.nms"
bl_label = "Import from SCENE file"
# ImportHelper mixin class uses this
filename_ext = ".EXML"
filter_glob: StringProperty(
default="*.scene.exml;*.SCENE.EXML;*.scene.mbin;*.SCENE.MBIN",
options={"HIDDEN"})
clear_scene: BoolProperty(
name='Clear scene',
description='Whether or not to clear the currently exiting scene in '
'blender.',
default=True)
import_recursively: BoolProperty(
name='Import recursively',
description='Whether or not to import reference nodes automatically.\n'
'For large scenes with many referenced scenes it is better'
' to set this as False to avoid long wait times, and then '
'only import the scenes you want after it has loaded.',
default=True)
# Collision related properties
import_collisions: BoolProperty(
name='Import collisions',
description='Whether or not to import the collision objects.',
default=True)
show_collisions: BoolProperty(
name='Draw collisions',
description='Whether or not to draw the collision objects.',
default=False)
# Animation related properties
import_bones: BoolProperty(
name='Import bones',
description="Whether or not to import the models' bones",
default=False)
import_anims: BoolProperty(
name='Import animations',
description='Whether or not to import animations for this scene',
default=False)
max_anims: IntProperty(
name='Max loaded animations',
description='Maximum number of animations to load. To Disable loading '
'animations set this to 0, or to force loading all set '
'this to -1',
default=10,
soft_min=-1)
# Extra "Debug" options
draw_hulls: BoolProperty(
name='Draw bounded hulls',
description='Whether or not to draw the points that make up the '
'bounded hulls of the materials. This is only for research'
'/debugging, so can safely be left as False.',
default=False,
)
draw_bounding_box: BoolProperty(
name='Draw bounding boxes',
description='Whether to draw the bounding boxes for each mesh',
default=False,
)
def draw(self, context):
layout = self.layout
layout.prop(self, 'clear_scene')
layout.prop(self, 'import_recursively')
coll_box = layout.box()
coll_box.label(text='Collisions')
coll_box.prop(self, 'import_collisions')
coll_box.prop(self, 'show_collisions')
animation_box = layout.box()
animation_box.label(text='Animation')
animation_box.prop(self, 'import_bones')
animation_box.prop(self, 'import_anims')
if self.import_anims:
animation_box.prop(self, 'max_anims')
debug_box = layout.box()
debug_box.label(text='Debug')
debug_box.prop(self, 'draw_hulls')
debug_box.prop(self, 'draw_bounding_box')
def execute(self, context):
keywords = self.as_keywords()
# set the state of the show_collisions button from the value specified
# when the import occurs
context.scene.nmsdk_settings.show_collisions = self.show_collisions
# Reset the animation data
context.scene.nmsdk_anim_data.reset()
fdir = self.properties.filepath
context.scene['_anim_names'] = ['None']
print(fdir)
if not bpy.context.scene.nmsdk_default_settings.MBINCompiler_path:
ShowMessageBox("No MBINCompiler specified or found", "Error",
'ERROR')
print("[ERROR]: No MBINCompiler specified or found")
return {'CANCELLED'}
importer = ImportScene(fdir, parent_obj=None, ref_scenes=dict(),
settings=keywords)
importer.render_scene()
status = importer.state
self.report({'INFO'}, "Models Imported Successfully")
print('Scene imported!')
if status:
return {'FINISHED'}
else:
return {'CANCELLED'}