[Bf-extensions-cvs] [78505ed4] xr-actions-D9124: VR Scene Inspection: Support operator properties

Peter Kim noreply at git.blender.org
Mon Oct 26 17:57:32 CET 2020


Commit: 78505ed4b358556d8b5123740fe15c4d636a7ba1
Author: Peter Kim
Date:   Tue Oct 27 01:56:45 2020 +0900
Branches: xr-actions-D9124
https://developer.blender.org/rBA78505ed4b358556d8b5123740fe15c4d636a7ba1

VR Scene Inspection: Support operator properties

===================================================================

M	viewport_vr_preview.py

===================================================================

diff --git a/viewport_vr_preview.py b/viewport_vr_preview.py
index 4a44a947..4f60248c 100644
--- a/viewport_vr_preview.py
+++ b/viewport_vr_preview.py
@@ -51,9 +51,122 @@ bl_info = {
     "category": "3D View",
 }
 
+# Key maps.
+ at persistent
+def vr_load_action_properties(context: bpy.context):
+    # Load operator properties for scene action sets.
+    wm = bpy.context.window_manager
+    kc = wm.keyconfigs.user
+    kc_addon = wm.keyconfigs.addon
+    if not (kc and kc_addon):
+        return
+
+    km_addon = kc_addon.keymaps.find("XR Session", space_type='EMPTY', region_type='XR')
+    if not km_addon:
+        return
+
+    km = kc.keymaps.find("XR", space_type='EMPTY', region_type='XR')
+    km_session = kc.keymaps.find("XR Session", space_type='EMPTY', region_type='XR')
+    if not (km or km_session):
+        return
+    
+    for action_set in bpy.context.scene.vr_action_sets:
+        for action in action_set.actions:
+            if (action.type != 'BUTTON') or not action.op:
+                continue
+
+            kmi_addon = km_addon.keymap_items.from_xr(action_set.name, action.name)
+            if kmi_addon:
+                continue
+
+            kmi = None
+            if km_session:
+                # 1. Check XR Session key map.
+                kmi = km_session.keymap_items.from_xr(action_set.name, action.name)
+
+            if not kmi and km:
+                # 2. Check XR key map.
+                kmi = km.keymap_items.from_xr(action_set.name, action.name)
+
+            if kmi:
+                kmi_addon = km_addon.keymap_items.new_from_item(kmi)
+                kmi_addon.active = True
+                kmi_addon.idname = action.op
+                kmi_addon.xr_action_set = action_set.name
+                kmi_addon.xr_action = action.name
+
+
+def vr_get_keymap(context, from_prefs):
+    if from_prefs:
+        kc = context.window_manager.keyconfigs.user
+        return (
+            None if (not kc)
+                else kc.keymaps.find("XR", space_type='EMPTY', region_type='XR')
+        )
+    else:
+        kc = context.window_manager.keyconfigs.addon
+        return (
+            None if (not kc)
+                else kc.keymaps.find("XR Session", space_type='EMPTY', region_type='XR')
+        )
+
+
+def vr_indented_layout(layout, level):
+    indentpx = 16
+    if level == 0:
+        level = 0.0001
+    indent = level * indentpx / bpy.context.region.width
+
+    split = layout.split(factor=indent)
+    col = split.column()
+    col = split.column()
+    return col
+
+
+# Similar to draw_kmi() from release/scripts/modules/rna_keymap_ui.py
+# but only displays operator properties.
+def vr_draw_kmi(display_keymaps, kc, km, kmi, layout, level):
+    map_type = kmi.map_type
+
+    col = vr_indented_layout(layout, level)
+
+    if kmi.show_expanded:
+        col = col.column(align=True)
+        box = col.box()
+    else:
+        box = col.column()
+
+    split = box.split()
+
+    # Header bar.
+    row = split.row(align=True)
+    row.prop(kmi, "show_expanded", text="", emboss=False)
+
+    row.label(text="Operator Properties")
+    if km.is_modal:
+        row.separator()
+        row.prop(kmi, "propvalue", text="")
+    else:
+        row.label(text=kmi.name)
+
+    # Expanded, additional event settings.
+    if kmi.show_expanded:
+        box = col.box()
+        
+        # Operator properties.
+        box.template_keymap_item_properties(kmi)
+
+       # # TODO_XR
+       # # Modal keymaps attached to this operator.
+       # if not km.is_modal:
+           # kmm = kc.keymaps.find_modal(kmi.idname)
+           # if kmm:
+               # draw_km(display_keymaps, kc, kmm, None, layout, level + 1)
+               # layout.context_pointer_set("keymap", km)
+
 
 @persistent
-def ensure_default_vr_landmark(context: bpy.context):
+def vr_ensure_default_landmark(context: bpy.context):
     # Ensure there's a default landmark (scene camera by default).
     landmarks = bpy.context.scene.vr_landmarks
     if not landmarks:
@@ -285,7 +398,7 @@ class VIEW3D_PT_vr_landmarks(Panel):
 
 
 @persistent
-def create_vr_actions(context: bpy.context):
+def vr_create_actions(context: bpy.context):
     # Create all vr action sets and actions.
     context = bpy.context
     wm = context.window_manager
@@ -318,35 +431,88 @@ def create_vr_actions(context: bpy.context):
             else:
                 continue
 
