Skip to content

Instantly share code, notes, and snippets.

@pra-dan
Created December 20, 2020 09:01
Show Gist options
  • Save pra-dan/fccffafb9f0ce9ba72d75a8274b6c5c3 to your computer and use it in GitHub Desktop.
Save pra-dan/fccffafb9f0ce9ba72d75a8274b6c5c3 to your computer and use it in GitHub Desktop.
# Run blender -b ~/Videos/blender/panel-synthetic-dataset.blend -P ~/Videos/blender/test_synthetic.py
"""
The default pose is
X: -7m
Y: -1m
Z: 1m
Rotation_X = 90 degrees
Rotation_Z = -90 degrees (a.k.a cam.rotation_euler[2])
"""
import bpy
import os
import numpy as np
from math import *
from mathutils import *
#set your own target here
target = bpy.data.objects['Shape_IndexedFaceSet.018']
cam = bpy.data.objects['Camera']
t_loc_x = target.location.x
t_loc_y = target.location.y
cam_loc_x = cam.location.x
cam_loc_y = cam.location.y
# The different radii range
radius_range = range(7,15)
R = (target.location.xy-cam.location.xy).length # Radius
num_steps_revolution = 10 #how many revolution steps in each circle/revolution
num_steps_rotation = 5 #how many rotation steps at each angle
rotation_range_limit = 3 # NOTE ! in degrees
init_angle = atan(cam_loc_y/cam_loc_x) #in rad
init_angle = init_angle + pi # as in 3rd quadrant
target_angle = (1.5*pi -pi/6.0 - init_angle) # Go 270-8 deg more (pi/6 or 30deg removed as no suitable frame can be found there
for r in radius_range:
for x in range(1, num_steps_revolution):
alpha = init_angle + (x)*target_angle/num_steps_revolution
lim_min = degrees(alpha)-rotation_range_limit #degrees
lim_max = degrees(alpha)+rotation_range_limit #degrees
offset = 1.0/num_steps_rotation #degrees
for dalpha in np.arange(lim_min, lim_max, offset):
#print(f'in r:{r}, and alpha: {alpha}, dalpha:{dalpha}')
print(r)
cam.rotation_euler[2] = pi/2 + radians(dalpha) #
"""
Use alpha to locate new camera position
Use dalpha to rotate it at the obtained position to get more frames
"""
cam.location.x = t_loc_x+cos(alpha)*r
cam.location.y = t_loc_y+sin(alpha)*r
# Define SAVEPATH and output filename
file = os.path.join('renders/', str(r)+'_'+str(round(dalpha-180,3))+'_'+str(round(cam.location.x, 3))+'_'+str(round(cam.location.y, 3))) #dalpha in degrees
# Render
bpy.context.scene.render.filepath = file
bpy.ops.render.render(write_still=True)
"""
# Place Dummy Cameras to visualise all potential calculated positions
dalpha = radians(dalpha)
# Randomly place the camera on a circle around the object at the same height as the main camera
new_camera_pos = Vector((r * cos(dalpha), r * sin(dalpha), cam.location.z))
bpy.ops.object.camera_add(enter_editmode=False, location=new_camera_pos)
# Set the new camera as active
bpy.context.scene.camera = bpy.context.object
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment