blender/scripts/templates_py/operator_modal_view3d.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
2.3 KiB
Python
Raw Permalink Normal View History

2010-07-08 16:24:24 +00:00
import bpy
2010-05-20 07:49:41 +00:00
from mathutils import Vector
from bpy.props import FloatVectorProperty
2010-05-20 07:49:41 +00:00
class ViewOperator(bpy.types.Operator):
"""Translate the view using mouse events"""
2010-05-20 07:49:41 +00:00
bl_idname = "view3d.modal_operator"
bl_label = "Simple View Operator"
offset: FloatVectorProperty(
2018-06-26 19:41:37 +02:00
name="Offset",
size=3,
)
2010-05-20 07:49:41 +00:00
def execute(self, context):
v3d = context.space_data
2010-05-20 07:49:41 +00:00
rv3d = v3d.region_3d
rv3d.view_location = self._initial_location + Vector(self.offset)
2010-05-20 07:49:41 +00:00
def modal(self, context, event):
v3d = context.space_data
2010-05-20 07:49:41 +00:00
rv3d = v3d.region_3d
if event.type == 'MOUSEMOVE':
self.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02
2010-05-20 07:49:41 +00:00
self.execute(context)
context.area.header_text_set("Offset {:.4f} {:.4f} {:.4f}".format(*self.offset))
2010-05-20 07:49:41 +00:00
elif event.type == 'LEFTMOUSE':
context.area.header_text_set(None)
2010-05-20 07:49:41 +00:00
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
2010-05-20 07:49:41 +00:00
rv3d.view_location = self._initial_location
context.area.header_text_set(None)
2010-05-20 07:49:41 +00:00
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.space_data.type == 'VIEW_3D':
v3d = context.space_data
2010-05-20 07:49:41 +00:00
rv3d = v3d.region_3d
if rv3d.view_perspective == 'CAMERA':
rv3d.view_perspective = 'PERSP'
self._initial_mouse = Vector((event.mouse_x, event.mouse_y, 0.0))
self._initial_location = rv3d.view_location.copy()
context.window_manager.modal_handler_add(self)
2010-05-20 07:49:41 +00:00
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "Active space must be a View3d")
return {'CANCELLED'}
def menu_func(self, context):
self.layout.operator(ViewOperator.bl_idname, text="Simple View Modal Operator")
# Register and add to the "view" menu (required to also use F3 search "Simple View Modal Operator" for quick access).
def register():
bpy.utils.register_class(ViewOperator)
bpy.types.VIEW3D_MT_view.append(menu_func)
def unregister():
bpy.utils.unregister_class(ViewOperator)
bpy.types.VIEW3D_MT_view.remove(menu_func)
if __name__ == "__main__":
register()