Hola!
Contrary secundar, I didn’t think the exporter worked that well, so since I couldn’t find any other blender2nuke scripts, needed to learn some Python and could use this for work and ignorantly thought it wouldn’t be so hard, I started to “port” this script to Blender 2.5.
This is only the 2. “thing” I ever done in Python, and this one is 96% from the Ter2Blend script so I can’t take too much credit I did however learn a lot, and crossed alot of personal dungeons (I haven’t touched math in ~12 years and completely forgotten about radians among many things!). The best resource is without a doubt the BLenses page. Thank you so much, chipmasque! In fact, the .chan-script is practically useless without BLenses or atleast a “basic” focal2fov formula (BLenses includes the distance to object in the formula). This is also why this script needs to be edited manually, since essential information isn’t available from Blender yet (like aperture/sensor dimensions).
The workflow is therefor not as smooth right now. First copy/paste this code to a file and open it in the Blender text-editor:
Edit 23/2/10:
Of course there was something wrong! I was apparently so focused (no pun intended) on just matching the focal-length and image, I didn’t think about testing animation! I automatically assumed it would work, because it work with the 2.49b script, but no. So the following is the updated script. It still misses a whole lot, and I hope to evolve it into a real exporter, so if you find some errors during testing/use, please tell. Thanks!
Edit 20/4/10:
Needed this script today but there’s some minor changes to the 2.5 Python API. Fixed below.
import math
import bpy
DATA = {
'EXPORT_CAM': True,
'EXPORT': "/home/san/Desktop/",
'ERROR': [],
'HAP': 22.3,
'VAP': 14.9,
}
def export_cam():
context = bpy.context.scene
cams = [ob for ob in context.objects if ob.type == 'CAMERA']
if len(cams)>0:
start = context.frame_start
end = context.frame_end
for cam in cams:
name = cam.name
lines = ""
for f in range(start,end+1):
context.frame_set(f)
loc = cam.location
if cam.data.type == 'PERSP':
hfov = cam.data.angle ### radians
vfov = math.degrees(2 * math.atan(math.tan(hfov/2) * (DATA['VAP']/DATA['HAP'])))
else:
vfov = False
lines += str(f)+" "
lines += str(loc[0])+" "+str(loc[2])+" "+str(-loc[1])+" "
### XZY
lines += str(math.degrees(cam.rotation_euler[0])-90)+" "
lines += str(math.degrees(cam.rotation_euler[2]))+" "
lines += str(math.degrees(-cam.rotation_euler[1]))+" "
if vfov:
lines += str(vfov)+" "
#lines = lines[:]
lines += "
"
chan = open(DATA['EXPORT']+name+".chan",'w')
chan.write(lines[:-1])
chan.close()
def ExportAll():
if DATA['EXPORT_CAM']:
export_cam()
ExportAll()
Change the values in the DATA variable to fit your setup. The HAP and VAP values are the Horizontal- and Vertical-Aperture size in mm. Here aperture is the sensor size on my Canon-50D (22.3mm x 14.9mm). This is a great database of camera-specs where you can most likely find your cameras sensor-size. The default values in Nuke Camera are HAP: 24.575 & VAP: 18.672 (Cineon 2K/4K).
The idea is to match the focal-length between Blender and Nuke, since the focal-length is usually known rather than the field-of-view in degrees/radians! Many cameras also writes this info to exif, so you can look it up later like here in Rawstudio:
Note: Aperture here is the shutter aperture (f/stop), nothing to do with sensor-size/film-back! :eek:
Blender Camera doesn’t have either focal-length or aperture dimensions, hence the need for BLenses or if calculating with infinite focus, this function:
def focal2fov(focal, aperture):
return math.degrees(2 * math.atan(aperture/(focal*2)))
Here ‘aperture’ is either the horizontal or vertical size, returning either the horizontal fov (hfov) or the vertical fov (vfov). In Blender Camera the value in “Angle/Degrees” is the hfov
But Nuke reads and uses the vfov. Looking in the import_chan_file.tcl Nuke script, the math formula (in Python)
def fov2focal( fov, aperture ):
return (aperture/2) / ( math.tan(math.radians(fov/2)) )
is looking for/using the vertical aperture size in the Nuke Camera properties, since this data is not included in the chan-file. This means you have to enter this data before importing the chan-file. Another important thing to do in Nuke, is setting the rotation-order to XZY:
Only then will the numbers, and ultimately image, match. Not totally smooth, but I’m working on it
The required actions in Nuke is hard to do something about, and as it is right know Blender Camera is lacking crucial information. Hopefully Blender Camera will work just like BLenses.
I would like to make this script a real Blender 2.5 exporter, but I’m not totally comfortable with creating custom properties (yet), and I still believe that this information should be part of Blenders camera. Until then I still have a lot of Python to learn! And hopefully I can make the chan-importer too
Edit: Completely forgot to mention that it is important to set the render output size in Blender with the right aspect ratio, taken from hap/vap. For example with a “Full frame 35mm” (36x24mm), if the render output is set to 800x600 it has to be changed to 800x533 to match, since 800/533 = 1.5. BLenses can calculate this for you.
I will also post the modified script for Blender 2.49b when I find it!