PDA

View Full Version : batch assign materials


pd
01-04-2008, 03:20 AM
Hi,

I'm on the brink of a very repetitive, dull task. There are nearly 500 objects that need a material in a recent scene of mine. Each object needs it's own material - they are all based on the same simple shader, though and only differ in a diffuse map(It's a standard material, 100% self illuminated). All those textures are finished already and are waiting to be applied.
The textures are named the same way as the objects are.

I can't say how hard it would be to create a script that automates this task, but I'd like to get some input here. I guess the script would have to do the following:

- take the first of the selected 500 objects and get its %name
- create a material(duplicate from a pre-defined material) and name it after the object
- assign a bitmap texture map to the diffuse slot. the path will be "textures\%name.jpg"
- go the next object and repeat the process until the last one is reached

Unfortunately, my maxscript skills are non-existent and since the deadline is coming close I won't be able to change this. I'm am however thankful for any comment.

If there is anyone out there, willing to write that script, I'd be able to reward him/her with a small fee.

Thanks!

RustyKnight
01-04-2008, 03:46 AM
Hi pd!

I can't see it be "very" difficult, famous last words...

so something like...
-- Get the master material from the material editor...
-- The material may come from any of the avaliable
-- material libraries...
local masterMaterial = meditMaterials["Master Material"]

-- Loop through all the objects in the scene...
-- This can be changed to loop through an array or the selection
for o in objects do (

-- Get the name of the current object
local sName = o.name

-- Make a copy of the master material
local materialCopy = (copy masterMaterial)
-- Change the name of the material to match that of the object
materialCopy.name = o.name
-- Assign the diffuseMap based on the name...
-- I've assumed a Standard material...you will need to check
-- your shaders requirements...
-- The path to the texture should also be absolute...but for this
-- example, I've used you path...
materialCopy.diffuseMap = BitMapTexture filename:("textures\\" + sName + ".jpg")

-- Finally assign the material to the copy
o.material = materialCopy

)
I've not tested this so other's may be able to clarify further, but there's a jumping off point...

U.S.S. Speed
01-04-2008, 04:19 AM
[edit]Oups... A bit late.

Here what I came with...
You can change the path to whatever you fit.
Copy paste in a empty text file... And name it something like BatchMat.ms and put it in UI/Macroscript/

If you do run script in the MaxScript menu, the macro will show up in the Customize > Customize User Interface window under the category Material.

macroScript BatchMaterial category:"Material" buttonText:"Batch Material" toolTip:"Batch Material"
(
curSelection = ($Selection) as array

for o in curSelection do
(
TheName = o.name

TheMat = standardMaterial()

TheMat.diffusemap = bitmapTexture()
TheMat.name = o.name

TheMat.Maps[2] = Bitmaptexture fileName:("C:\\Program Files\\Autodesk\\3ds Max 9\\Textures\\" + TheName + ".jpg")

o.material = TheMat
)
)

pd
01-04-2008, 04:22 AM
Wow, thank you so much for the effort. I had to change the first local to global and had a minor issue with the first backslash in the absolute path. Adding a second backslash did it however. I vaguely remember this being called escaping or something. Thanks a ton, mate.

pd
01-04-2008, 04:30 AM
Thanks a bunch U.S.S. Speed. Yours is even working on the current selection, that's fantastic. You guys saved me from a dull 2 days clickfest!

RustyKnight
01-04-2008, 04:32 AM
[edit]Oups... A bit late.

Here what I came with...
You can change the path to whatever you fit.
Copy paste in a empty text file... And name it something like BatchMat.ms and put it in UI/Macroscript/

If you do run script in the MaxScript menu, the macro will show up in the Customize > Customize User Interface window under the category Material.

macroScript BatchMaterial category:"Material" buttonText:"Batch Material" toolTip:"Batch Material"
(
curSelection = ($Selection) as array

for o in curSelection do
(
TheName = o.name

TheMat = standardMaterial()

TheMat.diffusemap = bitmapTexture()
TheMat.name = o.name

TheMat.Maps[2] = Bitmaptexture fileName:("C:\\Program Files\\Autodesk\\3ds Max 9\\Textures\\" + TheName + ".jpg")

o.material = TheMat
)
)

Nice work!

CGTalk Moderation
01-04-2008, 04:32 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.