Skip to content
On this page

Simple Reduction pipeline

This example shows how to create a new ReductionPipeline and how to set basic parameters for reduction and material baking.

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

    -- set DefaultTangentCalculatorType to Autodesk3dsMax (1)
    bResult = sgsdk_SetSetting reductionPipeline "GlobalSettings/DefaultTangentCalculatorType" 1
    defaultTangentCalculatorType = sgsdk_GetSetting reductionPipeline "GlobalSettings/DefaultTangentCalculatorType"
    print ("DefaultTangentCalculatorType: " + defaultTangentCalculatorType as string + "\n")

    -- set the triangle ratio to 50%
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatio" 0.5
    reductionTargetTriangleRatio = sgsdk_GetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatio"
    print ("ReductionTargetTriangleRatio: " + reductionTargetTriangleRatio as string + "\n")

    -- set the reduction target to triangle ratio
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatioEnabled" true
    reductionTargetTriangleRatioEnabled = sgsdk_GetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatioEnabled"
    print ("ReductionTargetTriangleRatioEnabled: " + reductionTargetTriangleRatioEnabled as string + "\n")

    -- disable triangle count reduction target
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleCountEnabled" false
    reductionTargetTriangleCountEnabled = sgsdk_GetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetTriangleCountEnabled"
    print ("ReductionTargetTriangleCountEnabled: " + reductionTargetTriangleCountEnabled as string + "\n")

    -- disable max deviation reduction target
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetMaxDeviationEnabled" false
    reductionTargetMaxDeviationEnabled = sgsdk_GetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetMaxDeviationEnabled"
    print ("ReductionTargetMaxDeviationEnabled: " + reductionTargetMaxDeviationEnabled as string + "\n")

    -- disable onscreensize reduction target
    bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetOnScreenSizeEnabled" false
    reductionTargetOnScreenSizeEnabled = sgsdk_GetSetting reductionPipeline "ReductionProcessor/ReductionSettings/ReductionTargetOnScreenSizeEnabled"
    print ("ReductionTargetOnScreenSizeEnabled: " + reductionTargetOnScreenSizeEnabled as string + "\n")

    -- 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!

    -- set bMaterialBake to true to enable material baking,
    -- if material baking is enabled Simplygon will generate
    -- a new material (with textures) shared by all the optimized meshes.
    bMaterialBake = true
    if bMaterialBake do
    (
        -- enable material baking
        -- mapping image is required for material baking
        bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/GenerateMappingImage" true
        generateMappingImage = sgsdk_GetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/GenerateMappingImage"
        print ("GenerateMappingImage: " + generateMappingImage as string + "\n")

        -- in this case we want to generate texture coordinates (UVs)
        bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/GenerateTexCoords" true
        generateTexCoords = sgsdk_GetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/GenerateTexCoords"
        print ("GenerateTexCoords: " + generateTexCoords as string + "\n")

        -- the name of the resulting texture coordinate field
        bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/TexCoordName" "MaterialLOD"
        texCoordLevel = sgsdk_GetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/TexCoordName"
        print ("TexCoordName: " + texCoordLevel as string + "\n")

        -- width of the baked textures
        bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/Output0/TextureWidth" 512
        textureWidth = sgsdk_GetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/Output0/TextureWidth"
        print ("TextureWidth: " + textureWidth as string + "\n")

        -- height of the baked textures
        bResult = sgsdk_SetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/Output0/TextureHeight" 512
        textureHeight = sgsdk_GetSetting reductionPipeline "ReductionProcessor/MappingImageSettings/Output0/TextureHeight"
        print ("TextureHeight: " + textureHeight as string + "\n")

        -- add material casters (Ambient_Color, Diffuse_Color, Specular_Color and Bump)
        casterIndex = sgsdk_AddMaterialCaster reductionPipeline "ColorCaster"
        bResult = sgsdk_SetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/ColorCasterSettings/MaterialChannel") "Ambient_Color"
        colorCaster0 = sgsdk_GetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/ColorCasterSettings/MaterialChannel")
        print ("ColorCaster0: " + colorCaster0 as string + "\n")

        casterIndex = sgsdk_AddMaterialCaster reductionPipeline "ColorCaster"
        bResult = sgsdk_SetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/ColorCasterSettings/MaterialChannel") "Diffuse_Color"
        colorCaster1 = sgsdk_GetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/ColorCasterSettings/MaterialChannel")
        print ("ColorCaster1: " + colorCaster1 as string + "\n")

        casterIndex = sgsdk_AddMaterialCaster reductionPipeline "ColorCaster"
        bResult = sgsdk_SetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/ColorCasterSettings/MaterialChannel") "Specular_Color"
        colorCaster2 = sgsdk_GetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/ColorCasterSettings/MaterialChannel")
        print ("ColorCaster2: " + colorCaster2 as string + "\n")

        -- note: normal caster for normals!
        casterIndex = sgsdk_AddMaterialCaster reductionPipeline "NormalCaster"
        bResult = sgsdk_SetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/NormalCasterSettings/MaterialChannel") "Bump"
        normalCaster3 = sgsdk_GetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/NormalCasterSettings/MaterialChannel")
        print ("NormalCaster3: " + normalCaster3 as string + "\n")

        -- set the correct tangent space type,
        -- in this example we use tangent space normals
        bResult = sgsdk_SetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/NormalCasterSettings/GenerateTangentSpaceNormals") true
        generateTangentSpaceNormals = sgsdk_GetSetting reductionPipeline ("MaterialCaster/" + casterindex + "/NormalCasterSettings/GenerateTangentSpaceNormals")
        print ("GenerateTangentSpaceNormals: " + generateTangentSpaceNormals as string + "\n")
    )

    if bMaterialBake then
    (
        bResult = sgsdk_SavePipeline reductionPipeline "D:/Pipelines/Reduction_With_Baking.json"
    )
    else
    (
        bResult = sgsdk_SavePipeline reductionPipeline "D:/Pipelines/Reduction.json"
    )

    -- clear all pipelines
    sgsdk_ClearPipelines()
