Switching on Nanites using Python

Hi folks,

So I’ve got a python script that loads a bunch of .fbx models into UE5, each has an associated text file to indicate whether it should have nanites switched on.

My question is: for the ones that require it, how do I switch on nanite generation using python?

Thanks in advance.

1 Like

Because this thread show in Google Search with following keyword unreal engine python nanite, I will leave answer here for other to discover.

Here is my Python code to enable Nanite on StaticMesh.
This is the code that I write in UE5.0.3.

    static_mesh_editor_subsystem:unreal.StaticMeshEditorSubsystem = unreal.get_editor_subsystem(ue.StaticMeshEditorSubsystem)
    for static_mesh in static_mesh_list:
        nanite_setting:unreal.MeshNaniteSettings = static_mesh_editor_subsystem.get_nanite_settings(static_mesh)
        if nanite_setting.enabled:
            continue
        nanite_setting.enabled = True
        static_mesh_editor_subsystem.set_nanite_settings(static_mesh, nanite_setting, True)
1 Like

Let me improve it a bit for a ready script (also a fix that asset is saved after editing):

import unreal

static_mesh_editor_subsystem = unreal.get_editor_subsystem(
    unreal.StaticMeshEditorSubsystem)

# Change me!
USE_NANITES = False
asset_root_dir = '/Game/PROPS/'

asset_reg = unreal.AssetRegistryHelpers.get_asset_registry()

assets = asset_reg.get_assets_by_path(asset_root_dir, recursive=True)

static_mesh_data = {}
for asset in assets:
    if asset.asset_class == "StaticMesh":
        asset_path = asset.object_path
        asset_data = unreal.EditorAssetLibrary.find_asset_data(asset_path).get_asset()
        static_mesh_data[asset_path] = asset_data

for static_mesh_path, static_mesh in static_mesh_data.items():
    nanite_setting = static_mesh_editor_subsystem.get_nanite_settings(static_mesh)
    if nanite_setting.enabled == USE_NANITES:
        print("Already done", static_mesh)
        continue
    print("Enabing", static_mesh)

    nanite_setting.enabled = USE_NANITES
    static_mesh_editor_subsystem.set_nanite_settings(static_mesh, nanite_setting, True)

    unreal.EditorAssetLibrary.save_asset(static_mesh_path, False)