Download

How to import static mesh lods that are stored in seperate .fbx files via python

I have many meshes and their lod representation in seperate fbx files, e.g.
tile0_LOD0.fbx
tile0_LOD1.fbx
tile0_LOD2.fbx
etc.
How do I import the LOD1, LOD2 of tile0 via python?

Thank you in advance!

The way I do, needs the mesh to be imported in unreal first, then used as a LOD, Here you go



import unreal


class SetLOD(object):
  def __init__(self, lod_path, prefix_chars_rem, suffix_chars_rem, prefix_add, suffix_add, lod_index, use_mat):
    self.actors = (actor for actor in unreal.EditorUtilityLibrary.get_selected_asset_data())
    self.lod_path = lod_path
    self.prefix_chars_rem = prefix_chars_rem
    self.suffix_chars_rem = suffix_chars_rem
    self.prefix_add = prefix_add
    self.suffix_add = suffix_add
    self.lod_index = lod_index
    self.use_mat = use_mat
    try:
      assets = self.actors
      lod_path = self.lod_path
      prefix_chars_rem = self.prefix_chars_rem
      suffix_chars_rem = self.suffix_chars_rem
      prefix_add = self.prefix_add
      suffix_add =  self.suffix_add
      lod_index = self.lod_index
      use_mat = self.use_mat
      
      asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
      project_mats = asset_registry.get_assets(unreal.ARFilter(recursive_classes=True, class_names="Material", "MaterialInstance"]))

      has_ending = lod_path.endswith("/")
      if not has_ending:
        lod_path += "/"
      if lod_path is not "/":
        if lod_index is not "":
          for asset in assets:
            object_path = unreal.StructBase.get_editor_property(asset, 'object_path')
            package_name = str(unreal.StructBase.get_editor_property(asset, 'package_name'))
            package_path = unreal.StructBase.get_editor_property(asset, 'package_path')
            asset_name = str(unreal.StructBase.get_editor_property(asset, 'asset_name'))
            asset_class = unreal.StructBase.get_editor_property(asset, 'asset_class')
            if (asset_class == 'StaticMesh'):
              load_asset = unreal.EditorAssetLibrary.load_asset(object_path)
              sm_component = unreal.StaticMeshComponent()
              sm_component.set_static_mesh(load_asset)
              tmp_trim = asset_name
              if prefix_chars_rem is not '' and int(prefix_chars_rem) > 0:
                tmp_trim = asset_name(int(prefix_chars_rem))::]
              if suffix_chars_rem is not '' and int(suffix_chars_rem) > 0:
                tmp_trim = tmp_trim: - (int(suffix_chars_rem)):]
              tmp_trim = prefix_add + tmp_trim + suffix_add
              path_to_lod = lod_path + tmp_trim + "." + tmp_trim
              if unreal.EditorAssetLibrary.does_asset_exist(path_to_lod):
                lod_mesh = unreal.load_asset(path_to_lod)
                target_staticMesh = unreal.load_asset(object_path)
                unreal.EditorStaticMeshLibrary.set_lod_from_static_mesh(target_staticMesh, int(lod_index), lod_mesh, 0, bool(use_mat))
                mat_slot_names = unreal.StaticMeshComponent.get_material_slot_names(sm_component)
                print(mat_slot_names)
                if bool(use_mat):
                  for mat in mat_slot_names:
                    for mats in project_mats:
                      mats_name = unreal.StructBase.get_editor_property(mats, 'asset_name')
                      if mats_name is mat:
                        target_staticMesh.set_material(int(lod_index), mats.get_asset())
                unreal.EditorAssetLibrary.save_loaded_asset(target_staticMesh)
    except Exception as error:
      print(error)




2 Likes