OUTLINER_OT_material_drop -- Drag & Drop materials onto objects in the outliner
authorDan Eicher <dan@eu.phorio.us>
Sun, 10 Jun 2012 22:22:26 +0000 (22:22 +0000)
committerDan Eicher <dan@eu.phorio.us>
Sun, 10 Jun 2012 22:22:26 +0000 (22:22 +0000)
Adds the material at materials + 1 unlike the DnD view3d one which replaces the first one

source/blender/editors/space_outliner/outliner_edit.c
source/blender/editors/space_outliner/outliner_intern.h
source/blender/editors/space_outliner/outliner_ops.c
source/blender/editors/space_outliner/space_outliner.c

index e5f7b8fd76ddc5bc1ac5f5943c6951778729592e..eb740e441b7c255516ad6888672f0e05a59f2e5a 100644 (file)
@@ -35,6 +35,7 @@
 #include "DNA_group_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_object_types.h"
+#include "DNA_material_types.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
@@ -48,6 +49,7 @@
 #include "BKE_main.h"
 #include "BKE_report.h"
 #include "BKE_scene.h"
+#include "BKE_material.h"
 
 #include "ED_object.h"
 #include "ED_screen.h"
@@ -1816,3 +1818,68 @@ void OUTLINER_OT_scene_drop(wmOperatorType *ot)
        RNA_def_string(ot->srna, "object", "Object", MAX_ID_NAME, "Object", "Target Object");
        RNA_def_string(ot->srna, "scene", "Scene", MAX_ID_NAME, "Scene", "Target Scene");
 }
+
+static int material_drop_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       Main *bmain = CTX_data_main(C);
+       Material *ma = NULL;
+       Object *ob = NULL;
+       SpaceOops *soops = CTX_wm_space_outliner(C);
+       ARegion *ar = CTX_wm_region(C);
+       TreeElement *te = NULL;
+       TreeElement *te_found = NULL;
+       char mat_name[MAX_ID_NAME - 2];
+       float fmval[2];
+
+       UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
+
+       /* Find object hovered over */
+       for (te = soops->tree.first; te; te = te->next) {
+               te_found = outliner_dropzone_parent(C, event, te, fmval);
+               if (te_found)
+                       break;
+       }
+
+       if (te_found) {
+               RNA_string_set(op->ptr, "object", te_found->name);
+               ob = (Object *)BKE_libblock_find_name(ID_OB, te_found->name);
+
+               RNA_string_get(op->ptr, "material", mat_name);
+               ma = (Material *)BKE_libblock_find_name(ID_MA, mat_name);
+
+               if (ELEM(NULL, ob, ma)) {
+                       return OPERATOR_CANCELLED;
+               }
+
+               assign_material(ob, ma, ob->totcol + 1);
+
+               DAG_ids_flush_update(bmain, 0);
+               WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));         
+               WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING, ma);
+
+               return OPERATOR_FINISHED;
+       }
+
+       return OPERATOR_CANCELLED;
+}
+
+void OUTLINER_OT_material_drop(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Drop Material on Object";
+       ot->description = "Drag material to object in Outliner";
+       ot->idname = "OUTLINER_OT_material_drop";
+
+       /* api callbacks */
+       ot->invoke = material_drop_invoke;
+
+       ot->poll = ED_operator_outliner_active;
+
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+       /* properties */
+       RNA_def_string(ot->srna, "object", "Object", MAX_ID_NAME, "Object", "Target Object");
+       RNA_def_string(ot->srna, "material", "Material", MAX_ID_NAME, "Material", "Target Material");
+}
+
index 5274410214a8e6f0591ee4a2c313912072951f34..65de2a27568ef821849f102da93e543bd1b7db14 100644 (file)
@@ -221,6 +221,7 @@ void OUTLINER_OT_drivers_delete_selected(struct wmOperatorType *ot);
 void OUTLINER_OT_parent_drop(struct wmOperatorType *ot);
 void OUTLINER_OT_parent_clear(struct wmOperatorType *ot);
 void OUTLINER_OT_scene_drop(struct wmOperatorType *ot);
+void OUTLINER_OT_material_drop(struct wmOperatorType *ot);
 
 /* outliner_tools.c ---------------------------------------------- */
 
index 0b3a226cca183c44255b512bde40bf3e75e7f854..1dd043409a549812019ff89d0156aeefd7b5381b 100644 (file)
@@ -77,6 +77,7 @@ void outliner_operatortypes(void)
        WM_operatortype_append(OUTLINER_OT_parent_drop);
        WM_operatortype_append(OUTLINER_OT_parent_clear);
        WM_operatortype_append(OUTLINER_OT_scene_drop);
+       WM_operatortype_append(OUTLINER_OT_material_drop);
 }
 
 void outliner_keymap(wmKeyConfig *keyconf)
index c78be8bd2235814ebf834ea08ea87e79619c3bc5..489a4efe891d9e082f58b7f7bd5506d31ca059dc 100644 (file)
@@ -194,6 +194,35 @@ static void outliner_scene_drop_copy(wmDrag *drag, wmDropBox *drop)
        RNA_string_set(drop->ptr, "object", id->name + 2);
 }
 
+static int outliner_material_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
+{
+       ARegion *ar = CTX_wm_region(C);
+       SpaceOops *soops = CTX_wm_space_outliner(C);
+       TreeElement *te = NULL;
+       float fmval[2];
+       UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
+
+       if (drag->type == WM_DRAG_ID) {
+               ID *id = (ID *)drag->poin;
+               if (GS(id->name) == ID_MA) {
+                       /* Ensure item under cursor is valid drop target */
+                       /* Find object hovered over */
+                       for (te = soops->tree.first; te; te = te->next) {
+                               if (outliner_dropzone_parent(C, event, te, fmval))
+                                       return 1;
+                       }
+               }
+       }
+       return 0;
+}
+
+static void outliner_material_drop_copy(wmDrag *drag, wmDropBox *drop)
+{
+       ID *id = (ID *)drag->poin;
+
+       RNA_string_set(drop->ptr, "material", id->name + 2);
+}
+
 /* region dropbox definition */
 static void outliner_dropboxes(void)
 {
@@ -202,6 +231,7 @@ static void outliner_dropboxes(void)
        WM_dropbox_add(lb, "OUTLINER_OT_parent_drop", outliner_parent_drop_poll, outliner_parent_drop_copy);
        WM_dropbox_add(lb, "OUTLINER_OT_parent_clear", outliner_parent_clear_poll, outliner_parent_clear_copy);
        WM_dropbox_add(lb, "OUTLINER_OT_scene_drop", outliner_scene_drop_poll, outliner_scene_drop_copy);
+       WM_dropbox_add(lb, "OUTLINER_OT_material_drop", outliner_material_drop_poll, outliner_material_drop_copy);
 }
 
 static void outliner_main_area_draw(const bContext *C, ARegion *ar)