-
Notifications
You must be signed in to change notification settings - Fork 488
Open
Labels
questionQuestion, not yet a bug ;)Question, not yet a bug ;)
Description
Describe the issue
Incorrect situations were encountered when rendering roughness and metallicity pixel by pixel.
The rendered RGB images have incorrect colors, and the positions of objects in the images are shifted. Both the blend files and glb files exported from Blender 3.6 and 4.5 have been tested, but the issue persists.
Minimal code example
import blenderproc as bproc
import numpy as np
from blenderproc.python.utility.BlenderUtility import load_image, get_all_blender_mesh_objects
from blenderproc.python.types.MeshObjectUtility import MeshObject, get_all_mesh_objects
from blenderproc.python.types.MeshObjectUtility import get_all_mesh_objects, convert_to_meshes
import imageio
import os
import cv2
import numpy as np
import json
# -------------------------- 配置参数(与参考脚本对齐) --------------------------
# 请根据实际路径修改以下参数
BLEND_PATH = "C:/Users/yyyzzz/linux-nk/script_gen_data/blender_download/blender_files/hotdog45.blend" # 模型路径
GLB_PATH = "C:/Users/yyyzzz/linux-nk/script_gen_data/blender_download/blender_files/hotdog45.glb"
HDR_PATH = "C:/Users/yyyzzz/linux-nk/script_gen_data/blender_download/code/nerfactor/high_res_envmaps_1k" # HDR环境贴图路径(对应参考脚本的light_path)
CAM_DIR = "C:/Users/yyyzzz/linux-nk/script_gen_data/blender_download/cameras" # 相机参数JSON文件所在目录(对应参考脚本的cam_dir)
OUTPUT_DIR = "./output" # 输出目录
RES = 512 # 渲染分辨率(正方形,对应参考脚本的FLAGS.res)
SPP = 256 # 每像素采样数(对应参考脚本的FLAGS.spp)
LIGHT_INTEN = 3 # 环境光强度(对应参考脚本的FLAGS.light_inten)
MODE = {"train": 6, "test": 98, "eval": 6} # 渲染模式(train/val/test,对应参考脚本的模式)
# HDR_LIST = ['bridge', 'city', 'courtyard', 'fireplace', 'forest', 'house', 'interior', 'museum', 'night', 'snow', 'square', 'studio', 'sunrise', 'sunset', 'tunnel']
HDR_LIST = ['bridge']
# ------------------------------------------------------------------------------
def render_view(hdr_name, output_path, mode):
# hdr_path = HDR_PATH + f"/{hdr_name}.hdr"
# bproc.world.set_world_background_hdr_img(hdr_path)
# Create a point light next to it
light = bproc.types.Light()
light.set_location([2, -2, 0])
light.set_energy(300)
bproc.renderer.set_output_format(enable_transparency=True)
bproc.renderer.enable_segmentation_output(map_by=["cp_roughness", "cp_metallic", "cp_specular", 'instance'],default_values={'cp_roughness': None, 'cp_metallic': None, 'cp_specular': None})
bproc.renderer.enable_normals_output()
bproc.renderer.enable_diffuse_color_output()
# Render the scene
data = bproc.renderer.render()
for j in range(MODE[mode]):
dir_name = f"{mode}_{j:03d}"
mode_dir = os.path.join(output_path, dir_name)
os.makedirs(mode_dir, exist_ok=True)
for i, arr in enumerate(data["cp_roughness_segmaps"]):
dir_name = f"{mode}_{i:03d}"
save_path = os.path.join(output_path, dir_name)
image_save = os.path.join(save_path, "roughness.exr")
cv2.imwrite(image_save, arr.astype(np.float32))
for i, arr in enumerate(data["cp_metallic_segmaps"]):
dir_name = f"{mode}_{i:03d}"
save_path = os.path.join(output_path, dir_name)
image_save = os.path.join(save_path, "metallic.exr")
cv2.imwrite(image_save, arr.astype(np.float32))
for i, arr in enumerate(data["colors"]):
dir_name = f"{mode}_{i:03d}"
save_path = os.path.join(output_path, dir_name)
image_save = os.path.join(save_path, f"rgba_{hdr_name}.png")
cv2.imwrite(image_save, arr)
for i, arr in enumerate(data["normals"]):
dir_name = f"{mode}_{i:03d}"
save_path = os.path.join(output_path, dir_name)
image_save = os.path.join(save_path, "normal.exr")
cv2.imwrite(image_save, arr)
for i, arr in enumerate(data["diffuse"]):
dir_name = f"{mode}_{i:03d}"
save_path = os.path.join(output_path, dir_name)
image_save = os.path.join(save_path, "albedo.png")
cv2.imwrite(image_save, arr)
def main():
bproc.init()
# objs = bproc.loader.load_blend(BLEND_PATH)
objs = bproc.loader.load_obj(GLB_PATH)
# 步骤1:获取包含扩展名的文件名(如 "train002.blend")
full_filename = os.path.basename(BLEND_PATH)
# 步骤2:去除扩展名,得到纯文件名(如 "train002")
filename_without_ext = os.path.splitext(full_filename)[0]
output_path = os.path.join(OUTPUT_DIR, filename_without_ext)
os.makedirs(output_path, exist_ok=True)
objs_with_mats = get_all_mesh_objects()
for obj in objs_with_mats:
materials = obj.get_materials()
for material in materials:
material.set_principled_shader_value("Normal", [1,1,1])
principled_bsdf = material.get_the_one_node_with_type("BsdfPrincipled")
for key in ["Roughness", "Metallic"]:
obj.set_cp("cp_"+ key.lower(), principled_bsdf.inputs[key].default_value)
# Find point of interest, all cam poses should look towards it
poi = bproc.object.compute_poi(objs)
# Sample five camera poses
for i in range(6):
# Sample random camera location around the object
location = bproc.sampler.part_sphere([0, 0, 0], radius=4, part_sphere_dir_vector=[1, 0, 0], mode="SURFACE")
# Compute rotation based on vector going from location towards poi
rotation_matrix = bproc.camera.rotation_from_forward_vec(poi - location)
# Add homog cam pose based on location an rotation
cam2world_matrix = bproc.math.build_transformation_mat(location, rotation_matrix)
bproc.camera.add_camera_pose(cam2world_matrix)
# for mode_select in ["train", "test", "eval"]:
for mode_select in ["train"]:
mode = mode_select
# # camera json read
# camera_json_dir = os.path.join(CAM_DIR, f"transforms_{mode}.json")
# with open(camera_json_dir, 'r', encoding='utf-8') as f:
# data = json.load(f) # 直接使用标准库解析JSON
# cam_angle_x = data['camera_angle_x']
# frames = data['frames']
# camera_num = len(frames)
# for j in range(camera_num):
# file_path = frames[j]["file_path"]
# cam_transform_mat = frames[j]["transform_matrix"]
# # 1. 读取外参矩阵(c2w矩阵)
# cam_transform_mat = np.array(cam_transform_mat, dtype=np.float32)
# # 注意:JSON中的矩阵可能是列表嵌套列表,需转换为numpy数组
# # 2. 设置相机内参
# # 计算焦距:基于视场角和分辨率(参考原脚本逻辑)
# # 公式:焦距 = 0.5 * 分辨率 / tan(0.5 * 视场角)
# focal_length = 0.5 * RES / np.tan(0.5 * cam_angle_x) * .045
# # 通过BlenderProc API设置内参
# bproc.camera.set_intrinsics_from_blender_params(
# lens=focal_length, # 焦距(毫米)
# image_width=RES,
# image_height=RES,
# lens_unit="MILLIMETERS" # 明确指定焦距单位为毫米
# )
# # 3. 设置相机外参(c2w矩阵)
# # 使用add_camera_pose将外参矩阵应用到相机
# # 注意:BlenderProc的add_camera_pose接受4x4的c2w矩阵(相机到世界)
# bproc.camera.add_camera_pose(cam_transform_mat)
for hdr_name in HDR_LIST:
render_view(hdr_name, output_path, mode)
if __name__ == "__main__":
main()Files required to run the code
No response
Expected behavior
it should be like this in render color. but
it seems has no geometry details
BlenderProc version
2.7.0
Metadata
Metadata
Assignees
Labels
questionQuestion, not yet a bug ;)Question, not yet a bug ;)