fix: reset draw cam rotation
1.7.8 - fix: reset rotation in draw cam mode keep view in the same place (counter camera rotation) - code: initial enhancement for palette linking
This commit is contained in:
+88
-2
@@ -1,10 +1,14 @@
|
||||
from time import ctime
|
||||
import bpy
|
||||
import mathutils
|
||||
from mathutils import Vector#, Matrix
|
||||
from pathlib import Path
|
||||
import math
|
||||
from math import radians
|
||||
|
||||
from . import utils
|
||||
|
||||
|
||||
class GPTB_OT_copy_text(bpy.types.Operator):
|
||||
bl_idname = "wm.copytext"
|
||||
bl_label = "Copy to clipboard"
|
||||
@@ -280,6 +284,89 @@ class GPTB_OT_set_view_as_cam(bpy.types.Operator):
|
||||
return {"FINISHED"}
|
||||
|
||||
|
||||
class GPTB_OT_reset_cam_rot(bpy.types.Operator):
|
||||
bl_idname = "gp.reset_cam_rot"
|
||||
bl_label = "Reset rotation"
|
||||
bl_description = "Reset rotation of the draw manipulation camera"
|
||||
bl_options = {"REGISTER"}
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
return context.scene.camera and not context.scene.camera.name.startswith('Cam')
|
||||
# return context.region_data.view_perspective == 'CAMERA'# check if in camera
|
||||
|
||||
def get_center_view(self, context, cam):
|
||||
from bpy_extras.view3d_utils import location_3d_to_region_2d
|
||||
frame = cam.data.view_frame()
|
||||
mat = cam.matrix_world
|
||||
frame = [mat @ v for v in frame]
|
||||
frame_px = [location_3d_to_region_2d(context.region, context.space_data.region_3d, v) for v in frame]
|
||||
center_x = frame_px[2].x + (frame_px[0].x - frame_px[2].x)/2
|
||||
center_y = frame_px[1].y + (frame_px[0].y - frame_px[1].y)/2
|
||||
return mathutils.Vector((center_x, center_y))
|
||||
|
||||
def get_ui_ratio(self, context):
|
||||
'''correct ui overlap from header/toolbars'''
|
||||
regs = context.area.regions
|
||||
if context.preferences.system.use_region_overlap:
|
||||
w = context.area.width
|
||||
# minus tool header
|
||||
h = context.area.height - regs[0].height
|
||||
else:
|
||||
# minus tool leftbar + sidebar right
|
||||
w = context.area.width - regs[2].width - regs[3].width
|
||||
# minus tool header + header
|
||||
h = context.area.height - regs[0].height - regs[1].height
|
||||
|
||||
self.ratio = h / w
|
||||
self.ratio_inv = w / h
|
||||
|
||||
def execute(self, context):
|
||||
cam = context.scene.camera
|
||||
|
||||
# self.cam_init_euler = self.cam.rotation_euler.copy()
|
||||
if not cam.parent or cam.parent.type != 'CAMERA':
|
||||
self.report({'ERROR'}, "No parents to refer to for rotation reset")
|
||||
return {"CANCELLED"}
|
||||
|
||||
# store original rotation mode
|
||||
org_rotation_mode = cam.rotation_mode
|
||||
|
||||
# set to euler to works with quaternions, restored at finish
|
||||
cam.rotation_mode = 'XYZ'
|
||||
# store camera matrix world
|
||||
org_cam_matrix = cam.matrix_world.copy()
|
||||
|
||||
org_cam_z = cam.rotation_euler.z
|
||||
|
||||
## initialize current view_offset in camera
|
||||
view_cam_offset = mathutils.Vector(context.space_data.region_3d.view_camera_offset)
|
||||
|
||||
# Do the reset to parent transforms
|
||||
cam.matrix_world = cam.parent.matrix_world # wrong, get the parent rotation offset
|
||||
|
||||
# Get diff angle
|
||||
angle = cam.rotation_euler.z - org_cam_z
|
||||
# create rotation matrix with negative angle (we want to counter the move)
|
||||
neg = -angle
|
||||
rot_mat2d = mathutils.Matrix([[math.cos(neg), -math.sin(neg)], [math.sin(neg), math.cos(neg)]])
|
||||
|
||||
# restore original rotation mode
|
||||
cam.rotation_mode = org_rotation_mode
|
||||
|
||||
self.get_ui_ratio(context)
|
||||
# apply rotation matrix
|
||||
new_cam_offset = view_cam_offset.copy()
|
||||
new_cam_offset = mathutils.Vector((new_cam_offset[0], new_cam_offset[1] * self.ratio)) # apply screen ratio
|
||||
new_cam_offset.rotate(rot_mat2d)
|
||||
new_cam_offset = mathutils.Vector((new_cam_offset[0], new_cam_offset[1] * self.ratio_inv)) # restore screen ratio
|
||||
|
||||
context.space_data.region_3d.view_camera_offset = new_cam_offset
|
||||
|
||||
|
||||
return {"FINISHED"}
|
||||
|
||||
""" # old simple cam draw fit to cam
|
||||
class GPTB_OT_reset_cam_rot(bpy.types.Operator):
|
||||
bl_idname = "gp.reset_cam_rot"
|
||||
bl_label = "Reset rotation"
|
||||
@@ -306,9 +393,8 @@ class GPTB_OT_reset_cam_rot(bpy.types.Operator):
|
||||
else:
|
||||
self.report({'ERROR'}, "No parents to refer to for rotation reset")
|
||||
return {"CANCELLED"}
|
||||
|
||||
|
||||
return {"FINISHED"}
|
||||
"""
|
||||
|
||||
class GPTB_OT_toggle_mute_animation(bpy.types.Operator):
|
||||
bl_idname = "gp.toggle_mute_animation"
|
||||
|
||||
Reference in New Issue
Block a user