Exercise – Create Multisub Material from imagefolder

This is a small example of the practical use of maxscript…

The scenario is this..

  1. You have a folder of image files in different sizes.
  2. Create a VrayMaterial for every image file
  3. 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