[Bf-blender-cvs] [3a6dbf907d2] temp-T90576-asset-browser-drop-on-material-slot: Initial changes to support material slot under cursor.

Jeroen Bakker noreply at git.blender.org
Tue Aug 10 17:08:14 CEST 2021


Commit: 3a6dbf907d23ba7b7bea8be14245ad897652820f
Author: Jeroen Bakker
Date:   Tue Aug 10 17:08:00 2021 +0200
Branches: temp-T90576-asset-browser-drop-on-material-slot
https://developer.blender.org/rB3a6dbf907d23ba7b7bea8be14245ad897652820f

Initial changes to support material slot under cursor.

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

M	source/blender/editors/include/ED_view3d.h
M	source/blender/editors/object/object_relations.c
M	source/blender/editors/space_view3d/view3d_select.c

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

diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h
index 2c958d282f9..31aae02fe22 100644
--- a/source/blender/editors/include/ED_view3d.h
+++ b/source/blender/editors/include/ED_view3d.h
@@ -638,6 +638,9 @@ void ED_view3d_draw_setup_view(const struct wmWindowManager *wm,
 
 struct Base *ED_view3d_give_base_under_cursor(struct bContext *C, const int mval[2]);
 struct Object *ED_view3d_give_object_under_cursor(struct bContext *C, const int mval[2]);
+struct Object *ED_view3d_give_material_slot_under_cursor(struct bContext *C,
+                                                         const int mval[2],
+                                                         int *r_material_slot);
 bool ED_view3d_is_object_under_cursor(struct bContext *C, const int mval[2]);
 void ED_view3d_quadview_update(struct ScrArea *area, struct ARegion *region, bool do_clip);
 void ED_view3d_update_viewmat(struct Depsgraph *depsgraph,
diff --git a/source/blender/editors/object/object_relations.c b/source/blender/editors/object/object_relations.c
index c0b954f3cff..1e71b71f390 100644
--- a/source/blender/editors/object/object_relations.c
+++ b/source/blender/editors/object/object_relations.c
@@ -2727,25 +2727,26 @@ char *ED_object_ot_drop_named_material_tooltip(bContext *C,
                                                PointerRNA *properties,
                                                const wmEvent *event)
 {
-  Object *ob = ED_view3d_give_object_under_cursor(C, event->mval);
+  int mat_slot = 0;
+  Object *ob = ED_view3d_give_material_slot_under_cursor(C, event->mval, &mat_slot);
   if (ob == NULL) {
     return BLI_strdup("");
   }
+  mat_slot = max_ii(mat_slot, 1);
 
   char name[MAX_ID_NAME - 2];
   RNA_string_get(properties, "name", name);
 
-  int active_mat_slot = max_ii(ob->actcol, 1);
-  Material *prev_mat = BKE_object_material_get(ob, active_mat_slot);
+  Material *prev_mat = BKE_object_material_get(ob, mat_slot);
 
   char *result;
   if (prev_mat) {
     const char *tooltip = TIP_("Drop %s on %s (slot %d, replacing %s)");
-    result = BLI_sprintfN(tooltip, name, ob->id.name + 2, active_mat_slot, prev_mat->id.name + 2);
+    result = BLI_sprintfN(tooltip, name, ob->id.name + 2, mat_slot, prev_mat->id.name + 2);
   }
   else {
     const char *tooltip = TIP_("Drop %s on %s (slot %d)");
-    result = BLI_sprintfN(tooltip, name, ob->id.name + 2, active_mat_slot);
+    result = BLI_sprintfN(tooltip, name, ob->id.name + 2, mat_slot);
   }
   return result;
 }
@@ -2753,7 +2754,10 @@ char *ED_object_ot_drop_named_material_tooltip(bContext *C,
 static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event)
 {
   Main *bmain = CTX_data_main(C);
-  Object *ob = ED_view3d_give_object_under_cursor(C, event->mval);
+  int mat_slot = 0;
+  Object *ob = ED_view3d_give_material_slot_under_cursor(C, event->mval, &mat_slot);
+  mat_slot = max_ii(mat_slot, 1);
+
   Material *ma;
   char name[MAX_ID_NAME - 2];
 
@@ -2763,9 +2767,7 @@ static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent
     return OPERATOR_CANCELLED;
   }
 
-  const short active_mat_slot = ob->actcol;
-
-  BKE_object_material_assign(CTX_data_main(C), ob, ma, active_mat_slot, BKE_MAT_ASSIGN_USERPREF);
+  BKE_object_material_assign(CTX_data_main(C), ob, ma, mat_slot, BKE_MAT_ASSIGN_USERPREF);
 
   DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
 
diff --git a/source/blender/editors/space_view3d/view3d_select.c b/source/blender/editors/space_view3d/view3d_select.c
index 2ce5684e874..56d70702570 100644
--- a/source/blender/editors/space_view3d/view3d_select.c
+++ b/source/blender/editors/space_view3d/view3d_select.c
@@ -2088,12 +2088,14 @@ static Base *mouse_select_eval_buffer(ViewContext *vc,
                                       int hits,
                                       Base *startbase,
                                       bool has_bones,
-                                      bool do_nearest)
+                                      bool do_nearest,
+                                      int *r_sub_selection)
 {
   ViewLayer *view_layer = vc->view_layer;
   View3D *v3d = vc->v3d;
   Base *base, *basact = NULL;
   int a;
+  int sub_selection_id = 0;
 
   if (do_nearest) {
     uint min = 0xFFFFFFFF;
@@ -2105,6 +2107,7 @@ static Base *mouse_select_eval_buffer(ViewContext *vc,
         if (min > buffer[4 * a + 1] && (buffer[4 * a + 3] & 0xFFFF0000)) {
           min = buffer[4 * a + 1];
           selcol = buffer[4 * a + 3] & 0xFFFF;
+          sub_selection_id = (buffer[4 * a + 3] & 0xFFFF0000) >> 16;
         }
       }
     }
@@ -2118,6 +2121,7 @@ static Base *mouse_select_eval_buffer(ViewContext *vc,
         if (min > buffer[4 * a + 1] && notcol != (buffer[4 * a + 3] & 0xFFFF)) {
           min = buffer[4 * a + 1];
           selcol = buffer[4 * a + 3] & 0xFFFF;
+          sub_selection_id = (buffer[4 * a + 3] & 0xFFFF0000) >> 16;
         }
       }
     }
@@ -2184,11 +2188,16 @@ static Base *mouse_select_eval_buffer(ViewContext *vc,
     }
   }
 
+  if (basact && r_sub_selection) {
+    *r_sub_selection = sub_selection_id;
+  }
+
   return basact;
 }
 
-/* mval comes from event->mval, only use within region handlers */
-Base *ED_view3d_give_base_under_cursor(bContext *C, const int mval[2])
+static Base *ed_view3d_give_base_under_cursor_ex(bContext *C,
+                                                 const int mval[2],
+                                                 int *r_material_slot)
 {
   Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
   ViewContext vc;
@@ -2206,14 +2215,25 @@ Base *ED_view3d_give_base_under_cursor(bContext *C, const int mval[2])
       &vc, buffer, mval, VIEW3D_SELECT_FILTER_NOP, do_nearest, false);
 
   if (hits > 0) {
-    const bool has_bones = selectbuffer_has_bones(buffer, hits);
-    basact = mouse_select_eval_buffer(
-        &vc, buffer, hits, vc.view_layer->object_bases.first, has_bones, do_nearest);
+    const bool has_bones = (r_material_slot == NULL) && selectbuffer_has_bones(buffer, hits);
+    basact = mouse_select_eval_buffer(&vc,
+                                      buffer,
+                                      hits,
+                                      vc.view_layer->object_bases.first,
+                                      has_bones,
+                                      do_nearest,
+                                      r_material_slot);
   }
 
   return basact;
 }
 
+/* mval comes from event->mval, only use within region handlers */
+Base *ED_view3d_give_base_under_cursor(bContext *C, const int mval[2])
+{
+  return ed_view3d_give_base_under_cursor_ex(C, mval, NULL);
+}
+
 Object *ED_view3d_give_object_under_cursor(bContext *C, const int mval[2])
 {
   Base *base = ED_view3d_give_base_under_cursor(C, mval);
@@ -2223,6 +2243,17 @@ Object *ED_view3d_give_object_under_cursor(bContext *C, const int mval[2])
   return NULL;
 }
 
+struct Object *ED_view3d_give_material_slot_under_cursor(struct bContext *C,
+                                                         const int mval[2],
+                                                         int *r_material_slot)
+{
+  Base *base = ed_view3d_give_base_under_cursor_ex(C, mval, r_material_slot);
+  if (base) {
+    return base->object;
+  }
+  return NULL;
+}
+
 bool ED_view3d_is_object_under_cursor(bContext *C, const int mval[2])
 {
   return ED_view3d_give_object_under_cursor(C, mval) != NULL;
@@ -2374,7 +2405,8 @@ static bool ed_object_select_pick(bContext *C,
         }
       }
       else {
-        basact = mouse_select_eval_buffer(&vc, buffer, hits, startbase, has_bones, do_nearest);
+        basact = mouse_select_eval_buffer(
+            &vc, buffer, hits, startbase, has_bones, do_nearest, NULL);
       }
 
       if (has_bones && basact) {
@@ -2436,7 +2468,7 @@ static bool ed_object_select_pick(bContext *C,
             if (!changed) {
               /* fallback to regular object selection if no new bundles were selected,
                * allows to select object parented to reconstruction object */
-              basact = mouse_select_eval_buffer(&vc, buffer, hits, startbase, 0, do_nearest);
+              basact = mouse_select_eval_buffer(&vc, buffer, hits, startbase, 0, do_nearest, NULL);
             }
           }
         }



More information about the Bf-blender-cvs mailing list