Skip to content

Instantly share code, notes, and snippets.

@dropmeaword
Last active May 20, 2018 11:50
Show Gist options
  • Save dropmeaword/493764d8708206249aa69c8cb84f481d to your computer and use it in GitHub Desktop.
Save dropmeaword/493764d8708206249aa69c8cb84f481d to your computer and use it in GitHub Desktop.
Blender scripts for holographic projection

Array of cameras

The following script creates an array of cameras in position pos, looking at target, with a name prefix of dimensions dimwxdimh, with a distance between cameras specified by hdist and wdist.

import bpy

def  get_cameras(prefix):
    return [obj for obj in bpy.data.objects if( (obj.type =='CAMERA') and (prefix in obj.name) )]

class CameraArray:
    def __init__(self, pos, target, name="array", dimw=10, dimh=8, hdist=1, vdist=1):
        self.name = name
        ref = target.matrix_world.to_translation()
        scn = bpy.context.scene
        for h in iter(range(1, dimh+1)):
            for w in iter(range(1, dimw+1)):
                cam = bpy.data.cameras.new(name)
                ob = bpy.data.objects.new(name, cam)
                ob.location = pos
                ob.location.y += w
                ob.location.z += h
                scn.objects.link(ob)
                scn.update()
                #self.__look_at(ob, target)

    
    def look_at(self, target):
        cams = get_cameras(self.name)
        for cam in cams:
            print("Processing ", cam.name)
            ref = target.matrix_world.to_translation()
            loc = cam.matrix_world.to_translation()

            direction = ref - loc

            rot_quat = direction.to_track_quat('-Z', 'Y')
            cam.rotation_euler = rot_quat.to_euler()


target = bpy.data.objects["Cube"]
arr = CameraArray((8.0, 0, 2.0), target, "arr", 8, 5, 0.5, 0.3)
arr.look_at(target)

001 002

Render scene from each camera

Untested. Renders scene from all cameras with prefix in their name.

# Render all cameras or cameras containing text given with command line argument "cameras".
# Example:
# Let's say test.blend file contains cameras "east.01", "east.02", "west.01", "west.02"
# By executing command "blender -b your_file.blend -P render_all_cameras.py" all 4 cameras will be rendered.
# Command "blender -b your_file.blend -P render_all_cameras.py  cameras=east" will render "east.01" and "east.02" cameras.
# Command "blender -b your_file.blend -P render_all_cameras.py  cameras=01" will render "east.01" and "west.01.02" cameras.


import bpy, bgl, blf,sys
from bpy import data, ops, props, types, context
print("\nThis Python script will render your scene with all available cameras")
print("or with camera(s) matching command line argument 'cameras'")
print("")
print("Usage:")
print("Render all cameras:")
print("blender -b your_file.blend -P render_all_cameras.py\n")
print("Render only matching cameras:")
print("blender -b your_file.blend -P render_all_cameras.py  cameras=east\n")

cameraNames=''

# Loop all command line arguments and try to find "cameras=east" or similar
for arg in sys.argv:
    words=arg.split('=')
    if ( words[0] == 'cameras'):
     cameraNames = words[1]

print('rendering cameras containing [' + cameraNames + ']')

print('\nPrint Scenes...')
sceneKey = bpy.data.scenes.keys()[0]
print('Using Scene['  + sceneKey + ']')

# Loop all objects and try to find Cameras
print('Looping Cameras')
c=0
for obj in bpy.data.objects:
    # Find cameras that match cameraNames
    if ( obj.type =='CAMERA') and ( cameraNames == '' or obj.name.find(cameraNames) != -1) :
      print("Rendering scene["+sceneKey+"] with Camera["+obj.name+"]")

      # Set Scenes camera and output filename
      bpy.data.scenes[sceneKey].camera = obj
      bpy.data.scenes[sceneKey].render.file_format = 'JPEG'
      bpy.data.scenes[sceneKey].render.filepath = '//camera_' + str(c)

      # Render Scene and store the scene
      bpy.ops.render.render( write_still=True )
      c = c + 1
print('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment