[Bf-blender-cvs] [9f5089b67a6] xr-controller-support: XR: Simplify action creation API

Peter Kim noreply at git.blender.org
Mon Aug 2 09:57:59 CEST 2021


Commit: 9f5089b67a6dfb2dc625615585ee9026752cbcc6
Author: Peter Kim
Date:   Mon Aug 2 16:53:04 2021 +0900
Branches: xr-controller-support
https://developer.blender.org/rB9f5089b67a6dfb2dc625615585ee9026752cbcc6

XR: Simplify action creation API

By passing an XrActionMap struct instead of individual action
parameters, action creation from Python can be reduced to a simple
call to XrSessionState.create_action_set().

This will also set any controller pose actions and optionally the
active action set for the session.

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

M	source/blender/makesrna/intern/rna_xr.c

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

diff --git a/source/blender/makesrna/intern/rna_xr.c b/source/blender/makesrna/intern/rna_xr.c
index 57406a6c250..b854e9e6106 100644
--- a/source/blender/makesrna/intern/rna_xr.c
+++ b/source/blender/makesrna/intern/rna_xr.c
@@ -35,6 +35,8 @@
 
 #ifdef RNA_RUNTIME
 
+#  include "BLI_listbase.h"
+
 #  include "WM_api.h"
 
 /* -------------------------------------------------------------------- */
@@ -918,214 +920,181 @@ static void rna_XrSessionState_reset_navigation(bContext *C)
 #  endif
 }
 
-static bool rna_XrSessionState_action_set_create(bContext *C, const char *action_set_name)
+static bool rna_XrSessionState_action_set_create(bContext *C,
+                                                 XrActionMap *actionmap,
+                                                 bool set_active)
 {
 #  ifdef WITH_XR_OPENXR
-  wmWindowManager *wm = CTX_wm_manager(C);
-  return WM_xr_action_set_create(&wm->xr, action_set_name);
-#  else
-  UNUSED_VARS(C, action_set_name);
-  return false;
-#  endif
-}
+  if (BLI_listbase_count(&actionmap->items) < 1) {
+    return false;
+  }
 
