Copy a material slot to an other one


#1

Hello,
I am trying to make a script to copy what ever is in a slot to an other one, ive made something that kind of works but it is strange, the listener return a result that make you think that it works but when you check on the material itself nothing happens, if i replace the variable a and b , with “mat.baseBumpTexmap = copy mat.baseTexmap” then it works, it is effectively copying the diffusemap to the bump mat of the selected object materials but its not working when i use variables a and b instead , the strange thing is that the listener return the same info about the name of the bitmap that has been copied in both case.
i am new to maxscript and i didn’t find anything in maxhelp that could help me understand what’s going on.
here is the code :

rollout progressTest "Corona copy material slot"
(
    radiobuttons copy_fromRB labels:#("Base color", "Base Roughness", "Base anisotropy", "Base aniso. rot", "Base IOR", "Edgecolor", "Transl. fraction", "Transl.color", "Metalness", "refraction am.",
        "thin abs.color", "opacity", "Self-illumination", "Base bump", "Displacement", "Clearcoat am.", "Clearcoat IOR", "Clearcoat rough.", "Clearcoat abs.", "Clearcoat bump", "Sheen amount", "Sheen color",
        "Sheen rough.", "SSS amount", "SSS radius", "SSS scatt. color", "Vol. absorption", "Vol.scattering", "Base tail", "Reflect BG override", "Refract BG override") pos:[15, 30]
    radiobuttons copy_toRB labels:#("Base color", "Base Roughness", "Base anisotropy", "Base aniso. rot", "Base IOR", "Edgecolor", "Transl. fraction", "Transl.color", "Metalness", "refraction am.",
        "thin abs.color", "opacity", "Self-illumination", "Base bump", "Displacement", "Clearcoat am.", "Clearcoat IOR", "Clearcoat rough.", "Clearcoat abs.", "Clearcoat bump", "Sheen amount", "Sheen color",
        "Sheen rough.", "SSS amount", "SSS radius", "SSS scatt. color", "Vol. absorption", "Vol.scattering", "Base tail", "Reflect BG override", "Refract BG override") across:2 pos:[160, 30]
    groupBox group1 "copy from" pos:[10, 10] width:130 height:500
    groupBox group2 "copy to" pos:[150, 10] width:130 height:500

    button applycopy "Copy" pos:[50, 530] width:200 height:50

    on applycopy pressed do
    (
        local copyfrom = copy_fromRB.state
        local copyto = copy_toRB.state
        local MatArr = #()

        for o in selection where o.material != undefined do
            join MatArr (getClassInstances CoronaPhysicalMtl target:o.material)

        makeUniqueArray MatArr

        for mat in MatArr do
        (
            local a
			local b 
					
            
            case copyfrom of
            (
                 1: (a = mat.baseTexmap)
				 2: (a = mat.baseRoughnessTexmap)
				 3: (a = mat.baseAnisotropyTexmap)
				 4: (a = mat.baseAnisoRotationTexmap)
				 5: (a = mat.baseIorTexmap)
				 6: (a = mat.edgeColorTexmap)
				 7: (a = mat.translucencyFractionTexmap)
				 8: (a = mat.translucencyColorTexmap)
				 9: (a = mat.metalnessTexmap)
				10: (a = mat.refractionAmountTexmap)
				11: (a = mat.thinAbsorptionTexmap)
				12: (a = mat.opacityTexmap)
				13: (a = mat.selfIllumTexmap)
				14: (a = mat.baseBumpTexmap)
				15: (a = mat.displacementTexmap)
				16: (a = mat.clearcoatAmountTexmap)
				17: (a = mat.clearcoatIorTexmap)
				18: (a = mat.clearcoatRoughnessTexmap)
				19: (a = mat.clearcoatAbsorptionTexmap)
				20: (a = mat.clearcoatBumpTexmap)
				21: (a = mat.sheenAmountTexmap)
				22: (a = mat.sheenColorTexmap)
				23: (a = mat.sheenRoughnessTexmap)
				24: (a = mat.sssAmountTexmap)
				25: (a = mat.sssRadiusTexmap)
				26: (a = mat.sssScatterTexmap)
				27: (a = mat.volumetricAbsorptionTexmap)
				28: (a = mat.volumetricScatteringTexmap)
				29: (a = mat.baseTailTexmap)
				30: (a = mat.bgOverrideReflectTexmap)
				31: (a = mat.bgOverrideRefractTexmap)
				
				
               
            )

            case copyto of
            (
				 1: (b = mat.baseTexmap)
				 2: (b = mat.baseRoughnessTexmap)
				 3: (b = mat.baseAnisotropyTexmap)
				 4: (b = mat.baseAnisoRotationTexmap)
				 5: (b = mat.baseIorTexmap)
				 6: (b = mat.edgeColorTexmap)
				 7: (b = mat.translucencyFractionTexmap)
				 8: (b = mat.translucencyColorTexmap)
				 9: (b = mat.metalnessTexmap)
				10: (b = mat.refractionAmountTexmap)
				11: (b = mat.thinAbsorptionTexmap)
				12: (b = mat.opacityTexmap)
				13: (b = mat.selfIllumTexmap)
				14: (b = mat.baseBumpTexmap)
				15: (b = mat.displacementTexmap)
				16: (b = mat.clearcoatAmountTexmap)
				17: (b = mat.clearcoatIorTexmap)
				18: (b = mat.clearcoatRoughnessTexmap)
				19: (b = mat.clearcoatAbsorptionTexmap)
				20: (b = mat.clearcoatBumpTexmap)
				21: (b = mat.sheenAmountTexmap)
				22: (b = mat.sheenColorTexmap)
				23: (b = mat.sheenRoughnessTexmap)
				24: (b = mat.sssAmountTexmap)
				25: (b = mat.sssRadiusTexmap)
				26: (b = mat.sssScatterTexmap)
				27: (b = mat.volumetricAbsorptionTexmap)
				28: (b = mat.volumetricScatteringTexmap)
				29: (b = mat.baseTailTexmap)
				30: (b = mat.bgOverrideReflectTexmap)
				31: (b = mat.bgOverrideRefractTexmap)
                
            )

            if a != undefined do
			(	
						
			b = copy a
			
			print a
			print b
			)
		)
            MatArr = #()
	)
)


