1 # SPDX-FileCopyrightText: 2021-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
7 importlib
.reload(defaults
)
9 from . import action_map_io
, defaults
12 from bpy
.app
.handlers
import persistent
13 from bpy_extras
.io_utils
import ExportHelper
, ImportHelper
18 def vr_actionset_active_update(context
):
19 session_state
= context
.window_manager
.xr_session_state
20 if not session_state
or len(session_state
.actionmaps
) < 1:
25 if scene
.vr_actions_use_gamepad
and session_state
.actionmaps
.find(session_state
, defaults
.VRDefaultActionmaps
.GAMEPAD
.value
):
26 session_state
.active_action_set_set(context
, defaults
.VRDefaultActionmaps
.GAMEPAD
.value
)
28 # Use first action map.
29 session_state
.active_action_set_set(context
, session_state
.actionmaps
[0].name
)
32 def vr_actions_use_gamepad_update(self
, context
):
33 vr_actionset_active_update(context
)
37 def vr_create_actions(context
: bpy
.context
):
39 session_state
= context
.window_manager
.xr_session_state
43 # Check if actions are enabled.
45 if not scene
.vr_actions_enable
:
48 # Ensure default action maps.
49 if not defaults
.vr_ensure_default_actionmaps(session_state
):
52 for am
in session_state
.actionmaps
:
53 if len(am
.actionmap_items
) < 1:
56 ok
= session_state
.action_set_create(context
, am
)
60 controller_grip_name
= ""
61 controller_aim_name
= ""
63 for ami
in am
.actionmap_items
:
64 if len(ami
.bindings
) < 1:
67 ok
= session_state
.action_create(context
, am
, ami
)
71 if ami
.type == 'POSE':
72 if ami
.pose_is_controller_grip
:
73 controller_grip_name
= ami
.name
74 if ami
.pose_is_controller_aim
:
75 controller_aim_name
= ami
.name
77 for amb
in ami
.bindings
:
78 # Check for bindings that require OpenXR extensions.
79 if amb
.name
== defaults
.VRDefaultActionbindings
.REVERB_G2
.value
:
80 if not scene
.vr_actions_enable_reverb_g2
:
82 elif amb
.name
== defaults
.VRDefaultActionbindings
.VIVE_COSMOS
.value
:
83 if not scene
.vr_actions_enable_vive_cosmos
:
85 elif amb
.name
== defaults
.VRDefaultActionbindings
.VIVE_FOCUS
.value
:
86 if not scene
.vr_actions_enable_vive_focus
:
88 elif amb
.name
== defaults
.VRDefaultActionbindings
.HUAWEI
.value
:
89 if not scene
.vr_actions_enable_huawei
:
92 ok
= session_state
.action_binding_create(context
, am
, ami
, amb
)
96 # Set controller pose actions.
97 if controller_grip_name
and controller_aim_name
:
98 session_state
.controller_pose_actions_set(context
, am
.name
, controller_grip_name
, controller_aim_name
)
100 # Set active action set.
101 vr_actionset_active_update(context
)
104 def vr_load_actionmaps(session_state
, filepath
):
105 if not os
.path
.exists(filepath
):
108 spec
= importlib
.util
.spec_from_file_location(os
.path
.basename(filepath
), filepath
)
109 file = importlib
.util
.module_from_spec(spec
)
110 spec
.loader
.exec_module(file)
112 action_map_io
.actionconfig_init_from_data(session_state
, file.actionconfig_data
, file.actionconfig_version
)
117 def vr_save_actionmaps(session_state
, filepath
, sort
=False):
118 action_map_io
.actionconfig_export_as_data(session_state
, filepath
, sort
=sort
)
120 print("Saved XR actionmaps: " + filepath
)
126 bpy
.types
.Scene
.vr_actions_enable
= bpy
.props
.BoolProperty(
127 name
="Use Controller Actions",
128 description
="Enable default VR controller actions, including controller poses and haptics",
131 bpy
.types
.Scene
.vr_actions_use_gamepad
= bpy
.props
.BoolProperty(
132 description
="Use input from gamepad (Microsoft Xbox Controller) instead of motion controllers",
134 update
=vr_actions_use_gamepad_update
,
136 bpy
.types
.Scene
.vr_actions_enable_huawei
= bpy
.props
.BoolProperty(
137 description
="Enable bindings for the Huawei controllers. Note that this may not be supported by all OpenXR runtimes",
140 bpy
.types
.Scene
.vr_actions_enable_reverb_g2
= bpy
.props
.BoolProperty(
141 description
="Enable bindings for the HP Reverb G2 controllers. Note that this may not be supported by all OpenXR runtimes",
144 bpy
.types
.Scene
.vr_actions_enable_vive_cosmos
= bpy
.props
.BoolProperty(
145 description
="Enable bindings for the HTC Vive Cosmos controllers. Note that this may not be supported by all OpenXR runtimes",
148 bpy
.types
.Scene
.vr_actions_enable_vive_focus
= bpy
.props
.BoolProperty(
149 description
="Enable bindings for the HTC Vive Focus 3 controllers. Note that this may not be supported by all OpenXR runtimes",
153 bpy
.app
.handlers
.xr_session_start_pre
.append(vr_create_actions
)
157 del bpy
.types
.Scene
.vr_actions_enable
158 del bpy
.types
.Scene
.vr_actions_use_gamepad
159 del bpy
.types
.Scene
.vr_actions_enable_huawei
160 del bpy
.types
.Scene
.vr_actions_enable_reverb_g2
161 del bpy
.types
.Scene
.vr_actions_enable_vive_cosmos
162 del bpy
.types
.Scene
.vr_actions_enable_vive_focus
164 bpy
.app
.handlers
.xr_session_start_pre
.remove(vr_create_actions
)