Sun Position: update translation
[blender-addons.git] / viewport_vr_preview / action_map.py
blob98cd9ece819c5014c585c46cf7bcb3a929f8e87d
1 # SPDX-FileCopyrightText: 2021-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 if "bpy" in locals():
6 import importlib
7 importlib.reload(defaults)
8 else:
9 from . import action_map_io, defaults
11 import bpy
12 from bpy.app.handlers import persistent
13 from bpy_extras.io_utils import ExportHelper, ImportHelper
14 import importlib.util
15 import os.path
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:
21 return
23 scene = context.scene
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)
27 else:
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)
36 @persistent
37 def vr_create_actions(context: bpy.context):
38 context = bpy.context
39 session_state = context.window_manager.xr_session_state
40 if not session_state:
41 return
43 # Check if actions are enabled.
44 scene = context.scene
45 if not scene.vr_actions_enable:
46 return
48 # Ensure default action maps.
49 if not defaults.vr_ensure_default_actionmaps(session_state):
50 return
52 for am in session_state.actionmaps:
53 if len(am.actionmap_items) < 1:
54 continue
56 ok = session_state.action_set_create(context, am)
57 if not ok:
58 return
60 controller_grip_name = ""
61 controller_aim_name = ""
63 for ami in am.actionmap_items:
64 if len(ami.bindings) < 1:
65 continue
67 ok = session_state.action_create(context, am, ami)
68 if not ok:
69 return
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:
81 continue
82 elif amb.name == defaults.VRDefaultActionbindings.VIVE_COSMOS.value:
83 if not scene.vr_actions_enable_vive_cosmos:
84 continue
85 elif amb.name == defaults.VRDefaultActionbindings.VIVE_FOCUS.value:
86 if not scene.vr_actions_enable_vive_focus:
87 continue
88 elif amb.name == defaults.VRDefaultActionbindings.HUAWEI.value:
89 if not scene.vr_actions_enable_huawei:
90 continue
92 ok = session_state.action_binding_create(context, am, ami, amb)
93 if not ok:
94 return
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):
106 return False
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)
114 return True
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)
122 return True
125 def register():
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",
129 default=True,
131 bpy.types.Scene.vr_actions_use_gamepad = bpy.props.BoolProperty(
132 description="Use input from gamepad (Microsoft Xbox Controller) instead of motion controllers",
133 default=False,
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",
138 default=False,
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",
142 default=False,
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",
146 default=False,
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",
150 default=False,
153 bpy.app.handlers.xr_session_start_pre.append(vr_create_actions)
156 def unregister():
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)