createDialog progressTest 290 600

If i place the “print a” and “print b” before the “b = copy a” i 've got a different result , for a i 've got the bitmap name and for b, i’ve got" undefined" which is fine because the slot is empty and if i place the print a and print b after the "b= copy a " then a and b print the bitmap name, no more undefined but nothin appear in the material, if someone could explain what is going on it would be much appreciated


#2

i ve tried something,

for obj in selection do ( local a = obj.material.baseTexmap local b = obj.material.baseRoughnessTexmap local c = obj.material.baseBumpTexmap b = copy a obj.material.baseBumpTexmap = b ) it is working but if i try

for obj in selection do ( local a = obj.material.baseTexmap local b = obj.material.baseRoughnessTexmap local c = obj.material.baseBumpTexmap b = copy a c = b ) it’s not working
i think i miss some knowledge about variables…


#3

i ve tried something,


for obj in selection do
	( 	
           local a = obj.material.baseTexmap
		local b = obj.material.baseRoughnessTexmap
		local c = obj.material.baseBumpTexmap
		b = copy a
		obj.material.baseBumpTexmap = b
	)

it is working but if i try


for obj in selection do
	( 	local a = obj.material.baseTexmap
		local b = obj.material.baseRoughnessTexmap
		local c = obj.material.baseBumpTexmap
		b = copy a
		c = b
	)

it’s not working
i think i miss some knowledge about variables…


#4

do it another way:


setproperty src_mat #baseBumpTexmap (getproperty trg_mat #baseTexmap)

-- or make a copy if you like:
setproperty src_mat #baseBumpTexmap (copy (getproperty trg_mat #baseTexmap))

#5

