Skip to content
On this page

Python Examples ​

For full control of the Simplygon processing of a set of actors in an Unreal scene, you can export the scene from Unreal into a Simplygon scene, and then have access to the full suite of tools in Simplygon for processing. The processed scene can later be imported into Unreal, either as a replacement of the original, or as e.g. 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_subsystem_examples.py

Note

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

Synchronous Export Example ​

Export example for exporting selected Actors into a Simplygon .sgscene file, and waiting for the call to finish before continuing.

python
# Export example for exporting selected Actors.
def export_example() :
    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)

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

    # Set export settings to use.
    export_settings = unreal.SgSceneExportSettings()
    export_settings.overwrite_existing_file = True
    export_settings.material_export_settings.export_baked_materials = True

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

    # Setup material properties to export
    if export_settings.material_export_settings.export_baked_materials:
        material_properties = export_settings.material_export_settings.material_properties

        material_properties.export_ambient_occlusion = False
        material_properties.ambient_occlusion_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_base_color = True
        material_properties.base_color_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_specular = True
        material_properties.specular_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_roughness = True
        material_properties.roughness_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_metallic = True
        material_properties.metallic_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_emissive_color = False
        material_properties.emissive_color_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_opacity = False
        material_properties.opacity_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_opacity_mask = False
        material_properties.opacity_mask_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_normal = True
        material_properties.normal_texture_dimensions = unreal.IntPoint(1024,1024)

    # Call export function.
    export_task_id = simplygon_subsystem.export_actors_to_scene(export_file_path, selected_actors, export_settings)

    # Fetch the result (which will wait for the result).
    export_result = simplygon_subsystem.retrieve_export_actors_to_scene_result(export_task_id)

    # Log result.
    if export_result.has_error :
        unreal.log_error(f'Export error: {export_result.error_message}')
    else :
        unreal.log(f'Export result: Scene written to file {export_result.output_file_path}')
    
    return export_result

Asynchronous Export Example ​

Export example for exporting selected Actors into a Simplygon .sgscene file using a callback to report when the export process has finished. Note that the code still waits for the processing to finish using the simplygon_subsystem.wait_for_all_tasks_to_complete(), but this is not needed, if you have other assets to export or other processings or tasks to run.

python
# Export example for exporting selected Actors using a callback.
def export_example_using_callback() :
    # Create callback.
    def on_export_completed(export_result):   
        unreal.log(f'Simplygon export task using subsystem callback completed.')

        # Log result.
        if export_result.has_error :
            unreal.log_error(f'Export error: {export_result.error_message}')
        else :
            unreal.log(f'Export result: Scene written to file {export_result.output_file_path}')
        
        # Remove callback from subsystem. Specific to these examples.
        # Not necessary to add & remove callbacks for each import/export.
        simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)
        simplygon_subsystem.on_export_actors_to_scene_completed.clear()

    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)

    # Add callback to subsystem.
    simplygon_subsystem.on_export_actors_to_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()

    # Set export settings to use.
    export_settings = unreal.SgSceneExportSettings()
    export_settings.overwrite_existing_file = True
    export_settings.material_export_settings.export_baked_materials = True

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

    # Setup material properties to export
    if export_settings.material_export_settings.export_baked_materials:
        material_properties = export_settings.material_export_settings.material_properties

        material_properties.export_ambient_occlusion = False
        material_properties.ambient_occlusion_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_base_color = True
        material_properties.base_color_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_specular = True
        material_properties.specular_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_roughness = True
        material_properties.roughness_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_metallic = True
        material_properties.metallic_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_emissive_color = False
        material_properties.emissive_color_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_opacity = False
        material_properties.opacity_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_opacity_mask = False
        material_properties.opacity_mask_texture_dimensions = unreal.IntPoint(1024,1024)

        material_properties.export_normal = True
        material_properties.normal_texture_dimensions = unreal.IntPoint(1024,1024)

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

    # Optional wait for tasks to finish
    simplygon_subsystem.wait_for_all_tasks_to_complete()

Synchronous Import Example ​

Import example for importing a Simplygon .sgscene file into Unreal, and waiting for the call to finish before continuing.

python
# Import example spawning imported actors into the current editor world.
def import_example(import_scene_path, tex_coord_name = "") :
    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)

    # 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()
    if ( len(tex_coord_name) > 0 ) :
        import_settings.geometry_settings.tex_coord_names = [tex_coord_name]

    # Call import function
    import_task_id = simplygon_subsystem.import_scene_as_actors(import_scene_path, world, import_settings)

    # Fetch the result (which will wait for the result).
    import_result = simplygon_subsystem.retrieve_import_scene_as_actors_result(import_task_id)

    # Log result.
    if import_result.has_error :
        unreal.log_error(f'Import error: {import_result.error_message}')
    else :
        unreal.log(f'Import result: Num Actors imported {len(import_result.imported_actor_paths)}')
        unreal.log(f'Import result: Num meshes imported: {len(import_result.imported_meshes)}')
        unreal.log(f'Import result: Num materials imported: {len(import_result.imported_materials)}')
        unreal.log(f'Import result: Num textures imported: {len(import_result.imported_textures)}')
    
    return import_result

Asynchronous Import Example ​

Import example which imports a Simplygon .sgscene file into Unreal, and uses a callback to report when the import process has finished. Note that the code still waits for the processing to finish using the simplygon_subsystem.wait_for_all_tasks_to_complete(), but this is not needed, if you have other assets to import or other processings or tasks to run.

python
# Import example spawning imported actors into the current editor world using a callback.
def import_example_using_callback(import_scene_path, tex_coord_name) :
    # Create callback
    def on_import_completed(import_result):
        unreal.log(f'Simplygon import task using subsystem callback completed.')

        # Log result.
        if import_result.has_error :
            unreal.log_error(f'Import error: {import_result.error_message}')
        else :
            unreal.log(f'Import result: Num Actors imported {len(import_result.imported_actor_paths)}')
            unreal.log(f'Import result: Num meshes imported: {len(import_result.imported_meshes)}')
            unreal.log(f'Import result: Num materials imported: {len(import_result.imported_materials)}')
            unreal.log(f'Import result: Num textures imported: {len(import_result.imported_textures)}')

        # Remove callback from subsystem. Specific to these examples.
        # Not necessary to add & remove callbacks for each import/export.
        simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)
        simplygon_subsystem.on_import_scene_as_actors_completed.clear()

    simplygon_subsystem = unreal.get_editor_subsystem(unreal.SimplygonSubsystem)

    # Add callback to subsystem.
    simplygon_subsystem.on_import_scene_as_actors_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)

    # Optional wait for tasks to finish
    simplygon_subsystem.wait_for_all_tasks_to_complete()