-            wm.xr_session_state.create_action(context, action_set.name, action.name, type, action.user_path0, action.user_path1, action.threshold, action.op, op_flag)         
+            wm.xr_session_state.create_action(context, action_set.name, action.name, type,
+                                              action.user_path0, action.user_path1, action.threshold, action.op, op_flag)         
 
             if action.type == 'POSE':
-                wm.xr_session_state.create_action_space(context, action_set.name, action.name, action.user_path0, action.user_path1, \
-                            action.pose_location, action.pose_rotation)
+                wm.xr_session_state.create_action_space(context, action_set.name, action.name,
+                                                        action.user_path0, action.user_path1, action.pose_location, action.pose_rotation)
                 if action.pose_is_controller:
                     wm.xr_session_state.set_controller_pose_action(context, action_set.name, action.name)
 
             interaction_path0 = action.user_path0 + action.component_path0
             interaction_path1 = action.user_path1 + action.component_path1
 
-            wm.xr_session_state.create_action_binding(context, action_set.name, action_set.profile, action.name, interaction_path0, interaction_path1)  
+            wm.xr_session_state.create_action_binding(context, action_set.name, action_set.profile, action.name,
+                                                      interaction_path0, interaction_path1)  
 
         wm.xr_session_state.set_active_action_set(context, action_set.name)
 
 
 class VRAction(PropertyGroup):
+    # Update key map item on operator / name change.
+    def update_kmi(self, context):
+        if self.ignore_update:
+            return
+
+        action_renamed = (self.name != self.name_prev)
+        action_name = ""
+        if action_renamed:
+            action_name = self.name_prev
+            self.name_prev = self.name
+        else:
+            action_name = self.name
+ 
+        action_set = VRActionSet.get_selected_action_set(context, self.from_prefs)
+        if action_set:
+            action_set_renamed = (action_set.name != action_set.name_prev)
+            action_set_name = ""
+            if action_set_renamed:
+                action_set_name = action_set.name_prev
+                # Don't update prev action set name (will be updated in VRActionSet.update_kmis())
+                #action_set.name_prev = action_set.name
+            else:
+                action_set_name = action_set.name
+            
+            km = vr_get_keymap(context, self.from_prefs)
+            if km:
+                kmi = km.keymap_items.from_xr(action_set_name, action_name)
+
+                if (self.type != 'BUTTON') or not self.op:
+                    if kmi:
+                        # Remove any existing key map item.
+                        km.keymap_items.remove(kmi)
+                    return
+
+                if not kmi:
+                    if action_set_renamed or action_renamed:
+                        return
+                    # Add key map item.
+                    kmi = km.keymap_items.new("", 'XR_ACTION', 'ANY')
+                    kmi.active = True
+
+                kmi.idname = self.op
+                kmi.xr_action_set = action_set.name
+                kmi.xr_action = self.name
+
     name: bpy.props.StringProperty(
-        name="VR action.\nMust not contain upper case letters or special characters other than '-', '_', or '.'",
+        name="VR action",
+        description= "Must not contain upper case letters or special characters other than '-', '_', or '.'",
+        default="action",
+        update=update_kmi,
+    )
+    name_prev: bpy.props.StringProperty(
         default="action",
     )
     type: bpy.props.EnumProperty(
-        name="VR action type",
+        name="Action Type",
         items=[
             ('BUTTON', "Button", "Button input"),
             ('POSE', "Pose", "Pose input"),
             ('HAPTIC', "Haptic", "Haptic output"),
         ],
         default='BUTTON',
+        update=update_kmi,
     )
     user_path0: bpy.props.StringProperty(
         name="OpenXR user path",
@@ -367,10 +533,11 @@ class VRAction(PropertyGroup):
         max=1.0,
     )
     op: bpy.props.StringProperty(
-        name="Python operator",
+        name="Operator",
+        update=update_kmi,
     )
     op_flag: bpy.props.EnumProperty(
-        name="Operator flag",
+        name="Operator Flag",
         items=[
             ('PRESS', "Press",
              "Execute operator on press "
@@ -404,9 +571,18 @@ class VRAction(PropertyGroup):
     haptic_amplitude: bpy.props.FloatProperty(
         name="Haptic amplitude",
     )
+    from_prefs: BoolProperty(
+        # Whether this action is a prefs (or scene) action. 		
+    )                   
+    ignore_update: BoolProperty(		
+        default=False
+    )        
+
+    def copy_from(self, o, from_prefs):
+        self.ignore_update = True # Prevent update on prop assignment.
 
-    def copy_from(self, o):
         self.name = o.name
+        self.name_prev = o.name_prev
         self.type = o.type
         self.user_path0 = o.user_path0
         self.component_path0 = o.component_path0
@@ -421,11 +597,29 @@ class VRAction(PropertyGroup):
         self.haptic_duration = o.haptic_duration
         self.haptic_frequency = o.haptic_frequency
         self.haptic_amplitude = o.haptic_amplitude
+        self.from_prefs = from_prefs
+
+        self.ignore_update = False
 
 
 class VRActionSet(PropertyGroup):
+    # Update key map items on name change.
+    def update_kmis(self, context):
+        action_set_renamed = (self.name != self.name_prev)
+
+        for action in self.actions:
+  

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list