blender-edutech/Tools/blender_add-on/camera_fit_view.py

72 lines
2.2 KiB
Python
Raw Normal View History

bl_info = {
"name": "Camera Fit View",
"author": "Yashar JafarKhanpour",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > View > Cameras > Camera Fit View, Camera From View",
"category": "3D View",
"description": "Add two operators to Cameras menu: Align Camera to View, Create Camera from view",
}
import bpy
from bpy.types import Operator
class VIEW3D_OT_camera_fit_view(Operator):
"""Align and fit camera to view and match lens properties.
Works on selected camera or scene camera if no camera is selected.
Creates new camera if checked in options"""
bl_idname = "view3d.camera_fit_view"
bl_label = "Camera to View"
bl_options = {'UNDO'}
new : bpy.props.BoolProperty(name="Create New Camera",options = {'SKIP_SAVE'})
@classmethod
def poll(cls,context):
return context.space_data.region_3d.view_perspective != 'CAMERA'
def execute(self, context):
if self.new:
bpy.ops.object.camera_add()
obj = context.object
spd = context.space_data
cam = context.scene.camera
if obj and obj.type == 'CAMERA' and obj != cam:
cam = context.scene.camera = obj
if cam:
bpy.ops.view3d.camera_to_view()
cam.data.sensor_width = 72
cam.data.lens = spd.lens
cam.data.clip_start = spd.clip_start
cam.data.clip_end = spd.clip_end
spd.region_3d.view_camera_offset = [0,0]
spd.region_3d.view_camera_zoom = 29.0746
return {'FINISHED'}
def add_menu_item(self, context):
self.layout.operator(VIEW3D_OT_camera_fit_view.bl_idname,text="Fit Camera to View")
self.layout.operator(VIEW3D_OT_camera_fit_view.bl_idname,text="Create Camera from View").new=True
def register():
bpy.utils.register_class(VIEW3D_OT_camera_fit_view)
bpy.types.VIEW3D_MT_view_cameras.append(add_menu_item)
def unregister():
bpy.utils.unregister_class(VIEW3D_OT_camera_fit_view)
bpy.types.VIEW3D_MT_view_cameras.remove(add_menu_item)
if __name__ == "__main__":
register()