python
from pymxs import runtime as rt

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

# set DefaultTangentCalculatorType to Autodesk3dsMax (1)
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'GlobalSettings/DefaultTangentCalculatorType', 1)
defaultTangentCalculatorType = rt.sgsdk_GetSetting(reductionPipeline, 'GlobalSettings/DefaultTangentCalculatorType')
print ('DefaultTangentCalculatorType: ' + str(defaultTangentCalculatorType) + '\n')

# set the triangle ratio to 50%
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatio', 0.5)
reductionTargetTriangleRatio = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatio')
print ('ReductionTargetTriangleRatio: ' + str(reductionTargetTriangleRatio) + '\n')

# set the reduction target to ReductionTargetTriangleRatio
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatioEnabled', True)
reductionTargetTriangleRatioEnabled = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleRatioEnabled')
print ('ReductionTargetTriangleRatioEnabled: ' + str(reductionTargetTriangleRatioEnabled) + '\n')

# disable triangle count reduction target
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleCountEnabled', False)
reductionTargetTriangleCountEnabled = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetTriangleCountEnabled')
print ('ReductionTargetTriangleCountEnabled: ' + str(reductionTargetTriangleCountEnabled) + '\n')

# disable max deviation reduction target
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetMaxDeviationEnabled', False)
reductionTargetMaxDeviationEnabled = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetMaxDeviationEnabled')
print ('ReductionTargetMaxDeviationEnabled: ' + str(reductionTargetMaxDeviationEnabled) + '\n')

# disable onscreensize reduction target
bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetOnScreenSizeEnabled', False)
reductionTargetOnScreenSizeEnabled = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/ReductionSettings/ReductionTargetOnScreenSizeEnabled')
print ('ReductionTargetOnScreenSizeEnabled: ' + str(reductionTargetOnScreenSizeEnabled) + '\n')

# 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!

# set bMaterialBake to True to enable material baking,
# if material baking is enabled Simplygon will generate
# a new material (with textures) shared by all the optimized meshes.
bMaterialBake = True
if bMaterialBake:
    # enable material baking
    # mapping image is required for material baking
    bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/GenerateMappingImage', True)
    generateMappingImage = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/GenerateMappingImage')
    print ('GenerateMappingImage: ' + str(generateMappingImage) + '\n')

    # in this case we want to generate texture coordinates (UVs)
    bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/GenerateTexCoords', True)
    generateTexCoords = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/GenerateTexCoords')
    print ('GenerateTexCoords: ' + str(generateTexCoords) + '\n')

    # the name of the resulting texture coordinate field
    bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/TexCoordName', 'MaterialLOD')
    texCoordLevel = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/TexCoordName')
    print ('TexCoordName: ' + texCoordLevel + '\n')

    # width of the baked textures
    bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/Output0/TextureWidth', 512)
    textureWidth = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/Output0/TextureWidth')
    print ('TextureWidth: ' + str(textureWidth) + '\n')

    # height of the baked textures
    bResult = rt.sgsdk_SetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/Output0/TextureHeight', 512)
    textureHeight = rt.sgsdk_GetSetting(reductionPipeline, 'ReductionProcessor/MappingImageSettings/Output0/TextureHeight')
    print ('TextureHeight: ' + str(textureHeight) + '\n')

    # add material casters (Ambient_Color, Diffuse_Color, Specular_Color and Bump)
    casterIndex = rt.sgsdk_AddMaterialCaster(reductionPipeline, 'ColorCaster')
    bResult = rt.sgsdk_SetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/ColorCasterSettings/MaterialChannel'), 'Ambient_Color')
    colorCaster0 = rt.sgsdk_GetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/ColorCasterSettings/MaterialChannel'))
    print ('ColorCaster0: ' + colorCaster0 + '\n')

    casterIndex = rt.sgsdk_AddMaterialCaster(reductionPipeline, 'ColorCaster')
    bResult = rt.sgsdk_SetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/ColorCasterSettings/MaterialChannel'), 'Diffuse_Color')
    colorCaster1 = rt.sgsdk_GetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/ColorCasterSettings/MaterialChannel'))
    print ('ColorCaster1: ' + colorCaster1 + '\n')

    casterIndex = rt.sgsdk_AddMaterialCaster(reductionPipeline, 'ColorCaster')
    bResult = rt.sgsdk_SetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/ColorCasterSettings/MaterialChannel'), 'Specular_Color')
    colorCaster2 = rt.sgsdk_GetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/ColorCasterSettings/MaterialChannel'))
    print ('ColorCaster2: ' + colorCaster2 + '\n')

    # note: normal caster for normals!
    casterIndex = rt.sgsdk_AddMaterialCaster(reductionPipeline, 'NormalCaster')
    bResult = rt.sgsdk_SetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/NormalCasterSettings/MaterialChannel'), 'Bump')
    normalCaster3 = rt.sgsdk_GetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/NormalCasterSettings/MaterialChannel'))
    print ('NormalCaster3: ' + normalCaster3 + '\n')

    # set the correct tangent space type,
    # in this example we use tangent space normals
    bResult = rt.sgsdk_SetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/NormalCasterSettings/GenerateTangentSpaceNormals'), True)
    generateTangentSpaceNormals = rt.sgsdk_GetSetting(reductionPipeline, ('MaterialCaster/' + str(casterIndex) + '/NormalCasterSettings/GenerateTangentSpaceNormals'))
    print ('GenerateTangentSpaceNormals: ' + str(generateTangentSpaceNormals) + '\n')

# save the generated Pipeline object to file for later use
if bMaterialBake:
    bResult = rt.sgsdk_SavePipeline(reductionPipeline, 'D:/Pipelines/Reduction_With_Baking.json')
else:
    bResult = rt.sgsdk_SavePipeline(reductionPipeline, 'D:/Pipelines/Reduction.json')

# clear all pipelines
rt.sgsdk_ClearPipelines()

Next steps

Get to know how to use the Reduction Pipeline: