Skip to content
On this page

LOD Recipe: Python scripting

The Simplygon Unreal Engine plugin has a Blueprint library of functions which gives you the possibility to run Simplygon processes using scripts. This guide will showcase a few examples on how to use these.

TIP

The plugin provides a helper script with python functions for doing Simplygon processing related tasks. You can find it under Content → Python → simplygon_helpers.py.

Basic LOD Recipe usage

A simple script creating a new LOD Recipe, assigns a few meshes to it, builds the LOD Recipe for each mesh and then saves everything.

python
# Get Simplygon Blueprint Library
SBL = unreal.SimplygonBlueprintLibrary

# Create a LOD Recipe in the /Game/LODRecipes folder
lr_small_props = unreal.AssetToolsHelpers.get_asset_tools().create_asset(
    asset_name="LR_SmallProps",
    package_path="/Game/LODRecipes",
    asset_class=unreal.LODRecipe,
    factory=unreal.LODRecipeFactory(),
)

# Set number of LODs
lr_small_props.set_editor_property("num_lo_ds", 3)

# Get the LOD pipelines
pipelines = lr_small_props.get_editor_property("per_lod_pipeline_settings")

# Create LOD 1 pipeline: Reduction with an 88% triangle ratio reduction target
reduction_pipeline = unreal.ReductionPipeline()
reduction_pipeline_settings = reduction_pipeline.get_editor_property("settings")
reduction_settings = reduction_pipeline_settings.get_editor_property(
    "reduction_settings"
)
reduction_settings.set_editor_property("reduction_target_triangle_ratio", 0.88)
pipelines[1] = reduction_pipeline

# Create LOD 2 pipeline: Remeshing with a screen size of 100px
remeshing_pipeline = unreal.RemeshingPipeline()
remeshing_pipeline_settings = remeshing_pipeline.get_editor_property("settings")
remeshing_settings = remeshing_pipeline_settings.get_editor_property(
    "remeshing_settings"
)
remeshing_settings.set_editor_property("on_screen_size", 100)
pipelines[2] = remeshing_pipeline

# Load the meshes you want to link with the recipe, assign them and build
asset_paths = ["/Game/Stone001", "/Game/Stone002", "/Game/Stone003"] # Example paths
for asset_path in asset_paths:
    asset = unreal.load_asset(asset_path)
    SBL.assign_recipe_to_asset(lr_small_props, asset)
    SBL.build_lod_recipe(asset)

# Save all dirty packages (LOD Recipe and assets)
unreal.EditorLoadingAndSavingUtils.save_dirty_packages(True, True)

Build LOD Recipes

The following script showcases a few different ways to build LODs for your meshes that have a LOD Recipe assigned.

python
# Get Simplygon Blueprint Library
SBL = unreal.SimplygonBlueprintLibrary

# Build LODs for all meshes in the project that have a LOD Recipe assigned 
SBL.build_all_lod_recipes()

# Filtered versions of the above method, building LODs for all static or 
# skeletal meshes in the project that have a LOD Recipe assigned
SBL.build_lod_recipes_only_for_static_meshes()
SBL.build_lod_recipes_only_for_skeletal_meshes()

# Build a single mesh that have a LOD Recipe assigned
asset_path = "/Game/Bench001" # Example path
asset = unreal.load_asset(asset_path)
SBL.build_lod_recipe(asset)

Assign LOD Recipe to all meshes in a folder

Following is an example script of how to assign a LOD Recipe to all meshes in a folder.

python
# Get Simplygon Blueprint Library
SBL = unreal.SimplygonBlueprintLibrary

# Load LOD Recipe
lod_recipe_path = "/Game/LR_LargeProps"  # Example path
lod_recipe_asset = unreal.load_asset(lod_recipe_path)

# Retrieve all the assets in the chosen folder
package_path = "/Game/FolderName"  # Example path
recursive = True  # Whether subfolders should be included or not
asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
assets_list = asset_registry.get_assets_by_path(package_path, recursive)

# Filter the assets and only get static and/or skeletal meshes
assign_all_static_meshes = True
assign_all_skeletal_meshes = True
meshes = []
if assign_all_static_meshes:
    static_path = unreal.TopLevelAssetPath("/Script/Engine", "StaticMesh")
    meshes.extend([x for x in assets_list if x.asset_class_path == static_path])
if assign_all_skeletal_meshes:
    skeletal_path = unreal.TopLevelAssetPath("/Script/Engine", "SkeletalMesh")
    meshes.extend([x for x in assets_list if x.asset_class_path == skeletal_path])

# Assign the LOD Recipe to the assets
for mesh in meshes:
    SBL.assign_recipe_to_asset(lod_recipe_asset, mesh.get_asset())

# Save the assets afterwards
unreal.EditorLoadingAndSavingUtils.save_dirty_packages(True, True)