camera2empty

Übersicht: Python & Blender


Objectives

Justify and positioning a camera is a little bit tricky. But a script makes it easy to point the camara to objects of interest in a scene.

Instructions

Tasks:
  1. create a new python file (use the template)
  2. create a new class
  3. record the steps to reach the target (once)
  4. copy the commands from Info window to the script
  5. modify the commands
  6. test the Script

Which actions are necessary?

  1. delete Cube and Camera

  2. add a new Camera

  3. add an Empty

    /blender-setup-set/camera2empty.png
  4. Select both objects the Empty and in a second step the camera

  5. Add a constraint »trac to« with the following settings:

    • Target: Empty
    • To: -Z
    • Up: Y
    /blender-setup-set/constraint-trac-to.png
  6. Copy the Protocoll of the click actions to the script (have a look to the info window).

    /blender-setup-set/copy-protocoll.png

    in addition you can:

    • delete the layer parameter (we don't need the layers)

    • replace the location and rotation values with zero's

      bpy.ops.object.delete(use_global=False)
      
      bpy.ops.object.camera_add(view_align=True,
                                enter_editmode=False,
                                location=(0, 0, 0),
                                rotation=(0, 0, 0))
      bpy.ops.object.empty_add(type='PLAIN_AXES',
                               view_align=False,
                               location=(0, 0, 0))
      bpy.ops.object.constraint_add(type='TRACK_TO')
      bpy.context.object.constraints["Track To"].target = bpy.data.objects["Empty"]
      bpy.context.object.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'
      bpy.context.object.constraints["Track To"].up_axis = 'UP_Y'
      

Now you can load the script und with one run, the Empty can be moved and the camera will always point to the new location of the Empty. Therefore it's easy to position the camera view.

The complete Script

blender-extended/blender-setup-set/camera2empty.py (Source)

#!bpy
"""
Name: 'SceneConfigurator'
Blender: 2.7x
Group: 'Low poly'
Tooltip: 'Configure standard settings in a new scene'
"""
import bpy


class SceneConfigurator:
    """Configure a scene"""

    def __init__(self):
        pass

    def cam2empty(self):
        """Trac camera to empty

        Move the empty and the camera will point in this direction
        """
        bpy.ops.object.empty_add(type='PLAIN_AXES',
                                 view_align=False,
                            location=(0, 0, 0))
        bpy.ops.object.camera_add(view_align=True,
                                  enter_editmode=False,
                             location=(8, 8, 30),
                             rotation=(0, 0, 0))
        bpy.ops.object.select_pattern(pattern='Empty')
        bpy.ops.object.select_pattern(pattern='Camera')
        bpy.ops.object.constraint_add(type='TRACK_TO')
        bpy.context.object.constraints["Track To"].target = bpy.data.objects["Empty"]
        bpy.context.object.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'
        bpy.context.object.constraints["Track To"].up_axis = 'UP_Y'

    def sky(self):
        bpy.context.scene.world.use_sky_paper = True
        bpy.context.scene.world.use_sky_blend = True
        bpy.context.scene.world.horizon_color = (0.606907, 0.613911, 0.837134)

    def remove(self, name=None):
        """ Delete an object by name"""

        if name:
            bpy.ops.object.select_pattern(pattern=name)
        else:
            print('nothing to do')

        bpy.ops.object.delete()

if __name__ == "__main__":

    # switch to object mode, if nessasary
    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode='OBJECT')
    ctrl = SceneConfigurator()
    ctrl.remove(name="Empty")
    ctrl.remove(name="Camera")
    ctrl.cam2empty()
    ctrl.sky()

Comments