Skip to content

Instantly share code, notes, and snippets.

@tomrunia
Created August 23, 2018 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomrunia/14c42babbdcc733ed29cdbad80e9ab90 to your computer and use it in GitHub Desktop.
Save tomrunia/14c42babbdcc733ed29cdbad80e9ab90 to your computer and use it in GitHub Desktop.
# Initialize the camera
camera = bpy.data.cameras.new('Camera')
camera_obj = bpy.data.objects.new('Camera', camera)
scene.objects.link(camera_obj)
scene.objects.active = camera_obj
scene.camera = camera_obj
camera_obj.select = True
camera_obj.location = (0, 0, 0)
camera_obj.rotation_euler = (np.radians(45), 0, 0)
############################################################################
#obj = blender_utils.object_to_scene(OBJECT_DEFINITIONS[0][0], OBJECT_DEFINITIONS[0][1])
loc = (0,6,-6)
bpy.ops.mesh.primitive_cube_add(location=loc)
obj = bpy.context.selected_objects[0]
obj.location = loc
obj.rotation_euler = (0, 0.0, 0.0)
obj.scale = (1.2, 1.2, 1.2)
obj.select = True
resolution_x = scene.render.resolution_x
resolution_y = scene.render.resolution_y
aspect_ratio = resolution_x / resolution_y
vertices = obj.data.vertices
screen_coords = []
for v in obj.data.vertices:
co_3d = obj.matrix_world * v.co
co_2d = bpy_extras.object_utils.world_to_camera_view(scene, camera_obj, co_3d)
# If you want pixel coords
render_scale = scene.render.resolution_percentage / 100.0
render_size = (int(scene.render.resolution_x * render_scale),
int(scene.render.resolution_y * render_scale))
x = round(viewport_size[0] * (1.0- co_2d.x))
y = round(viewport_size[1] * (1.0- co_2d.y))
screen_coords.append((x,y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment