[Closed] scripted zdepth renders
Hello all, I have another question that I’d be overjoyed if someone could assist me. I have an automated render script for what I’m working on, but have had issues making it at all functional with zdepth channel for a separate later to be used render. If anyone could give me some very basic assistance on getting a zdepth gray scale image to use that would be most helpful.
RLA, RPF and OpenEXR can all save Z-Depth as part of their file format. Most other applications won’t read these, however (combustion does, of course – Photoshop can with ProEXR if you’re using the OpenEXR format, and I think it might open RLA with the Z-Buffer (but none of the others?)).
If you absolutely must have a separate file…
Have you looked at the File Output render effect? This render effect has a Z-Depth output option.
If you absolutely must write your own, look into…
<bitmap>getChannelAsMask <bitmap with Z-Buffer> #zdepth
Which will automatically use the closest to furthest z-depth values to map to a greyscale output. You can’t specify the min/max ranges yourself.
If you have to specify the min/max ranges yourself, then use:
<array (with single float value)>getChannel <bitmap with Z-Buffer> [x,y] #zdepth
Then map that float value to the greyscale range you desire. Note that you can’t get a whole row at a time as you could with getPixels – only 1 pixel at a time… so it can be rather slow.
====
Edit: just pondering that your tool may not need a render effect at all – in which case you can also try setting up a Z-Depth Render Element (check the docs on that one), or even go ghetto with applying a self-illuminating material that uses a Falloff map set to Distance to all objects.
Try all objects pure white, self illuminated material, then add black fog to your scene to make a ZDepth pass.
Or even easier/faster – Standard Material with a falloff in the selfillum. Falloff type: Camera Distance. Dont forget to put diffuse color to black or it will be multiplied with the selfillum and you can control the Z with the near/far distance in the falloff.
Forgot to mention, you need to set environment ranges on your camera to control depth, render with scanline, nice n quick.
I personally prefer this to using falloff near/far, as gives u more control.
Dave
lots of options
Depending on the intended use, I’d probably go with the Falloff method… you’re already going to replace your original materials, so you might as well add some code that would have the new materials retain any transparency of the original materials; this becomes important if you have opacity-mapped surfaces as most other methods will record the z-depth of the geometry (as they should), regardless of transparency, while fogging tends to dislike transparency (have to hit the logarithmic option, etc.)
Sorry, I’m still confused, what code do I actually append to my maxscript in order to save out a zdepth grayscale bitmap that’s as small (KB not pixels) as possible?
The rest of my render scripts are looking like. I have a bunch of variables defined elsewhere and this works for what I’m already doing.
render camera: cam fromframe: 9 toframe: 360 outputwidth: UPCS quiet: true outputheight: UPCS outputfile: (imagename as string)
cam3 = Targetcamera orthoProjection:true target:(targetObject pos:centroid)
cam3.name = "temp.bounding.cam.3"
cam3.pos = [centroid.x,centroid.y,centroid.z + radius]
viewport.setCamera cam3
render camera: cam3 outputwidth: UPCS outputheight: UPCS outputfile: ((newImageFolder as string) + "\\" + "Top" +".png")
cam3.pos = [centroid.x,centroid.y,centroid.z - radius]
render camera: cam3 outputwidth: UPCS outputheight: UPCS outputfile: ((newImageFolder as string) + "\\" + "Bottom" +".png")
What I want is to take those Camera 3 top and bottom views and render another copy of them that’s some small format of image as a grayscale zdepth image. Being able to do this for a masks channel would be useful as well, thank you very much for you assistance in advance.
Well, if you don’t have materials with opacity, you can do the falloff trick I’ve mentioned in the earlier post and do a material override to the scene and then just render the same cam’s and save as JPG I guess…
And if they do have opacity? Some of the things that need to be fed through have glass pieces which could be a real pain with that.
Also I’m confused as to how to script the material and its attributes, sorry for my complete lack of knowledge in the area.
No problem, glad to help.
I’ve wrote a script that works only with mentalray arch and design materials, it works with opacity and by opacity I mean cutout maps, like trees and stuff, it works OK. But things like glass it doesnt, actually I havent thought of that yet, and I really dont know how a Max Render Element Z pass handles that also… but either way you can use this script and then do a mask of the glasses and defocus the things that are seen thru the glasses in a diferent layer… dunno, it’s just a wild guess. Here’s the script, hope it helps.
(
local z_pass_main
local maxbgcolor
local maxuseenv
local maxlstate
local mateditstate
local z_pass_main
fn FreeStuff bol =
(
with undo off
(
case bol of
(
true:
(
clearUndoBuffer()
gc()
--disableSceneRedraw()
if MatEditor.isOpen() then
(
MatEditor.Close()
mateditstate=true
)
else mateditstate=false
--freeSceneBitmaps()
)
false:
(
clearUndoBuffer()
gc()
--freeSceneBitmaps()
--enableSceneRedraw()
--completeRedraw()
if mateditstate==true then MatEditor.Open()
)
)
)
)
fn setRolloutsOnOff roll bol =
(
with undo off
(
case bol of
(
true:
(
if roll!=z_pass_main then z_pass_main.controls.enabled=true
)
false:
(
if roll!=z_pass_main then z_pass_main.controls.enabled=false
)
)
)
)
fn SetMaxLights bol =
(
with undo off
(
case bol of
(
true:
(
for i in maxlstate do
(
i.on=true
)
)
false:
(
maxlstate=#()
for i in lights do
(
if superclassof i==light then
(
if i.on==true then
(
append maxlstate i
i.on=false
)
)
)
)
)
)
)
fn SetMaxBG mode bol =
(
with undo off
(
case mode of
(
"white":
(
ibgcolor=color 255 255 255
)
"black":
(
ibgcolor=color 0 0 0
)
)
case bol of
(
true:
(
useEnvironmentMap=maxuseenv
backgroundColor=maxbgcolor
)
false:
(
maxuseenv=useEnvironmentMap
useEnvironmentMap=off
maxbgcolor=backgroundColor
backgroundColor=ibgcolor
)
)
)
)
fn setMtlParam mtl param val =
(
with undo off
(
if (isProperty mtl param) then SetProperty mtl param val
)
)
fn getMtlParam mtl param =
(
with undo off
(
if (isProperty mtl param) then
(
val=GetProperty mtl param
return val
)
)
)
fn setMtlZSpecParam omat fmat =
(
local t
t=getMtlParam omat #name
setMtlParam fmat #name t
t=getMtlParam omat #diff_color
setMtlParam fmat #diff_color t
t=getMtlParam omat #diff_color_map_on
setMtlParam fmat #diff_color_map_on t
t=getMtlParam omat #diff_rough
setMtlParam fmat #diff_rough t
t=getMtlParam omat #refl_weight
setMtlParam fmat #refl_weight t
t=getMtlParam omat #refr_weight
setMtlParam fmat #refr_weight t
t=getMtlParam omat #add_color_map
setMtlParam fmat #add_color_map t
t=getMtlParam omat #add_color_map_on
setMtlParam fmat #add_color_map_on t
)
rollout z_pass_main "Z" width:200 height:200
(
checkbox chkonoff "On/Off" align:#center
spinner sp_near "Near Distance" range:[0,100000,0] type:#float align:#center
spinner sp_far "Far Distance" range:[0,100000,100] type:#float align:#center
local zsurface
on z_pass_main open do
(
zsurface=falloff()
zsurface.type=4
zsurface.direction=0
zsurface.nearDistance=0
zsurface.farDistance=100
zsurface.name="Z_Surface"
)
on z_pass_main close do
(
if chkonoff.state==true then chkonoff.changed false
)
on chkonoff changed state do
(
case of
(
(state==true):
(
FreeStuff true
setRolloutsOnOff z_pass_main false
SetMaxLights false
SetMaxBG "black" false
ss=substring maxFileName 1 (maxFilename.count-4)
ss="$matlibs/"+ss+"_Z.mat"
saveTempMaterialLibrary scenematerials ss
for imat in scenematerials do
(
case classof imat of
(
mrArchMaterial:
(
t=imat.name+"_Z"
setMtlParam imat #name t
setMtlParam imat #diff_color (color 0 0 0 255)
setMtlParam imat #diff_color_map_on off
setMtlParam imat #diff_rough 0
setMtlParam imat #refl_weight 0
setMtlParam imat #refr_weight 0
setMtlParam imat #add_color_map_on on
setMtlParam imat #add_color_map zsurface
)
MultiMaterial:
(
t=imat.name+"_Z"
setMtlParam imat #name t
for u = 1 to imat.numsubs do
(
if (classof imat[u]==mrArchMaterial) then
(
t=imat[u].name+"_Z"
setMtlParam imat[u] #name t
setMtlParam imat[u] #diff_color (color 0 0 0 255)
setMtlParam imat[u] #diff_color_map_on off
setMtlParam imat[u] #diff_rough 0
setMtlParam imat[u] #refl_weight 0
setMtlParam imat[u] #refr_weight 0
setMtlParam imat[u] #add_color_map_on on
setMtlParam imat[u] #add_color_map zsurface
)
)
)
)
)
)
(state==false):
(
ss=substring maxFileName 1 (maxFilename.count-4)
ss="$matlibs/"+ss+"_Z.mat"
tscene=loadTempMaterialLibrary ss
for tmat in tscene do
(
for imat in scenematerials do
(
tmatname=tmat.name+"_Z"
if imat.name==tmatname then
(
case classof imat of
(
mrArchMaterial:
(
setMtlZSpecParam tmat imat
)
MultiMaterial:
(
t=tmat.name
setMtlParam imat #name t
for u = 1 to imat.numsubs do
(
if (classof imat[u]==mrArchMaterial) then
(
setMtlZSpecParam tmat[u] imat[u]
)
)
)
)
)
)
)
setRolloutsOnOff z_pass_main true
SetMaxLights true
setMaxBG "black" true
FreeStuff false
)
)
)
on sp_far changed arg do
(
zsurface.farDistance=arg
)
on sp_near changed arg do
(
zsurface.nearDistance=arg
)
)
gc light:true
main_passes_floater = newrolloutfloater "Render Passes" 200 510
addrollout z_pass_main main_passes_floater
)
PS : Be careful of the gc() that I have in the script and the “with undo off” if you dont want to loose your undo stack.
Aaah, Zbuffer… has anyone ever got a good composite out of it. I know in fusion it requires a coverage channel too and renderID (i think) 2 extra channels to work with a zbuffer properly. But i never have gotten that to work.Anyone has. I know it’s the best way, because it’s pixel accurate, a fallof has antialiasing but technically that’s wrong to work with in comp. And don’t get me started about transparancy, it’s a nightmare really. I would really like to see some working workflow for transparancy and pixel accurate z buffers…
-Johan
Everything works fine… the problem is that only afterFX and combustion has the right RPF and RLA import module…