File size: 22,306 Bytes
d5f2893 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | import argparse, sys, os, math, re
import bpy
import bmesh
from bpy_extras.image_utils import load_image
import math
import numpy as np
import os
import bpy
import numpy as np
from scipy.signal import correlate2d
from scipy.ndimage import shift
import json
from mathutils import Vector
from utils.blender_utils import reset_scene, setup_background, create_wall_mesh, add_material, setup_camera, load_hdri, set_rendering_settings, apply_texture_to_object
import utils.transformations as tra
from utils.colors import get_categorical_colors
import trimesh
import cv2
from utils.blender_utils import get_pixel_coordinates, reset_blender
from utils.plot_utils import annotate_image_with_coordinates
def get_visual_marks(floor_vertices, scene, cam, interval=1):
# The world coordinate we want to project
visual_marks = dict()
min_vertices = np.min(floor_vertices, axis=0)
max_vertices = np.max(floor_vertices, axis=0)
for min_x in range(math.floor(min_vertices[0]), math.ceil(max_vertices[0])+2, interval):
for min_y in range(math.floor(min_vertices[1]), math.ceil(max_vertices[1])+2, interval):
world_coord = Vector((min_x, min_y, 0))
pixel_x, pixel_y = get_pixel_coordinates(scene, cam, world_coord)
visual_marks[(min_x, min_y)] = (pixel_x, pixel_y)
return visual_marks
def create_bounding_box(position, rotation, scale, color, transparency=0.5):
"""
Example:
create_bounding_box(position=(0, 0, 0), rotation=(0, 0, 0), scale=(1, 2, 0.5), color=(1, 0, 0), transparency=0.3)
:param position: xyz coordinates, in meters
:param rotation: euler angles, in radians
:param scale: xyz dimensions, in meters
:param color: rgb values in the range [0, 1]
:param transparency: alpha value in the range [0, 1]
:return:
"""
# Add a cube
bpy.ops.mesh.primitive_cube_add(size=1, location=position, rotation=rotation)
bounding_box = bpy.context.object
# Scale the cube to the desired dimensions
bounding_box.scale = scale
# Create a new material with transparency
mat = bpy.data.materials.new(name="BoundingBoxMaterial")
mat.use_nodes = True
bsdf = mat.node_tree.nodes.get("Principled BSDF")
bsdf.inputs["Base Color"].default_value = (*color, 1) # Set the color
bsdf.inputs["Alpha"].default_value = transparency
mat.blend_method = 'BLEND'
# Assign the material to the bounding box
if bounding_box.data.materials:
bounding_box.data.materials[0] = mat
else:
bounding_box.data.materials.append(mat)
# Optional: Enable backface culling for better visual appearance
mat.use_backface_culling = True
return bounding_box
def get_wall_normal(corner1, corner2, add_radian=None):
"""corner1 and corner2 are the two points on the wall, given in counter-clockwise order"""
vector = np.array([corner2[0] - corner1[0], corner2[1] - corner1[1]], dtype=np.float32)
if add_radian:
# Assuming add_radian is the angle to add in radians
cos_theta = np.cos(add_radian)
sin_theta = np.sin(add_radian)
# Construct the rotation matrix
rotation_matrix = np.array([[cos_theta, -sin_theta],
[sin_theta, cos_theta]], dtype=np.float32)
# Multiply the rotation vector by the rotation matrix
vector = np.dot(rotation_matrix, vector)
vector = vector / np.linalg.norm(vector)
return vector
def get_obj_dimensions(obj, frame="object"):
# Ensure the object is of type 'MESH'
if obj.type != 'MESH':
raise ValueError(f"The object '{obj.name}' is not a mesh.")
# Get the bounding box coordinates in local space
bbox = [Vector(corner) for corner in obj.bound_box]
# Convert the local bounding box coordinates to world space
if frame == "world":
bbox = [obj.matrix_world @ corner for corner in bbox]
# Calculate the minimum and maximum coordinates along each axis
min_x = min(corner.x for corner in bbox)
max_x = max(corner.x for corner in bbox)
min_y = min(corner.y for corner in bbox)
max_y = max(corner.y for corner in bbox)
min_z = min(corner.z for corner in bbox)
max_z = max(corner.z for corner in bbox)
# Calculate the dimensions
width = max_x - min_x
depth = max_y - min_y
height = max_z - min_z
return [width, depth, height]
# Function to create an arrow representing an axis
def create_arrow(start, end, radius=0.02, color=(1, 0, 0, 1), name="Arrow"):
# Create a cylinder (for the shaft)
bpy.ops.mesh.primitive_cylinder_add(radius=radius, depth=(end - start).length, location=(start + end) / 2)
shaft = bpy.context.object
shaft.name = name + "_shaft"
# Align the shaft to point towards the end
direction = end - start
rot_quat = direction.to_track_quat('Z', 'Y')
shaft.rotation_euler = rot_quat.to_euler()
# Create a cone (for the tip)
tip_length = radius * 2
bpy.ops.mesh.primitive_cone_add(radius1=radius * 2, depth=tip_length, location=end)
tip = bpy.context.object
tip.name = name + "_tip"
# Align the tip to point towards the end
tip.rotation_euler = rot_quat.to_euler()
# Create a material for the arrow
mat = bpy.data.materials.new(name + "_Material")
mat.diffuse_color = color
shaft.data.materials.append(mat)
tip.data.materials.append(mat)
# Combine shaft and tip into one object
bpy.ops.object.select_all(action='DESELECT')
shaft.select_set(True)
tip.select_set(True)
bpy.ops.object.join()
# Function to add a coordinate frame at a specific location
def add_coordinate_frame(location=Vector((0, 0, 0.)), scale=1.0):
# Define the length and color of each axis
axis_length = scale
x_color = (1, 0, 0, 1) # Red
y_color = (0, 1, 0, 1) # Green
z_color = (0, 0, 1, 1) # Blue
# Create the X axis arrow
create_arrow(location, location + Vector((axis_length, 0, 0)), color=x_color, name="X_Axis")
# Create the Y axis arrow
create_arrow(location, location + Vector((0, axis_length, 0)), color=y_color, name="Y_Axis")
# Create the Z axis arrow
create_arrow(location, location + Vector((0, 0, axis_length)), color=z_color, name="Z_Axis")
def show_current_render(save_dir):
render_path = f"{save_dir}/tmp.png"
bpy.context.scene.render.filepath = render_path
bpy.ops.render.render(write_still=True)
img = cv2.imread(render_path)
cv2.imshow("render", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def render_existing_scene(placed_assets, task, save_dir, add_hdri=True, topdown_save_file=None, sideview_save_file=None, add_coordinate_mark=True,
annotate_object=True, annotate_wall=True, render_top_down=True, adjust_top_down_angle=None, high_res=False, rotate_90=True,
apply_3dfront_texture=False, recenter_mesh=True, fov_multiplier=1.1, default_font_size=None,
combine_obj_components=False, side_view_phi=45, side_view_indices=[3], save_blend=False,
add_object_bbox=False, ignore_asset_instance_idx=False, floor_material="Travertine008"):
"""
:param placed_assets: the set of assets that have been placed in the scene / to be rendered
:param task: just for getting the boundary
:param save_dir:
:param add_hdri:
:param topdown_save_file:
:param sideview_save_file:
:param add_coordinate_mark:
:param annotate_object:
:param annotate_wall:
:param render_top_down:
:param adjust_top_down_angle:
:param high_res:
:param rotate_90:
:param apply_3dfront_texture: this is needed because loading material directly from .obj will fail
:param recenter_mesh: whether to re-center the mesh to centroid.
:param fov_multiplier: a factor for increasing the fov of the camera.
:param combine_obj_components: whether to combine the components of the obj file into one object.
Recommend to set to True if loading 3d front .obj files.
If set to False, one example failure case is 0003d406-5f27-4bbf-94cd-1cff7c310ba1_LivingRoom-54780
:param apply_3dfront_orientation_correction: 3d front .obj faces y axis by default, if we want to make it face x axis, set to True.
:param ignore_asset_instance_idx: useful for generating object renderings for each group of objects
:return:
"""
if add_object_bbox:
assert annotate_object, "add_object_bbox can only be True when annotate_object is True"
reset_blender()
setup_background()
# compute scene boundary
floor_vertices = np.array(task["boundary"]["floor_vertices"])
floor_x_values = [point[0] for point in floor_vertices]
floor_y_values = [point[1] for point in floor_vertices]
floor_center_x = (max(floor_x_values) + min(floor_x_values)) / 2
floor_center_y = (max(floor_y_values) + min(floor_y_values)) / 2
floor_width = max(max(floor_x_values) - min(floor_x_values), max(floor_y_values) - min(floor_y_values))
wall_height = np.array(task["boundary"]["wall_height"]) if "wall_height" in task["boundary"] else 1
# Clear existing mesh objects and lights in the scene
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
# Create a new empty scene
# Set the new empty scene as the active scene
bpy.context.window.scene = bpy.context.scene
# Update the user interface
bpy.context.view_layer.update()
# build floor and walls
# floor_obj = create_wall_mesh("floor", floor_vertices)
floor_obj = create_wall_mesh("floor", floor_vertices)
if adjust_top_down_angle is not None:
# asset centric rendreing
floor_material = "Travertine008"
add_material(floor_obj, os.path.join("/viscam/projects/SceneAug/ambientcg", floor_material))
bpy.ops.object.select_all(action='DESELECT')
floor_obj.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
# Unwrap using Smart UV Project
bpy.ops.uv.smart_project()
bpy.ops.object.mode_set(mode='OBJECT')
if annotate_object:
candidate_colors = get_categorical_colors(20, colormap_name='tab20', color_range="0-1", color_format="rgb")
asset_count = 0
asset_dict = {}
for instance_id, asset in task["assets"].items():
if instance_id not in placed_assets.keys():
continue
objects_before_import = set(bpy.context.scene.objects)
file_path = asset["path"]
if ".gltf" in file_path or ".glb" in file_path:
bpy.ops.import_scene.gltf(filepath=file_path)
elif ".obj" in file_path:
bpy.ops.wm.obj_import(filepath=file_path)
else:
raise ValueError(f"Unsupported file format: {file_path}")
if not combine_obj_components:
loaded = bpy.context.view_layer.objects.active
else:
objects_after_import = set(bpy.context.scene.objects)
new_objects = objects_after_import - objects_before_import
bpy.ops.object.select_all(action='DESELECT')
for obj in new_objects:
obj.select_set(True)
try:
bpy.ops.object.join()
except Exception as e:
print(f"Error joining objects: {e}, ignoring the join operation")
loaded = bpy.context.view_layer.objects.active
# add texture for front 3d objects
if apply_3dfront_texture:
texture_path = os.path.join(os.path.split(file_path)[0], "texture.png")
if os.path.exists(texture_path):
apply_texture_to_object(loaded, texture_path)
bpy.ops.object.select_all(action='DESELECT')
loaded.select_set(True)
if recenter_mesh:
bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
# rotate the object according to pose["rotation"] (0, 90, 90)
# after preprocessing, the object faces -y axis by default
# make the object face +x by default (Blender rotates the object clockwise!)
if rotate_90:
bpy.ops.transform.rotate(value=-math.radians(90), orient_axis='Z')
# for i in range(3): assert loaded.rotation_euler[i] ==0
bpy.context.object.rotation_mode = "XYZ"
if "scale" in placed_assets[instance_id]:
loaded.scale = placed_assets[instance_id]["scale"]
#elif "scale" in task["assets"][instance_id]:
# loaded.scale = placed_assets[instance_id]["scale"]
# TODO (Weiyu): Why are we reading both from the `placed_assets` dict and the `task["assets"]` dict? Can we remove one?
if isinstance(placed_assets[instance_id]["rotation"], float):
loaded.rotation_euler[-1] += np.deg2rad(placed_assets[instance_id]["rotation"])
else:
for i in range(3):
loaded.rotation_euler[i] += np.deg2rad(placed_assets[instance_id]["rotation"][i])
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
for i in range(3): assert loaded.rotation_euler[i] ==0
#bpy.ops.transform.rotate(value=math.radians(pose['rotation']['y']), orient_axis='Z')
# dim = get_dimensions_with_hierarchy(loaded)
# holo_dim = data_holodeck[obj['assetId']]['assetMetadata']['boundingBox']
xyz_location = placed_assets[instance_id]["position"]
# default place the object on the floor
if len(xyz_location) == 2:
xyz_location = [xyz_location[0], xyz_location[1], placed_assets[instance_id]["scale"] * placed_assets[instance_id]["assetMetadata"]["boundingBox"]["z"]/2]
loaded.location = xyz_location
if annotate_object:
# create obb for the object
# NOTE: for 3d front assets, the center is at the bottom, but the center of the bbox is at the center
#asset_location = loaded.location.copy()
#asset_location[2] = asset_location[2] + asset_scale[2] / 2 # add z
# extract location, rotation, scale from asset
asset_position = placed_assets[instance_id]["position"]
asset_rotation = np.deg2rad(placed_assets[instance_id]["rotation"]) #loaded.rotation_euler
try:
asset_scale = get_obj_dimensions(loaded, frame="object")
except Exception as e:
print(f"Error getting object dimensions: {e}, using the bounding box instead")
import pdb;pdb.set_trace()
continue
# asset_scale_2 = trimesh.load(file_path).bounding_box.extents
# asset_tf = tra.euler_matrix(asset_rotation[0], asset_rotation[1], asset_rotation[2])
# asset_tf = asset_tf @ tra.euler_matrix(0, 0, np.pi/2)
# asset_rotation = tra.euler_from_matrix(asset_tf)
if add_object_bbox:
bbox_rotation = loaded.rotation_euler
create_bounding_box(asset_position, bbox_rotation, scale=asset_scale, color=candidate_colors[asset_count], transparency=0.3)
if ignore_asset_instance_idx:
asset_name = asset["asset_var_name"]
else:
asset_name = f"{asset['asset_var_name']}[{asset['instance_idx']}]"
asset_dict[asset_count] = {
"position": asset_position,
"rotation": asset_rotation,
"size": asset_scale,
"name": asset_name,
"path": file_path, # "texture_path": texture_path,
"category": asset["category"]
}
asset_count += 1
if add_coordinate_mark:
# asset-centric mode, add coordinate frame at the bottom left corner of the room
if adjust_top_down_angle is not None:
add_coordinate_frame(Vector((floor_center_x-floor_width/2, floor_center_y-floor_width/2, 0)))
# scene-centric mode, add coordinate frame at the origin
else:
add_coordinate_frame()
### add light
if add_hdri:
load_hdri()
output_images = []
set_rendering_settings(high_res=high_res)
visual_marks = dict()
### render top down images
if render_top_down:
### setup camera
cam, cam_constraint = setup_camera(
floor_center_x, floor_center_y, floor_width, wall_height,
fov_multiplier=fov_multiplier, use_damped_track=(adjust_top_down_angle is not None)
)
interval = 2
if adjust_top_down_angle is not None:
# _phi = math.radians(20)
cam.rotation_euler = (0, 0, 0)
original_z = cam.location.z # * 1.5
theta = 0
phi = math.radians(adjust_top_down_angle)
point = (
floor_center_x + original_z * math.sin(phi) * math.cos(theta),
floor_center_y + original_z * math.sin(phi) * math.sin(theta),
original_z * math.cos(phi),
)
cam.location = point
interval = 1
render_path = topdown_save_file if topdown_save_file else f"{save_dir}/top_down_rendering.png"
bpy.context.scene.render.filepath = render_path
bpy.ops.render.render(write_still=True)
if add_coordinate_mark:
_visual_marks = get_visual_marks(floor_vertices, bpy.context.scene, cam, interval=interval)
annotate_image_with_coordinates(image_path=render_path, visual_marks=_visual_marks, output_path=render_path, format="coordinate")
_visual_marks = []
if annotate_object:
for asset_name in asset_dict:
asset_data = asset_dict[asset_name]
asset_rotation = asset_data["rotation"]
pixel_x, pixel_y = get_pixel_coordinates(bpy.context.scene, cam, asset_data["position"])
end_arrow_pixel_x, end_arrow_pixel_y = get_pixel_coordinates(
bpy.context.scene, cam,
[
asset_data["position"][0] + 0.75 * math.cos(asset_rotation[-1]),
asset_data["position"][1] + 0.75 * math.sin(asset_rotation[-1]),
asset_data["position"][2]
]
)
_visual_marks.append({ "text": asset_data["name"], "pixel": (pixel_x, pixel_y), "end_arrow_pixel": (end_arrow_pixel_x, end_arrow_pixel_y)})
print(f"Asset {asset_data['name']} is at pixel ({pixel_x}, {pixel_y})")
if annotate_wall:
# Add walls to _visual_marks
for i, vertex in enumerate(floor_vertices):
next_vertex = floor_vertices[(i + 1) % len(floor_vertices)]
# if wall is less than 2 meters, don't annotate
if np.linalg.norm(np.array(vertex) - np.array(next_vertex)) < 2:
continue
wall_center = [(vertex[0] + next_vertex[0]) / 2, (vertex[1] + next_vertex[1]) / 2, 0]
# Move the room center 0.5 meters away from [center_x, center_y]
# direction_vector should be the wall normal direction
# direction_vector = np.array([floor_center_x, floor_center_y]) - np.array(wall_center[:2])
# direction_vector = direction_vector / np.linalg.norm(direction_vector) # Normalize
direction_vector = get_wall_normal(vertex, next_vertex, add_radian=np.pi/2)
# get the wall normal
new_wall_center = np.array(wall_center[:2]) - 0.2 * direction_vector
wall_center = [new_wall_center[0], new_wall_center[1], 0]
pixel_x, pixel_y = get_pixel_coordinates(bpy.context.scene, cam, wall_center)
end_arrow_pixel_x, end_arrow_pixel_y = get_pixel_coordinates(
bpy.context.scene, cam,
[
wall_center[0] + 0.75 * direction_vector[0],
wall_center[1] + 0.75 * direction_vector[1],
0
]
)
_visual_marks.append({"text": f"walls[{i}]", "pixel": (pixel_x, pixel_y), "end_arrow_pixel": (end_arrow_pixel_x, end_arrow_pixel_y), "color": "white"})
if adjust_top_down_angle is not None:
# asset centric mode
annotate_image_with_coordinates(image_path=render_path, visual_marks=_visual_marks, output_path=render_path, format="text", default_font_size=24)
else:
# scene centric mode
annotate_image_with_coordinates(image_path=render_path, visual_marks=_visual_marks, output_path=render_path, format="text", default_font_size=24 if default_font_size is None else default_font_size)
output_images.append(render_path)
### render side images
cam, cam_constraint = setup_camera(
floor_center_x, floor_center_y, floor_width, wall_height,
fov_multiplier=fov_multiplier, use_damped_track=False
)
original_z = cam.location.z
# remove all cam constraints
for side_view_index in side_view_indices:
# set the camera position
theta = (side_view_index / 4) * math.pi * 2
_phi = math.radians(side_view_phi)
point = (
floor_center_x + original_z * math.sin(_phi) * math.cos(theta),
floor_center_y + original_z * math.sin(_phi) * math.sin(theta),
original_z * math.cos(_phi),
)
cam.location = point
# render the image
render_path = sideview_save_file if sideview_save_file else f"{save_dir}/side_rendering_{side_view_phi}_{side_view_index}.png"
bpy.context.scene.render.filepath = render_path
bpy.ops.render.render(write_still=True)
if add_coordinate_mark:
visual_marks = get_visual_marks(floor_vertices, bpy.context.scene, cam, interval=2)
annotate_image_with_coordinates(image_path=render_path, visual_marks=visual_marks, output_path=render_path)
output_images.append(render_path)
if save_blend:
bpy.ops.file.pack_all()
bpy.ops.wm.save_as_mainfile(filepath=f"{save_dir}/scene.blend")
return output_images, visual_marks
|