Blender:导入obj渲染及导出图片+深度图+法向图

本文介绍如何使用Blender软件渲染模型并输出深度图、法向图及影像图。主要内容包括:导入模型文件、设置相机参数、配置渲染设置等步骤。适合初学者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

数据样本是深度学习的基础,尤其是针对几何类的深度学习问题。目前网上已经有许许多多的数据集,其中尤其多的是用Blender得到的,例如BlendedMVS。
在本博客中,只是简单的尝试一下用Blender渲染一下模型,然后导出影像和深度图和法向图。
值得注意的是,本博主充分发挥了“能用就行”的原则,对细节几乎都不求甚解。

参考资料


https://blenderartists.org/t/render-settings-for-depth-normal-albedo-in-2-80/1199454
https://github.com/panmari/stanford-shapenet-renderer/blob/master/render_blender.py

结果

由于最终结果均匀exr文件,需要使用RenderDoc查看。

深度图

在这里插入图片描述

法向图

在这里插入图片描述

影像图

在这里插入图片描述

流程

1. 导入obj模型

首先第一步必然是导入obj模型,代码如下,仅仅需要指定obj文件所在文件夹即可。

import bpy 
import os 
 

# folder path for importing data files
in_dir_ply = r"C:\Users\Kevin\Desktop\5a3cb4e4270f0e3f14d12f43\textured_mesh"
lst_ply = os.listdir(in_dir_ply)
 

# Filter file list by valid file types.
file = []
for item in lst_ply:
    fileName, fileExtension = os.path.splitext(item)
    if fileExtension == ".obj":
        file.append(item)
 
# To import mesh.ply in batches
for i in range(len(file)):
    bpy.ops.import_scene.obj(filepath=os.path.join(in_dir_ply,file[i]))

以下就是BlendedMVS的一个模型的导入结果,如左上图所示
在这里插入图片描述

2. 设置相机位置和姿态

当然这里可能还需要设置一下灯啊啥的,我就不写了。

import bpy

tx = 0.0
ty = 0.0
tz = -5.0

rx = 180.0
ry = 0.0
rz = 0.0


pi = 3.14159265


camera = bpy.data.objects['Camera']

# Set camera rotation in euler angles
camera.rotation_mode = 'XYZ'
camera.rotation_euler[0] = rx*(pi/180.0)
camera.rotation_euler[1] = ry*(pi/180.0)
camera.rotation_euler[2] = rz*(pi/180.0)

# Set camera translation
camera.location.x = tx
camera.location.y = ty
camera.location.z = tz
  
3. 导出渲染结果
# Set up rendering of depth map.
import bpy

bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links
bpy.context.scene.render.image_settings.color_depth = '32'
bpy.context.scene.render.image_settings.color_mode = 'RGB'

# 必须设置,否则无法输出深度
bpy.context.scene.render.image_settings.file_format = "OPEN_EXR"

# 必须设置,否则无法输出法向
bpy.context.view_layer.use_pass_normal = True 

# Clear default nodes
for n in tree.nodes:
    tree.nodes.remove(n)

# Create input render layer node.
render_layers = tree.nodes.new('CompositorNodeRLayers')

depth_file_output = tree.nodes.new(type="CompositorNodeOutputFile")
depth_file_output.label = 'Depth Output'
links.new(render_layers.outputs['Depth'], depth_file_output.inputs[0])

scale_normal = tree.nodes.new(type="CompositorNodeMixRGB")
scale_normal.blend_type = 'MULTIPLY'
scale_normal.inputs[2].default_value = (0.5, 0.5, 0.5, 1)
links.new(render_layers.outputs['Normal'], scale_normal.inputs[1])
bias_normal = tree.nodes.new(type="CompositorNodeMixRGB")
bias_normal.blend_type = 'ADD'
bias_normal.inputs[2].default_value = (0.5, 0.5, 0.5, 0)
links.new(scale_normal.outputs[0], bias_normal.inputs[1])
normal_file_output = tree.nodes.new(type="CompositorNodeOutputFile")
normal_file_output.label = 'Normal Output'
links.new(bias_normal.outputs[0], normal_file_output.inputs[0])

image_file_output = tree.nodes.new(type="CompositorNodeOutputFile")
image_file_output.label = 'Image'
links.new(render_layers.outputs['Image'], image_file_output.inputs[0])

scene = bpy.context.scene
# 设置输出分辨率,可以自行修改
scene.render.resolution_x = 300
scene.render.resolution_y = 200

scene.render.resolution_percentage = 100
cam = scene.objects['Camera']

# fov
cam.data.angle = 10*(3.1415926/180.0)

for output_node in [depth_file_output, normal_file_output, image_file_output]:
    output_node.base_path = ''

# 输出路径            
scene.render.filepath = r'C:\Users\Kevin\Desktop\5a3cb4e4270f0e3f14d12f43\\'
depth_file_output.file_slots[0].path = scene.render.filepath + 'depth_'
normal_file_output.file_slots[0].path = scene.render.filepath + 'normal_'
image_file_output.file_slots[0].path = scene.render.filepath + 'image_'

bpy.ops.render.render()

总结

总的来说,第一次用Blender,感觉还有诸多不懂,先记一下代码。

评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值