Skip to content
On this page

Reduction using ReductionPipeline

This section includes 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 Maya 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 Maya. The export and import could be done manually if preferred.

python
import maya.cmds as cmds
from simplygon10 import simplygon_loader
from simplygon10 import Simplygon
import gc

def run_pipeline(sg):
    # Set Maya's handedness setting
    sg.SetGlobalDefaultTangentCalculatorTypeSetting(Simplygon.ETangentSpaceMethod_OrthonormalRightHanded)

    # 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/reductionPipeline.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 Maya scene
cmds.file(f=True, new=True)
cmds.file('D:/Assets/MyScene.fbx', i=True, mergeNamespacesOnClash=True,
          namespace=':', importFrameRate=True)

# Clear previous export mapping
bResult = cmds.Simplygon(cgm=True)

# Select all objects
cmds.select(all=True)

# Export scene to file
bResult = cmds.Simplygon(exp='D:/Assets/ExportedScene.sb', cte=False)

# Initialize Simplygon
sg = simplygon_loader.init_simplygon()

# Reduce exported file
run_pipeline(sg)

# Import processed file into Maya, LOD-index = 1, CopyTextures = False, LinkMaterials = True
bResult = cmds.Simplygon(imp='D:/Assets/ProcessedScene.sb',
                         cte=False, lma=True, mnf="My_{MeshName}_LOD{LODIndex}", ili=1)

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

:::

Next steps

Get to know how to use the Reduction Pipeline: