[Bf-blender-cvs] [fcc42b3d46b] asset-browser-snap-dragging: Basic snapping support for dropping objects

Julian Eisel noreply at git.blender.org
Thu Apr 22 12:06:18 CEST 2021


Commit: fcc42b3d46b9954b464b95e84e387fb882029fe2
Author: Julian Eisel
Date:   Thu Apr 22 12:02:02 2021 +0200
Branches: asset-browser-snap-dragging
https://developer.blender.org/rBfcc42b3d46b9954b464b95e84e387fb882029fe2

Basic snapping support for dropping objects

Uses the same snapping logic as the placement plane from the Add Object
tool, which means it matches the plane that we already draw as a
preview.

This is quite primitive at this point - you can't change options for the
snapping like the up-axis or the snapping target - but it already feels
quite great.
Another limitation is that it uses the origin of the dropped object to
place it, the bounding box should give better results in practice. This
is something we can add.

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

M	source/blender/editors/include/ED_view3d.h
M	source/blender/editors/object/object_add.c
M	source/blender/editors/space_view3d/view3d_placement.c

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

diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h
index 499f28beb60..80b717ad21c 100644
--- a/source/blender/editors/include/ED_view3d.h
+++ b/source/blender/editors/include/ED_view3d.h
@@ -734,6 +734,13 @@ void ED_view3d_buttons_region_layout_ex(const struct bContext *C,
 bool ED_view3d_local_collections_set(struct Main *bmain, struct View3D *v3d);
 void ED_view3d_local_collections_reset(struct bContext *C, const bool reset_all);
 
+/* view3d_placement.c */
+
+void ED_view3d_placement_plane_calc(struct bContext *C,
+                                    const int mval[2],
+                                    float r_co_src[3],
+                                    float r_mat_orient[3][3]);
+
 #ifdef WITH_XR_OPENXR
 void ED_view3d_xr_mirror_update(const struct ScrArea *area,
                                 const struct View3D *v3d,
diff --git a/source/blender/editors/object/object_add.c b/source/blender/editors/object/object_add.c
index a0de1e0fa84..d23bdd35646 100644
--- a/source/blender/editors/object/object_add.c
+++ b/source/blender/editors/object/object_add.c
@@ -3398,8 +3398,10 @@ static int object_add_named_exec(bContext *C, wmOperator *op)
 
   int mval[2];
   if (object_add_drop_xy_get(C, op, &mval)) {
-    ED_object_location_from_view(C, basen->object->loc);
-    ED_view3d_cursor3d_position(C, mval, false, basen->object->loc);
+    float rotmat[3][3];
+    ED_view3d_placement_plane_calc(C, mval, basen->object->loc, rotmat);
+    BLI_assert(basen->object->rotmode == ROT_MODE_XYZ);
+    mat3_to_eul(basen->object->rot, rotmat);
   }
 
   /* object_add_duplicate_internal() doesn't deselect other objects, unlike object_add_common() or
diff --git a/source/blender/editors/space_view3d/view3d_placement.c b/source/blender/editors/space_view3d/view3d_placement.c
index 816755071ec..d439121f0e3 100644
--- a/source/blender/editors/space_view3d/view3d_placement.c
+++ b/source/blender/editors/space_view3d/view3d_placement.c
@@ -377,6 +377,25 @@ static wmGizmoGroup *idp_gizmogroup_from_region(ARegion *region)
   return gzmap ? WM_gizmomap_group_find(gzmap, view3d_gzgt_placement_id) : NULL;
 }
 
+static wmGizmo *idp_snap_gizmo_from_region(ARegion *region)
+{
+  /* Assign snap gizmo which is may be used as part of the tool. */
+  wmGizmoGroup *gzgroup = idp_gizmogroup_from_region(region);
+  if ((gzgroup == NULL) || (gzgroup->gizmos.first == NULL)) {
+    return NULL;
+  }
+
+  return gzgroup->gizmos.first;
+}
+
+static void idp_paintcursor_from_gizmo_visible_set(wmGizmo *gz, bool visible)
+{
+  /* Can be NULL when gizmos are disabled. */
+  if (gz->parent_gzgroup->customdata != NULL) {
+    preview_plane_cursor_visible_set(gz->parent_gzgroup, visible);
+  }
+}
+
 /**
  * Calculate 3D view incremental (grid) snapping.
  *
@@ -1006,6 +1025,39 @@ static void view3d_interactive_add_calc_plane(bContext *C,
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name Public Placement Plane Functionality
+ * \{ */
+
+void ED_view3d_placement_plane_calc(bContext *C,
+                                    const int mval[2],
+                                    float r_co_src[3],
+                                    float r_mat_orient[3][3])
+{
+  Scene *scene = CTX_data_scene(C);
+  ARegion *region = CTX_wm_region(C);
+  View3D *v3d = CTX_wm_view3d(C);
+
+  wmGizmo *snap_gizmo = idp_snap_gizmo_from_region(region);
+  const float mval_fl[] = {mval[0], mval[1]};
+
+  view3d_interactive_add_calc_plane(C,
+                                    scene,
+                                    v3d,
+                                    region,
+                                    mval_fl,
+                                    snap_gizmo,
+                                    PLACE_SNAP_TO_GEOMETRY,
+                                    PLACE_DEPTH_SURFACE,
+                                    PLACE_ORIENT_SURFACE,
+                                    2,
+                                    false,
+                                    r_co_src,
+                                    r_mat_orient);
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Add Object Modal Operator
  * \{ */
@@ -1032,19 +1084,8 @@ static void view3d_interactive_add_begin(bContext *C, wmOperator *op, const wmEv
   struct InteractivePlaceData *ipd = op->customdata;
 
   /* Assign snap gizmo which is may be used as part of the tool. */
-  {
-    wmGizmoGroup *gzgroup = idp_gizmogroup_from_region(ipd->region);
-    if (gzgroup != NULL) {
-      if (gzgroup->gizmos.first) {
-        ipd->snap_gizmo = gzgroup->gizmos.first;
-      }
-
-      /* Can be NULL when gizmos are disabled. */
-      if (gzgroup->customdata != NULL) {
-        preview_plane_cursor_visible_set(gzgroup, false);
-      }
-    }
-  }
+  ipd->snap_gizmo = idp_snap_gizmo_from_region(ipd->region);
+  idp_paintcursor_from_gizmo_visible_set(ipd->snap_gizmo, false);
 
   /* For tweak events the snap target may have changed since dragging,
    * update the snap target at the cursor location where tweak began.
@@ -1243,12 +1284,8 @@ static void view3d_interactive_add_exit(bContext *C, wmOperator *op)
   ED_region_tag_redraw(ipd->region);
 
   {
-    wmGizmoGroup *gzgroup = idp_gizmogroup_from_region(ipd->region);
-    if (gzgroup != NULL) {
-      if (gzgroup->customdata != NULL) {
-        preview_plane_cursor_visible_set(gzgroup, true);
-      }
-    }
+    wmGizmo *snap_gz = idp_snap_gizmo_from_region(ipd->region);
+    idp_paintcursor_from_gizmo_visible_set(snap_gz, true);
   }
 
   MEM_freeN(ipd);
@@ -1832,13 +1869,7 @@ static void gizmo_plane_update_cursor(const bContext *C,
   View3D *v3d = CTX_wm_view3d(C);
 
   /* Assign snap gizmo which is may be used as part of the tool. */
-  wmGizmo *snap_gizmo = NULL;
-  {
-    wmGizmoGroup *gzgroup = idp_gizmogroup_from_region(region);
-    if ((gzgroup != NULL) && gzgroup->gizmos.first) {
-      snap_gizmo = gzgroup->gizmos.first;
-    }
-  }
+  wmGizmo *snap_gizmo = idp_snap_gizmo_from_region(region);
 
   /* This ensures the snap gizmo has settings from this tool.
    * This function call could be moved a more appropriate place,



More information about the Bf-blender-cvs mailing list