[Bf-committers] Camera view navigation rant

bartius crouch bartius.crouch at gmail.com
Tue Apr 26 15:43:15 CEST 2011


> Also a lock camera to view option would be great so we can just move
> the camera like we move any other viewport

Like Campbell said, that isn't very hard to do in python. Below you can find
some code for it.
Copy the text below into the text-editor, Run Script (alt+P), go to 3d-view,
search (spacebar) for Lock camera to view.



import bpy
import mathutils


class ModalOperator(bpy.types.Operator):
    '''Move camera along with the viewport'''
    bl_idname = "view3d.lock_camera"
    bl_label = "Lock camera to view"

    @classmethod
    def poll(cls, context):
        camera = context.scene.camera
        if not camera:
            return(False)
        return(camera.name in context.scene.objects)

    def modal(self, context, event):
        camera = context.scene.camera
        rv3d = context.space_data.region_3d

        # rotation + location
        camera.matrix_world = rv3d.view_matrix.copy().inverted()

        # override location to take view_distance into account
        rotation = rv3d.view_matrix.copy().to_3x3().inverted()
        z_normal = mathutils.Vector([0.0, 0.0, 1.0]) * rotation
        camera.location = rv3d.view_location + (rv3d.view_distance *
z_normal)

        # handle events
        if event.type in ['LEFTMOUSE', 'NUMPAD_ENTER', 'RET']:
            context.area.header_text_set()
            return {'FINISHED'}
        elif event.type == 'ESC':
            camera.matrix_world = self.camera_matrix
            context.area.header_text_set()
            return {'CANCELLED'}

        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        context.window_manager.modal_handler_add(self)
        self.camera_matrix = context.scene.camera.matrix_world.copy()
        context.space_data.region_3d.view_perspective = 'PERSP'
        context.area.header_text_set("ESC to cancel, ENTER or LMB to
confirm")
        return {'RUNNING_MODAL'}


def register():
    bpy.utils.register_class(ModalOperator)


def unregister():
    bpy.utils.unregister_class(ModalOperator)


if __name__ == "__main__":
    register()


More information about the Bf-committers mailing list