A script to export camera properties (position, rotation, fov) to file?

Hi all, vexe here, from Unity community. First post here :slight_smile:

I’m relatively new to blender, I have my modeler - but I occasionally jump to mess with some renders, camera settings, make quick levels for testing, etc.

In our game we deal with static camera angles and pre-rendered backgrounds. Problem is, if I change a camera angle/position in blender I’d have to re-export the level back to Unity. So I was wondering if I could just write a script that helps ease the process for me by just writing out the cameras positions to a file and then I could just read that from Unity. I looked up blender scripting and I found out that I could either do it via a python script or blender addons? – I’m not sure about addons, like do I write C++ there? or … ?

The cameras in my blender scene have a consistent naming: Camera.N where N is the camera number (starts from 0)

I have no trouble writing python or C++ - but I’m not sure what the relevant API is.

PS: It would be nice if I could also access the camera’s fov. Currently, I’m writing the fov values manually to a file and then getting the values in unity and converting them to vertical fov (since blender uses horizontal fov and unity vertical)

An example file/output of what I would like to do:
Camera.1: p: 10.2, 0, 1 r: 12, 30, 0 fov: 30
Camera.2: p: etc

p: position, r: rotation

Any pointers or guides are appreciated.

Cheers!

Anyone? Just looking for API pointers here/ where to look

Hi,

I hope this helps. You can crete a loop to iterate over the camera’s and write tha data to a file.

import bpy

cam = bpy.data.objects[‘Camera.1’]
posX = cam.location.x
posY = cam.location.y
posZ = cam.location.z

Rotation are in radians I think not in degrees

rotX = camj.rotation_euler.x
rotY = cam.rotation_euler.y
rotZ = cam.rotation_euler.z

Tip: when you move the mousepointer over an UI item in Blender, in the tooltip you see the python code for that item.

Here is a link to the docs that you can lookup anything using the search box at the right of the page, also the top of the page is a downloadable zip of all the docs that you can use for ofline browsing that also has a search system.

http://www.blender.org/api/blender_python_api_2_73_release/contents.html

here is all the results from searching camera http://www.blender.org/api/blender_python_api_2_73_release/search.html?q=camera&check_keywords=yes&area=default