Skip to content

Instantly share code, notes, and snippets.

@teessider
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teessider/e45ac1ca80368a0643d7 to your computer and use it in GitHub Desktop.
Save teessider/e45ac1ca80368a0643d7 to your computer and use it in GitHub Desktop.
MAXScript - Example Material Batch Render. This script runs through and renders the assets with different materials that you specify also uses the object and material names for the filename.
/*
tsab_myMatBatchRender.ms
Example Material Batch Render Script in 3ds Max 2014
For my blog post on using MAXScript for automation in 3ds Max:
http://www.andybellart.co.uk/blog/tech/maxscript/a-maxscript-persective-the-power-of-automation#more-286
Developed by Andrew "teessider" Bell
Copyright 2015
*/
(
-- These are the variables which are being declared here for just the script.
local matBatch
local currentSel
local imagePath
global rl_egMatBatch
/*
Define the rollout as a global variable so that the DestroyDialog
can close any rollout with that name.
*/
-- SETS PNG OPTIONS (Give user options for this in future iteration)
pngio.settype(#true24) --setting png option to be 8bits/channel
pngio.setAlpha false --Don't render an alpha (for now at least)
local png = ".png" -- png filetype as string
Try(DestroyDialog rl_egMatBatch)catch()
Rollout rl_egMatBatch "TSAB Batch Render"
(
button render_btn "Batch Render"
on render_btn pressed do
(
imagePath = getSavePath() --opens save dialog browser to get file path
if imagePath != undefined then
(
imagePath += "\\" -- appends these to complete a valid file path
)
print imagePath
currentSel = selection as array
-- In order to "keep" the current selection, I've made a new variable to store it.
-- only need to change everytime the button is pressed.
--user will be able to define these materials in future iteration
--the loop is used in this case for demonstration purposes. There are 4 materials in the example slots 1-4
for i = 1 to 4 do
(
matBatch i
)
)
)
Createdialog rl_egMatBatch
fn matBatch mat1 =
(
--define the current selection(s) material
currentSel.material = meditMaterials[mat1]
--Variables for the strings of the assets for the final filename. --------------------
-- the if statement is for groups and single object.
if selection.count == 1 then
local objName = currentSel[1].name as string
else
local objName = currentSel[2].name as string
local objMat = meditMaterials[mat1].name as string
local uScore = "_" -- Variable instead of writing "_" all the time and its used multiple times!
local hyphen = "-" -- This is mainly for the date (eg 2015-03-05)
local dateManip -- Setup the variable for my date formatting function. This may end up in a separate script in future.
-- Custom function for formatting the date values so that they have a 0 if they are single digits eg 5 = 05
fn dateManip dateVal = -- Function has 1 argument (the index of date array)
(
formattedPrint dateVal format: "02u"
/*
FORMATTEDPRINT HELP
formats the value so that there is a leading zero - 0
and becomes two digits - 2
and is an unsigned decimal integer - u
> It also converts it to a string! <
*/
)
-- Setting up the "Dynamic Date" variables
-- Careful with this as it relies on Operating System (OS) time/date settings so it could change the index values
local gLocalTime = getLocalTime()
local year = gLocalTime[1] as string
-- Use function to format month and day
local month = dateManip gLocalTime[2]
local day = dateManip gLocalTime[4]-- 4 equals day of the month
local tempSS = stringStream "" -- Create a temp stringStream to store the strings for the filename.
-- The % signs are placeholders for the strings to be formatted, arguments come afterwords.
-- Add all my date values together to form my date for filename.
format "%%%%%" \
year \
hyphen \
month \
hyphen \
day to:tempSS
myDate = tempSS as string
free tempSS -- clear the stringstream so i can use it again!
-- Add all the object attributes (eg. object name and material) to the date for filename.
format "%%%%%" \
objName \
uScore \
objMat \
uScore \
myDate to:tempSS
objFileName = tempSS as string
free tempSS
-- Finally add the file path before the filename and end with the image type.
format "%%%" \
imagePath \ --file path
objFileName \ --object name and material
png to:tempSS --image type
imageFile = tempSS as string
---------------------------------------------------------------------------------------
-- Where imagePath is undefined (cancelled), it catches the error and goes back to main rollout.
-- May try and put it a conditional statement in the future.
try
(
render \ -- Each argument is separated onto a new line with the \ otherwise it would be a very long line!
camera: $camera001 \ -- Explicitly select camera001
outputsize: [640, 480] \ --[width, height in pixels] - Fixed (static) at the moment.
outputfile: imageFile \
vfb: false \
progressbar: true
-- Make sure to change the file output directory to wherever you wish.
-- Next iteration will have this as a choice in the GUI
)
catch()
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment