------------------------------------------------
-- "Maxwell Material Converter beta3b"
--
-- Author: Sitnikov Ivan aka "vansan"
-- Created On: 11/07/05 for M~R 1.2.2 beta
-- Updated On: 27.06.07 
-- tested using: 3dsmax 9.2
------------------------------------------------

-- Description:
--
-- 	"Maxwell 1.5 Material Converter beta3b public version"
-- 	Uses 23 and 24 slots in MaterialEditor as temporary storage for old and converted materials.
--
--	Just drag&drop this file to your 3dsmax viewport to run it.
--
--	It converts Standard, Raytrace, and MentalRay Arch&Design materials to maxwell materials.
--	Works with Multi/Sub material.
-- 	Don't forget to "save as..." your material library after conversion!
--
-- 	Send your replies, wishes and found bugs to vansan@list.ru
--
------------------------------------------------

--Variables--

global conv_method
global ENTIRESCENE = 4, SELECTEDOBJECTS = 3, MATERIAL_LIB = 2,
CURRENT_SLOT = 1

global convert_selected_objects
global mc_rollout
global mc_info
global mc_floater
global mc_conv_org
global mc_conv
global mc_conv_sub
global mc_smart_convert
global mc_smart_convert2maxwell
global mc_scene_smart_convert 
global mc_matlib_smart_convert 
global mc_selection_smart_convert
global rendermat

rendermat  = 1


------------------------------------------ The Beginning of Conversion modules -------------------------------------------

-------------------------------
-- Maxwell Conversion Modules--
-------------------------------

fn mc_smart_convert2maxwell scene_mat =
(

if rendermat == 1 then 
(
case classof scene_mat of 
	(

-----------------------------------------------------------------------------------------------
standardmaterial: 
(
	setMeditMaterial 23 scene_mat
	meditMaterials[24] = Maxwell_Material ()
	oldmat = getMeditMaterial 23
	newMat = getMeditMaterial 24
	newMat.'BSDF_Reflected_Color' = oldMat.diffuse
	newMat.'BSDF_Reflected_Color_Texture' = oldMat.diffuseMap
	--newMat.'Fresnel_Color' = oldMat.ambient
	newMat.'BSDF_Bump_Strength' = oldMat.bumpMapAmount
	newMat.'BSDF_Bump_Strength_Texture' = oldMat.bumpMap
	newMat.'Transparency_Color' = oldMat.filterColor
	newMat.'BSDF_Transparency_Color_Texture' = oldMat.opacityMap
	newMat.'BSDF_Roughness' = 100 - oldMat.glossiness
	newMat.'BSDF_Nd' = oldMat.ior
	newMat.name = oldMat.name
	replaceInstances oldMat newMat
)
			
raytracematerial: 
(
	setMeditMaterial 23 scene_mat
	meditMaterials[24] = Maxwell_Material ()
	oldmat = getMeditMaterial 23
	newMat = getMeditMaterial 24
	newMat.'BSDF_Reflected_Color' = oldMat.Diffuse
	newMat.'BSDF_Reflected_Color_Texture' = oldMat.diffuseMap
	if newMat.'BSDF_Reflected_Color_Texture' != undefined then
	(newMat.'BSDF_Reflected_Color_Use_Texture' = on) else (newMat.'BSDF_Reflected_Color_Use_Texture' = off)
	newMat.'BSDF_Bump_Strength_Texture' = oldMat.BumpMap
	newMat.'BSDF_Bump_Strength' = oldMat.bumpMapAmount
	if newMat.'BSDF_Bump_Strength_Texture' != undefined then
	(newMat.'BSDF_Bump_Strength_Use_Texture' = on) else (newMat.'BSDF_Bump_Strength_Use_Texture' = off)
	newMat.'BSDF_Roughness' = oldMat.glossiness
	newMat.'BSDF_Nd' = oldMat.Index_of_Refraction
	newMat.'Transparency_Color' = oldMat.transparecy
	newMat.'BSDF_Transparency_Color_Texture' = oldMat.transparencyMap
	if newMat.'BSDF_Transparency_Color_Texture' != undefined then
	(newMat.'BSDF_Transparency_Color_Use_Texture' = on) else (newMat.'BSDF_Transparency_Color_Use_Texture' = off)
	newMat.name = oldMat.name
	replaceInstances oldMat newMat
)

Arch___Design__mi:
	(
	setMeditMaterial 23 scene_mat
	meditMaterials[24] = Maxwell_Material ()
	oldmat = getMeditMaterial 23
	newMat = getMeditMaterial 24
	newMat.'BSDF_Reflected_Color' = oldMat.diff_color
	newMat.'BSDF_Reflected_Color_Texture' = oldMat.diff_color_map
	newMat.'Layer_Weight' = oldMat.diff_weight * 100
	newMat.'BSDF_Roughness' = (1 - oldMat.diff_rough)*100
--	newMat.'BSDF_Roughness' = oldMat.refl_gloss
	if oldmat.refl_metal = on then newmat.'BSDF_Nd' = 20
	newMat.'BSDF_Nd' = oldMat.refr_ior
	newMat.'BSDF_Absorption' = 5
	--newMat.'Transparency Color' = oldMat.refr_weight * 255 / 100 + 1
	--newMat.'Transparency Color' = oldMat.refr_color
--	newMat.'BSDF_Roughness' = oldMat.refr_gloss
	newMat.'BSDF_Anisotropy' = oldMat.anisotropy * 10
	newMat.'BSDF_Anisotropy_Angle' = oldMat.anisoangle * 360
	newMat.'BBSDF_Roughness_Texture' = oldMat.diff_rough_map
	--newMat.'BBSDF Anisotropy Texture' = oldmap.anisotropy_map --- Error, maybe script access bug.
	--newMat.'BSDF Anisotropy Angle Texture' = oldmap.anisoangle_map --- Error, maybe script access bug.
	newMat.'BSDF_Bump_Strength' = oldMat.bump_map_amt * 10
	newMat.'BSDF_Bump_Strength_Texture' = oldMat.bump_map
	newMat.name = oldMat.name
	replaceInstances oldMat newMat
	)
		
Architectural:
	(
	
	)

VRayMtl:
	(
	
	)

VRayLightMtl:
	(
	
	)
	
VRayFastSSS:
	(
	
	)
	
VRayBlendMtl:
	(
	
	)

)
)
)
------------------------------------
--endof Maxwell Conversion Modules--
------------------------------------

