Skip to content

Instantly share code, notes, and snippets.

@tin2tin
Created October 11, 2018 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tin2tin/b95fa8686db3a03565f9064b692c71d5 to your computer and use it in GitHub Desktop.
Save tin2tin/b95fa8686db3a03565f9064b692c71d5 to your computer and use it in GitHub Desktop.
Add Camera to View by Dmitry aka Rockbard
bl_info = {
"name": "Add Camera to View",
"category": "3D View",
"description": """Adding camera to view with ctrl+shift+D""",
"author": "Dmitry aka Rockbard",
"version": (1, 0),
"blender": (2, 79, 0),
}
import bpy
import mathutils
from mathutils import Matrix
class AddCamToView(bpy.types.Operator):
"""Adds dynamic brush(es) and canvas in one click"""
bl_idname = "object.add_cam_to_view"
bl_label = "Add Camera to View"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# Setting variables
context = bpy.context
scene = context.scene
active = bpy.context.scene.objects.active
selected = bpy.context.selected_objects
#getting status of view
#print(bpy.context.space_data.region_3d.is_perspective)
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
if space.region_3d.view_perspective == 'CAMERA':
return {'FINISHED'}
#getting vieport focal lens
area = bpy.context.area
old_type = area.type
area.type = 'VIEW_3D'
focal = bpy.context.space_data.lens
area.type = old_type
# Adding camera
bpy.ops.object.camera_add()
#getting vieport focal lens
area = bpy.context.area
old_type = area.type
area.type = 'VIEW_3D'
focal = bpy.context.space_data.lens
area.type = old_type
#setting focal length
bpy.context.object.data.lens = focal
#Setting as active camera
currentCameraObj = bpy.data.objects[bpy.context.active_object.name]
scene.camera = currentCameraObj
#setting focal length
active_cam = bpy.context.scene.camera
bpy.data.cameras[active_cam.name].lens = focal
#setting sensor
#bpy.context.object.data.sensor_width = 52.5
#camera to view
bpy.ops.view3d.camera_to_view()
# Range mapping focus to locZ
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
x = rightMin + (valueScaled * rightSpan)
return(x)
locZ = float("%.1f" % translate(focal,13,45,0.1,0.7))
#fit canera to viewport
def viewFrom(screen):
for obj in screen.areas:
if (obj.type == 'VIEW_3D'):
return obj
return None
v3d = viewFrom(bpy.context.screen)
r3d = v3d.spaces[0].region_3d
r3d.view_camera_offset = (0,0)
r3d.view_camera_zoom = 29.13
#Move camera backwards on local axis
#ob = bpy.context.scene.objects.active
#loc = Matrix.Translation((0.0, 0.0, locZ))
#ob.matrix_basis *= loc
# Deselecting all
bpy.ops.object.select_all(action='DESELECT')
# Selecting previously selected object and making it active
bpy.context.scene.objects.active = active
#if something was selected, select it again
if len(selected) != 0:
active.select = True
#camera off
#bpy.ops.view3d.viewnumpad(type='CAMERA')
# Unchecking "Lock camera"
bpy.context.space_data.lock_camera = False
return {'FINISHED'}
class LockCameraToggle(bpy.types.Operator):
"""Adds dynamic brush(es) and canvas in one click"""
bl_idname = "object.lock_cam_toggle"
bl_label = "Lock Camera Toggle"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# Lock camera to View toggle
bpy.context.space_data.lock_camera = not bpy.context.space_data.lock_camera
return {'FINISHED'}
# register/unregister addon classes
addon_keymaps = []
def register():
bpy.utils.register_class(AddCamToView)
bpy.utils.register_class(LockCameraToggle)
# Add custom shortcuts
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(
name="Window", space_type='EMPTY', region_type='WINDOW')
kmi = km.keymap_items.new(
"object.add_cam_to_view", type='NUMPAD_0', value='PRESS', ctrl=True, shift=True)
kmi = km.keymap_items.new(
"object.lock_cam_toggle", type='L', value='PRESS', ctrl=True, shift=True)
addon_keymaps.append((km, kmi))
def unregister():
bpy.utils.unregister_class(CamToView)
bpy.utils.unregister_class(LockCameraToggle)
# Remove custom shortcuts
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment