REQUEST - Replace scene material with library material by referencing RGB values

Hi, hope this isn't too big of an ask.

I'm in need of a maxscript that can automatically assign pre-built library materials to the scenes that i import from my CAD program. The difficult part is this:

My CAD program doesn't give names to the materials in the exported files. So in 3Ds Max, the imported scene materials have non-unique names. (e.g. Material #1, Material #2... and so on.) The only constant that I can reference on the imported materials is the RGB values which are always the same across every scene that i import. I need to reference the RGB values of the imported scene materials and replace those mats with the library materials which are already pre-built in a mat library.

So for example if one imported material has an RGB value of '248 255 117', then i need to replace that material with my library material called 'PC_Aluminium_mr'

Is someone able to help me with this? I have very basic ability with Maxscript. Very thankful for any help. Hope this makes sense!

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jahman's picture

.

You can attach some arbitrary data to any of your materials/maps in library
google for "Access to MAXWrapper AppData" if you need more info on the subject

1. Load and Save your mtl library:

tmpMtlLib = loadTempMaterialLibrary @"C:\...\yourMatrialLibrary.mat"
 
saveTempMaterialLibrary tmpMtlLib @"C:\...\yourMatrialLibrary.mat"

2. Get, Set, Delete app data

setAppData tmpMtlLib[2] 1 "255,22,128"
getAppData tmpMtlLib[33] 1
deleteAppData tmpMtlLib["polished metal"] 1

3. Solving your task:

1. get material RGB value
2. convert rgb value to string
3. iterate over material library and check if library material app data is matching given value
4. replace current material with library material if there's a match

-- 1
importedMaterial = $.material -- this is your imported node material
rgbColor = importedMaterial.diffuse -- you didn't specify where do you get RGB values from so lets assume it has StandardMaterial applied by default
 
-- 2
rgbValue = (int rgbColor.r) as string + "," + (int rgbColor.g) as string + "," + (int rgbColor.b) as string 
 
 
-- 3
tmpMtlLib = loadTempMaterialLibrary @"C:\...\yourMatrialLibrary.mat"
 
for mtl in tmpMtlLib where (getAppData mtl 1) == rgbValue do (
 
    -- 4
    replaceInstances importedMaterial mtl
 
)

I didn't test it, but the main idea is pretty clear.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.