|
| 1 | +bl_info = { |
| 2 | + "name": "Focus to Cursor", |
| 3 | + "category": "Object", |
| 4 | + "author": "CodeMaster7000" |
| 5 | +} |
| 6 | + |
| 7 | +import bpy |
| 8 | +import math |
| 9 | +from mathutils import Vector |
| 10 | + |
| 11 | +class FocusToCursor(bpy.types.Operator): |
| 12 | + """Camera Focus to 3D Cursor""" |
| 13 | + bl_idname = "object.focus_to_cursor" |
| 14 | + bl_label = "Camera Focus to Cursor" |
| 15 | + bl_options ={'REGISTER', 'UNDO'} |
| 16 | + def execute(self, context): |
| 17 | + scene = context.scene |
| 18 | + debug = False |
| 19 | + obj_camera = bpy.context.scene.camera |
| 20 | + if obj_camera is None or obj_camera.type != 'CAMERA' : |
| 21 | + self.report({'ERROR'}, 'No active camera to set focus for.') |
| 22 | + return {'CANCELLED'} |
| 23 | + cameraLocation= obj_camera.location |
| 24 | + cursorLocation= bpy.context.scene.cursor_location |
| 25 | + cameraPlaneNormal = obj_camera.matrix_world.to_quaternion() * Vector((0.0, 0.0, -1.0)) |
| 26 | + if debug: |
| 27 | + print("VectorCamera:") |
| 28 | + print(cameraPlaneNormal[0]) |
| 29 | + print(cameraPlaneNormal[1]) |
| 30 | + print(cameraPlaneNormal[2]) |
| 31 | + print("LocationCamera:") |
| 32 | + print(cameraLocation.x) |
| 33 | + print(cameraLocation.y) |
| 34 | + print(cameraLocation.z) |
| 35 | + print("LocationCursor:") |
| 36 | + print(cursorLocation.x) |
| 37 | + print(cursorLocation.y) |
| 38 | + print(cursorLocation.z) |
| 39 | + d = (cameraPlaneNormal[0]*cameraLocation.x + cameraPlaneNormal[1]*cameraLocation.y + cameraPlaneNormal[2]*cameraLocation.z) |
| 40 | + distance = (cameraPlaneNormal[0]*cursorLocation.x + cameraPlaneNormal[1]*cursorLocation.y + cameraPlaneNormal[2]*cursorLocation.z - d) / math.sqrt(cameraPlaneNormal[0]**2 + cameraPlaneNormal[1]**2 + cameraPlaneNormal[2]**2) |
| 41 | + obj_camera.data.dof_distance = distance |
| 42 | + return {'FINISHED'} |
| 43 | +def menu_func(self, context): |
| 44 | + self.layout.operator(FocusToCursor.bl_idname) |
| 45 | +def register(): |
| 46 | + bpy.utils.register_class(FocusToCursor) |
| 47 | + bpy.types.VIEW3D_MT_object.append(menu_func) |
| 48 | +def unregister(): |
| 49 | + bpy.utils.unregister_class(FocusToCursor) |
| 50 | +if __name__ == "__main__": |
| 51 | + register() |
0 commit comments