[Bf-blender-cvs] [d330d0762b2] vr_scene_inspection: Add Py API for querying VR session state

Julian Eisel noreply at git.blender.org
Thu Dec 12 18:37:12 CET 2019


Commit: d330d0762b2a5a4595c18cedce3ba9206a9bb934
Author: Julian Eisel
Date:   Thu Dec 12 18:27:02 2019 +0100
Branches: vr_scene_inspection
https://developer.blender.org/rBd330d0762b2a5a4595c18cedce3ba9206a9bb934

Add Py API for querying VR session state

This should be a rather important step to get the Python defined VR UIs
to work.

Also:
* Adds a bpy.types.XrSessionState.is_running() class method for
  the UI to use.
* Adds a XR-data-changed notifier type for the window manager category
  to ensure proper redraws on state changes.

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

M	source/blender/editors/space_view3d/space_view3d.c
M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/rna_wm.c
M	source/blender/makesrna/intern/rna_xr.c
M	source/blender/windowmanager/WM_api.h
M	source/blender/windowmanager/WM_types.h
M	source/blender/windowmanager/intern/wm_operators.c
M	source/blender/windowmanager/intern/wm_xr.c

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

diff --git a/source/blender/editors/space_view3d/space_view3d.c b/source/blender/editors/space_view3d/space_view3d.c
index 5c7263d458d..fd4cec61515 100644
--- a/source/blender/editors/space_view3d/space_view3d.c
+++ b/source/blender/editors/space_view3d/space_view3d.c
@@ -1365,6 +1365,11 @@ static void view3d_buttons_region_listener(wmWindow *UNUSED(win),
         ED_region_tag_redraw(ar);
       }
       break;
+    case NC_WM:
+      if (wmn->data == ND_XR_DATA_CHANGED) {
+        ED_region_tag_redraw(ar);
+      }
+      break;
   }
 }
 
diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h
index 182f41873d1..806faa092e0 100644
--- a/source/blender/makesrna/RNA_access.h
+++ b/source/blender/makesrna/RNA_access.h
@@ -749,6 +749,7 @@ extern StructRNA RNA_WorldTextureSlot;
 extern StructRNA RNA_XnorController;
 extern StructRNA RNA_XorController;
 extern StructRNA RNA_XrSessionSettings;
+extern StructRNA RNA_XrSessionState;
 extern StructRNA RNA_uiPopover;
 extern StructRNA RNA_wmOwnerIDs;
 
diff --git a/source/blender/makesrna/intern/rna_wm.c b/source/blender/makesrna/intern/rna_wm.c
index 22129d7e481..442b138ccdb 100644
--- a/source/blender/makesrna/intern/rna_wm.c
+++ b/source/blender/makesrna/intern/rna_wm.c
@@ -2469,6 +2469,13 @@ static void rna_def_windowmanager(BlenderRNA *brna)
   RNA_def_property_pointer_sdna(prop, NULL, "xr.session_settings");
   RNA_def_property_flag(prop, PROP_NEVER_NULL);
   RNA_def_property_ui_text(prop, "XR Session Settings", "");
+
+  prop = RNA_def_property(srna, "xr_session_state", PROP_POINTER, PROP_NONE);
+  RNA_def_property_struct_type(prop, "XrSessionState");
+  RNA_def_property_pointer_sdna(prop, NULL, "xr.session_state");
+  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+  RNA_def_property_ui_text(
+      prop, "XR Session State", "Runtime state information about the VR session");
 #  endif
 
   RNA_api_wm(srna);
diff --git a/source/blender/makesrna/intern/rna_xr.c b/source/blender/makesrna/intern/rna_xr.c
index 0c5280b3a19..c57f8b92f52 100644
--- a/source/blender/makesrna/intern/rna_xr.c
+++ b/source/blender/makesrna/intern/rna_xr.c
@@ -24,6 +24,8 @@
 #include "RNA_define.h"
 #include "RNA_enum_types.h"
 
+#include "WM_types.h"
+
 #include "rna_internal.h"
 
 #ifndef WITH_OPENXR
