Skip to content
On this page

Simple reduction - Scripting

This example will create a simple reduction pipeline with a target Triangle Ratio of 0.5 (50%), send the selected scene off to Simplygon for optimization and return the result back to Max once completed.

The first step is to create a minimalistic ReductionPipeline, just a simple reduction on geometries while preserving original materials, nothing more, nothing less.

MaxScript
-- create a Reduction Pipeline object
    reductionPipeline = sgsdk_CreatePipeline "ReductionPipeline"

    -- set the triangle ratio to 50%
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatio" 0.5

    -- set the reduction target to ReductionTargetTriangleRatio
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatioEnabled" true

    -- disable triangle count reduction target
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleCountEnabled" false

    -- disable max deviation reduction target
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetMaxDeviationEnabled" false

    -- disable onscreensize reduction target
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetOnScreenSizeEnabled" false

    -- more settings can be set for the Reduction pipeline,
    -- see Pipeline documentation and / or inspect the generated file
    -- that is saved out by this script!

    -- save the generated Pipeline object to file for later use,
    -- this is optional for this example and is mostly intended for inspection
    -- of the settings for the given Pipeline.
    bResult = sgsdk_SavePipeline reductionPipeline "D:/Pipelines/Reduction.json"
python
from pymxs import runtime as rt

# create a Reduction Pipeline object
reductionPipeline = rt.sgsdk_CreatePipeline('ReductionPipeline')

# set the triangle ratio to 50%
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatio', 0.5)

# set the reduction target to ReductionTargetTriangleRatio
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatioEnabled', True)

# disable triangle count reduction target
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleCountEnabled', False)

# disable max deviation reduction target
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetMaxDeviationEnabled', False)

# disable onscreensize reduction target
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetOnScreenSizeEnabled', False)

# more settings can be set for the Reduction pipeline,
# see Pipeline documentation and / or inspect the generated file
# that is saved out by this script!

# save the generated Pipeline object to file for later use,
# this is optional for this example and is mostly intended for inspection
# of the settings for the given Pipeline.
bResult = rt.sgsdk_SavePipeline(reductionPipeline, 'D:/Pipelines/Reduction.json')

The sgsdk_RunPipelineOnSelection function is one of the main functions that starts the optimization from Max, it takes a Pipeline settings-object or a settings-path as input which specifies what kind of optimization Simplygon will do, for example Reduction, Aggregation, Remeshing and Material Baking - for this example we will use a Reduction pipeline from the previous section.

The second part is to select the scene that is to be optimized (or parts of it) and start the optimization using the sgsdk_RunPipelineOnSelection function. The optimized result will get imported automatically once the optimization has completed.

MaxScript
-- select objects in scene
    select $*

    -- execute Simplygon with the given Pipeline (settings-object)
    bResult = sgsdk_RunPipelineOnSelection reductionPipeline
python
# select objects in scene
rt.select(rt.objects)

# execute Simplygon with the given Pipeline (settings-object)
bResult = rt.sgsdk_RunPipelineOnSelection(reductionPipeline)

The third and last part is simply to clean up created and / or loaded Pipelines that resides in memory, they can of course be reused but it is easy to forget they were ever there.

MaxScript
-- clear all pipelines that resides in memory
    sgsdk_ClearPipelines()
python
# clear all pipelines that resides in memory
rt.sgsdk_ClearPipelines()

Next steps

Get to know how to use the Simplygon Max plug-in: