PDA

View Full Version : scripted zdepth renders


mugenxero
10-06-2008, 09:15 PM
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.

ZeBoxx2
10-06-2008, 09:22 PM
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.

DaveWortley
10-06-2008, 11:20 PM
Try all objects pure white, self illuminated material, then add black fog to your scene to make a ZDepth pass.

Kameleon
10-07-2008, 10:54 AM
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.

LoneRobot
10-07-2008, 11:32 AM
sweet tip, artur :bowdown:

DaveWortley
10-08-2008, 05:29 PM
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

ZeBoxx2
10-08-2008, 06:34 PM
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.)

mugenxero
10-14-2008, 10:19 PM
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.

Kameleon
10-14-2008, 10:32 PM
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...

mugenxero
10-14-2008, 10:36 PM
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.

Kameleon
10-14-2008, 10:58 PM
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.

JHN
10-15-2008, 03:03 PM
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

ZeBoxx2
10-15-2008, 06:08 PM
Your workflow quest hinges on the word 'working', though.

anti-aliasing is a problem you can't easily get around... there's two basic options there...
A. Don't anti-alias. Problem completely solved but you need to render a larger image and you don't get to take advantage of any adaptive anti-aliasing routines.
B. render everything in layers (render layers, that is). 3ds max does support render layers, and with per-layer z-buffer data, a post-app that would take advantage of this would have a 'correct' z-depth value for each pixel regardless of anti-aliasing (as the anti-aliasing becomes a mask for the layer's pixels, rather than pixels getting blended together on each layer).
I say 'correct' in single quotes because it's still the z-depth as measured, typically, through the center of the ray.. there is no integration of the entire surface underneath that pixel to get a more accurate value. Unlikely to have a huge impact, but there ya go.

transparency is relatively easy, as long as it isn't refractive transparency; the ray passes straight through. Z-Buffers ignore transparency so you're out of luck on that one, but a material replacement routine could preserve such simple transparency... the result would then work with both Falloff:Distance as well as Fog (as long as the fog plays nice with transparency) type solutions.

The above should work for the vast majority of scenes.

--

But now you've got something as simple as a mirror in the scene.. all of a sudden you don't just need the z-depth value for the hit on the mirror, but also for the hit of the reflected ray. You can't get both in one go with a material replacement method.
Worse still is having a glass object... the surface hit, the reflected ray hit, the refracted ray hit - perhaps another refracted ray hit if it hits an inner surface first, and so on.
Before long, you realize that for panacea you would need per-layer, per-element, per-raydepth, with ray association data, and probably stuff I'm forgetting, z-depth values... hellish on the renderer and I'd imagine not a simple task for writing a compositor for either.
So instead, for these special cases, studios tend to send the output to the 2D guy and ask them to paint things to specification and they'll have it done in no time; it might not be mathematically accurate but it'll be done and it'll probably look either good or even better than the mathematically accurate result would have been.. usually this is part of the regular process of post-work anyway. They can still benefit greatly from the z-depth data output in the earlier two sections, of course.

gasmasocet
03-13-2009, 02:44 PM
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....

j-man
03-13-2009, 04:17 PM
...as long as it isn't refractive transparency; ...

afterall, who's using that kind of stuff nowdays < ; ?


J.

ZeBoxx2
03-13-2009, 04:47 PM
exactly! raytracing.. pshaw ;)

gasmasocet
03-15-2009, 09:54 AM
Hmmmm... actualy a lot of people using this stufff nowdays... PRMAN for example...

ZeBoxx2
03-15-2009, 12:03 PM
woosh ;)
http://www.urbandictionary.com/define.php?term=woosh

gasmasocet
03-15-2009, 06:20 PM
Very usefulll...

CGTalk Moderation
03-15-2009, 06:20 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.