Multi\Sub-object Materials ID organization script from 1, 2, 3... to Set number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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?
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()