Skip to content

Instantly share code, notes, and snippets.

@bnolan
Created April 7, 2014 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnolan/10016675 to your computer and use it in GitHub Desktop.
Save bnolan/10016675 to your computer and use it in GitHub Desktop.
import sys
import bpy, mathutils, math
from mathutils import Vector
from math import pi
def findMidPoint():
sum = Vector((0,0,0))
n = 0
for ob in bpy.data.objects:
if ob.type not in ['CAMERA', 'LAMP', 'EMPTY']:
sum += ob.location
n += 1
if n == 0:
return sum
else:
return sum/n
pi = 3.14159265
fov = 50
scene = bpy.data.scenes["Scene"]
def createLamp(name, lamptype, loc):
bpy.ops.object.add(
type='LAMP',
location=loc
)
ob = bpy.context.object
ob.name = name
lamp = ob.data
lamp.name = 'Lamp'+name
lamp.type = lamptype
return ob
def addTrackToConstraint(ob, name, target):
cns = ob.constraints.new('TRACK_TO')
cns.name = name
cns.target = target
cns.track_axis = 'TRACK_NEGATIVE_Z'
cns.up_axis = 'UP_Y'
cns.owner_space = 'WORLD'
cns.target_space = 'WORLD'
return
def createCamera(origin, target):
cameraLocation = origin + Vector((100,-100,0))
# Create object and camera
bpy.ops.object.add(
type='CAMERA',
location=cameraLocation,
rotation=(0,0,0)
)
ob = bpy.context.object
ob.name = 'MyCamOb'
cam = ob.data
cam.name = 'MyCam'
addTrackToConstraint(ob, 'TrackMiddle', target)
sun = createLamp('sun', 'SUN', origin+Vector((-50,-50,50)))
lamp = sun.data
lamp.type = 'SUN'
addTrackToConstraint(sun, 'TrackMiddle', target)
# Lens
cam.type = 'PERSP'
cam.lens = 90
cam.lens_unit = 'MILLIMETERS'
cam.shift_x = -0.05
cam.shift_y = 0.1
cam.clip_start = 10.0
cam.clip_end = 250.0
empty = bpy.data.objects.new('DofEmpty', None)
empty.location = origin
cam.dof_object = empty
# Display
cam.show_title_safe = True
cam.show_name = True
# Make this the current camera
scn = bpy.context.scene
scn.camera = ob
return ob
def run(origin):
# Delete all old cameras and lamps
scn = bpy.context.scene
for ob in scn.objects:
# if ob.type == 'CAMERA': # or ob.type == 'LAMP':
scn.objects.unlink(ob)
# bpy.ops.import_scene.obj(filepath="/Users/ben/Desktop/suzanne.obj", axis_forward='-Z', axis_up='Y')
# bpy.ops.wm.collada_import(filepath="/Users/ben/Desktop/suzanne.dae")
bpy.ops.wm.collada_import(filepath="/Users/ben/Desktop/f15.dae")
infinity = 10000
minx = infinity
maxx = -infinity
miny = infinity
maxy = -infinity
minz = infinity
maxz = -infinity
for obj in scn.objects:
scale = obj.scale
minx = min(minx, obj.bound_box[0][0] * scale.x)
maxx = max(maxx, obj.bound_box[4][0] * scale.x)
miny = min(miny, obj.bound_box[0][1] * scale.y)
maxy = max(maxy, obj.bound_box[2][1] * scale.y)
minz = obj.bound_box[0][2] * scale.z
maxz = obj.bound_box[1][2] * scale.z
dx = maxx - minx
dy = maxy - miny
dz = maxz - minz
v = Vector((dx,dy,dz))
if v.length == 0.0:
factor = 1.0
else:
factor = 1.0 / v.length
obj.scale.x = factor
obj.scale.y = factor
obj.scale.z = factor
print(v.length)
# print(ob.bound_box)
# print(ob.scale)
# Add an empty at the middle of all render objects
# midpoint = findMidPoint()
bpy.ops.object.add(
type='EMPTY',
location=origin
)
target = bpy.context.object
target.name = 'Target'
createCamera(origin, target)
# createLamps(origin, target)
return
run(Vector((0,0,0)))
# Set render resolution
scene.render.resolution_x = 512
scene.render.resolution_y = 512
fov = 50 # float(sys.argv[6])
# Set camera fov in degrees
# scene.camera.data.angle = fov*(pi/180.0)
# # Set camera rotation in euler angles
# scene.camera.rotation_mode = 'XYZ'
# scene.camera.rotation_euler[0] = 0.0*(pi/180.0)
# scene.camera.rotation_euler[1] = 0.0*(pi/180.0)
# scene.camera.rotation_euler[2] = -30.0*(pi/180.0)
# # Set camera translation
# scene.camera.location.x = 10.0
# scene.camera.location.y = 10.0
# scene.camera.location.z = 10.0
# Set Scenes camera and output filename
bpy.data.scenes["Scene"].render.file_format = 'PNG'
bpy.data.scenes["Scene"].render.filepath = '//out'
# Render Scene and store the scene
bpy.ops.render.render( write_still=True )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment