Quantcast
Viewing latest article 1
Browse Latest Browse All 2

Answer by Secrop for How can I convert data from camera data node(view distance) into a file output(image or CSV or anything)?

Here's a small script that renders the scene and stores the Z_Pass into a CSV file, with {X, Y, Distance} in each entry.All you need to do is paste this in the text editor and execute.

import bpyimport numpy as npdest = 'C:/tmp/z_data.csv'backgroundD = 10**10def getCoordFn(x):    def gc(i):        return i%x, i//x    return gcdef getpixelz(image):    arr = np.array(image.pixels)    pix = arr[0:-1:4]    gc = getCoordFn(image.size[0])    zpix = [(gc(i), d) for i, d in enumerate(pix) if d < backgroundD]    return zpixdef writeCsv(data):    with open(dest, 'w') as file:        for p in data:            file.write(f'{p[0][0]} , {p[0][1]} , {p[1]}\n')def prepareRender():    rn = bpy.context.scene.render    rn.engine = 'CYCLES'    rn.image_settings.file_format = 'OPEN_EXR'    rn.image_settings.color_depth = '32'    cl = bpy.context.scene.cycles    cl.samples = 1    cl.pixel_filter_type = 'BOX'def prepareOutput():    scn = bpy.context.scene    scn.use_nodes = True    bpy.context.view_layer.use_pass_z = True    nt = scn.node_tree    nt.nodes.clear()    r = nt.nodes.new('CompositorNodeRLayers')          v = nt.nodes.new('CompositorNodeViewer')    nt.links.new(r.outputs['Depth'], v.inputs['Image'])  def execute():    prepareRender()    prepareOutput()    bpy.ops.render.render()    image = bpy.data.images['Viewer Node']    writeCsv(getpixelz(image))execute()

Note that this overwrites any setup you have in the compositor.


Viewing latest article 1
Browse Latest Browse All 2

Trending Articles