You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
5.3 KiB
134 lines
5.3 KiB
"""Blender 头图渲染脚本。""" |
|
|
|
# pyright: reportMissingImports=false |
|
|
|
import bpy |
|
import numpy as np |
|
from pathlib import Path |
|
import os |
|
import argparse |
|
import sys |
|
|
|
|
|
bpy.ops.preferences.addon_enable(module="photogrammetry_importer") |
|
|
|
def load_obj_vertices(obj_file_path): |
|
""" |
|
使用bpy加载OBJ文件并获取顶点数据。 |
|
返回网格object和顶点数组。 |
|
""" |
|
# 清除当前场景中的所有网格对象 |
|
bpy.ops.object.select_all(action='DESELECT') |
|
bpy.ops.object.select_by_type(type='MESH') |
|
bpy.ops.object.delete(use_global=False, confirm=False) |
|
|
|
# 判断文件是否存在 |
|
if not os.path.exists(obj_file_path): |
|
print(f"{obj_file_path} not exist") |
|
return None, None |
|
|
|
# 导入OBJ |
|
bpy.ops.wm.obj_import(filepath=obj_file_path, forward_axis='Y', up_axis='Z') |
|
obj_original = [obj for obj in bpy.data.objects if obj.type == 'MESH'][0] |
|
bpy.context.view_layer.objects.active = obj_original |
|
obj_original.select_set(True) |
|
mesh = obj_original.data |
|
vertices = np.array([v.co for v in mesh.vertices]) |
|
return obj_original, vertices |
|
|
|
def render_and_save(camera, output_path, cam_name): |
|
""" |
|
设置渲染参数并执行渲染操作,最后保存为图片 |
|
:param camera: 相机对象 |
|
:param output_path: 输出图片的路径 |
|
:param cam_name: 相机名称 |
|
""" |
|
# 设置渲染引擎为 Workbench |
|
bpy.context.scene.render.engine = 'BLENDER_WORKBENCH' |
|
# bpy.context.scene.render.engine = 'WORKBENCH' |
|
# 设置 Workbench 渲染的着色模式为 Flat 光和 Texture 颜色 |
|
bpy.context.scene.display.shading.light = 'FLAT' |
|
# bpy.context.scene.display.shading.color_type = 'ATTRIBUTE' |
|
bpy.context.scene.display.shading.color_type = 'TEXTURE' |
|
# 设置 Viewtransform 为 standard |
|
bpy.context.scene.view_settings.view_transform = 'Standard' |
|
|
|
# 设置背景颜色为白色 |
|
bpy.context.scene.world.color = (1, 1, 1) |
|
|
|
# 设置输出路径 |
|
bpy.context.scene.render.filepath = os.path.join(output_path, f"123_2.jpg") |
|
# 设置图像格式为 PNG |
|
bpy.context.scene.render.image_settings.file_format = 'JPEG' |
|
# 设置渲染分辨率为 100% |
|
bpy.context.scene.render.resolution_percentage = 100 |
|
|
|
# 设置当前相机为活动相机 |
|
bpy.context.scene.camera = camera |
|
# 执行渲染操作 |
|
bpy.ops.render.render(write_still=True) |
|
|
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument("--sparse_file", type=str, required=True) |
|
parser.add_argument("--mesh_file", type=str, required=True) |
|
parser.add_argument("--output_path", type=str, required=True) |
|
args = parser.parse_args(sys.argv[sys.argv.index("--") + 1:]) |
|
# --------------------- 示例使用 --------------------- |
|
sparse_file = args.sparse_file |
|
mesh_file = args.mesh_file |
|
output_path = args.output_path |
|
|
|
|
|
# 调用操作符 |
|
bpy.ops.import_scene.colmap_model( |
|
directory = sparse_file, # 指向模型文件夹 |
|
import_points = False, # 导入稀疏点云 |
|
import_cameras = True, # 导入相机 |
|
add_background_images = False, # 把原始照片挂到相机上 |
|
adjust_render_settings = True, # 自动把渲染分辨率改成相机分辨率 |
|
adjust_clipping_distance= False # 自动把裁剪面设置得更合适 |
|
) |
|
print("导入成功!") |
|
|
|
mesh_object, vertices = load_obj_vertices(mesh_file) |
|
# 场景中查找相机名称为74_8_cam的相机 |
|
camera = None |
|
for obj in bpy.data.objects: |
|
if obj.type == 'CAMERA': |
|
camera = obj |
|
if camera.name == "123_2_cam": |
|
render_and_save(camera, str(output_path), camera.name) |
|
# break |
|
# 加载 mesh,得到 mesh_object 和 顶点数组 |
|
|
|
# if camera: |
|
# print("找到名称为74_8_cam的相机。") |
|
# else: |
|
# print("未找到名称为74_8_cam的相机。") |
|
|
|
""" |
|
blender --background --python /data/code/pose_toolkit/render_img.py -- \ |
|
--sparse_file "/data/datasets_20t/Downloads_google/rander_obj_image/2124/render_pose/" \ |
|
--mesh_file "/data/datasets_20t/Downloads_google/rander_obj_image/2124/2124.obj" \ |
|
--output_path "/data/datasets_20t/Downloads_google/rander_obj_image/2124/output" |
|
|
|
|
|
blender --background --python /data/code/pose_toolkit/render_img.py -- \ |
|
--sparse_file "/data/datasets_20t/Downloads_google/rander_obj_image/1030/render_pose/" \ |
|
--mesh_file "/data/datasets_20t/Downloads_google/rander_obj_image/1030/obj/1030.obj" \ |
|
--output_path "/data/datasets_20t/Downloads_google/rander_obj_image/1030/output" |
|
|
|
|
|
blender --background --python /data/code/pose_toolkit/render_img.py -- \ |
|
--sparse_file "/data/datasets_20t/Downloads_google/rander_obj_image/1026/render_pose/" \ |
|
--mesh_file "/data/datasets_20t/Downloads_google/rander_obj_image/1026/obj/1026.obj" \ |
|
--output_path "/data/datasets_20t/Downloads_google/rander_obj_image/1026/output" |
|
|
|
blender --background --python /data/code/pose_toolkit/render_img.py -- \ |
|
--sparse_file "/data/datasets_20t/Downloads_google/rander_obj_image/959/render_pose/" \ |
|
--mesh_file "/data/datasets_20t/Downloads_google/rander_obj_image/959/obj/959.obj" \ |
|
--output_path "/data/datasets_20t/Downloads_google/rander_obj_image/959/output" |
|
|
|
"""
|
|
|