Reduction using ReductionProcessor
This section includes an example of how to use the Simplygon Python API to optimize the scene. 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 sets up a reduction processor 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_reduction (sg):
# Set Max's handedness setting
sg.SetGlobalDefaultTangentCalculatorTypeSetting(Simplygon.ETangentSpaceMethod_Autodesk3dsMax)
# Load the scene from file
lodScene = sg.CreateScene()
lodScene.LoadFromFile('D:/Assets/ExportedScene.sb')
# Create the reduction-processor, and set which scene to reduce
reductionProcessor = sg.CreateReductionProcessor()
reductionProcessor.SetScene(lodScene)
# The reduction settings object contains settings pertaining to the actual decimation
reductionSettings = reductionProcessor.GetReductionSettings()
# Try, when possible to reduce symmetrically
reductionSettings.SetKeepSymmetry(True)
# Auto-detect the symmetry plane, if one exists. Can, if required, be set manually instead
reductionSettings.SetUseAutomaticSymmetryDetection(True)
# Drastically increases the quality of the LODs normals, at the cost of extra processing time
reductionSettings.SetUseHighQualityNormalCalculation(True)
# Choose between "fast" and "consistent" processing
reductionSettings.SetReductionHeuristics(Simplygon.EReductionHeuristics_Consistent)
# The reduction stops when any of the targets below is reached
reductionSettings.SetReductionTargetStopCondition(Simplygon.EStopCondition_Any)
# Selects which targets should be considered when reducing
reductionSettings.SetReductionTargetTriangleRatioEnabled(True)
reductionSettings.SetReductionTargetTriangleCountEnabled(True)
reductionSettings.SetReductionTargetMaxDeviationEnabled(True)
reductionSettings.SetReductionTargetOnScreenSizeEnabled(True)
# Targets at 50% of the original triangle count
reductionSettings.SetReductionTargetTriangleRatio(0.5)
# Targets when only 10 triangle remains
reductionSettings.SetReductionTargetTriangleCount(10)
# Targets when an error of the specified size has been reached. As set here it never happens
reductionSettings.SetReductionTargetMaxDeviation(1e27)
# Targets when the LOD is optimized for the selected on screen pixel size
reductionSettings.SetReductionTargetOnScreenSize(50)
# The repair settings object contains settings to fix the geometries
repairSettings = reductionProcessor.GetRepairSettings()
repairSettings.SetTJuncDist(0.0) # Removes t-junctions with distance 0.0f
repairSettings.SetWeldDist(0.0) # Welds overlapping vertices
# The normal calculation settings deal with the normal-specific reduction settings
normalSettings = reductionProcessor.GetNormalCalculationSettings()
# If True, this will turn off normal handling in the reducer and recalculate them all
# afterwards instead.
normalSettings.SetReplaceNormals(False)
# Run the actual processing.
# After this, the set geometry will have been reduced according to the settings
reductionProcessor.RunProcessing()
# Save the processed scene to file
lodScene.SaveToFile('D:/Assets/ProcessedScene.sb')
# 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()
# Run reduction
run_reduction(sg)
# Format import name
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 processor
Get to know how to use the Reduction Processor: