[Closed] Remove Previous Material and Assign new Issue
if any currentmateriallibrary material is an instance of a scene material:
for m in currentmateriallibrary collect
(
nodes = for n in (refs.dependentnodes m) where iskindof n.mat Material and n.mat.name == m.name collect n
#(m, nodes)
)
if every currentmateriallibrary material has unique name but material is not an instance of a scene material:
mats_array = #()
name_array = #()
for m in currentmateriallibrary do
(
append mats_array #(m, #())
append name_array m.name
)
for n in (geometry as array) where iskindof n.mat Material and (i = finditem name_array n.mat.name) != 0 do
(
append mats_array[i][2] n
)
mats_array
they are the fastest ways to search for…
Thanks Dennis,
From looking it sort of makes sense – I dont quite get soem of it but I will cross ref what you have provided with the help section.
Thanks for the assitance I will respond to let you know how things go.
Col
OK Dennis I have broken it down to understand whats going on and hopefully help others that come along with similar query. btw this script is awesome :applause:
--First part loops through the current material library and puts each material into an array (mats_Array) and puts the materials name into another (name_array).
mats_array = #() --Makes empty array
name_array = #() --Makes empty array
for m in currentmateriallibrary do --loop through every material in the current material library
(
append mats_array #(m, #()) --Adds each material (variable m) into the array and add an empty nested array.
append name_array m.name --adds the name of every material in this array.
)
--This part loops through each geometry in the scene, if the geometry has a material applied 'AND' the material name is found in the array 'name_array' (Created previously) then access the element (in array 'mats_array') containing the material, then add the geometry that has this material applied to it inside the nested (multidimensional) array (created above).
for n in (geometry as array) where iskindof n.mat Material and (i = finditem name_array n.mat.name) != 0 do -- make each geometry int he scene into an array and loop through it. Filter the search to ensure a material is applied to the geometry and find that material in the array of material names we created above (name_array), IF both are NOT 0 (false) then proceed.
(
append mats_array[i][2] n --add the geometry into the materials nested array.
)
mats_array --display the array
Thanks for the code it makes sense (i believe so anyway any coments i have misinterpretted please say so).
My next step is to take this information and in a other max file with the same geometry (nmes) I can search the scene for the geometry and assign the materials from the current material library (will have to be loaded first) – all the geometry and material names are stored in the mats_array.
Hi all,
Ok here is my script finished.
@Dennis I tried your method and although I managed to understand what it was doing I tried for hours to actually use it and access the multidimensional array and couldnt get it to work.
@Eric I tried your suggestion also and I couldnt get it to work I think it must be something to do with path literals or somethign alogn them lines when to addd in certain characters such as ‘’ “” /n. I used format instead which seemed to work unsure why the execute didnt – more practice and I am sure I will understand.
There are many stages to this script. the first one is not shown – i actually used MS to find all the objects and all the materials in a list. Once I gathered this list together I compoiled all the information and added more scripts to it – which is what you see below. I execute this script below into a file with older textures (that i want replacing with new ones) and replaces them.
loadMaterialLibrary "R:\material_Library.mat"
----Functions to determine which objects (that will be selected with the *) are to have one texture applied and the others to not. This is filtered via the numfaces on the object. These contain the array in here rather than further down.
fn flap_front =
(
objLuftwaffe_Grey = #()
objLuftwaffe_base_Colour = $shocker_front* as array + $flap_movable_rear* as array + $landing_gear_door_front* as array + $landing_gear_door_rear* as array + $p_winglet_points* as array + $turbine_holder* as array + $wing_flap_rear* as array + $wing_front* as array + $winglet* as array
for i in $flap_front* do
(
[INDENT]a = i.numfaces
case (if a != 97 then a = 1 else a = 97) of
(
[INDENT]1 : append objLuftwaffe_Grey i
97 : append objLuftwaffe_base_Colour i
) [/INDENT]
) [/INDENT]
)
fn turbine =
(
objLuftwaffe_Engine_Outer_Casing = #()
for i in $turbine* where i.numfaces == 622 do
(
append objLuftwaffe_Engine_Outer_Casing i
)
)
--list of all the arrays with there respective objects inside.
objBlack = $wiper* as array
objGlass = $p_headlight_glass* as array
objLuftwaffe_Main_Base = $chassis* as array
objLuftwaffe_Slats = $flap_movable_front* as array
objLuftwaffe_Tails = $tail* as array + $tail_rudder*
objMaterial_1658 = $p_hinge_parts* as array + $p_hinge_rod* as array + $p_landing_gear_parts_lower* as array + $p_landing_gear_parts_rear* as array objMaterial_2164 = $wing_rear* as array
objMaterial24 = $wing_lines* as array
objMetal_1 = $p_flap_support* as array + $p_headlight_chrome* as array + $p_hinge_pivot* as array + $p_suspention_cable* as array + $p_wing_hinges_front* as array objMetal_2 = $door_suspention* as array + $p_headlight_cone* as array + $rim_front* as array + $rim_rear* as array + $suspention_hinge* as array + $suspwntion_hinge* as array
objRed_Light = $navigation_light* as array
objTires = $tyre_* as array objWindow_Frames = $window_frame* as array
flap_front()
turbine()
objLuftwaffe_Turbine = $p_turbine_fan* as array
objLuftwaffe_Engine_Exterior = $turbine_exhaust* as array objMaterial_1132 = $p_landing_gear_parts_front* as array
objMaterial_1521 = $landing_gear_front* as array + $landing_gear_lower_rear* as array + $landing_gear_upper_rear* as array
--Assigning the respective materials to the Arrays that contain the geometry.
objBlack.material = currentMaterialLibrary["black"]
objGlass.material = currentMaterialLibrary["glass"]
objLuftwaffe_Main_Base.material = currentMaterialLibrary["Luftwaffe_Main_Base"] objLuftwaffe_Slats.material = currentMaterialLibrary["Luftwaffe_Slats"] objLuftwaffe_Tails.material = currentMaterialLibrary["Luftwaffe_Tails"] objMaterial_1658.material = currentMaterialLibrary["Material_#1658"] objMaterial_2164.material = currentMaterialLibrary["Material_#2164"]
objMaterial24.material = currentMaterialLibrary["material24"]
objMetal_1.material = currentMaterialLibrary["metal_1"]
objMetal_2.material = currentMaterialLibrary["metal_2"]
objRed_Light.material = currentMaterialLibrary["Red_Light"]
objTires.material = currentMaterialLibrary["tires"]
objWindow_Frames.material = currentMaterialLibrary["Window_Frames"] objLuftwaffe_Grey.material = currentMaterialLibrary["Luftwaffe_Grey"] objLuftwaffe_base_Colour.material = currentMaterialLibrary["Luftwaffe_base_Colour"] objLuftwaffe_Engine_Outer_Casing.material = currentMaterialLibrary["Luftwaffe_Engine_Outer_Casing"]
objLuftwaffe_Turbine.material = currentMaterialLibrary["Luftwaffe_Turbine"] objLuftwaffe_Engine_Exterior.material = currentMaterialLibrary["Luftwaffe_Engine_Exterior"] objMaterial_1132.material = currentMaterialLibrary["Material_#1132"] objMaterial_1521.material = currentMaterialLibrary["Material_#1521"]
If anyone has any methods they wish to share, or how I can improve a certain aspect such as all the as arrays I have, maybe I dont need to have them all their is a easier way.
Anyways THANKS to everyone for helping this process has been very educational for me and although has been a long journey it has been worthwhile and will help the project greatly.
Thank you.
Alan