thank you Denis,
i try to copy texture slot within the same material, it’s intended to mass setup material from their diffuse map to automaticaly create bump and glossiness maps and other maps , i need to give the user the possiblity to choose any slot copied to any other slot.
in the example i posted, i try to transfer the diffuse map to the bump but it could be to the translucency or any other slot , that why i’ve setup the all 31 possible slot in a CoronaPhysicalMtl.
i need to use variables since it would be 961 possible case scenario .
when i don’t use variable and use explicit expression like
“obj.material.baseBumpTexmap = obj.material.baseTexmap” or “obj.material.baseBumpTexmap = copy obj.material.baseTexmap” it works well, but it will be a huge work to do the 961 different possible expression
thats why i try to do it with variables and i fail at every attempt.
in your example src_mat and trg_mat are different material, so i modified it a little to fit my needs

for obj in selection do
	( 	
	
	local src_mat = obj.material

setproperty src_mat #baseBumpTexmap  (copy(getproperty src_mat #baseTexmap))
		
	)

it works, but if i try with variables instead, i get some errors

for obj in selection do
	( 	
	local a = baseTexmap
	local b = baseBumpTexmap
	local src_mat = obj.material

setproperty src_mat #b  (copy(getproperty src_mat #a))
		
	)

here is the full script i am trying to make :

rollout Test "Corona copy material slot"
(
    radiobuttons copy_fromRB labels:#("Base color", "Base Roughness", "Base anisotropy", "Base aniso. rot", "Base IOR", "Edgecolor", "Transl. fraction", "Transl.color", "Metalness", "refraction am.",
        "thin abs.color", "opacity", "Self-illumination", "Base bump", "Displacement", "Clearcoat am.", "Clearcoat IOR", "Clearcoat rough.", "Clearcoat abs.", "Clearcoat bump", "Sheen amount", "Sheen color",
        "Sheen rough.", "SSS amount", "SSS radius", "SSS scatt. color", "Vol. absorption", "Vol.scattering", "Base tail", "Reflect BG override", "Refract BG override") pos:[15, 30]
    radiobuttons copy_toRB labels:#("Base color", "Base Roughness", "Base anisotropy", "Base aniso. rot", "Base IOR", "Edgecolor", "Transl. fraction", "Transl.color", "Metalness", "refraction am.",
        "thin abs.color", "opacity", "Self-illumination", "Base bump", "Displacement", "Clearcoat am.", "Clearcoat IOR", "Clearcoat rough.", "Clearcoat abs.", "Clearcoat bump", "Sheen amount", "Sheen color",
        "Sheen rough.", "SSS amount", "SSS radius", "SSS scatt. color", "Vol. absorption", "Vol.scattering", "Base tail", "Reflect BG override", "Refract BG override") across:2 pos:[160, 30]
    groupBox group1 "copy from" pos:[10, 10] width:130 height:500
    groupBox group2 "copy to" pos:[150, 10] width:130 height:500

    button applycopy "Copy" pos:[50, 530] width:200 height:50

    
 fn copyslot =
 (
	local copyfrom = copy_fromRB.state
	local copyto = copy_toRB.state
	local MatArr = #()

        for o in selection where o.material != undefined do
            join MatArr (getClassInstances CoronaPhysicalMtl target:o.material)

        makeUniqueArray MatArr

        for mat in MatArr do
        (
            local a
			local b 
					
            
            case copyfrom of
            (
                 1: (a = mat.baseTexmap)
				 2: (a = mat.baseRoughnessTexmap)
				 3: (a = mat.baseAnisotropyTexmap)
				 4: (a = mat.baseAnisoRotationTexmap)
				 5: (a = mat.baseIorTexmap)
				 6: (a = mat.edgeColorTexmap)
				 7: (a = mat.translucencyFractionTexmap)
				 8: (a = mat.translucencyColorTexmap)
				 9: (a = mat.metalnessTexmap)
				10: (a = mat.refractionAmountTexmap)
				11: (a = mat.thinAbsorptionTexmap)
				12: (a = mat.opacityTexmap)
				13: (a = mat.selfIllumTexmap)
				14: (a = mat.baseBumpTexmap)
				15: (a = mat.displacementTexmap)
				16: (a = mat.clearcoatAmountTexmap)
				17: (a = mat.clearcoatIorTexmap)
				18: (a = mat.clearcoatRoughnessTexmap)
				19: (a = mat.clearcoatAbsorptionTexmap)
				20: (a = mat.clearcoatBumpTexmap)
				21: (a = mat.sheenAmountTexmap)
				22: (a = mat.sheenColorTexmap)
				23: (a = mat.sheenRoughnessTexmap)
				24: (a = mat.sssAmountTexmap)
				25: (a = mat.sssRadiusTexmap)
				26: (a = mat.sssScatterTexmap)
				27: (a = mat.volumetricAbsorptionTexmap)
				28: (a = mat.volumetricScatteringTexmap)
				29: (a = mat.baseTailTexmap)
				30: (a = mat.bgOverrideReflectTexmap)
				31: (a = mat.bgOverrideRefractTexmap)
               
            )

            case copyto of
            (
				 1: (b = mat.baseTexmap)
				 2: (b = mat.baseRoughnessTexmap)
				 3: (b = mat.baseAnisotropyTexmap)
				 4: (b = mat.baseAnisoRotationTexmap)
				 5: (b = mat.baseIorTexmap)
				 6: (b = mat.edgeColorTexmap)
				 7: (b = mat.translucencyFractionTexmap)
				 8: (b = mat.translucencyColorTexmap)
				 9: (b = mat.metalnessTexmap)
				10: (b = mat.refractionAmountTexmap)
				11: (b = mat.thinAbsorptionTexmap)
				12: (b = mat.opacityTexmap)
				13: (b = mat.selfIllumTexmap)
				14: (b = mat.baseBumpTexmap)
				15: (b = mat.displacementTexmap)
				16: (b = mat.clearcoatAmountTexmap)
				17: (b = mat.clearcoatIorTexmap)
				18: (b = mat.clearcoatRoughnessTexmap)
				19: (b = mat.clearcoatAbsorptionTexmap)
				20: (b = mat.clearcoatBumpTexmap)
				21: (b = mat.sheenAmountTexmap)
				22: (b = mat.sheenColorTexmap)
				23: (b = mat.sheenRoughnessTexmap)
				24: (b = mat.sssAmountTexmap)
				25: (b = mat.sssRadiusTexmap)
				26: (b = mat.sssScatterTexmap)
				27: (b = mat.volumetricAbsorptionTexmap)
				28: (b = mat.volumetricScatteringTexmap)
				29: (b = mat.baseTailTexmap)
				30: (b = mat.bgOverrideReflectTexmap)
				31: (b = mat.bgOverrideRefractTexmap)
                
            )

            if a != undefined do
				
		
			
			b = copy a
			--print b
			
			
			
		)
            MatArr = #()
	)
	
	on applycopy pressed do
		
	copyslot()
	
	
)

