Example #1
0
    def viewrotate_apply(self, context, event):
        # FIXME
        vod = self.vod
        x, y = event.x, event.y
        if context.user_preferences.inputs.view_rotate_method == 'TRACKBALL':
            newvec = calctrackballvec(context.region, event.x, event.y)
            dvec = newvec - vod.trackvec
            angle = (dvec.length / (2.0 * TRACKBALLSIZE)) * math.pi
            angle = angle_wrap_rad(angle)

            axis = vod.trackvec.cross(newvec)
            q1 = Quaternion(axis, angle)
            vod.viewquat = q1 * vod.oldquat

            self.viewrotate_apply_dyn_ofs(vod.viewquat)
        else:
            zvec_global = Vector([0, 0, 1])
            sensitivity = 0.007
            m = vod.viewquat.to_matrix()
            m_inv = m.inverted()

            if (zvec_global - m_inv.col[2]).length > 0.001:
                xaxis = zvec_global.closs(m_inv.col[0])
                if xaxis.dot(m_inv.col[0]) < 0:
                    xaxis.negate()
                fac = zvec_global.angle(m_inv.col[2]) / math.pi
                fac = abs(fac - 0.5) * 2
                fac *= fac
                xaxis = xaxis.lerp(m_inv.col[0], fac)
            else:
                xaxis = m_inv[0].copy()
            quat_local_x = Quaternion(xaxis, sensitivity * - (y - vod.oldy))
            quat_local_x = vod.viewquat * quat_local_x

            def axis_angle_to_quat_single(axis, angle):
                angle_half = angle * 0.5
                angle_cos = math.cos(angle_half)
                angle_sin = math.sin(angle_half)
                axis_index = ['X', 'Y', 'Z'].index(axis)
                q = Quaternion([angle_cos, 0, 0, 0])
                q[axis_index + 1] = angle_sin
                return q
            quat_global_z = axis_angle_to_quat_single(
                'Z', sensitivity * vod.reverse * (x - vod.oldx))
            vod.viewquat = quat_local_x * quat_global_z

            self.viewrotate_apply_dyn_ofs(vod.viewquat)

        vod.viewquat.normalize()
        context.region_data.view_rotation = vod.viewquat.inverted()
        if vod.axis_snap:
            self.viewrotate_apply_snap(vod)

        vod.oldx = x
        vod.oldy = y

        ED_view3d_camera_lock_sync(vod.v3d, context.region_data)

        context.region.tag_redraw()
        pass