Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMaster7000 authored Dec 18, 2022
1 parent b76ee77 commit 5609875
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Focus_To_Cursor_Blender_Addon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
bl_info = {
"name": "Focus to Cursor",
"category": "Object",
"author": "CodeMaster7000"
}

import bpy
import math
from mathutils import Vector

class FocusToCursor(bpy.types.Operator):
"""Camera Focus to 3D Cursor"""
bl_idname = "object.focus_to_cursor"
bl_label = "Camera Focus to Cursor"
bl_options ={'REGISTER', 'UNDO'}
def execute(self, context):
scene = context.scene
debug = False
obj_camera = bpy.context.scene.camera
if obj_camera is None or obj_camera.type != 'CAMERA' :
self.report({'ERROR'}, 'No active camera to set focus for.')
return {'CANCELLED'}
cameraLocation= obj_camera.location
cursorLocation= bpy.context.scene.cursor_location
cameraPlaneNormal = obj_camera.matrix_world.to_quaternion() * Vector((0.0, 0.0, -1.0))
if debug:
print("VectorCamera:")
print(cameraPlaneNormal[0])
print(cameraPlaneNormal[1])
print(cameraPlaneNormal[2])
print("LocationCamera:")
print(cameraLocation.x)
print(cameraLocation.y)
print(cameraLocation.z)
print("LocationCursor:")
print(cursorLocation.x)
print(cursorLocation.y)
print(cursorLocation.z)
d = (cameraPlaneNormal[0]*cameraLocation.x + cameraPlaneNormal[1]*cameraLocation.y + cameraPlaneNormal[2]*cameraLocation.z)
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)
obj_camera.data.dof_distance = distance
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(FocusToCursor.bl_idname)
def register():
bpy.utils.register_class(FocusToCursor)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.utils.unregister_class(FocusToCursor)
if __name__ == "__main__":
register()

0 comments on commit 5609875

Please sign in to comment.