Multi\Sub-object Materials ID organization script from 1, 2, 3... to Set number

Multi\Sub-object Materials ID organization script from 1, 2, 3... to Set number

idealight
Explorer Explorer
798 Views
2 Replies
Message 1 of 3

Multi\Sub-object Materials ID organization script from 1, 2, 3... to Set number

idealight
Explorer
Explorer

Good day everyone. 

 

I have notice that during exporting multisub-objects with ID that are not in strict order 1, 2, 3, etc, for example 1, 12, 108 ... etc there is an issue in exported models. Some IDs are messed up with each other and are not in correct order like in 3dsmax. I am using GLTF exported and GLTF and PHISICAL materials. I noted this "bug" and made a correct IDs order 1, 2, 3... and its work good. BUT... I have a lot of stuff in my scene and its take a lot of time to change the polygon ID and Material IDs in MultiSub object material.

 

In general I need to change the ID numbers from 1 to SetNumber in Multi\Sub and change the polygon Ids accordingly. Of course, if this script will be working with Shell material and Multisub will be as original objects its great but its not so vital. I do not know will the shell material influence the script, should it be considered too? 

MultiSub.jpgShell material.jpg

 

May be there is already such solution, script, will be greatfull.

 

So... I tried chat GPT to wright me a script) Please don't judge me in advance. 

 

There is a syntax error: at else, expected <factor>. In line 46, else i

 

Best regards from Ukraine.

 

 

 

 

 

 

-- Function to update material IDs
fn updateMaterialIDs objID oldID newID =
(
    -- Check if the object is an Editable Poly or Editable Mesh
    if (classof objID == Editable_Poly) or (classof objID == Editable_Mesh) then
    (
        -- Get the material IDs array
        matIDs = if (classof objID == Editable_Poly) then polyOp.getFaceMatID objID else getPolygonMatID objID
        
        -- Loop through each face and update the material ID
        for i = 1 to matIDs.count do
        (
            -- If the face material ID matches the old ID, update it to the new ID
            if matIDs[i] == oldID then
            (
                matIDs[i] = newID
            )
        )
        
        -- Set the updated material IDs array back to the object
        if (classof objID == Editable_Poly) then
        (
            polyOp.setFaceMatID objID matIDs
        )
        else
        (
            setPolygonMatID objID matIDs
        )
    )
    else
    (
        print "Object is not an Editable Poly or Editable Mesh."
    )
)

-- Function to update material IDs of materials within Multi/Sub-Object
fn updateMultiSubMaterialIDs material oldID newID =
(
    if classof material == Multimaterial do
    (
        for i = 1 to material.numsubs do
        (
            if classof material[i] == StandardMaterial or classof material[i] == MultiMaterial or classof material[i] == ShellMaterial do
            (
                updateMaterialIDs material[i] oldID newID
            )
            else if classof material[i] == Multimaterial do
            (
                updateMultiSubMaterialIDs material[i] oldID newID
            )
        )
    )
)

-- Main function to update material IDs of selected objects
fn updateMaterialIDsOfSelectedObjects =
(
    -- Get the selected objects
    selectedObjects = selection as array
    
    -- Loop through each selected object
    for obj in selectedObjects do
    (
        -- Check if the object is an Editable Poly or Editable Mesh
        if (classof obj == Editable_Poly) or (classof obj == Editable_Mesh) then
        (
            -- Get the material modifier of the object
            matMod = if (classof obj == Editable_Poly) then obj.material else obj
                    
            -- Loop through each material slot
            for i = 1 to matMod.numsubs do
            (
                -- Check if the material slot contains a material
                if (matMod[i] != undefined) then
                (
                    -- Update material IDs within Multi/Sub-Object or Shell material
                    if classof matMod[i] == Multimaterial or classof matMod[i] == ShellMaterial do
                    (
                        updateMultiSubMaterialIDs matMod[i] 1 i
                    )
                    else
                    (
                        -- Update the material ID from old to new
                        matMod[i].id = i
                    )
                )
            )
            
            -- Update the material IDs of the faces in the object
            updateMaterialIDs obj 1 matMod.numsubs
        )
        else
        (
            print ("Object " + obj.name + " is not an Editable Poly or Editable Mesh.")
        )
    )
)

-- Call the main function
updateMaterialIDsOfSelectedObjects()

 

 

 

 

 

 

0 Likes
799 Views
2 Replies
Replies (2)
Message 2 of 3

denisT.MaxDoctor
Advisor
Advisor

First, what export format are you doing? How do you know that something is going wrong?

0 Likes
Message 3 of 3

idealight
Explorer
Explorer

Good day Denis

Thank you for your reply.

I am Exporting multisub object as GLTF .GLB file. materials are GLTF and Phisical, both have same issues.

Screenshot_4.jpg

This is official help from Autodesk

https://help.autodesk.com/view/3DSMAX/2024/ENU/?guid=GUID-5B4C8EC2-2230-4F9F-B3C6-48D9E347E37D

And I notice this limitation, and as I admit gap also could be a non empty slot but a skipped numbers.

Screenshot_1.jpg

 

Also I found on a web same issue with multi\sub .OBJ export? When there is mixed materials in output file if Indexes are not in strict order.  

0 Likes