Script to batch import Bitmaps on CoronaLayeredMtl Mask, render + save, and loop

Script to batch import Bitmaps on CoronaLayeredMtl Mask, render + save, and loop

tberthe
Explorer Explorer
964 Views
4 Replies
Message 1 of 5

Script to batch import Bitmaps on CoronaLayeredMtl Mask, render + save, and loop

tberthe
Explorer
Explorer

Hey everyone!

 

I'm trying to work on an object where I need to apply multiple masks, render and save them individually. I've attached a screenshot of the material, and it's the bitmap Mask on Layer 2 that needs to be changed each time from a folder of png files.

I tried with the code below, but since I'm not really experienced with MaxScript, I'm having some trouble making it work.

Would really appreciate it if anyone could help me out with this. 

 

aomBD.png

 

source_dir = "C:\\Users\\Thierry\\ScriptTextures\\"
output_dir = "C:\\Users\\Thierry\\ScriptOutput\\"


mat_name = "Material #841"
mat_node = getNodeByName mat_name


layer2_mask = mat_node.materialList[2].materialMapList[1]


png_files = getFiles (source_dir + "*.png")


for i = 1 to png_files.count do
(
    -- Change the mask to the next PNG file in the sequence
    layer2_mask.filename = png_files[i]

    -- Render the scene and save the output image
    output_file = output_dir + "render_" + i as string + ".png"
    rendSaveFile output_file
    render rendOutputFilename:(output_dir + "render_") outputfile:(output_dir + "render_" + i as string + ".png")
)


Thanks a lot !

0 Likes
Accepted solutions (1)
965 Views
4 Replies
Replies (4)
Message 2 of 5

ManniiCode
Contributor
Contributor

Without that screenshot, I wouldn't know that you're using Corona. Please specify the renderers and max version. Might be useful for next time.

 

Let's start with the full working code:

 

source_dir = "C:\\Users\\Thierry\\ScriptTextures\\"
output_dir = "C:\\Users\\Thierry\\ScriptOutput\\"

--I come back to add this following line in case someone copy it from here.
global mat_node

--Your mat name
mat_name = "Material #841"

--one way to collect mat_node, remember, this is using sceneMaterials, so, the material needs to be applied to something in the scene first.
for i in sceneMaterials where i.name == mat_name do (mat_node = i)

--collect png files
png_files = getFiles (source_dir + "*.png")

--here I rewrite your for loop from scratch.

for png_image in png_files do
(
	-- This changes the bitmap filename from the bitmap node to the new png
	-- mixmaps are masks for CoronaLayeredMtl
	-- [2] is the mask for layer 2
	mat_node.mixmaps[2].filename = png_image
	
	-- Close the render dialog before changing the settings, or it'll not work.
	renderSceneDialog.close()
	
	--That checkbox for the output
	rendSaveFile = true
	--The render output
	rendOutputFilename = output_dir + (getFilenameFile png_image) + ".png"
	--Hit render
	max quick render

	--Reopen the render dialog, this is optional.
	renderSceneDialog.open()
)

 

 

 

 

Now I'll explain why your code didn't work.

This is not for the material. It's for a scene object.

 

 

 

mat_node = getNodeByName mat_name

 

 

 

Not sure why ".materialList" is here. It's for Composite mtl, not CoronaLayeredMtl

 

 

 

layer2_mask = mat_node.materialList[2].materialMapList[1]

 

 

 

rendSaveFile accept Boolean value, not filename

 

 

 

rendSaveFile output_file

 

 

 

0 Likes
Message 3 of 5

tberthe
Explorer
Explorer

Thank you so much for taking the time to respond and explain your code, that's really great for beginners like me !

 

I tried your code and it is weird because I'm getting an error on another material (#826) which is the base material of Material #841. I added a screenshot of the slate editor because it might be a little specific then. The error I'm getting is 
--  Unknown property: "mixmaps" in Material #826: CoronaLegacyMtl

SlateEditor.jpg

0 Likes
Message 4 of 5

ManniiCode
Contributor
Contributor
Accepted solution

Hi! I tried recreating the issue and I got the same error.

If you run the script twice, it'll probably work the second time.

 

This is the fixed version of the full code.

 

 

source_dir = "C:\\Users\\Thierry\\ScriptTextures\\"
output_dir = "C:\\Users\\Thierry\\ScriptOutput\\"

global mat_node

--Your mat name
mat_name = "Material #841"

--one way to collect mat_node, remember, this is using sceneMaterials, so, the material needs to be applied to something in the scene first.
for i in sceneMaterials where i.name == mat_name do (mat_node = i)

--collect png files
png_files = getFiles (source_dir + "*.png")

--here I rewrite your for loop from scratch.

for png_image in png_files do
(
	-- This changes the bitmap filename from the bitmap node to the new png
	-- mixmaps are masks for CoronaLayeredMtl
	-- [2] is the mask for layer 2
	mat_node.mixmaps[2].filename = png_image
	
	-- Close the render dialog before changing the settings, or it'll not work.
	renderSceneDialog.close()
	
	--That checkbox for the output
	rendSaveFile = true
	--The render output
	rendOutputFilename = output_dir + (getFilenameFile png_image) + ".png"
	--Hit render
	max quick render

	--Reopen the render dialog, this is optional.
	renderSceneDialog.open()
)

 

 

 

Here, I didn't define the mat_node variable before, so, it's undefined.

 

 

for i in sceneMaterials where i.name == mat_name do (mat_node = i)

 

 

 

I fixed this by defining a mat_node variable in line 4

global mat_node

 

Regarding this error

--  Unknown property: "mixmaps" in Material #826: CoronaLegacyMtl

 I was unable to recreate this same error.

This error indicates that somehow the script grabbed the wrong material to operate on.

I tried recreating the same nodes as what you have in the screenshot with the same node names, but the issue didn't happen. 

0 Likes
Message 5 of 5

tberthe
Explorer
Explorer

@ManniiCode  thank you so much for your help it works perfectly.


I was still getting the error

--  Unknown property: "mixmaps" in Material #826: CoronaLegacyMtl

 

Turns out it is because the material was part of a Multi/Sub-Object material (you could not see it on my screenshot), so it was not assigned itself to any object in the scene.

It seems that it needs to be assigned itself to an object otherwise you get this error.

 

I created a random object outside the view and assigned the material itself to it, and there's no error anymore.

Thank you again for having taken the time to help, it is really a huge deal for me and I hope it helps others as well.

0 Likes