Skip to content

Instantly share code, notes, and snippets.

@chunibyo-wly
Created March 1, 2023 10:02
Show Gist options
  • Save chunibyo-wly/398577c88b693258b22d31f08df3a3e1 to your computer and use it in GitHub Desktop.
Save chunibyo-wly/398577c88b693258b22d31f08df3a3e1 to your computer and use it in GitHub Desktop.
import bpy
from os.path import join
from bpy import context
import mathutils
import math
import builtins as __builtin__
import copy
def console_print(*args, **kwargs):
for a in context.screen.areas:
if a.type == "CONSOLE":
c = {}
c["area"] = a
c["space_data"] = a.spaces.active
c["region"] = a.regions[-1]
c["window"] = context.window
c["screen"] = context.screen
s = " ".join([str(arg) for arg in args])
for line in s.split("\n"):
bpy.ops.console.scrollback_append(c, text=line)
def print(*args, **kwargs):
"""Console print() function."""
console_print(*args, **kwargs) # to py consoles
__builtin__.print(*args, **kwargs) # to system console
# 设置渲染参数
render_settings = bpy.context.scene.render
render_settings.resolution_x = 1920
render_settings.resolution_y = 1080
folder = r"E:\workspace\dataset\97_own\01_blender\01_cottage\render"
target = bpy.data.objects["Cottage_Free"]
cam = bpy.context.scene.camera
origin_camera_matrix = copy.deepcopy(cam.matrix_world)
cnt = 0
offset1 = 15
offset2 = 15
for j in range(70 // offset2):
for i in range(360 // offset1):
# 渲染图像
render_settings.filepath = join(folder, f"{cnt:03d}.png")
cnt += 1
bpy.ops.render.render(write_still=True)
# 移动到下一个位置
rotation_matrix = mathutils.Matrix.Rotation(offset1 / 180 * math.pi, 4, "Z")
translation_matrix = mathutils.Matrix.Translation(cam.location)
camera_matrix = rotation_matrix @ cam.matrix_world
cam.matrix_world = copy.deepcopy(camera_matrix)
# 移动到下一个位置
rotation_matrix = mathutils.Matrix.Rotation(offset2 / 180 * math.pi, 4, "X")
translation_matrix = mathutils.Matrix.Translation(cam.location)
camera_matrix = rotation_matrix @ cam.matrix_world
cam.matrix_world = copy.deepcopy(camera_matrix)
print("Done")
# 还原初始位姿
cam.matrix_world = origin_camera_matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment