Skip to content
On this page

Reduction using ReductionPipeline

This section include an example of how to use the Simplygon Python API to optimize the scene. A pipeline encapsulates a certain functionality and exposes settings which can be tweaked for the expected result. A Pipeline can also be executed internally or externally. If it is to be executed externally the BatchProcessor has to be specified in the pipeline (through SetSimplygonBatchPath).

Note that some of these API examples can be done using Pipelines, if some features are not exposed in Pipelines or that some custom behavior is required then Simplygon API is the way to go.

The first part exports the selected Max scene to file. The second part loads a pre-defined reduction pipeline from file and optimizes the previously saved scene. The third part imports the processed scene back into Max. The export and import could be done manually if preferred.

python
from pymxs import runtime as rt
from simplygon10 import simplygon_loader
from simplygon10 import Simplygon
import gc

def run_pipeline(sg):
    # Set Max's handedness setting
    sg.SetGlobalDefaultTangentCalculatorTypeSetting(Simplygon.ETangentSpaceMethod_Autodesk3dsMax)

    # Create pipeline serializer
    sgSerializer = sg.CreatePipelineSerializer()
    if sgSerializer.IsNull():
        print('Failed to create pipeline serializer.')
        exit(-100)

    # Load pipeline settings from file
    sgPipeline = sgSerializer.LoadPipelineFromFile('D:/Pipelines/Reduction.json')
    if sgPipeline.IsNull():
    print('Invalid pipeline definition file')
        exit(-200)

    # Run pipeline (in this process)
    sgPipeline.RunSceneFromFile('D:/Assets/ExportedScene.sb', 'D:/Assets/ProcessedScene.sb', Simplygon.EPipelineRunMode_RunInThisProcess)

# Load Max scene
rt.resetMaxFile(rt.Name('noPrompt'))
rt.importFile('D:/Assets/MyScene.fbx', rt.Name('noPrompt'))

# Clear previous export mapping
rt.sgsdk_ClearGlobalMapping()

# Select all objects
rt.select(rt.objects)

# Export scene to file
bResult = rt.sgsdk_ExportToFile('D:/Assets/ExportedScene.sb', False)

# Initialize Simplygon
sg = simplygon_loader.init_simplygon()

# Reduce exported file
run_pipeline(sg)

# Format import string
rt.sgsdk_SetInitialLODIndex(1)
rt.sgsdk_SetMeshNameFormat('{MeshName}_LOD{LODIndex}')

# Import processed file into Max, LOD-index = 1,
# CopyTextures = False, LinkMeshes = True, LinkMaterials = True
bResult = rt.sgsdk_ImportFromFile('D:/Assets/ProcessedScene.sb', False, True, True)

# De-initialize Simplygon
sg = None
gc.collect()

More about reduction pipeline

Get to know how to use the Reduction Pipeline: