I wrote a script based on Exiftool that reads RollAngle, PitchAngle, FocalLength, ImageWidth, ImageHeight and Timestamp #tags from a digital photography's EXIF data. It then matches Blender's camera X and Y rotation and focal length, X and Y res and Sun Position's date and time parameters with values read from the EXIF.
So when adding a mesh, it's 'automagically' aligned with the real-life picture/world. Using it as a camera background allows for (almost) seamless inserting of 3D objects in 'reality'. Check the odd thing on the stage, in the attached image.
I try to img = bpy.something.background_image.path
and getVal(img)
because Blender doesn't load EXIF data, and of course my getVal()
function relies on them. But so far, the API reference, tooltips and button context menu research didn't help.
It works if I manually assign the path to a variable within the code, but it's a pain.
`import bpy
import sys
import os
import math
from exiftool import ExifToolHelper
lv=[0]# liste valeurs
def getVal(im) :
with ExifToolHelper() as et:
try :
for d in et.get_tags(im,tags = ["RollAngle","PitchAngle","FocalLengthIn35mmFormat","ImageWidth","ImageHeight","TimeStamp"]):
for v in d.values():
lv.append(v)
except :
pass
return (lv)
rad = math.radians
cam=bpy.data.objects['Camera']
cam.data.show_background_images=True
# using this block doesn't work and returns 'list index out of range'
items = bpy.data.cameras['Camera'].background_images.items()
camera_image = items[0][1]
filepath = camera_image.image.filepath
prms = getVal(filepath)# if I 'print(prms)' prints '[0]'
#this block works, provided my camera object is selected
#Img = "/home/user/Images/2019/20191030/P1040273.jpg"
#prms = getVal(Img)
cam.rotation_euler[0] = rad(prms[2])+rad(90)# zero RL camera roll = 90° rotation on X axis in Blender
cam.rotation_euler[1] = rad(prms[3])# y rotation
cam.rotation_euler[2] = 0 # my cameras don't have magnetic sensors
bpy.data.objects["Camera"].lock_rotation[0]# this doesn't work either
bpy.data.objects["Camera"].lock_rotation[1]
bpy.data.objects["Camera"].lock_location[2]
cam.location[0] = 0
cam.location[1] = 0
cam.location[2] = 1.7# my eyes' height
#print(prms)
bpy.data.cameras["Camera"].lens = prms[4]
bpy.data.scenes["Scene"].render.resolution_x=prms[5]
bpy.data.scenes["Scene"].render.resolution_y=prms[6]
stamp = prms[-1].split(' ')
date = stamp[0].split(':')
time = stamp[1].split(':')
hours = time[0]
mins = time[1]
secs = time[2]
#########sun pos date##########
bpy.context.scene.sun_pos_properties.year = int(date[0])
bpy.context.scene.sun_pos_properties.month = int(date[1])
bpy.context.scene.sun_pos_properties.day = int(date[2])
#########sun pos time##########
decimals = (int(mins)*60+int(secs))/3600
bpy.context.scene.sun_pos_properties.time = float(hours)+decimals
#bpy.context.scene.sun_pos_properties.latitude = 43.152
#bpy.context.scene.sun_pos_properties.longitude = -3.1
`
bgImg = bpy.data.cameras['Camera'].background_images.filepath
, I got an error saying 'AttributeError: 'bpy_prop_collection' object has no attribute 'filepath'. I guess it's about context, but I can't find anything. $\endgroup$print(filepath
just before the call togetval
what gets printed? $\endgroup$//../../../../Images/2019/20191030/P1040273.jpg
... $\endgroup$prms = getVal(bpy.path.abspath(filepath))
$\endgroup$