Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Natural panning to older versions of Blender
bl_info = {
'name': 'Pan naturally',
'author': '',
'version': (0, 0, 1),
'blender': (2, 6, 0),
'location': '3d view > Alt + LMB-drag',
'description': 'Pan view naturally for older Blender versions.',
'wiki_url': '',
'tracker_url': '',
'category': '3D View'}
import bpy
import pprint
from mathutils import *
from math import *
class PanNaturally(bpy.types.Operator):
'''Pan naturally.'''
bl_idname = "view3d.pan_naturally"
bl_label = "Pan naturally"
horizontal_distance=0
vertical_distance=0
mouse_last_pos=Vector((0,0))
def execute(self, context):
region_3d = context.space_data.region_3d
xa=Vector((1,0,0))
ya=Vector((0,1,0))
xa.rotate(region_3d.view_rotation)
ya.rotate(region_3d.view_rotation)
region_3d.view_location = region_3d.view_location + self.horizontal_distance * xa + self.vertical_distance * ya
return {'FINISHED'}
def modal(self, context, event):
region_3d = context.space_data.region_3d
if event.type == 'TRACKPADPAN':
self.report({'INFO'}, str(event.mouse_x))
#find the yaw and pitch angles from the mouse position variation
mouse_pos=Vector((event.mouse_region_x,event.mouse_region_y))
self.horizontal_distance=-(mouse_pos.x-self.mouse_last_pos.x)/500.0*region_3d.view_distance
self.vertical_distance=-(mouse_pos.y-self.mouse_last_pos.y)/500.0*region_3d.view_distance
self.execute(context)
self.mouse_last_pos=Vector((event.mouse_region_x,event.mouse_region_y))
elif event.type in {'MOUSEMOVE', 'LEFTMOUSE', 'MIDDLEMOUSE', 'RIGHTMOUSE', 'ESC'}:
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.space_data.type == 'VIEW_3D':
region_3d = context.space_data.region_3d
self.mouse_last_pos=Vector((event.mouse_region_x,event.mouse_region_y))
context.window_manager.modal_handler_add(self)
if region_3d.view_perspective == 'CAMERA':
region_3d.view_perspective = 'PERSP'
return {'RUNNING_MODAL'}
else:
return {'CANCELLED'}
def register():
bpy.utils.register_class(PanNaturally)
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name="3D View", space_type='VIEW_3D')
#kmi = km.keymap_items.new('view3d.pan_naturally', 'EVT_TWEAK_L', 'ANY', shift=False, ctrl=False, alt=False)
kmi = km.keymap_items.new('view3d.pan_naturally', 'TRACKPADPAN', 'ANY')
def unregister():
bpy.utils.unregister_class(PanNaturally)
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps['3D View']
for kmi in km.keymap_items:
if kmi.idname == 'view3d.pan_naturally':
km.keymap_items.remove(kmi)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.