working render script ?

This commit is contained in:
Laureηt 2023-01-18 21:20:07 +01:00
parent c4d9fcf3f7
commit 36fd4947a1
Signed by: Laurent
SSH key fingerprint: SHA256:kZEpW8cMJ54PDeCvOhzreNr4FSh6R13CMGH/POoO8DI
9 changed files with 106 additions and 34 deletions

14
poetry.lock generated
View file

@ -125,6 +125,18 @@ files = [
{file = "distlib-0.3.6.tar.gz", hash = "sha256:14bad2d9b04d3a36127ac97f30b12a19268f211063d8f8ee4f47108896e11b46"},
]
[[package]]
name = "fake-bpy-module-latest"
version = "20230118"
description = "Collection of the fake Blender Python API module for the code completion."
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "fake-bpy-module-latest-20230118.tar.gz", hash = "sha256:a3f45879347ef37b6fa5cd66ccc49634a14b420fa1b0fd196dfbf177d8602053"},
{file = "fake_bpy_module_latest-20230118-py3-none-any.whl", hash = "sha256:66d0b76ba65e01a91f74e77114705d744f3c2ad1078d060abf4a42c7fff9e79b"},
]
[[package]]
name = "filelock"
version = "3.9.0"
@ -692,4 +704,4 @@ testing = ["coverage (>=6.2)", "coverage-enable-subprocess (>=1)", "flaky (>=3.7
[metadata]
lock-version = "2.0"
python-versions = ">=3.8.1,<4.0"
content-hash = "78e5e2403aaceb237ee187dd395971a4ee3a07af884a3d2be70e201a8285b2f4"
content-hash = "70277d35aa55c1022524e37b75fcd711637ee16fb1737bc157244cf83d57efff"

View file

@ -10,6 +10,7 @@ numpy = "^1.23.5"
python = ">=3.8.1,<4.0"
rich = "^12.6.0"
opencv-python = "^4.6.0.66"
fake-bpy-module-latest = "^20230118"
[tool.poetry.group.dev.dependencies]
Flake8-pyproject = "^1.1.0"

View file

@ -1,10 +1,5 @@
# https://blender.stackexchange.com/questions/38009/3x4-camera-matrix-from-blender-camera
import bpy
from mathutils import Matrix
import pickle
import numpy as np
#---------------------------------------------------------------
# 3x4 P matrix from Blender camera
@ -117,28 +112,25 @@ def get_3x4_P_matrix_from_blender(cam):
RT = get_3x4_RT_matrix_from_blender(cam)
return K@RT, K, RT
def run_script(scene):
with open(PICKLE_PATH, 'rb') as file:
matrices = pickle.load(file)
# ----------------------------------------------------------
if __name__ == "__main__":
# Insert your camera name here
cam = bpy.data.objects['Camera']
P, K, RT = get_3x4_P_matrix_from_blender(cam)
print("K")
print(K)
print("RT")
print(RT)
print("P")
print(P)
projection_matrix, _, _ = get_3x4_P_matrix_from_blender(scene.camera)
matrices.append(np.array(projection_matrix))
print("==== 3D Cursor projection ====")
pc = P @ bpy.context.scene.cursor.location
pc /= pc[2]
print("Projected cursor location")
print(pc)
with open(PICKLE_PATH, 'wb') as file:
pickle.dump(matrices, file)
def setup_script(scene):
matrices = []
with open(PICKLE_PATH, 'wb') as file:
pickle.dump(matrices, file)
PICKLE_PATH = "/tmp/pickle.truc"
# clear handlers
bpy.app.handlers.render_init.clear()
bpy.app.handlers.frame_change_pre.clear()
bpy.app.handlers.frame_change_post.clear()
# add handler
bpy.app.handlers.render_init.append(setup_script)
bpy.app.handlers.frame_change_post.append(run_script)
# Bonus code: save the 3x4 P matrix into a plain text file
# Don't forget to import numpy for this
#nP = numpy.matrix(P)
#numpy.savetxt("/tmp/P3x4.txt", nP) # to select precision, use e.g. fmt='%.2f'

67
src/blender/render.py Normal file
View file

@ -0,0 +1,67 @@
import bpy
import math
import itertools
import pathlib
import pickle
import sys
import numpy as np
# dirty hack to import proj.py
dir = pathlib.Path(bpy.data.filepath).parent
dir = dir / "src" / "blender"
sys.path.append(str(dir))
from proj import get_3x4_P_matrix_from_blender
# setup output path
EXPORT_PATH = pathlib.Path("/tmp/")
# setup camera poses
phis = [-45, 0, 45]
thetas = [0, 45, 90, 135, 180, 225, 270, 315]
# convert to radians
phis = [math.radians(phi) for phi in phis]
thetas = [math.radians(theta) for theta in thetas]
# create all possible combinations
poses = list(itertools.product(phis, thetas))
# create export folders
pathlib.Path(EXPORT_PATH / "images").mkdir(parents=True, exist_ok=True)
pathlib.Path(EXPORT_PATH / "masks").mkdir(parents=True, exist_ok=True)
pathlib.Path(EXPORT_PATH / "cameras").mkdir(parents=True, exist_ok=True)
# set "File Output" nodes' "Base Path" property
bpy.data.scenes["Scene"].node_tree.nodes["Image Output"].base_path = str(EXPORT_PATH / "images")
bpy.data.scenes["Scene"].node_tree.nodes["Mask Output"].base_path = str(EXPORT_PATH / "masks")
# get camera
cam = bpy.data.objects["Camera"]
for i, (phi, theta) in enumerate(poses):
print(f"Rendering pose {i}...")
# set camera pose
bpy.context.scene.objects["Empty"].rotation_euler[0] = phi
bpy.context.scene.objects["Empty"].rotation_euler[2] = theta
# get camera matrices
P, K, RT = get_3x4_P_matrix_from_blender(cam)
# save camera matrices
with open(EXPORT_PATH / "cameras" / f"{i:04d}.pickle", "wb") as f:
pickle.dump({
"P": np.array(P),
"K": np.array(K),
"RT": np.array(RT),
}, f)
print(f"Saved camera matrices: {i:04d}.pickle")
# set frame number
bpy.context.scene.frame_current = i
# render the frame
bpy.ops.render.render(write_still=False)

View file

Binary file not shown.