Last active
December 31, 2021 15:56
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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() |
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
@shadiakiki1986 Thanks. Fixed.