@@ -32,6 +34,14 @@ BLI_STATIC_ASSERT(false, "Tried to compile rna_xr.c even though WITH_OPENXR is n
 
 #ifdef RNA_RUNTIME
 
+#  include "WM_api.h"
+
+static bool rna_XrSessionState_is_running(bContext *C)
+{
+  const wmWindowManager *wm = CTX_wm_manager(C);
+  return WM_xr_is_session_running(&wm->xr);
+}
+
 #else /* RNA_RUNTIME */
 
 static void rna_def_xr_session_settings(BlenderRNA *brna)
@@ -46,30 +56,55 @@ static void rna_def_xr_session_settings(BlenderRNA *brna)
   prop = RNA_def_property(srna, "shading_type", PROP_ENUM, PROP_NONE);
   RNA_def_property_enum_items(prop, rna_enum_shading_type_items);
   RNA_def_property_ui_text(prop, "Shading Type", "Method to display/shade objects in the VR View");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
 
   prop = RNA_def_property(srna, "show_floor", PROP_BOOLEAN, PROP_NONE);
   RNA_def_property_boolean_sdna(prop, NULL, "draw_flags", V3D_OFSDRAW_SHOW_GRIDFLOOR);
   RNA_def_property_ui_text(prop, "Display Grid Floor", "Show the ground plane grid");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
 
   prop = RNA_def_property(srna, "show_annotation", PROP_BOOLEAN, PROP_NONE);
   RNA_def_property_boolean_sdna(prop, NULL, "draw_flags", V3D_OFSDRAW_SHOW_ANNOTATION);
   RNA_def_property_ui_text(prop, "Show Annotation", "Show annotations for this view");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
 
   prop = RNA_def_property(srna, "clip_start", PROP_FLOAT, PROP_DISTANCE);
   RNA_def_property_range(prop, 1e-6f, FLT_MAX);
   RNA_def_property_ui_range(prop, 0.001f, FLT_MAX, 10, 3);
   RNA_def_property_ui_text(prop, "Clip Start", "VR View near clipping distance");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
 
   prop = RNA_def_property(srna, "clip_end", PROP_FLOAT, PROP_DISTANCE);
   RNA_def_property_range(prop, 1e-6f, FLT_MAX);
   RNA_def_property_ui_range(prop, 0.001f, FLT_MAX, 10, 3);
   RNA_def_property_ui_text(prop, "Clip End", "VR View far clipping distance");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
 
   prop = RNA_def_property(srna, "use_positional_tracking", PROP_BOOLEAN, PROP_NONE);
   RNA_def_property_boolean_sdna(prop, NULL, "flag", XR_SESSION_USE_POSITION_TRACKING);
   RNA_def_property_ui_text(prop,
                            "Positional Tracking",
                            "Limit view movements to rotation only (three degrees of freedom)");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+}
+
+static void rna_def_xr_session_state(BlenderRNA *brna)
+{
+  StructRNA *srna;
+  FunctionRNA *func;
+  PropertyRNA *parm;
+
+  srna = RNA_def_struct(brna, "XrSessionState", NULL);
+  RNA_def_struct_clear_flag(srna, STRUCT_UNDO);
+  RNA_def_struct_ui_text(srna, "Session State", "Runtime state information about the VR session");
+
+  func = RNA_def_function(srna, "is_running", "rna_XrSessionState_is_running");
+  RNA_def_function_ui_description(func, "Query if the VR session is currently running");
+  RNA_def_function_flag(func, FUNC_NO_SELF);
+  parm = RNA_def_pointer(func, "context", "Context", "", "");
+  RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
+  parm = RNA_def_boolean(func, "result", 0, "Result", "");
+  RNA_def_function_return(func, parm);
 }
 
 void RNA_def_xr(BlenderRNA *brna)
@@ -77,6 +112,7 @@ void RNA_def_xr(BlenderRNA *brna)
   RNA_define_animate_sdna(false);
 
   rna_def_xr_session_settings(brna);
+  rna_def_xr_session_state(brna);
 
   RNA_define_animate_sdna(true);
 }
diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h
index cb95353554f..16e70dcbad5 100644
--- a/source/blender/windowmanager/WM_api.h
+++ b/source/blender/windowmanager/WM_api.h
@@ -848,6 +848,11 @@ void WM_generic_callback_free(struct wmGenericCallback *callback);
 
 void WM_generic_user_data_free(struct wmGenericUserData *user_data);
 
+#ifdef WITH_OPENXR
+/* wm_xr.c */
+bool WM_xr_is_session_running(const struct wmXrData *xr);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/windowmanager/WM_types.h b/source/blender/windowmanager/WM_types.h
index 15ad8cbedc4..50be095bd1f 100644
--- a/source/blender/windowmanager/WM_types.h
+++ b/source/blender/windowmanager/WM_types.h
@@ -311,6 +311,7 @@ typedef struct wmNotifier {
 #define ND_HISTORY (4 << 16)
 #define ND_JOB (5 << 16)
 #define ND_UNDO (6 << 16)
+#define ND_XR_DATA_CHANGED (7 << 17)
 
 /* NC_SCREEN */
 #define ND_LAYOUTBROWSE (1 << 16)
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index c862009161d..29324c6a557 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -3509,6 +3509,8 @@ static int wm_xr_session_toggle_exec(bContext *C, wmOperator *UNUSED(op))
 
   wm_xr_session_toggle(C, wm->xr.context);
 
+  WM_event_add_notifier(C, NC_WM | ND_XR_DATA_CHANGED, NULL);
+
   return OPERATOR_FINISHED;
 }
 
diff --git a/source/blender/windowmanager/intern/wm_xr.c b/source/blender/windowmanager/intern/wm_xr.c
index c214cee7312..831403b6731 100644
--- a/source/blender/windowmanager/intern/wm_xr.c
+++ b/source/blender/windowmanager/intern/wm_xr.c
@@ -248,6 +248,10 @@ void *wm_xr_session_gpu_binding_context_create(GHOST_TXrGraphicsBinding graphics
 
   wm_surface_add(surface);
 
+  /* Some regions may need to redraw with updated session state after the session is entirely up
+   * and running. */
+  WM_main_add_notifier(NC_WM | ND_XR_DATA_CHANGED, NULL);
+
   return data->secondary_ghost_ctx ? data->secondary_ghost_ctx : surface->ghost_ctx;
 }
 
@@ -259,6 +263,10 @@ void wm_xr_session_gpu_binding_context_destroy(GHOST_TXrGraphicsBinding UNUSED(g
   }
 
   wm_window_reset_drawable();
+
+  /* Some regions may need to redraw with updated session state after the session is entirely
+   * stopped. */
+  WM_main_add_notifier(NC_WM | ND_XR_DATA_CHANGED, NULL);
 }
 
 static void wm_xr_session_begin_info_create(const bXrRuntimeSessionState *state,
@@ -273,7 +281,7 @@ void wm_xr_session_toggle(bContext *C, void *xr_context_ptr)
   GHOST_XrContextHandle xr_context = xr_context_ptr;
   wmWindowManager *wm = CTX_wm_manager(C);
 
-  if (xr_context && GHOST_XrSessionIsRunning(xr_context)) {
+  if (WM_xr_is_session_running(&wm->xr)) {
     GHOST_XrSessionEnd(xr_context);
     wm_xr_runtime_session_state_free(&wm->xr.session_state);
   }
@@ -287,6 +295,13 @@ void wm_xr_session_toggle(bContext *C, void *xr_context_ptr)
   }
 }
 
+bool WM_xr_is_session_running(const wmXrData *xr)
+{
+  /* wmXrData.session_state will be NULL if session end was requested. In that case, pretend like
+   * it's already  */
+  return xr->context && GHOST_XrSessionIsRunning(xr->context);
+}
+
 /** \} */ /* XR-Session */
 
 /* -------------------------------------------------------------------- */



More information about the Bf-blender-cvs mailing list