Skip to content

Instantly share code, notes, and snippets.

@hsab
Created May 22, 2019 20:54
Show Gist options
  • Save hsab/1bc4562d4fd3b6d29be4e5baa589420d to your computer and use it in GitHub Desktop.
Save hsab/1bc4562d4fd3b6d29be4e5baa589420d to your computer and use it in GitHub Desktop.
Blender World Space to Render Pixel Space
# from https://blender.stackexchange.com/questions/882/how-to-find-image-coordinates-of-the-rendered-vertex
# ===================
# |(0,0) |
# | |
# | |
# | |
# | (x,y)|
# ===================
import bpy
import bpy_extras
D = bpy.data
C = bpy.context
scene = C.scene
cam = C.scene.camera
# Change to your own object or use a handler to track an object
co = D.objects["Cube"].matrix_world.translation
co_2d = bpy_extras.object_utils.world_to_camera_view(scene, cam, co)
# If you want pixel coords
render_scale = scene.render.resolution_percentage / 100
render_size = (
int(scene.render.resolution_x * render_scale),
int(scene.render.resolution_y * render_scale),
)
pixelCoords = (
round(co_2d.x * render_size[0]),
round( render_size[1] - co_2d.y * render_size[1]),
)
print("Pixel Coords:", pixelCoords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment