Skip to content
On this page

Mapping of Physical materials

The following sections contains examples on how to setup, export and import materials through MaxScript and Python in Max 2021 and later. It is recommended to read Shading network concepts before proceeding.

Physical Material

This script loops through all Physical Materials in the scene and sets up Simplygon shading networks accordingly. After the material setup the selected assets gets sent to Simplygon for optimization. When the optimization has completed the result will be returned to Max. This example utilize existing material channels for the Physical Material pipeline and will therefor get mapped back automatically. Custom channels will require manual import (using the query functions at the end of this example).

MaxScript
clear
sgsdk_Reset()
sgsdk_SetTextureOutputDirectory "D:\\CubeExample\\OutputTextures\\"

-- reduction pipeline with material baking
reductionPipelineWithBaking = "D:\\Pipelines\\reductionPipelineWithBaking.json"

-- list that will be populated with Physical Material
MaxPhysicalMaterials = #()

-- loop all scene materials
for mat in scenematerials do
(
    print ("Material name: " + mat.name)
    print ("\t\t\t.type: " + classOf mat as string)

    -- if Physical_Material, store for later use
    if classof mat == Physical_Material then
    (
        appendIfUnique MaxPhysicalMaterials  mat
    )
        
    -- print all material properties
    props = getPropNames  mat
    for i = 1 to props.count do
    (
        prop = getProperty  mat props[i]
        print ("\t\t\t." + props[i] + ": " + prop as string + "")
    )
    print ("\n")

    -- loop through all sub-materials
    subMatCount = getNumSubMtls mat
    print ("\tNum sub-materials: " + subMatCount as string)
    for i = 1  to subMatCount do
    (
        subMat = getSubMtl mat i
        print ("\t\tSub-material name: " + subMat.name)
        print ("\t\t\t.type: " + classOf mat as string)

        -- if Physical_Material, store for later use
        if classof subMat == Physical_Material then
        (
            appendIfUnique  MaxPhysicalMaterials  subMat
        )
        
        -- print all sub-material properties
        props = getPropNames subMat
        for i = 1 to props.count do
        (
            prop = getProperty subMat props[i]
            print ("\t\t\t." + props[i] + ": " + prop as string + "")
        )
        print ("\n")
    )
)

-- setup shading network for each physical material
for mat in MaxPhysicalMaterials do
(
    MaterialName = mat.name
    print ("Setting up shading network for: " + MaterialName)

    -- create material override
    MyPhysicalMaterial = sgsdk_CreateMaterialMetadata MaterialName

    -- override base_color (base_color * base_color_map)
    if mat.base_color_map != undefined and mat.base_color_map_on then
    (
        -- create nodes (map via name)
        BaseColor = sgsdk_CreateShadingColorNode "base_color"
        BaseColorMap = sgsdk_CreateShadingTextureNode "base_color_map"
    
        -- base_color * base_color_map
        BaseColorMultiply = sgsdk_CreateShadingMultiplyNode "multiply"
        sgsdk_ConnectNodes BaseColorMultiply 0 BaseColor
        sgsdk_ConnectNodes BaseColorMultiply 1 BaseColorMap
        
        -- connect shading network to Simplygon material channel
        sgsdk_ConnectNodeToChannel BaseColorMultiply MyPhysicalMaterial "base_color"
    )
    
    -- override base_color (if no texture exists)
    else
    (
        BaseColor = sgsdk_CreateShadingColorNode "base_color"
        sgsdk_ConnectNodeToChannel BaseColor MyPhysicalMaterial "base_color"
    )
    
    -- override bump, if any
    if mat.bump_map != undefined and mat.bump_map_on then
    (
        -- create node, map via name
        BumpMap = sgsdk_CreateShadingTextureNode("bump_map");

        -- connect shading network to Simplygon material channel
        sgsdk_ConnectNodeToChannel BumpMap MyPhysicalMaterial "bump"
    )
)

-- execute Simplygon
sgsdk_RunPipelineOnSelection reductionPipelineWithBaking

-- get list with processed meshes
processedMeshes = sgsdk_GetProcessedMeshes()
for mesh in processedMeshes do
(
    print ("\tMesh: " + mesh)

    -- get materials attached to this mesh
    materials = sgsdk_GetMaterialsForMesh mesh
    for material in materials do
    (
        print ("\n\t\tMaterial: " + material)
        
        -- see if there are reused materials
        reusingMaterials = sgsdk_GetMeshReusesMaterials mesh
        for reuseMaterial in reusingMaterials do
        (
            print ("\t\tReusing: " + reuseMaterial)
        )
        
        -- get material cahnnels for the material, if any
        channels = sgsdk_GetChannelsForMaterial material
        for channel in channels do
        (
            print ("\t\t\tChannel: " + channel)
            texture = sgsdk_GetTexturePathForChannel material channel
            print ("\t\t\t\tTexture: " + texture)
            mappingChannel = sgsdk_GetMappingChannelForChannel material channel
            print ("\t\t\t\tMappingChannel: " + mappingChannel as string)
        )
        
        -- get sub-materials, if any
        materials = sgsdk_GetSubMaterials material
        for submaterial in materials do
        (
            print ("\n\t\t\tSubMaterial: " + submaterial)
            reusingMaterial = sgsdk_GetMaterialReusesSubMaterial material submaterial
            print ("\t\t\tReusing: " + reusingMaterial)
            
            channels = sgsdk_GetChannelsForMaterial submaterial
            for channel in channels do
            (
                print ("\t\t\tChannel: " + channel)
                texture = sgsdk_GetTexturePathForChannel submaterial channel
                print ("\t\t\t\tTexture: " + texture)
                mappingChannel = sgsdk_GetMappingChannelForChannel submaterial channel
                print ("\t\t\t\tMappingChannel: " + mappingChannel as string)
            )
            
            materialIndex = sgsdk_GetSubMaterialIndex material submaterial
            print ("\t\t\tIndex: " + materialIndex as string)
        )
    )
)

Get to know how to work with Shading Networks: