Skip to content

Python Examples ​

For full control of the Simplygon workflow you can export a set of meshes (actors, components and static meshes) directly from Unreal Engine into a Simplygon scene, and then have access to the full suite of tools in Simplygon for processing. The processed Simplygon scene can be imported into Unreal (as actors or static meshes), that can be used either as a replacement of the original, a combined static mesh which can be hooked up as a stand in runtime replacement, or as hierarchical LOD replacement. In this section, we present a few short examples of how to export a scene from Unreal to Simplygon, or import a scene into Unreal from Simplygon.

The examples are located in:

  • Content → Python → simplygon_export_import_examples.py
  • Content → Python → simplygon_merge_actors_example.py

The managed-scene examples in simplygon_export_import_examples.py export geometry, capture material channels or append landscape clipping geometry, and save one scene. See Managed Scene Export and Capture for the settings and data conventions used by these workflows.

Note

The Simplygon Subsystem is an Editor Subsystem. This means it is only available for Editor Utility Blueprints or Editor Utility Widgets.

Export Examples ​

Export example for exporting selected Actors into a Simplygon .sgscene file using a callback to report when the export process has finished.

Actors ​

py
# Export example for exporting selected Actors using a callback.
def export_actors_example():
    """Example for exporting actors from a level and get the result via a callback."""
    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)
    simplygon_subsystem.on_export_scene_completed.add_callable(on_export_completed)

    # Get actors to export.
    EAS = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
    selected_actors = EAS.get_selected_level_actors()

    # The target file path of where the exported Simplygon scene will be saved.
    export_file_path = (
        unreal.Paths.project_intermediate_dir()
        + "/Simplygon/"
        + "PythonExportActorsExampleCallback.sgscene"
    )

    # Call export function which will call the callback once it's finished.
    simplygon_subsystem.export_actors_to_scene(
        export_file_path, selected_actors, get_default_scene_export_settings(True)
    )

Static Mesh Components ​

py
# Export example for exporting Static Mesh Components using a callback.
def export_components_example(add_components):
    """Example for exporting static mesh components and get the result via a callback."""
    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)
    simplygon_subsystem.on_export_scene_completed.add_callable(on_export_completed)

    static_mesh_components = []
    if add_components:
        static_mesh_components = setup_components()

    # The target file path of where the exported Simplygon scene will be saved.
    export_file_path = (
        unreal.Paths.project_intermediate_dir()
        + "/Simplygon/"
        + "PythonExportComponentsExampleCallback.sgscene"
    )

    simplygon_subsystem.export_static_mesh_components_to_scene(
        export_file_path,
        static_mesh_components,
        get_default_scene_export_settings(True),
    )

Static Mesh ​

py
# Export example for exporting Static Mesh using a callback.
def export_mesh_example(setup_mesh, mesh_path):
    """Example for exporting a mesh and get the result via a callback."""
    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)
    simplygon_subsystem.on_export_scene_completed.add_callable(on_export_completed)

    mesh_to_export = None
    if setup_mesh:
        mesh_to_export = get_static_mesh_from_path(mesh_path)

    # The target file path of where the exported Simplygon scene will be saved.
    export_file_path = (
        unreal.Paths.project_intermediate_dir()
        + "/Simplygon/"
        + "PythonExportMeshExampleCallback.sgscene"
    )

    simplygon_subsystem.export_static_mesh_to_scene(
        export_file_path, mesh_to_export, get_default_mesh_export_settings(True)
    )

Managed Scene Capture ​

Select one or more actors with static mesh components, then call managed_scene_capture_selected_components_example(). Pass include_landscape_clipping=True to also extract intersecting landscape geometry and append it to ClippingGeometrySet before camera generation and capture. Only landscape proxies with Use Landscape for Culling Invisible HLOD Vertices (bUseLandscapeForCullingInvisibleHLODVertices) enabled contribute geometry.

python
# Export and capture the selected components.
managed_scene_capture_selected_components_example()

# Also append intersecting landscape clipping geometry before capture.
managed_scene_capture_selected_components_example(include_landscape_clipping=True)

The workflow retains its context and components, filters shared completion delegates by job ID, and writes the resulting scene to the project's Intermediate/Simplygon directory. When processing a scene containing the optional landscape geometry, enable clipping geometry and use ClippingGeometrySet as the clipping geometry selection-set name.

Import Examples ​

Import example which imports a Simplygon .sgscene file into Unreal, and uses a callback to report when the import process has finished.

Actors ​

py
# Import example spawning imported actors into the current editor world using a callback.
def import_actors_example(import_scene_path, tex_coord_name):
    """Import example spawning imported actors into the current editor world using a callback."""
    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)
    simplygon_subsystem.on_import_scene_completed.add_callable(on_import_completed)

    # Get world to spawn imported actors into.
    editor_subsystem = unreal.get_editor_subsystem(unreal.UnrealEditorSubsystem)
    world = editor_subsystem.get_editor_world()

    # Set import settings to use.
    import_settings = unreal.SgSceneImportSettings()
    import_settings.geometry_settings.tex_coord_names = [tex_coord_name]

    # Call import function which will call the callback once it's finished.
    simplygon_subsystem.import_scene_as_actors(
        import_scene_path, world, import_settings
    )

Static Meshes ​

py
# Import example for importing a scene as static meshes using a callback.
def import_meshes_example(import_scene_path, tex_coord_name):
    """Import example that imports meshes as static meshes."""
    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)
    simplygon_subsystem.on_import_scene_completed.add_callable(on_import_completed)

    # Set import settings to use.
    import_settings = unreal.SgSceneImportSettings()
    import_settings.geometry_settings.tex_coord_names = [tex_coord_name]

    # Call import function which will call the callback once it's finished.
    simplygon_subsystem.import_scene_as_static_meshes(
        import_scene_path, import_settings
    )

Merge Actor Examples ​

An example workflow for scripting a merging tool can be found in the simplygon_merge_actors_example.py. It runs the full workflow, starting at exporting the selected actors, then running a Simplygon pipeline and finally importing the processed result. The example is quite extensive and it is therefore only the starting points that are added as examples below.

Merging using aggregation ​

py
def merge_selected_actors(bakeMaterials: bool = False):
    merge_tool = SgMergeActorsTool()
    merge_tool.merge_selected_actors(EMergeType.AGGREGATION, bakeMaterials)

Merging using remeshing for proxies in the distance ​

py
def create_proxy_actor_from_selected_actors():
    merge_tool = SgMergeActorsTool()
    merge_tool.merge_selected_actors(EMergeType.REMESHING, True)