material converter and duplicated material merging

material converter and duplicated material merging

serdar.balcik
Explorer Explorer
1,807 Views
4 Replies
Message 1 of 5

material converter and duplicated material merging

serdar.balcik
Explorer
Explorer

hi folks,

 

i try maxscript for specific situation. but icant solve my problem. problem is ;

 

1. Physical Material. i need all physical material ( include Multi/Sub sub materials) converted to Standart Materials.

2. All materials must have bitmap texture file name (ex. file name "Brick.jpg , Material name Brick_MAT)

3. All duplicated materials (same texture on diffuse channel) must be combined. 

 

I can convert materials physical to standart but every mesh has every uniqe material. then i can merge by hand. but i have so much material in scene.

 

Please help.

Thanks everyone

0 Likes
Accepted solutions (1)
1,808 Views
4 Replies
Replies (4)
Message 2 of 5

denisT.MaxDoctor
Advisor
Advisor

@serdar.balcik wrote:

3. All duplicated materials (same texture on diffuse channel) must be combined. 

 


How should they be combined? What is to 'combine materials'?

0 Likes
Message 3 of 5

serdar.balcik
Explorer
Explorer

thanks for reply,

ill try explain this situation.

 

i have different asset like building. these asset share same texture and material. (like BUILD_ATLAS_MAT(StandardMaterial))

when i import fbx files build_1, build_2 etc.. they always import new material(BUILD_ATLAS_MAT_1,BUILD_ATLAS_MAT_2 etc.). but its same material and same textures.

 

if i merge max scene , i can merge these duplicate material.

but all my assets in seperate FBX file. 

 

in my assets , texture name always be material name ( BUILD_ATLAS.jpg ---> BUILD_ATLAS_MAT )

 

also if i import fbx , also Standard Material converting to Physical Material. i found in forum different script for Physical to Standard but i have some issue. 

 

im very new to maxscript, i cant figure how can i do this with maxscript.

 

thanks for help 

 

 

 

0 Likes
Message 4 of 5

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

 

/*************************** CONVERSION METHODS  *****************************/
mapped fn convertPhysicalToStandard mat replace:on = if iskindof mat Physical_Material do
(
	std = Standard name:mat.name
	--format "mat% >> std:%" mat std
	
	/***************** DO A SIMPLE PROP MAPPING ************************/
	std.diffuse = mat.base_color
	std.diffusemap = mat.base_color_map
	
	if replace do replaceinstances mat std
	std
)

mapped fn renameStandardByDiffuseMap mat upper:on suffix:"_MAT" = if iskindof mat Standard do
(
	old = mat.name
	
	txt = mat.diffusemap
	if iskindof txt BitmapTexture and iskindof txt.filename String do
	(
		name = getfilenamefile txt.filename
		if upper do name = toupper name
		name += suffix
			
		mat.name = name
	)
	#(old, mat.name)	
)

mapped fn groupStandardByName mat dict: = if iskindof mat Standard do
(
	if dict[mat.name] == undefined do dict[mat.name] = #()
	appendifunique dict[mat.name] mat
)

fn uniqueDupMaterials mats = 
(
	dict = Dictionary #string
	groupStandardByName mats dict:dict
	
	mapped fn _replaceInstances mat trg = replaceinstances mat trg
		
	for d in dict where d.value.count > 1 do _replaceInstances d.value d.value[1]
)
/*************************** MAKE A SAMPLE SCENE *****************************/
delete objects
gc()

with redraw off
(
	t0 = teapot mapcoords:on rotation:(eulerangles 0 0 -90) pos:[-30,0,0] wirecolor:green
	t1 = teapot mapcoords:on rotation:(eulerangles 0 0 -90) pos:[30,0,0] wirecolor:orange

	meditmaterials[1] = mat0 = Physical_Material name:#mat0 base_color:green base_color_map:(bitmaptexture filename:"JAGLEAF.TGA")
	meditmaterials[2] = mat1 = Physical_Material name:#mat1 base_color:orange base_color_map:(bitmaptexture filename:"FLOWER3.TGA")

	t0.mat = mat0
	t1.mat = mat1
	
	for k=1 to 3 do (copy t0 pos:[t0.pos.x, t0.pos.y, t0.pos.z + k*40] material:(copy t0.mat))
	for k=1 to 3 do (copy t1 pos:[t1.pos.x, t1.pos.y, t1.pos.z + k*40] material:(copy t1.mat))
)
/***************************** CONVERT AND RENAME MATERIALS ******************/
(
	phys = getclassinstances Physical_Material target:rootnode
	convertPhysicalToStandard phys -- (join #() sceneMaterials)

	stds = getclassinstances Standard target:rootnode
	renameStandardByDiffuseMap stds

	--dict = Dictionary #string
	--groupStandardByName stds dict:dict
	uniqueDupMaterials stds
	medit.SetActiveMtlSlot (medit.GetActiveMtlSlot()) on
		
	updateSceneMaterialLib()	
	mats = join #() sceneMaterials
)

 

 

Looks like that's all you need. Of course, the conversion of physical to standard may be more fancy, but this is not a point of issue of this topic ...

Message 5 of 5

serdar.balcik
Explorer
Explorer

thank you, works perfectly. 

 

this life saver for me. ill study on this to improve my codding skills. 

best regards...

 

 

0 Likes