Welcome to mirror list, hosted at ThFree Co, Russian Federation.

action_map.py « viewport_vr_preview - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbd16fc24fa9d7bcf914cece2fc6f373f54b47c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# SPDX-License-Identifier: GPL-2.0-or-later

# <pep8 compliant>

if "bpy" in locals():
    import importlib
    importlib.reload(action_map_io)
    importlib.reload(defaults)
else:
    from . import action_map_io, defaults

import bpy
from bpy.app.handlers import persistent
from bpy_extras.io_utils import ExportHelper, ImportHelper
import importlib.util
import os.path


def vr_actionmap_active_get(session_settings):
    actionmaps = session_settings.actionmaps
    return (
        None if (len(actionmaps) <
                 1) else actionmaps[session_settings.active_actionmap]
    )


def vr_actionmap_selected_get(session_settings):
    actionmaps = session_settings.actionmaps
    return (
        None if (len(actionmaps) <
                 1) else actionmaps[session_settings.selected_actionmap]
    )


def vr_actionmap_item_selected_get(am):
    actionmap_items = am.actionmap_items
    return (
        None if (len(actionmap_items) <
                 1) else actionmap_items[am.selected_item]
    )


def vr_actionmap_user_path_selected_get(ami):
    user_paths = ami.user_paths
    return (
        None if (len(user_paths) <
                 1) else user_paths[ami.selected_user_path]
    )


def vr_actionmap_binding_selected_get(ami):
    actionmap_bindings = ami.bindings
    return (
        None if (len(actionmap_bindings) <
                 1) else actionmap_bindings[ami.selected_binding]
    )


def vr_actionmap_component_path_selected_get(amb):
    component_paths = amb.component_paths
    return (
        None if (len(component_paths) <
                 1) else component_paths[amb.selected_component_path]
    )


def vr_actionset_active_update(context):
    session_state = context.window_manager.xr_session_state
    if not session_state:
        return

    session_settings = context.window_manager.xr_session_settings
    am = vr_actionmap_active_get(session_settings)
    if not am:
        return

    session_state.active_action_set_set(context, am.name)


@persistent
def vr_create_actions(context: bpy.context):
    context = bpy.context
    session_state = context.window_manager.xr_session_state
    if not session_state:
        return

    # Check if actions are enabled.
    scene = context.scene
    if not scene.vr_actions_enable:
        return

    session_settings = context.window_manager.xr_session_settings

    if (len(session_settings.actionmaps) < 1):
        # Ensure default action maps.
        if not defaults.vr_ensure_default_actionmaps(session_settings):
            return

    for am in session_settings.actionmaps:    
        if len(am.actionmap_items) < 1:
            continue

        # Check for action maps where all bindings require OpenXR extensions.
        if am.name == defaults.VRDefaultActionmaps.TRACKER.value:
            if not session_settings.enable_vive_tracker_extension: #scene.vr_actions_enable_vive_tracker:
                continue

        ok = session_state.action_set_create(context, am)
        if not ok:
            return

        controller_grip_name = ""
        controller_aim_name = ""
        tracker_names = []

        for ami in am.actionmap_items:
            if len(ami.bindings) < 1:
                continue

            ok = session_state.action_create(context, am, ami)
            if not ok:
                return

            if ami.type == 'POSE':
                if ami.pose_is_controller_grip:
                    controller_grip_name = ami.name
                if ami.pose_is_controller_aim:
                    controller_aim_name = ami.name
                if ami.pose_is_tracker:
                    tracker_names.append(ami.name)

            for amb in ami.bindings:
                # Check for bindings that require OpenXR extensions.
                if amb.name == defaults.VRDefaultActionbindings.REVERB_G2.value:
                   if not scene.vr_actions_enable_reverb_g2:
                       continue
                elif amb.name == defaults.VRDefaultActionbindings.VIVE_COSMOS.value:
                   if not scene.vr_actions_enable_vive_cosmos:
                       continue
                elif amb.name == defaults.VRDefaultActionbindings.VIVE_FOCUS.value:
                   if not scene.vr_actions_enable_vive_focus:
                       continue
                elif amb.name == defaults.VRDefaultActionbindings.HUAWEI.value:
                   if not scene.vr_actions_enable_huawei:
                       continue

                ok = session_state.action_binding_create(context, am, ami, amb)
                if not ok:
                    return

        # Set controller and tracker pose actions.
        if controller_grip_name and controller_aim_name:
            session_state.controller_pose_actions_set(context, am.name, controller_grip_name, controller_aim_name)

        for tracker_name in tracker_names:
            session_state.tracker_pose_action_add(context, am.name, tracker_name)

    # Set active action set.
    vr_actionset_active_update(context)


def vr_load_actionmaps(session_settings, filepath):
    if not os.path.exists(filepath):
        return False

    spec = importlib.util.spec_from_file_location(os.path.basename(filepath), filepath)
    file = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(file)

    action_map_io.actionconfig_init_from_data(session_settings, file.actionconfig_data, file.actionconfig_version)

    return True


def vr_save_actionmaps(session_settings, filepath, sort=False):
    action_map_io.actionconfig_export_as_data(session_settings, filepath, sort=sort)

    print("Saved XR actionmaps: " + filepath)
    
    return True


def register():
    bpy.types.Scene.vr_actions_enable = bpy.props.BoolProperty(
        name="Use Controller Actions",
        description="Enable default VR controller actions, including controller poses and haptics",
        default=True,
    )
    bpy.types.Scene.vr_actions_enable_huawei = bpy.props.BoolProperty(
        description="Enable bindings for the Huawei controllers. Note that this may not be supported by all OpenXR runtimes",
        default=False,
    )
    bpy.types.Scene.vr_actions_enable_reverb_g2 = bpy.props.BoolProperty(
        description="Enable bindings for the HP Reverb G2 controllers. Note that this may not be supported by all OpenXR runtimes",
        default=False,
    )
    bpy.types.Scene.vr_actions_enable_vive_cosmos = bpy.props.BoolProperty(
        description="Enable bindings for the HTC Vive Cosmos controllers. Note that this may not be supported by all OpenXR runtimes",
        default=False,
    )
    bpy.types.Scene.vr_actions_enable_vive_focus = bpy.props.BoolProperty(
        description="Enable bindings for the HTC Vive Focus 3 controllers. Note that this may not be supported by all OpenXR runtimes",
        default=False,
    )
    # Stored in session settings to use in session creation as a workaround for SteamVR controller/tracker compatibility issues. 
    #bpy.types.Scene.vr_actions_enable_vive_tracker = bpy.props.BoolProperty(
    #    description="Enable bindings for the HTC Vive Trackers. Note that this may not be supported by all OpenXR runtimes",
    #    default=False,
    #)

    bpy.app.handlers.xr_session_start_pre.append(vr_create_actions)


def unregister():
    del bpy.types.Scene.vr_actions_enable
    del bpy.types.Scene.vr_actions_enable_huawei
    del bpy.types.Scene.vr_actions_enable_reverb_g2
    del bpy.types.Scene.vr_actions_enable_vive_cosmos
    del bpy.types.Scene.vr_actions_enable_vive_focus
    #del bpy.types.Scene.vr_actions_enable_vive_tracker

    bpy.app.handlers.xr_session_start_pre.remove(vr_create_actions)