SketchUP exports camera in python

what requirements to build a script in python that creates a camera and then adds TrackToConstraint to this camera?

Sketchup script export the following code:

import Blender

scene = Blender.Scene.getCurrent()
context = scene.getRenderingContext()
context.imageSizeX(1316)
context.imageSizeY(632)

cam1 = Blender.Camera.New(‘persp’, ‘cam1’)
cam1.setLens(19.9596387112738)
objcam1 = Blender.Object.New(‘Camera’, ‘CameraSkpAdvanced’)
objcam1.setLocation([0.0, 0.0, 0.0])
objcam1.link(cam1)
scene.link(objcam1)
empcam1 = Blender.Object.New(‘Empty’, ‘CameraSkpAdvanced’)
empcam1.setLocation([65.0, -0.0, 0.0])
scene.link(empcam1)
objcam1.makeTrack(empcam1)

This code got error because is old. I think.
Someone can upgrade this code? Or point out a reading to create something like this.
Please!

import bpy

scene = bpy.context.scene


scene.render.resolution_x = 1316
scene.render.resolution_y = 632


cam1 = bpy.data.cameras.new('cam1')
cam1.lens = 19.9596387112738


objcam1 = bpy.data.objects.new('CameraSkpAdvanced', cam1)
objcam1.location = (0.0, 0.0, 0.0)
scene.objects.link(objcam1)


empcam1 = bpy.data.objects.new('CameraSkpAdvanced', None)
empcam1.location = (65.0, -0.0, 0.0)
scene.objects.link(empcam1)


con = objcam1.constraints.new('TRACK_TO')
con.target = empcam1
con.track_axis = 'TRACK_NEGATIVE_Z'
con.up_axis = 'UP_Y'


scene.update()

Thank you so much! God bless you dude.

I can insert rotation in camera because is blocked or you recommends add a transform constraint?

What do you mean? The camera tracks the empty, so it won’t accept rotation. You could however animate the camera shift.

About rotation, i thought in a photo tilted.
The old code that i have, has a section to animation and uses IPO curves. Do you can upgrade this function too?

import bpy


scene = bpy.context.scene



scene.render.resolution_x = 1316
scene.render.resolution_y = 632



sucam = bpy.data.cameras.new('sucam')
objsucam = bpy.data.objects.new('Camera', 'SU Anim Cam')
objsucam.link(sucam)
scene.link(objsucam)
empsucam = bpy.data.objects.new('SU Anim Cam', None')
scene.link(empsucam)
objsucam.makeTrack(empsucam)


suipolens = Blender.Ipo.New('Camera', 'suipolens')
sucam.setIpo(suipolens)


suipolens.addCurve('Lens')
suipol = suipolens.getCurve('Lens')
suipol.setInterpolation('Linear')


# add a IPO curve to the location X, Y and Z values and rotation X, Y, and Z values
suipoloc = Blender.Ipo.New('Object', 'suipoloc')
objsucam.setIpo(suipoloc)


suiporot = Blender.Ipo.New('Object', 'suiporot')
empsucam.setIpo(suiporot)


suipoloc.addCurve('LocX')
suipolx = suipoloc.getCurve('LocX')
suipolx.setInterpolation('Linear')
suipoloc.addCurve('LocY')
suipoly = suipoloc.getCurve('LocY')
suipoly.setInterpolation('Linear')
suipoloc.addCurve('LocZ')
suipolz = suipoloc.getCurve('LocZ')
suipolz.setInterpolation('Linear')


suiporot.addCurve('LocX')
suiporx = suiporot.getCurve('LocX')
suiporx.setInterpolation('Linear')
suiporot.addCurve('LocY')
suipory = suiporot.getCurve('LocY')
suipory.setInterpolation('Linear')
suiporot.addCurve('LocZ')
suiporz = suiporot.getCurve('LocZ')
suiporz.setInterpolation('Linear')


# for camera position add bezier points to the IPO curves, LocX, LocY, LocZ, RotX, RotY, RotZ and Lens
suipol.addBezier((1, 28.6766700350581))
suipolx.addBezier((1, -0.0))
suipoly.addBezier((1, -0.0))
suipolz.addBezier((1, 0.0))
suiporx.addBezier((1, -0.0))
suipory.addBezier((1, 12.0))
suiporz.addBezier((1, -0.0))


suipol.addBezier((2, 28.6766700350581))
suipolx.addBezier((2, 0.0))
suipoly.addBezier((2, -0.0))
suipolz.addBezier((2, 0.0))
suiporx.addBezier((2, -0.0))
suipory.addBezier((2, 12.0))
suiporz.addBezier((2, -0.0)) 

what does “Object” refer to? The empty that is used for the track-to constraint? Or an object not added by the code?

I think will be the Empty. Not sure. To the camera walk by the curve.

here is a possible solution:

import bpyfrom math import radians




# Expects fcurve, data_list (pairs of frame and value) and interpolation type
def add_keyframes(curve, data_list, interpolation):
    for entry in data_list:
        point = curve.keyframe_points.insert(entry[0], entry[1])
        point.interpolation = 'LINEAR'




scene = bpy.context.scene




scene.render.resolution_x = 1316
scene.render.resolution_y = 632




cam1 = bpy.data.cameras.new('cam1')
cam1.lens = 19.9596387112738




objcam1 = bpy.data.objects.new('CameraSkpAdvanced', cam1)
objcam1.location = (0.0, 0.0, 0.0)
scene.objects.link(objcam1)




empcam1 = bpy.data.objects.new('CameraSkpAdvanced', None)
empcam1.location = (65.0, -0.0, 0.0)
scene.objects.link(empcam1)


bpy.ops.mesh.primitive_monkey_add()
monkey = bpy.context.object
monkey.scale = (5,5,5)
con = monkey.constraints.new('COPY_LOCATION')
con.target = empcam1




con = objcam1.constraints.new('TRACK_TO')
con.target = empcam1
con.track_axis = 'TRACK_NEGATIVE_Z'
con.up_axis = 'UP_Y'


# Could do low-level, but let's keep it simple here
cam1.lens = 28.6766700350581
cam1.keyframe_insert(data_path="lens", frame=1)
cam1.lens = 28.6766700350581 # same value?!
cam1.keyframe_insert(data_path="lens", frame=2)
for point in cam1.animation_data.action.fcurves[0].keyframe_points:
    point.interpolation = 'LINEAR'


# Do fully low-level here
empcam1.animation_data_create()
action = bpy.data.actions.new("Object")
empcam1.animation_data.action = action


locx = action.fcurves.new(data_path="location", index=0, action_group="Location")
locy = action.fcurves.new(data_path="location", index=1, action_group="Location")
locz = action.fcurves.new(data_path="location", index=2, action_group="Location")


rotx = action.fcurves.new(data_path="rotation_euler", index=0, action_group="Rotation")
roty = action.fcurves.new(data_path="rotation_euler", index=1, action_group="Rotation")
rotz = action.fcurves.new(data_path="rotation_euler", index=2, action_group="Rotation")


locs_z = ((1, 22), (20, 40), (35, 5), (50, 8))
add_keyframes(locz, locs_z, 'LINEAR')


locs_x = ((1, empcam1.location.x), (10, 44.4), (20, 40), (60, 30))
add_keyframes(locx, locs_x, 'LINEAR')


rots_y = ((10, 0), (20, radians(35)), (40, radians(-45)), (50, radians(-10)))
add_keyframes(roty, rots_y, 'BEZIER')


scene.update()