Skip to content

Instantly share code, notes, and snippets.

@jirihnidek
Last active December 31, 2021 15:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jirihnidek/0f2734916f5d138ccdbd8a7ee38f384c to your computer and use it in GitHub Desktop.
Save jirihnidek/0f2734916f5d138ccdbd8a7ee38f384c to your computer and use it in GitHub Desktop.
This is simple Blender Python script for setting 3D background empty object.
"""
This example try to create new empty object visualized as
image. Image fits to the background of current active camera.
When you set X,Y coordinates of empty object called 'Pixel',
then this object is position at corresponding X,Y coordinate
at image in 3D space.
"""
import bpy
import math
import mathutils
def main():
"""
Try to set 3D background image
"""
factor = 10.0
# Get current camera
camera = bpy.context.scene.camera
# Make sure camera is in perspective mode
camera.data.type = 'PERSP'
# Save resolution
x_res = bpy.context.scene.render.resolution_x
y_res = bpy.context.scene.render.resolution_y
# Note: when you want to render this background image, then
# you will have to use mesh plane, set UV cordinates and material
# Create new empty object
bpy.ops.object.empty_add(type='IMAGE')
# Newly created object will be selected. Save it in variable.
empty = bpy.context.selected_objects[0]
# Get image or generate new one
try:
img = bpy.data.images['background.png']
except KeyError:
bpy.ops.image.new(name='background.png', width=x_res, height=y_res,
generated_type='COLOR_GRID')
img = bpy.data.images['background.png']
# Set image of empty
empty.data = img
# Set parent
empty.parent = camera
# Get angle of camera
angle = camera.data.angle
# Make sure resolution is in floats
x_res = float(x_res)
y_res = float(y_res)
z_coord = -factor
x_coord = z_coord * math.tan(angle/2.0)
y_coord = x_coord / (x_res/y_res)
empty.location = (x_coord, y_coord, z_coord)
# Set scale of image to fit view of camera
empty.empty_draw_size = factor * 2.0 * math.tan(angle/2.0)
# Create another empty object
bpy.ops.object.empty_add()
# Save current object to variable
img_coord_syst = bpy.context.selected_objects[0]
img_coord_syst.name = 'Img_Coord_System'
img_coord_syst.parent = empty
# Set location (sam position as empty)
img_coord_syst.location = (0.0, 0.0, 0.0)
img_coord_syst.scale[0] = img_coord_syst.scale[1] = factor * 2.0 * math.tan(angle/2.0) / x_res
# Create yet another empty object
bpy.ops.object.empty_add()
# Save current object to variable
pixel = bpy.context.selected_objects[0]
pixel.name = 'Pixel'
pixel.parent = img_coord_syst
# Try to set pixel position (right top corner)
pixel.location = (x_res, y_res, 0.0)
# Try to do the same thing using matrix multiplication
bpy.ops.object.empty_add()
# Save current object to variable
point = bpy.context.selected_objects[0]
point.name = 'Point'
mat_scale = mathutils.Matrix(
[[2.0 * factor * math.tan(angle/2.0)/x_res, 0, 0, 0],
[0, 2.0 * factor * math.tan(angle/2.0)/x_res, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]
)
mat_img = mathutils.Matrix(
[[1, 0, 0, -factor * math.tan(angle/2.0)],
[0, 1, 0, -factor * math.tan(angle/2.0) * (y_res/x_res)],
[0, 0, 1, -factor],
[0, 0, 0, 1]]
)
point.location = camera.matrix_world * mat_img * mat_scale * mathutils.Vector((x_res, y_res, 0))
if __name__ == '__main__':
main()
@RevengeNeeded
Copy link

Where do I get the bpy and the math for the imports you used?

@jirihnidek
Copy link
Author

The bpy in API module of Blender: https://www.blender.org/. The math is built-in Python module. You can run this script only in Blender.

@RevengeNeeded
Copy link

Thanks for the help!

@shadiakiki1986
Copy link

missing import mathutils

@jirihnidek
Copy link
Author

@shadiakiki1986 Thanks. Fixed.

@Neuroup
Copy link

Neuroup commented Jan 20, 2021

Hello
I'm trying lunch this code on Blender 2.9x and I have such a problem in line 23
AttributeError: 'NoneType' object has no attribute 'data'
And in line 108
Can You write me how to fix it ?

Greets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment