MaxScript Beginner - 05 Basics, materials and textures
MaxScript Beginner - 05 Materials and textures
set active renderer
--This sets the current renderer to
--the latest version of Vray if you
--have it installed
renderers.current = Vray()
-- Store the current render in a variable
MyVray = renderers.current
-- Now you can inspect what properties
-- you can reach from maxscipt
-- Just press shift + enter to run this line
ShowProperties MyVray
-- and look in the listener to see what
-- properties are available
--Quite a few..
print all texture paths in all bitmapTextures in the current scene
AllBitmapsInMyScene = GetClassInstances bitmapTex
for bm in AllBitmapsInMyScene do
(
print bm.filename
)
Update all bitmap paths to a new root folder
--This updates all paths to a new root folder
--IMPORTANT it ONLY updates bitmaps... NOT
--VrayHdri or other map. If you want to support more maps
--You have to get all instances of that class
NewRootFolder = "c:\\temp"
--you can use this command to show
--a dialog to select a folder
--NewRootFolder = getSavePath()
AllBitmapsInMyScene = GetClassInstances bitmapTex
for bm in AllBitmapsInMyScene do
(
TheFilename = getFileNameFile bm.filename
TheFileType = getFileNameType bm.filename
bm.filename = (NewRootFolder + "\\" + TheFilename + TheFileType)
)
change names on materials
--this prints all the names of the materials..
for m in sceneMaterials do (
print m.name
)
--this changes the names
for m in sceneMaterials do (
m.name = ("Test" + m.name )
)
Select objects with a certain material
--This is the name of the material
FindThisMat = "03 - Default"
--This tries to find the instance of the Material with the name "03 - Default"
--Remember the for where collect always returns an array
mat = for m in sceneMaterials where m.name == FindThisMat collect m
--This selects all objects in the scene with that material applied to it
--check all objects if they have the same material as the first Material in
--our array created above
TheMats = for item in objects where item.material == mat[1] collect item
Select TheMats
Exercise - Create Multisub Material from imagefolder
This is a small example of the practical use of maxscript… The scenario is this…
- You have a folder of image files in different sizes.
- Create a VrayMaterial for every image file
- Add all materials to a MultiSubMaterial
To make this more modular I will create a function that creates a VrayMaterial from a texturePath.
--UseFul function that checks if a filepath works..
fn existFile fname = (getfiles fname).count != 0
fn MakeVrayMaterialFromTexture TheTexturePath = (
if (existFile TheTexturePath) then (
--Create a new Vray Mat
theNewMat = VrayMtl()
--New bitmap
theBitmap = bitmapTex()
theBitmap.fileName = TheTexturePath
theNewMat.texmap_diffuse = theBitmap
--Just for fun we rename the Material to the bitmapName
theNewMat.name = ( (getFilenameFile TheTexturePath) + " - AutoGen" )
return theNewMat
)
else (
print ( "The texture path: " + TheTexturePath + " does not exist" )
)
)
--define the folder where the images reside
MyTextureFolder = "c:\\temp\\"
--Create an array of all png files inside that folder
MyPngTextures = getFiles (MyTextureFolder + "*.png")
--Now lets create a multisub material
MyMultiSub = multisubMaterial()
--Change the multisub count to match the number of textures
MyMultiSub.count = MyPngTextures.count
--Loop through all textures in
--the MyPngTextures array
for t = 1 to MyPngTextures.count do (
NewMat = MakeVrayMaterialFromTexture MyPngTextures[t]
MyMultiSub[t] = NewMat
--This turns on the show map in viewport button
showTextureMap MyMultiSub[t] true
)
--Load the multisub into the second slot in the material editor
setMeditMaterial 2 MyMultiSub
Exercise - Randomize Material ID
This small scripts applies a Material modifier to all selected nodes and adds a random value to all of them.
--Randomize the material ID on selection...
NumberOfMultiMaterials = 8
for item in (selection as array) do (
rndVal = random 1 NumberOfMultiMaterials
--This clears already existing Material modifiers
ExistMod = for m in item.modifiers where m.name == "MyRandMat" collect m
for m in ExistMod do ( deleteModifier item m)
Mat = materialModifier name:"MyRandMat"
Mat.materialID = rndVal
AddModifier item Mat
)
--end script