Maxscript: Get all Materials and Maps functions

You may find answers here and there to collect all the materials and maps in some odd ways but none ever worked as intended for me: Collect ALL materials and ALL maps, not just the root material assigned to an object or a map directly connected to it.

These functions do what I wanted. You may never need to do such thing, or you may.


global lwan_materials = #()
global lwan_textures  = #()

-- ### seek for sub-maps
function lwan_get_sub_maps current_node =
(
    local sub_maps_count = getNumSubTexmaps current_node

    for map = 1 to sub_maps_count do
    (
        local child_map = getSubTexmap current_node map

        if  ( child_map != undefined ) then
        (
            local sub_maps_count = getNumSubTexmaps child_map

            if ( sub_maps_count > 0 ) then
            (
                appendIfUnique lwan_textures child_map

                lwan_get_sub_maps child_map
            )
            else
            (
                appendIfUnique lwan_textures child_map
            )
        )
    )
)

-- ### seek for sub-materials and sub-maps
function lwan_get_sub_materials root_mtl =
(
    mtl = root_mtl

    appendIfUnique lwan_materials mtl

    if ( ( superclassof mtl ) == Material ) then
    (
        local sub_mtl_count = getNumSubMtls mtl

        local sub_map_count = getNumSubTexmaps mtl
        
        if ( sub_mtl_count > 0 ) then
        (
            for child_material = 1 to sub_mtl_count do
            (
                local sub_mtl = getSubMtl mtl child_material

                if  ( sub_mtl != undefined ) then
                (
                    appendIfUnique lwan_materials sub_mtl

                    lwan_get_sub_materials sub_mtl
                )
            )
        )
        if ( sub_map_count > 0 ) then
        (
            lwan_get_sub_maps mtl
        )
    )
)

-- ### get the scene root materials (the ones applied to objects)
function lwan_get_materials_tree =
(
    for mtl in sceneMaterials do
    (
        lwan_get_sub_materials mtl
    )
)
lwan_get_materials_tree()

Hope it’s helpfull to someone!

Maxscript: Get all Materials and Maps functions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s