Skip to content

Commit

Permalink
allow importing fgm using the contextual menu in the browser window
Browse files Browse the repository at this point in the history
Moved classes to the operators module
  • Loading branch information
ilodev committed Oct 20, 2023
1 parent c312d46 commit 5a81a59
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
40 changes: 14 additions & 26 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
from plugin.utils.matrix_util import handle_errors

from plugin.modules_import.operators import ImportBanis, ImportManis, ImportMatcol, ImportFgm, ImportMS2, ImportSPL, \
ImportVoxelskirt
from plugin.modules_export.operators import ExportMS2, ExportSPL, ExportManis, ExportBanis
ImportVoxelskirt, ImportMS2FromBrowser, ImportFGMFromBrowser

from plugin import import_ms2
from plugin.modules_export.operators import ExportMS2, ExportSPL, ExportManis, ExportBanis

from root_path import root_dir
from generated.formats.ms2.enums.MeshFormat import MeshFormat
Expand Down Expand Up @@ -123,24 +122,6 @@ def execute(self, context):
return {'FINISHED'}


#
class WM_OT_button_import_ms2(bpy.types.Operator):
"""Imports ms2 content as new scenes from the file browser"""
bl_idname = "ct_wm.import_ms2"
bl_label = "Import ms2"

@classmethod
def poll(cls, context):
return context.active_object is not None

def execute(self, context):
folder = context.space_data.params.directory.decode('ascii')
file = context.space_data.params.filename
filepath = os.path.join(folder, file).replace("\\","/")
print("Importing: " + filepath)
handle_errors(self, import_ms2.load, { 'filepath': filepath } )
return {'FINISHED'}

class CreateFins(bpy.types.Operator):
"""Create fins for all objects with shells in this scene, and overwrite existing fin geometry"""
bl_idname = "object.create_fins"
Expand Down Expand Up @@ -297,10 +278,16 @@ def CT_FileBrowser_Context_Menu(self, context):
filepath = os.path.join(folder, file)
fileext = os.path.splitext(file)[1]

if os.path.isfile(filepath) and fileext.lower() == ".ms2":
layout = self.layout
layout.separator()
layout.operator(WM_OT_button_import_ms2.bl_idname)
if os.path.isfile(filepath):
if fileext.lower() == ".ms2":
layout = self.layout
layout.separator()
layout.operator(ImportMS2FromBrowser.bl_idname)

if fileext.lower() == ".fgm":
layout = self.layout
layout.separator()
layout.operator(ImportFGMFromBrowser.bl_idname)


from plugin.addon_updater_ops import classes as updater_classes
Expand All @@ -312,6 +299,8 @@ def CT_FileBrowser_Context_Menu(self, context):
ImportFgm,
ImportMS2,
ImportSPL,
ImportMS2FromBrowser,
ImportFGMFromBrowser,
ExportMS2,
ExportSPL,
ExportBanis,
Expand All @@ -328,7 +317,6 @@ def CT_FileBrowser_Context_Menu(self, context):
MESH_PT_CobraTools,
SCENE_PT_CobraTools,
InstallDependencies,
WM_OT_button_import_ms2,
*updater_classes
)

Expand Down
7 changes: 4 additions & 3 deletions plugin/import_fgm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def load(filepath):
b_mat = create_material(in_dir, material)

if o_mat:
print('material exists')
# the material already existed, we have just updated, make
# sure we update all objects using it
for obj in bpy.data.objects:
Expand All @@ -37,9 +36,11 @@ def load(filepath):

# I think this should only be appended if the material didn't really exist before
# for this object?
# todo: decide what to do with the selected object and the new material
if not object_has_material(bpy.context.object, material):
b_me = bpy.context.object.data
b_me.materials.append(b_mat)
#b_me = bpy.context.object.data
#b_me.materials.append(b_mat)
pass

messages.add(f"Finished fgm import")
return messages
37 changes: 37 additions & 0 deletions plugin/modules_import/operators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import bpy.utils.previews
from bpy.props import StringProperty, BoolProperty, IntProperty, FloatProperty, EnumProperty, CollectionProperty
from bpy_extras.io_utils import ImportHelper
Expand Down Expand Up @@ -110,3 +111,39 @@ class ImportVoxelskirt(bpy.types.Operator, ImportHelper):
def execute(self, context):
keywords = self.as_keywords(ignore=("axis_forward", "axis_up", "filter_glob"))
return handle_errors(self, import_voxelskirt.load, keywords)


class ImportMS2FromBrowser(bpy.types.Operator):
"""Imports ms2 content as new scenes from the file browser"""
bl_idname = "ct_wm.import_ms2"
bl_label = "Import ms2"

@classmethod
def poll(cls, context):
return context.active_object is not None

def execute(self, context):
folder = context.space_data.params.directory.decode('ascii')
file = context.space_data.params.filename
filepath = os.path.join(folder, file).replace("\\","/")
print("Importing: " + filepath)
handle_errors(self, import_ms2.load, { 'filepath': filepath } )
return {'FINISHED'}

class ImportFGMFromBrowser(bpy.types.Operator):
"""Imports fgm as a new material from the file browser"""
bl_idname = "ct_wm.import_fgm"
bl_label = "Import fgm"

@classmethod
def poll(cls, context):
return context.active_object is not None

def execute(self, context):
folder = context.space_data.params.directory.decode('ascii')
file = context.space_data.params.filename
filepath = os.path.join(folder, file).replace("\\","/")
print("Importing: " + filepath)
handle_errors(self, import_fgm.load, { 'filepath': filepath } )
return {'FINISHED'}

0 comments on commit 5a81a59

Please sign in to comment.