------------------------------------------ The End of Conversion modules -------------------------------------------

fn mc_smart_convert scene_mat =
(	
	mc_smart_convert2maxwell scene_mat
)	
	
fn mc_scene_smart_convert scene_mat =
(
	for scene_mat in scenematerials do
	(
		if classof scene_mat == multimaterial then
		(
		for i=1 to scene_mat.numsubs do
		mc_smart_convert scene_mat[i]
		)
		else (mc_smart_convert scene_mat)
	)
)

fn mc_selection_smart_convert scene_mat =
(
	for scene_mat in selection do
	(
		if classof scene_mat == multimaterial then
		(
		for i=1 to scene_mat.numsubs do
		mc_smart_convert scene_mat[i]
		)
		else (mc_smart_convert scene_mat)
	)
)

fn mc_matlib_smart_convert scene_mat =
(
	for scene_mat in currentMaterialLibrary do
	(
		if classof scene_mat == multimaterial then
		(
		for i=1 to scene_mat.numsubs do
		mc_smart_convert scene_mat[i]
		)
		else (mc_smart_convert scene_mat)
	)
)


rollout mc_rollout " Maxwell Material converter beta3b"
	(
group "Scene Materials"
	(
		dropdownlist newmat_toconvert "Select your renderer: (M~R by default)" items:#("Maxwell Render 1.1") selection:1 
		radiobuttons objectselection labels:#("Current MatEd Slot", "Material Library", "Selected Objects", "Entire Scene")
		button select_ml "Select Material Library..." enabled:false
		editText matlib_name_text "" enabled:false pos:[10,160]
		button convertScene "Convert Materials" enabled:true width:190
		
	)

group "About"
	(
		label lbl0 "Material Converter beta 3b"
		label lbl00 "-------------------------------------------------------------"
		label lbl1 "This script is for personal"
		label lbl2 "and non-commercial use only."
		label lbl3 "(c) 2007"
		label lbl4 "written by Ivan Sitnikov (vansan)"
		label lbl5 "vansan@list.ru"		
	)

on newmat_toconvert selected renderer do
		(
			rendermat = case newmat_toconvert.selection of 
			(
			1: 1 -- Maxwell 1.5
			)
		)

	on oldSlotSpinner changed value do oldSlot = value
	on newSlotSpinner changed value do newSlot = value
	on objectselection changed state do
	(
		
		if objectselection.state == CURRENT_SLOT then
			(
			)
		if objectselection.state == MATERIAL_LIB then
			(
			matlib_name_text.text = "Open MatEd window slows conversion!"
			select_ml.enabled = true
			saveMaterialLibrary "$scripts/temp.mcc"
			print "Select material library!"
			)
			else
			(
			select_ml.enabled = false
			matlib_name_text.text = ""
			loadMaterialLibrary "$scripts/temp.mcc"
			deletefile "$scripts/temp.mcc"
			)
		)
	on convert_to_b pressed do 
	(
		mc_conv_org meditMaterials[oldSlot]
	)

	on select_ml pressed do 
		(
		fileOpenMatLib()
		matlib_name_text.text = getMatLibFileName()
		)

	on convertScene pressed do 
		(
		conv_method = case objectselection.state of 
		(
		1: 1 -- Current Slot
		2: 2 -- Material Library
		3: 3 -- Selected Objects
		4: 4 -- Entire Scene
		)

		if conv_method == 1 then 
	(
	scene_mat = meditMaterials[medit.GetActiveMtlSlot()]
	if classof scene_mat == multimaterial then
	(
	for i=1 to scene_mat.numsubs do
	mc_smart_convert scene_mat[i]
	)
	else (mc_smart_convert scene_mat)
	)

		if conv_method == 2 then 
		(
		for m=1 to currentMaterialLibrary.count do
		(
		mc_matlib_smart_convert currentMaterialLibrary[m]
		)
		matlib_name_text.text = "MatLib is converted! Save it NOW!"
		)
		
		if conv_method == 3 then 
(
		for obj in selection do
	(
		scene_mat = obj.material
		if classof scene_mat == multimaterial then
		(
		for i=1 to scene_mat.numsubs do
		mc_smart_convert scene_mat[i]
		)
		else (mc_smart_convert scene_mat)
	)
	
matlib_name_text.text = "Selected objects are converted!"
)

		if conv_method == 4 then 
		(
		mc_scene_smart_convert the_mat
		matlib_name_text.text = "Scene materials are converted!"
		)
	)
)
if mc_floater != undefined then CloseRolloutFloater mc_floater
mc_floater = newRolloutFloater "MMC beta3b" 235 235
addRollout mc_rollout mc_floater
------------------------------------------------