createDialog Test 290 600

listener return the name of the map as it has copied it to the “b” slot but when i look at the material there is nothing in the “b” slot?


#6

i’ve manage to make the example work


for obj in selection do
( 	
    local a = "baseTexmap"
    local b = "baseBumpTexmap"
    local src_mat = obj.material

    setproperty src_mat b (copy (getproperty src_mat a))
)

now i’ll try in the actual script…


#7

It’s finally working, thank you Denis, you put me on the right path ;),
here is the full script if anyone interested


rollout Test "Corona copy material slot"
(
    radiobuttons copy_fromRB labels:#("Base color", "Base Roughness", "Base anisotropy", "Base aniso. rot", "Base IOR", "Edgecolor", "Transl. fraction", "Transl.color", "Metalness", "refraction am.",
        "thin abs.color", "opacity", "Self-illumination", "Base bump", "Displacement", "Clearcoat am.", "Clearcoat IOR", "Clearcoat rough.", "Clearcoat abs.", "Clearcoat bump", "Sheen amount", "Sheen color",
        "Sheen rough.", "SSS amount", "SSS radius", "SSS scatt. color", "Vol. absorption", "Vol.scattering", "Base tail", "Reflect BG override", "Refract BG override") pos:[15, 30]
    radiobuttons copy_toRB labels:#("Base color", "Base Roughness", "Base anisotropy", "Base aniso. rot", "Base IOR", "Edgecolor", "Transl. fraction", "Transl.color", "Metalness", "refraction am.",
        "thin abs.color", "opacity", "Self-illumination", "Base bump", "Displacement", "Clearcoat am.", "Clearcoat IOR", "Clearcoat rough.", "Clearcoat abs.", "Clearcoat bump", "Sheen amount", "Sheen color",
        "Sheen rough.", "SSS amount", "SSS radius", "SSS scatt. color", "Vol. absorption", "Vol.scattering", "Base tail", "Reflect BG override", "Refract BG override") across:2 pos:[160, 30]
    groupBox group1 "copy from" pos:[10, 10] width:130 height:500
    groupBox group2 "copy to" pos:[150, 10] width:130 height:500

    button applycopy "Copy" pos:[50, 530] width:200 height:50

    
 fn copyslot =
 (
	local copyfrom = copy_fromRB.state
	local copyto = copy_toRB.state
	local MatArr = #()

        for o in selection where o.material != undefined do
            join MatArr (getClassInstances CoronaPhysicalMtl target:o.material)

        makeUniqueArray MatArr

        for mat in MatArr do
        (
            local a
			local b 
					
            
            case copyfrom of
            (
                 1: a = "baseTexmap"
				 2: a = "baseRoughnessTexmap"
				 3: a = "baseAnisotropyTexmap"
				 4: a = "baseAnisoRotationTexmap"
				 5: a = "baseIorTexmap"
				 6: a = "edgeColorTexmap"
				 7: a = "translucencyFractionTexmap"
				 8: a = "translucencyColorTexmap"
				 9: a = "metalnessTexmap"
				10: a = "refractionAmountTexmap"
				11: a = "thinAbsorptionTexmap"
				12: a = "opacityTexmap"
				13: a = "selfIllumTexmap"
				14: a = "baseBumpTexmap"
				15: a = "displacementTexmap"
				16: a = "clearcoatAmountTexmap"
				17: a = "clearcoatIorTexmap"
				18: a = "clearcoatRoughnessTexmap"
				19: a = "clearcoatAbsorptionTexmap"
				20: a = "clearcoatBumpTexmap"
				21: a = "sheenAmountTexmap"
				22: a = "sheenColorTexmap"
				23: a = "sheenRoughnessTexmap"
				24: a = "sssAmountTexmap"
				25: a = "sssRadiusTexmap"
				26: a = "sssScatterTexmap"
				27: a = "volumetricAbsorptionTexmap"
				28: a = "volumetricScatteringTexmap"
				29: a = "baseTailTexmap"
				30: a = "bgOverrideReflectTexmap"
				31: a = "bgOverrideRefractTexmap"
               
            )

            case copyto of
            (
				 1: b = "baseTexmap"
				 2: b = "baseRoughnessTexmap"
				 3: b = "baseAnisotropyTexmap"
				 4: b = "baseAnisoRotationTexmap"
				 5: b = "baseIorTexmap"
				 6: b = "edgeColorTexmap"
				 7: b = "translucencyFractionTexmap"
				 8: b = "translucencyColorTexmap"
				 9: b = "metalnessTexmap"
				10: b = "refractionAmountTexmap"
				11: b = "thinAbsorptionTexmap"
				12: b = "opacityTexmap"
				13: b = "selfIllumTexmap"
				14: b = "baseBumpTexmap"
				15: b = "displacementTexmap"
				16: b = "clearcoatAmountTexmap"
				17: b = "clearcoatIorTexmap"
				18: b = "clearcoatRoughnessTexmap"
				19: b = "clearcoatAbsorptionTexmap"
				20: b = "clearcoatBumpTexmap"
				21: b = "sheenAmountTexmap"
				22: b = "sheenColorTexmap"
				23: b = "sheenRoughnessTexmap"
				24: b = "sssAmountTexmap"
				25: b = "sssRadiusTexmap"
				26: b = "sssScatterTexmap"
				27: b = "volumetricAbsorptionTexmap"
				28: b = "volumetricScatteringTexmap"
				29: b = "baseTailTexmap"
				30: b = "bgOverrideReflectTexmap"
				31: b = "bgOverrideRefractTexmap"
                
            )

            if a != undefined do
				
			
			
			

			setproperty mat b (copy (getproperty mat a))
			
			print a
			print b
			
			
		)
            MatArr = #()
	)
	
	on applycopy pressed do
		
	copyslot()
	
	
)


createDialog Test 290 600