-static bool rna_XrSessionState_action_create(bContext *C,
-                                             const char *action_set_name,
-                                             const char *action_name,
-                                             int type,
-                                             const char *user_path0,
-                                             const char *user_path1,
-                                             const char *op,
-                                             int op_flag,
-                                             bool bimanual,
-                                             const char *haptic_name,
-                                             bool haptic_match_user_paths,
-                                             float haptic_duration,
-                                             float haptic_frequency,
-                                             float haptic_amplitude,
-                                             int haptic_flag)
-{
-#  ifdef WITH_XR_OPENXR
+  /* Create action set. */
   wmWindowManager *wm = CTX_wm_manager(C);
-  unsigned int count_subaction_paths = 0;
-  const char *subaction_paths[2];
+  wmXrData *xr = &wm->xr;
+  const char *action_set_name = actionmap->idname;
 
-  if (user_path0[0]) {
-    subaction_paths[0] = user_path0;
-    ++count_subaction_paths;
+  if (!WM_xr_action_set_create(xr, action_set_name)) {
+    return false;
+  }
 
-    if (user_path1[0]) {
-      subaction_paths[1] = user_path1;
-      ++count_subaction_paths;
+  /* Create actions. */
+  const char *controller_grip_name = NULL;
+  const char *controller_aim_name = NULL;
+
+  LISTBASE_FOREACH (XrActionMapItem *, ami, &actionmap->items) {
+    if (BLI_listbase_count(&ami->bindings) < 1) {
+      continue;
     }
-  }
-  else {
-    if (user_path1[0]) {
-      subaction_paths[0] = user_path1;
+
+    unsigned int count_subaction_paths = 0;
+    const char *subaction_paths[2];
+
+    if (ami->user_path0[0]) {
+      subaction_paths[0] = ami->user_path0;
       ++count_subaction_paths;
+
+      if (ami->user_path1[0]) {
+        subaction_paths[1] = ami->user_path1;
+        ++count_subaction_paths;
+      }
     }
     else {
-      return false;
+      if (ami->user_path1[0]) {
+        subaction_paths[0] = ami->user_path1;
+        ++count_subaction_paths;
+      }
+      else {
+        return false;
+      }
     }
-  }
 
-  const bool is_button_action = (type == XR_BOOLEAN_INPUT || type == XR_FLOAT_INPUT ||
-                                 type == XR_VECTOR2F_INPUT);
-  wmOperatorType *ot = NULL;
-  IDProperty *op_properties = NULL;
-  int64_t haptic_duration_msec;
-  eXrActionFlag flag = 0;
-
-  if (is_button_action) {
-    if (op[0]) {
-      char idname[OP_MAX_TYPENAME];
-      WM_operator_bl_idname(idname, op);
-      ot = WM_operatortype_find(idname, true);
-      if (ot) {
-        /* Get properties from active XR actionmap. */
-        XrActionConfig *ac = WM_xr_actionconfig_active_get(&wm->xr.session_settings);
-        if (ac) {
-          XrActionMap *am = WM_xr_actionmap_list_find(&ac->actionmaps, action_set_name);
-          if (am) {
-            XrActionMapItem *ami = WM_xr_actionmap_item_list_find(&am->items, action_name);
-            if (ami && STREQ(ami->op, op)) {
-              op_properties = ami->op_properties;
-            }
-          }
+    const char *action_name = ami->idname;
+    const bool is_float_action = (ami->type == XR_FLOAT_INPUT || ami->type == XR_VECTOR2F_INPUT);
+    const bool is_button_action = (is_float_action || ami->type == XR_BOOLEAN_INPUT);
+    const bool is_pose_action = (ami->type == XR_POSE_INPUT);
+    wmOperatorType *ot = NULL;
+    IDProperty *op_properties = NULL;
+    const char *haptic_name = NULL;
+    int64_t haptic_duration_msec;
+
+    if (is_button_action) {
+      if (ami->op[0]) {
+        char idname[OP_MAX_TYPENAME];
+        WM_operator_bl_idname(idname, ami->op);
+        ot = WM_operatortype_find(idname, true);
+        if (ot) {
+          op_properties = ami->op_properties;
         }
       }
-    }
-
-    haptic_duration_msec = (int64_t)(haptic_duration * 1000.0f);
 
-    flag |= (op_flag | haptic_flag);
-    if (bimanual) {
-      flag |= XR_ACTION_BIMANUAL;
+      haptic_name = &ami->haptic_idname[0];
+      haptic_duration_msec = (int64_t)(ami->haptic_duration * 1000.0f);
     }
-    if (haptic_match_user_paths) {
-      flag |= XR_ACTION_HAPTIC_MATCHUSERPATHS;
-    }
-  }
 
-  return WM_xr_action_create(&wm->xr,
+    if (!WM_xr_action_create(xr,
                              action_set_name,
                              action_name,
-                             type,
+                             ami->type,
                              count_subaction_paths,
                              subaction_paths,
                              ot,
                              op_properties,
                              is_button_action ? &haptic_name : NULL,
                              is_button_action ? &haptic_duration_msec : NULL,
-                             is_button_action ? &haptic_frequency : NULL,
-                             is_button_action ? &haptic_amplitude : NULL,
-                             flag);
-#  else
-  UNUSED_VARS(C,
-              action_set_name,
-              action_name,
-              type,
-              user_path0,
-              user_path1,
-              op,
-              op_flag,
-              bimanual,
-              haptic_name,
-              haptic_match_user_paths,
-              haptic_duration,
-              haptic_frequency,
-              haptic_amplitude);
-  return false;
-#  endif
-}
-
-bool rna_XrSessionState_action_binding_create(bContext *C,
-                                              const char *action_set_name,
-                                              const char *action_name,
-                                              int type,
-                                              const char *profile,
-                                              const char *user_path0,
-                                              const char *user_path1,
-                                              const char *component_path0,
-                                              const char *component_path1,
-                                              float threshold,
-                                              int axis0_flag,
-                                              int axis1_flag,
-                                              float location[3],
-                                              float rotation[3])
-{
-#  ifdef WITH_XR_OPENXR
-  wmWindowManager *wm = CTX_wm_manager(C);
-  unsigned int count_subaction_paths = 0;
-  const char *subaction_paths[2];
-  const char *component_paths[2];
-
-  if (user_path0[0]) {
-    subaction_paths[0] = user_path0;
-    component_paths[0] = component_path0;
-    ++count_subaction_paths;
-
-    if (user_path1[0]) {
-      subaction_paths[1] = user_path1;
-      component_paths[1] = component_path1;
-      ++count_subaction_paths;
+                             is_button_action ? &ami->haptic_frequency : NULL,
+                             is_button_action ? &ami->haptic_amplitude : NULL,
+                             ami->action_flag)) {
+      return false;
     }
-  }
-  else {
-    if (user_path1[0]) {
-      subaction_paths[0] = user_path1;
-      component_paths[0] = component_path1;
-      ++count_subaction_paths;
+
+    if ((ami->pose_flag & XR_POSE_GRIP) != 0) {
+      BLI_assert(is_pose_action);
+      controller_grip_name = action_name;
     }
-    else {
-      return false;
+    if ((ami->pose_flag & XR_POSE_AIM) != 0) {
+      BLI_assert(is_pose_action);
+      controller_aim_name = action_name;
     }
-  }
 
-  const bool is_float_action = (type == XR_FLOAT_INPUT || type == XR_VECTOR2F_INPUT);
-  const bool is_button_action = (is_float_action || type == XR_BOOLEAN_INPUT);
-  const bool is_pose_action = (type == XR_POSE_INPUT);
-  float float_thresholds[2];
-  eXrAxisFlag axis_flags[2];
-  wmXrPose poses[2];
+    /* Create action bindings. */
+    LISTBASE_FOREACH (XrActionMapBinding *, amb, &ami->bindings) {
+      unsigned int count_component_paths = 0;
+      const char *component_paths[2];
+
+      if (amb->component_path0[0]) {
+        component_paths[0] = amb->component_path0;
+        ++count_component_paths;
+
+        if (amb->component_path1[0]) {
+          component_paths[1] = amb->component_path1;
+          ++count_component_paths;
+        }
+      }
+      else {
+        if (amb->component_path1[0]) {
+          component_paths[0] = amb->component_path1;
+          ++count_component_paths;
+        }
+        else {
+          return false;
+        }
+      }
+
+      if (count_component_paths != count_subaction_paths) {
+        return false;
+      }
+
+      float float_thresholds[2];
+      eXrAxisFlag axis_flags[2];
+      wmXrPose poses[2];
 
-  if (is_float_action) {
-    float_thresholds[0] = float_thresholds[1] = threshold;
+      if (is_float_action) {
+        float_thresholds[0] = float_thresholds[1] = amb->float_threshold;
+ 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list