Commit 57620a51 authored by Oleg Dzhimiev's avatar Oleg Dzhimiev
Browse files

initial

parents
Loading
Loading
Loading
Loading
+106 −0
Original line number Diff line number Diff line
import bpy
import numpy as np
import os

import sys

'''
Usage:

~$ blender -b -P blender_generate_image_and_depth.py -- file1,file2,..,fileN
or
~$ <path-to-blender>/blender -b -P blender_generate_image_and_depth.py -- file1,file2,..,fileN

  - file names must include path
  - must be .obj format
  - at the same path all accompanying files must be present - .mtl and textures
  - no space after commas
  - blender executable must be in the path
    - ~$ which blender
    - alternatively can call from blender path

Example:

~$ blender -b -P blender_generate_image_and_depth.py -- input/1527256815_550165_v01/1527256815_550165.obj,input/1527256858_750165_v01/1527256858_750165.obj

Output:

  - <model_output_folder>/name-image.jpeg - RGB
  - <model_output_folder>/name-depth.exr  - D, 32-bit float data

Comment:

  - for some reason it worked ok with Blender v2.80.75
  - didn't work for some models in Blender v2.82.7

'''

model_output_folder = "output"

scene = bpy.context.scene
world = scene.world
camera = scene.camera

scene.render.resolution_x = 2592
scene.render.resolution_y = 1902
scene.render.resolution_percentage = 100

world.use_nodes = False
world.color = (1.00 , 1.00 , 1.00)

objs = bpy.data.objects
objs.remove(objs["Cube"], do_unlink=True)
objs.remove(objs["Light"], do_unlink=True)

camera.location = (0,0,0)
camera.rotation_mode = "XYZ"
camera.rotation_euler = (90*np.pi/180.0, 0, 0)
camera.data.clip_end = 10000
camera.data.clip_start = 0.1
camera.data.angle = 66.8*np.pi/180

bpy.context.scene.use_nodes = True

rn = scene.node_tree.nodes.new('CompositorNodeRLayers')
depth = scene.node_tree.nodes.new('CompositorNodeOutputFile')
depth.format.file_format = 'OPEN_EXR'
depth.format.color_depth = '32'
depth.format.color_mode = 'RGB'
depth.format.exr_codec = 'ZIP'
depth.base_path = model_output_folder
depth.file_slots[0].path = 'depth-'

scene.node_tree.links.new(rn.outputs[2], depth.inputs[0])

fpaths = sys.argv[-1].split(',')

TEST=False
if (TEST):
    fpaths = ['input/1527256815_550165_v01/1527256815_550165.obj',
              'input/1527256903_350165/v03/1527256903_350165.obj',
              'input/1488240695_710844_x3d105/1488240695_710844.obj']

print(fpaths)

if(not (os.path.isdir(model_output_folder))):
    os.mkdir(model_output_folder)

for fpath in fpaths:

    obj_name = fpath.split('/')[-1][:-4]
    print(obj_name)

    bpy.ops.import_scene.obj(filepath=fpath, use_smooth_groups=False)

    obj = bpy.data.objects[obj_name]
    for mat in obj.material_slots:
        mat.material.use_nodes = True

    scene.render.filepath = os.path.join(model_output_folder,obj_name+"-image.jpeg")
    scene.render.image_settings.file_format='JPEG'
    bpy.ops.render.render(write_still = True)
    bpy.data.objects.remove(obj)
    # rename exr
    os.rename(os.path.join(model_output_folder,'depth-0001.exr'),os.path.join(model_output_folder,obj_name+"-depth.exr"))

exr2png.py

0 → 100644
+57 −0
Original line number Diff line number Diff line
import OpenEXR
import Imath
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from PIL import Image

# From here: https://gist.github.com/arseniy-panfilov/4dc8fc5131277affe64619b1a9d00da0
def exr2arr(exrfile):
    file = OpenEXR.InputFile(exrfile)
    dw = file.header()['dataWindow']

    channels = file.header()['channels'].keys()
    print(channels)
    channels_list = list()
    for c in ('R', 'G', 'B', 'A'):
        if c in channels:
            channels_list.append(c)

    #size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
    size = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1)
    color_channels = file.channels(channels_list, Imath.PixelType(Imath.PixelType.FLOAT))
    channels_tuple = [np.fromstring(channel, dtype='f') for channel in color_channels]
    res = np.dstack(channels_tuple)
    res.reshape(size + (len(channels_tuple),))
    return res[:,:,0]


if __name__ == "__main__":
    fname = "depth-0001.exr"
    arr = exr_to_array(fname)
    print(arr.shape)

    dmap = arr[:,:,0]
    print(np.max(dmap))

    dmap[dmap==65504]=0
    dmap[dmap==10000000000.0]=0

    print(np.max(dmap))

    k = 1
    #k = 0xffff/np.max(dmap)
    #k = 5 # 20cm
    k = 100 # 1 cm
    #k = 10 # 10cm /unit

    print("scale = "+str(k))
    dmap = k*dmap
    dmap[dmap>65535]=0

    im = Image.fromarray(dmap.astype(np.uint16))
    im.save('model-depth.png')

    plt.imshow(dmap)
    plt.colorbar()
    plt.show()
 No newline at end of file

obj2rgbd.py

0 → 100644
+17 −0
Original line number Diff line number Diff line
import os
import subprocess

import exr2png as e2p

PATH_TO_BLENDER = ""

fpaths = ['input/1527256815_550165_v01/1527256815_550165.obj',
            'input/1527256903_350165/v03/1527256903_350165.obj',
            'input/1488240695_710844_x3d105/1488240695_710844.obj']

filelist = ','.join(fpaths)

# this will run by Blender's bundled python
subprocess.run(os.path.join(PATH_TO_BLENDER,'blender')+" -b -P blender_generate_image_and_depth.py -- "+filelist, shell=True)

model_output_folder = "output"
 No newline at end of file