Commit e0e46c57 authored by Dalai Felinto's avatar Dalai Felinto
Browse files

basic oculus iteration (highly untested)

... since I have no Oculus ;) but it should be easy to get it to work once someone tests it with oculus
parent 5596b6c2
Loading
Loading
Loading
Loading
+19 −4
Changes for space_view3d_virtual_reality/__init__.py: 19 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ from .opengl_helper import (
        resize,
        )

from . import oculus

def get_context_3dview (context):
    """returns area and space"""
@@ -88,9 +89,15 @@ class VirtualRealityViewportOperator(bpy.types.Operator):
    _enabled = True
    _timer = None

    @classmethod
    def poll(cls, context):
        camera = context.scene.camera
        return camera and camera.type == 'CAMERA'

    def modal(self, context, event):
        if event.type == 'ESC':
            return self.cancel(context)
            self.oculus.quit()
            return self.cancel()

        if event.type == 'TIMER':

@@ -107,6 +114,8 @@ class VirtualRealityViewportOperator(bpy.types.Operator):
            if (self.width != context.region.width) or (self.height != context.region.height):
                resize(self, context)

            self.oculus.update(context)


        return {'PASS_THROUGH'}

@@ -120,10 +129,16 @@ class VirtualRealityViewportOperator(bpy.types.Operator):
        """

        if context.area.type == 'VIEW_3D':
            scene = context.scene
            self.oculus = oculus.Oculus(scene.camera, self.report)

            if not self.oculus.isAvailable():
                return {'CANCELLED'}

            #if bpy.ops.wm.window_fullscreen_toggle.poll():
            #    bpy.ops.wm.window_fullscreen_toggle()

            context.scene.render.use_multiview = True
            scene.render.use_multiview = True
            context.window.stereo_3d_display.display_mode = 'SIDEBYSIDE'

            #if bpy.ops.screen.screen_full_area.poll():
@@ -185,11 +200,11 @@ class VirtualRealityViewportOperator(bpy.types.Operator):


def register():
    bpy.utils.register_module(__name__)
    bpy.utils.register_class(VirtualRealityViewportOperator)


def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.utils.unregister_class(VirtualRealityViewportOperator)


if __name__ == '__main__':
+106 −0
Changes for space_view3d_virtual_reality/oculus.py: 106 added lines, 0 removed lines.
Original line number Diff line number Diff line
class Oculus():
    def __init__(self, camera, report):
        self._report = report
        self._available = True
        self._hmd = None
        self._description = None
        self._camera = camera

        self._matrix_world = camera.matrix_world.copy()
        self._lens = camera.data.lens

        self._checkModule()
        self._start()

    def isAvailable(self):
        return self._available

    def _checkModule(self):
        """if library exists append it to sys.path"""
        import sys
        import os

        addon_path = os.path.dirname(os.path.abspath(__file__))
        oculus_path = os.path.join(addon_path, "lib", "python-ovrsdk")

        if oculus_path not in sys.path:
            sys.path.append(oculus_path)

    def _start(self):
        try:
            from oculusvr import (
                    Hmd,
                    cast,
                    POINTER,
                    )

            Hmd.initialize()

        except SystemError as err:
            self._error("Oculus initialization failed, check the physical connections and run again")
            return

        if Hmd.detect() == 1:
            self._hmd = Hmd()
            self._description = cast(self._hmd.hmd, POINTER(ovrHmdDesc)).contents
            self._frame = 0
            self._eye_offset = [ [0.0, 0.0, 0.0], [0.0, 0.0, 0.0] ]
            self._hmd.configure_tracking()

            print(self._description.ProductName)
            self._camera.data.lens = 16

        else:
            self._error("Oculus not connected")

    def _error(self, message):
        self._report({'ERROR'}, message)
        self._available = False

    def update(self):
        # update the camera
        matrix = self._getMatrix()

        if not matrix:
            return

        self._camera.matrix_world = self._matrix_world * matrix

    def quit(self):
        from oculusvr import Hmd

        if self._camera:
            self._camera.matrix_world = self._matrix_world
            self._camera.data.lens = self._lens

        if Hmd.detect() == 1:
            self._hmd.destroy()
            self._hmd = None
            Hmd.shutdown()

    def _getMatrix(self):
        from oculusvr import Hmd
        from mathutils import (
                Quaternion,
                Matrix,
                )

        if self._hmd and Hmd.detect() == 1:
            self_frame += 1

            poses = self._hmd.get_eye_poses(self._frame, self._eyes_offset)

            # oculus may be returning the matrix for both eyes
            # but we are using a single eye without offset

            rotation_raw = poses[0].Orientation.toList()
            position_raw = poses[0].Position.toList()

            rotation = Quaternion(rotation_raw).to_matrix().to_4x4()
            position = Matrix.Translation(position_raw)

            matrix = position * rotation
            matrix.invert()

            return matrix
        return None