Notifications
Clear all

[Closed] Remove Previous Material and Assign new Issue

Hi everyone,

I am having a few issues which worked when testing but when trying to do it issues arrised and I have no idea how to solve it and what I have read I do not understand. Any adivce is greatly appreciate. Note I am new, please excuse the code if it is no optimized, I will attempt this later when it works.

Task

We have made many max files containing the same aircraft and each has different animations and other objects inside. We wish to take this aircraft change the textures and re-render the files again. Since I will be applying this script to the same aircraft containing the same ‘named objects’ I decided to use this as a means of removing and applying the materials.

Code


--Function that removes all materials applied to the geometry in the array.
fn matRemoval = 
	(
		--Array containing the aircraft geometry
		objArray = #($tail01,$tail_support02,$landing_gear_door_rear06,$chassis01,$wing_rear02,$tail_support03,$window_frame_front02,$window_frame_side02,$window_frame_rear02,$landing_gear_door_rear07,$tail_rudder01,$wing_flap_rear02,$wing_flap_rear03,$navigation_light02,$turbine_holder02,$turbine02,$p_turbine_fan02,$flap_movable_rear11,$flap_front18,$flap_front19,$flap_front20,$flap_front21,$flap_front22,$flap_front23,$flap_front24,$p_flap_support02,$turbine_holder03,$turbine03,$flap_front25,$landing_gear_door_rear08,$p_landing_gear_parts_rear02,$door_suspention02,$suspention_hinge10,$p_hinge_pivot04,$suspention_hinge11,$landing_gear_upper_rear02,$suspention_hinge12,$p_hinge_parts03,$suspwntion_hinge02,$p_hinge_pivot05,$landing_gear_lower_rear02,$tyre_rear08,$tyre_rear09,$rim_rear08,$rim_rear09,$rim_rear10,$rim_rear11,$p_landing_gear_parts_lower02,$p_hinge_rod02,$suspention_hinge13,$p_hinge_parts04,$tyre_rear10,$tyre_rear11,$suspention_hinge14,$flap_front26,$flap_front27,$flap_front28,$flap_front29,$flap_front30,$flap_front31,$flap_front32,$p_flap_support03,$p_winglet_points02,$winglet02,$navigation_light03,$flap_movable_rear12,$flap_movable_rear13,$flap_movable_rear14,$flap_movable_rear15,$flap_front33,$window_frame_front03,$window_frame_rear03,$window_frame_side03,$wiper02,$wiper03,$turbine_exhaust02,$turbine_exhaust03,$wing_rear03,$wing_lines03,$p_turbine_fan03,$wing_front02,$flap_movable_front04,$wing_front03,$flap_movable_front03,$p_wing_hinges_front03,$flap_movable_rear16,$wing_lines04,$winglet03,$p_winglet_points03,$p_wing_hinges_front02,$landing_gear_door_rear05,$p_landing_gear_parts_rear01,$door_suspention01,$suspention_hinge05,$p_hinge_pivot02,$suspention_hinge06,$landing_gear_upper_rear01,$suspention_hinge07,$p_hinge_parts01,$suspwntion_hinge01,$p_hinge_pivot03,$landing_gear_lower_rear01,$tyre_rear04,$tyre_rear05,$rim_rear04,$rim_rear05,$rim_rear06,$rim_rear07,$p_landing_gear_parts_lower01,$p_hinge_rod01,$suspention_hinge08,$p_hinge_parts02,$tyre_rear06,$tyre_rear07,$suspention_hinge09,$landing_gear_door_front_small03,$landing_gear_door_front_small02,$landing_gear_door_front02,$landing_gear_door_front03,$landing_gear_front01,$p_headlight_cone02,$p_headlight_glass02,$p_headlight_chrome02,$p_headlight_glass03,$p_headlight_chrome03,$p_headlight_cone03,$shocker_front01,$p_landing_gear_parts_front01,$tyre_front02,$tyre_front03,$rim_front02,$rim_front03)

		--Wipes all the materials from the objects in the array.
		for i=1 to objArray.count do
		 (
			objArray[i].material = undefined
		 )
	
	)

--Function that applies new materials
fn applyNewMats =
	(
		--Arrays containing objects that are to have the same material applied to them.
		matEngineOuterCasing = #($turbine02,$turbine03)
		matTires = #($tyre_rear08,$tyre_rear09,$tyre_rear10,$tyre_rear11,$tyre_rear04,$tyre_rear05,$tyre_rear06,$tyre_rear07,$tyre_front02,$tyre_front03)
		matBaseColour = #($landing_gear_door_rear06,$landing_gear_door_rear07,$wing_flap_rear02,$wing_flap_rear03,$turbine_holder02,$flap_movable_rear11,$turbine_holder03,$flap_front25,$landing_gear_door_rear08,$p_winglet_points02,$winglet02,$flap_movable_rear12,$flap_movable_rear13,$flap_movable_rear14,$flap_movable_rear15,$flap_front33,$wing_front02,$wing_front03,$flap_movable_rear16,$winglet03,$p_winglet_points03,$landing_gear_door_rear05,$landing_gear_door_front_small03,$landing_gear_door_front_small02,$landing_gear_door_front02,$landing_gear_door_front03)
		matGrey = #($tail_support02,$tail_support03,$flap_front18,$flap_front19,$flap_front20,$flap_front21,$flap_front22,$flap_front23,$flap_front24,$flap_front26,$flap_front27,$flap_front28,$flap_front29,$flap_front30,$flap_front31,$flap_front32)
		matMainBase = #($chassis01)
		matSlats = #($flap_movable_front04,$flap_movable_front03)
		matEngineExterior = #($p_turbine_fan02,$landing_gear_upper_rear02,$suspention_hinge12,$suspwntion_hinge02,$landing_gear_lower_rear02,$suspention_hinge13,$suspention_hinge14,$turbine_exhaust02,$turbine_exhaust03,$p_turbine_fan03,$landing_gear_front01,$p_landing_gear_parts_front01)
		mat_2164 = #($wing_rear02,$wing_rear03)
		matTails = #($tail_rudder01,$tail01)
		matGlass = #($p_headlight_glass02,$p_headlight_glass03)
		matCanadianBase = #($shocker_front01)
		matBlank15 = #($window_frame_front02,$window_frame_side02,$window_frame_rear02,$window_frame_front03,$window_frame_rear03,$window_frame_side03)
		mat_1521 = #($suspention_hinge09,$suspention_hinge08,$suspwntion_hinge01,$suspention_hinge07,$landing_gear_upper_rear01)
		matMetal_1 = #($p_flap_support02,$p_hinge_pivot04,$p_suspention_cable05,$p_suspention_cable06,$p_hinge_pivot05,$p_flap_support03,$p_wing_hinges_front03,$p_wing_hinges_front02,$p_hinge_pivot02,$p_suspention_cable04,$p_suspention_cable03,$p_hinge_pivot03,$p_headlight_chrome02,$p_headlight_chrome03)
		matMetal_2 = #($rim_front03,$rim_front02,$p_headlight_cone03,$p_headlight_cone02,$rim_rear07,$rim_rear06,$rim_rear05,$rim_rear04,$suspention_hinge06,$suspention_hinge05,$door_suspention01,$rim_rear11,$rim_rear10,$rim_rear09,$rim_rear08,$suspention_hinge11,$suspention_hinge10,$door_suspention02)
		mat_1658 = #($p_landing_gear_parts_rear02,$p_hinge_parts03,$p_landing_gear_parts_lower02,$p_hinge_rod02,$p_hinge_parts04,$p_landing_gear_parts_rear01,$p_hinge_parts01,$p_landing_gear_parts_lower01,$p_hinge_rod01,$p_hinge_parts02)
		mat_24 = #($wing_lines03,$wing_lines04)
		mat_1132 = #($p_landing_gear_parts_front01)
		mat_1788 = #($landing_gear_lower_rear01)
		mat_1036 = #($landing_gear_front01)
		matBlack = #($wiper03,$wiper02)
		mat_08 = #($navigation_light02, $navigation_light03)	
		
		--Load a material Library from the specified location
		loadMaterialLibrary "R:\MaterialLibraryFile.mat"
		
		--Assign the named material from the current library to the array
		matTires.material = currentMaterialLibrary["tires"]
		matEngineOuterCasing.material = currentMaterialLibrary["Luftwaffe Engine_Outer_Casing"]
		matBaseColour.material = currentMaterialLibrary["Luftwaffe base Colour"]
		matGrey.material = currentMaterialLibrary["Luftwaffe _Grey"]
		matMainBase.material = currentMaterialLibrary["Luftwaffe main Base"]
		matSlats.material = currentMaterialLibrary["Luftwaffe Slats"]
		matEngineExterior.material = currentMaterialLibrary["Luftwaffe_Engine_Exterior"]
		mat_2164.material = currentMaterialLibrary["Material #2164"]
		matTails.material = currentMaterialLibrary["Luftwaffe Tails"]
		matCanadianBase.material = currentMaterialLibrary["Canadian_Base"]
		matGlass.material = currentMaterialLibrary["glass"]
		matBlank15.material = currentMaterialLibrary["Blank 15"]
		mat_1521.material = currentMaterialLibrary["Material #1521"]
		matMetal_1.material = currentMaterialLibrary["metal_1"]
		matMetal_2.material = currentMaterialLibrary["metal_2"]
		mat_1658.material = currentMaterialLibrary["Material #1658"]
		mat_24.material = currentMaterialLibrary["material24"]
		mat_1132.material = currentMaterialLibrary["Material #1132"]
		mat_1788.material = currentMaterialLibrary["Material #1788"]
		mat_1036.material = currentMaterialLibrary["Material #1036"]
		matBlack.material = currentMaterialLibrary["black"]
		mat_08.material = currentMaterialLibrary["08 - Default"]
	)

 --executing the functions	
matRemoval()
applyNewMats()

I would like to thank everyone in advance for your help.

Col

13 Replies
 PEN

You don’t need to remove them first, you can just apply new ones. Do you have a question in there some where?

 JHN

Maybe you can post what the problem is with the script, error messages etc. Since you don’t provide a sample scene/matlib it’s hard to guess what errors did arise. And it’s too much work (well for me anyway ;)) to build a sample scene myself.

But if I’m speculating, I’m not certain you can assign materials to arrays of objects like that. Maybe you have to do a for loop just like when your cleaning the materials.

Please provide some more info,

Thanks,
-Johan

hello

You don’t need to remove them first, you can just apply new ones. Do you have a question in there some where?

Your absolutely correct – it is mainly visual clarification for myself as textures go black when unassigned so when i reapply materials I can clearly see which havnt worked. My apologies I should have added in my errors messages.

Maybe you can post what the problem is with the script, error messages etc. Since you don’t provide a sample scene/matlib it’s hard to guess what errors did arise. And it’s too much work (well for me anyway ) to build a sample scene myself. But if I’m speculating, I’m not certain you can assign materials to arrays of objects like that. Maybe you have to do a for loop just like when your cleaning the materials. Please provide some more info, Thanks, -Johan

the script worked when experiementing on one (which contained all the material I wanted on anyway I just removed them and then reapplied – all semed to work perfectly – until I tried it on a max scene with the old materials applied to it.

Please see below the error messages I receive when executing the script (Without the ‘matRemoval’ function).

applyNewMats()  
-- Error occurred in applyNewMats(); filename: C:\Filename.ms; position: 6315; line: 58  --  
Frame:  
--   matBlack: #($wiper03, $wiper02)  
--   Mat_24: #($wing_lines03, $wing_lines04)  
--   matMetal_2: #($rim_front03, $rim_front02, $p_headlight_cone03, $p_headlight_cone02, undefined, undefined, undefined, undefined, undefined, undefined, undefined, $rim_rear11, $rim_rear10, $rim_rear09, $rim_rear08, $suspention_hinge11, $suspention_hinge10, $door_suspention02)  
--   mat_08: #($navigation_light02, $navigation_light03)  
--   mat_1788: #(undefined)  
--   matSlats: #(undefined, $flap_movable_front03)  
--   mat_1521: #(undefined, undefined, undefined, undefined, undefined)  
--   matEngineOuterCasing: #($turbine02, $turbine03)  
--   matBlank15: #($window_frame_front02, $window_frame_side02, $window_frame_rear02, $window_frame_front03, $window_frame_rear03, $window_frame_side03)  
--   matEngineExterior: #($p_turbine_fan02, $landing_gear_upper_rear02, $suspention_hinge12, $suspwntion_hinge02, $landing_gear_lower_rear02, $suspention_hinge13, $suspention_hinge14, $turbine_exhaust02, $turbine_exhaust03, $p_turbine_fan03, $landing_gear_front01, $p_landing_gear_parts_front01)  
--   matMetal_1: #($p_flap_support02, $p_hinge_pivot04, $p_suspention_cable05, $p_suspention_cable06, $p_hinge_pivot05, $p_flap_support03, $p_wing_hinges_front03, $p_wing_hinges_front02, undefined, undefined, undefined, undefined, $p_headlight_chrome02, $p_headlight_chrome03)  
--   mat_1658: #($p_landing_gear_parts_rear02, $p_hinge_parts03, $p_landing_gear_parts_lower02, $p_hinge_rod02, $p_hinge_parts04, undefined, undefined, undefined, undefined, undefined)  
--   mat_2164: #($wing_rear02, $wing_rear03)  
--   mat_1036: #($landing_gear_front01)  
--   matTails: #($tail_rudder01, $tail01)  
--   mat_1132: #($p_landing_gear_parts_front01)  
--   matBaseColour: #($landing_gear_door_rear06, $landing_gear_door_rear07, $wing_flap_rear02, $wing_flap_rear03, $turbine_holder02, $flap_movable_rear11, $turbine_holder03, $flap_front25, $landing_gear_door_rear08, $p_winglet_points02, $winglet02, $flap_movable_rear12, $flap_movable_rear13, $flap_movable_rear14, $flap_movable_rear15, $flap_front33, $wing_front02, $wing_front03, $flap_movable_rear16, $winglet03, ...)  
--   matGlass: #($p_headlight_glass02, $p_headlight_glass03)  
--   matGrey: #($tail_support02, $tail_support03, $flap_front18, $flap_front19, $flap_front20, $flap_front21, $flap_front22, $flap_front23, $flap_front24, $flap_front26, $flap_front27, $flap_front28, $flap_front29, $flap_front30, $flap_front31, $flap_front32)  
--   matMainBase: #($chassis01)  
--   matCanadianBase: #($shocker_front01)  
-- Unknown property: "material" in undefined  
OK

I initially thought the same Johan but applying the material to the array worked previously…

Thanks for the help and sorry for nto being more clearer.

Col

P.S. Ok looking at it more closely it appears the file I opened to change from one material set to another actually as inconsistencies with the model names…after executing an array there was some ‘undefined’ elements, when i execute an array without any issues the material can then be applied. But due to the arrays (containing the geometry) obviously needing to be called first then the material assignment doesnt get reached due to error. I will have to put some code to tell the script that is a element is undefined skip it and continue.

I will post back when I manage to incorporate this.

Thank you.

 JHN

Looks like a naming error somehow.
What I would suggest is to wrap all material assignment calls in a try catch context.

For example:


try(matTires.material = currentMaterialLibrary["tires"])catch(print "Error in tires")

That way the script won’t crash and you can determine where the error occurs more exactly.

Welcome to the world of debugging!

-Johan

hehe thanks you beat me to it, i found the cause of this error i will incorporate a solution and mention the results. thanks for the cathall.

Ok it appears the newer version someone had renamed the models, so when search via model names I was getting errors. I have gone back into the original file of the aircraft took all the names and manually ensured the newer version had the correct names.

Last time i did this procedure I done it a long and tedious way, now i want to try a better way but i am a little bit stuck any guidance is appreciated.

Task

[ol]
[li]For each material in the current library make a array
[/li][li]Each array needs to have a logical name such as: arrMat_MaterialName
[/li][li]Search through each geometry for the material name.
[/li][li]put this object into the relevant array (if it has ‘Material A’ assign it would go into array ‘arrMat_Material A’
[/li][/ol]

Once I have this I can use the information in another script to assign the material to the objects. which will replace the old with the new

Code


for i = 1 to currentMaterialLibrary.count do -- Their are 23 materials
(

                 --STUCK Help please,  I need to generate an empty array #() for each material with a logical name.
currentMaterialLibrary[i].name = #() -- Doesnt work cannot convert string
	
		for m = 1 to geometry.count where geometry[m].material.name == currentMaterialLibrary[i].name do 
		(
			print geometry[m].name -- Here I will appendIfUnique to the above array
		)
		
)

If anyone has ‘any’ suggesstions at all I am happy to hear them.

Thank you.

Col

Ok i have done what was required – although manually! :banghead: Code is below and everything in red I changed 23 times in order to get each array with the correct geometry inside.

		
[B]arr08_Default[/B] = #()			  	  		
for m = 1 to geometry.count where geometry[m].material.name == currentMaterialLibrary[[B]1[/B]].name do   		
(  			
appendIfUnique [B]arr08_Default[/B] geometry[m].name  			 		
)

I know what needs to be done (I think) but I cant find out how to do it. If anyone has the answer please comment here so in future I can do similar processes once instead of 23 times.

Thanks

Col

You would want to create a string and wrap it in a execute command. The result of the execute will be a variable equal to the name and the array assigned:

execute (currentMaterialLibrary[i].name+" = #()") -- currentMaterialLibrary[i].name returns a string, you then add the rest of the code in side a string.

-Eric

Thanks Eric

I just read through the MaxScript help and at the first read it does not make any sense. I will implement what you have typed and this should help me to fully understand when I see what happens.

Thank you for your response.

Col

Page 1 / 2