2.5: Object material slot operators add/remove/assign/select/deselect.
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Wed, 24 Jun 2009 14:07:48 +0000 (14:07 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Wed, 24 Jun 2009 14:07:48 +0000 (14:07 +0000)
13 files changed:
source/blender/blenkernel/BKE_material.h
source/blender/blenkernel/intern/material.c
source/blender/editors/curve/curve_intern.h
source/blender/editors/curve/curve_ops.c
source/blender/editors/curve/editcurve.c
source/blender/editors/curve/editfont.c
source/blender/editors/include/ED_curve.h
source/blender/editors/include/ED_mesh.h
source/blender/editors/mesh/editmesh.c
source/blender/editors/mesh/editmesh_mods.c
source/blender/editors/space_buttons/buttons_intern.h
source/blender/editors/space_buttons/buttons_ops.c
source/blender/editors/space_buttons/space_buttons.c

index 38a1d9b13b703ded471127d02057210cc459ae1c..382754ee2b287545ee4e84b2356ab9d979ee6b1d 100644 (file)
@@ -39,6 +39,8 @@ struct Material;
 struct ID;
 struct Object;
 
+/* materials */
+
 void init_def_material(void);
 void free_material(struct Material *sc); 
 void test_object_materials(struct ID *id);
@@ -47,15 +49,22 @@ struct Material *add_material(char *name);
 struct Material *copy_material(struct Material *ma);
 void make_local_material(struct Material *ma);
 
+void automatname(struct Material *);
+
+/* material slots */
+
 struct Material ***give_matarar(struct Object *ob);
 short *give_totcolp(struct Object *ob);
 struct Material *give_current_material(struct Object *ob, int act);
 struct ID *material_from(struct Object *ob, int act);
 void assign_material(struct Object *ob, struct Material *ma, int act);
-void new_material_to_objectdata(struct Object *ob);
 
 int find_material_index(struct Object *ob, struct Material *ma);
 
+void object_add_material_slot(struct Object *ob);
+void object_remove_material_slot(struct Object *ob);
+
+/* rendering */
 
 void init_render_material(struct Material *, int, float *);
 void init_render_materials(int, float *);
@@ -64,12 +73,8 @@ void end_render_materials(void);
 
 int material_in_material(struct Material *parmat, struct Material *mat);
 
-void automatname(struct Material *);
-void delete_material_index(struct Object *ob);            
-
 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col);
 
-
 #ifdef __cplusplus
 }
 #endif
index b410c521dea635e8de87c04a0384fa6ecddc25e9..57b88bb0b3f66e69548c51b7d3713aa40ef510f1 100644 (file)
@@ -615,7 +615,7 @@ int find_material_index(Object *ob, Material *ma)
        return 0;          
 }
 
-void new_material_to_objectdata(Object *ob)
+void object_add_material_slot(Object *ob)
 {
        Material *ma;
        
@@ -854,7 +854,7 @@ void automatname(Material *ma)
 }
 
 
-void delete_material_index(Object *ob)
+void object_remove_material_slot(Object *ob)
 {
        Material *mao, ***matarar;
        Object *obt;
index a73a54323ee6d5cd0ea5938181f104e5beaf444b..2146855a75ef87098902070c4fc2e82b5978bd79 100644 (file)
@@ -49,7 +49,6 @@ void FONT_OT_case_toggle(struct wmOperatorType *ot);
 void FONT_OT_case_set(struct wmOperatorType *ot);
 void FONT_OT_style_toggle(struct wmOperatorType *ot);
 void FONT_OT_style_set(struct wmOperatorType *ot);
-void FONT_OT_material_set(struct wmOperatorType *ot);
 
 void FONT_OT_text_copy(struct wmOperatorType *ot);
 void FONT_OT_text_cut(struct wmOperatorType *ot);
index 5292d86d3c9ab1385a0e6f63f2a98bc8151845c9..66cde772f3e3f6f6946e327cea211af69090028f 100644 (file)
@@ -106,7 +106,6 @@ void ED_operatortypes_curve(void)
        WM_operatortype_append(FONT_OT_case_set);
        WM_operatortype_append(FONT_OT_style_toggle);
        WM_operatortype_append(FONT_OT_style_set);
-       WM_operatortype_append(FONT_OT_material_set);
 
        WM_operatortype_append(FONT_OT_text_copy);
        WM_operatortype_append(FONT_OT_text_cut);
index 466908c562c4ffe42176b11ae16a85c8e9c1d41e..5283aacf39e0537d846fe73ecd654298326dc92c 100644 (file)
@@ -189,7 +189,7 @@ static short swap_selection_bpoint(BPoint *bp)
                return select_bpoint(bp, SELECT, 1, VISIBLE);
 }
 
-short isNurbsel(Nurb *nu)
+int isNurbsel(Nurb *nu)
 {
        BezTriple *bezt;
        BPoint *bp;
index 5389db9e2eebca4d309d5f55b395156e96490bdd..9f2bd6f26f9ca4f3e9ff162c6ef0f7cf167bc155 100644 (file)
@@ -700,50 +700,6 @@ void FONT_OT_style_toggle(wmOperatorType *ot)
        RNA_def_enum(ot->srna, "style", style_items, CU_BOLD, "Style", "Style to set selection to.");
 }
 
-/******************* set material operator ********************/
-
-static int set_material_exec(bContext *C, wmOperator *op)
-{
-       Scene *scene= CTX_data_scene(C);
-       Object *obedit= CTX_data_edit_object(C);
-       Curve *cu= obedit->data;
-       EditFont *ef= cu->editfont;
-       int i, mat_nr, selstart, selend;
-
-       if(!BKE_font_getselection(obedit, &selstart, &selend))
-               return OPERATOR_CANCELLED;
-
-       if(RNA_property_is_set(op->ptr, "index"))
-               mat_nr= RNA_int_get(op->ptr, "index");
-       else
-               mat_nr= obedit->actcol;
-
-       for(i=selstart; i<=selend; i++)
-               ef->textbufinfo[i].mat_nr = mat_nr;
-
-       DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
-       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, obedit);
-
-       return OPERATOR_FINISHED;
-}
-
-void FONT_OT_material_set(wmOperatorType *ot)
-{
-       /* identifiers */
-       ot->name= "Set Material";
-       ot->idname= "FONT_OT_material_set";
-       
-       /* api callbacks */
-       ot->exec= set_material_exec;
-       ot->poll= ED_operator_editfont;
-       
-       /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-
-       /* properties */
-       RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Material Index", "Material slot index.", 0, INT_MAX);
-}
-
 /******************* copy text operator ********************/
 
 static void copy_selection(Object *obedit)
index c619ee80f707802d2bbc32895326edb6ed517ee3..2cebc6a572a688414e10f9417ff870f37d2e8873 100644 (file)
@@ -30,6 +30,7 @@
 
 struct Base;
 struct bContext;
+struct Nurb;
 struct Object;
 struct Scene;
 struct Text;
@@ -52,6 +53,8 @@ void  mouse_nurb              (struct bContext *C, short mval[2], int extend);
 
 struct Nurb *add_nurbs_primitive(struct bContext *C, int type, int newname);
 
+int            isNurbsel               (struct Nurb *nu);;
+
 /* editfont.h */
 void   undo_push_font  (struct bContext *C, char *name);
 void   make_editText   (struct Object *obedit);
index 6dff4ee6ca472e7e617491d610ddf1845baad8e5..937f6384f6a1e860f65ca0991cfb19ee28d6e4e0 100644 (file)
@@ -147,6 +147,9 @@ int                 EM_init_backbuf_circle(struct ViewContext *vc, short xs, short ys, short r
 void           EM_hide_mesh(struct EditMesh *em, int swap);
 void           EM_reveal_mesh(struct EditMesh *em);
 
+void           EM_select_by_material(struct EditMesh *em, int index);
+void           EM_deselect_by_material(struct EditMesh *em, int index); 
+
 /* editface.c */
 struct MTFace  *EM_get_active_mtface(struct EditMesh *em, struct EditFace **act_efa, struct MCol **mcol, int sloppy);
 
index c05751feddccc9d1f53677766156da19158fa470..7f5201f4704f026660fbeef6d1b6f5276969219e 100644 (file)
@@ -1556,7 +1556,7 @@ static int mesh_separate_material(Scene *scene, Base *editbase)
                /* clear selection, we're going to use that to select material group */
                EM_clear_flag_all(em, SELECT);
                /* select the material */
-               editmesh_select_by_material(em, curr_mat);
+               EM_select_by_material(em, curr_mat);
                /* and now separate */
                if(0==mesh_separate_selected(scene, editbase)) {
                        BKE_mesh_end_editmesh(me, em);
index 07ed6ae7a0136d38368e475bf488c32576ae0263..7e9fcb10984ff8e3c09e4028ed7cc13be57ec019 100644 (file)
@@ -3484,7 +3484,7 @@ void MESH_OT_select_random(wmOperatorType *ot)
        RNA_def_float_percentage(ot->srna, "percent", 0.5f, 0.0f, 1.0f, "Percent", "Percentage of vertices to select randomly.", 0.0001f, 1.0f);
 }
 
-void editmesh_select_by_material(EditMesh *em, int index) 
+void EM_select_by_material(EditMesh *em, int index) 
 {
        EditFace *efa;
        
@@ -3497,7 +3497,7 @@ void editmesh_select_by_material(EditMesh *em, int index)
        EM_selectmode_flush(em);
 }
 
-void editmesh_deselect_by_material(EditMesh *em, int index) 
+void EM_deselect_by_material(EditMesh *em, int index) 
 {
        EditFace *efa;
        
index 196647a37503eb73319da3bd3c77ea0a34d3fbee..b213e4288be407dd67245f205f469412ab7e0f2c 100644 (file)
@@ -61,6 +61,12 @@ void buttons_context_draw(const struct bContext *C, struct uiLayout *layout);
 void buttons_context_register(struct ARegionType *art);
 
 /* buttons_ops.c */
+void OBJECT_OT_material_slot_add(struct wmOperatorType *ot);
+void OBJECT_OT_material_slot_remove(struct wmOperatorType *ot);
+void OBJECT_OT_material_slot_assign(struct wmOperatorType *ot);
+void OBJECT_OT_material_slot_select(struct wmOperatorType *ot);
+void OBJECT_OT_material_slot_deselect(struct wmOperatorType *ot);
+
 void MATERIAL_OT_new(struct wmOperatorType *ot);
 void TEXTURE_OT_new(struct wmOperatorType *ot);
 void WORLD_OT_new(struct wmOperatorType *ot);
index 63469a8294d887b5c7f964589e0685ab321d4e4d..6755a2be1b7fea4b419984917ec2972eec68380b 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "DNA_curve_types.h"
 #include "DNA_object_types.h"
 #include "DNA_material_types.h"
 #include "DNA_texture_types.h"
 #include "DNA_world_types.h"
 
 #include "BKE_context.h"
+#include "BKE_depsgraph.h"
+#include "BKE_font.h"
 #include "BKE_library.h"
 #include "BKE_material.h"
 #include "BKE_texture.h"
+#include "BKE_utildefines.h"
 #include "BKE_world.h"
 
+#include "BLI_editVert.h"
+
 #include "RNA_access.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
 
+#include "ED_curve.h"
+#include "ED_mesh.h"
+
 #include "buttons_intern.h"    // own include
 
+/********************** material slot operators *********************/
+
+static int material_slot_add_exec(bContext *C, wmOperator *op)
+{
+       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+
+       if(!ob)
+               return OPERATOR_CANCELLED;
+
+       object_add_material_slot(ob);
+       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
+       
+       return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_material_slot_add(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Add Material Slot";
+       ot->idname= "OBJECT_OT_material_slot_add";
+       
+       /* api callbacks */
+       ot->exec= material_slot_add_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int material_slot_remove_exec(bContext *C, wmOperator *op)
+{
+       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+
+       if(!ob)
+               return OPERATOR_CANCELLED;
+
+       object_remove_material_slot(ob);
+       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
+       
+       return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Remove Material Slot";
+       ot->idname= "OBJECT_OT_material_slot_remove";
+       
+       /* api callbacks */
+       ot->exec= material_slot_remove_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int material_slot_assign_exec(bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+
+       if(!ob)
+               return OPERATOR_CANCELLED;
+
+       if(ob && ob->actcol>0) {
+               if(ob->type == OB_MESH) {
+                       EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
+                       EditFace *efa;
+
+                       if(em) {
+                               for(efa= em->faces.first; efa; efa=efa->next)
+                                       if(efa->f & SELECT)
+                                               efa->mat_nr= ob->actcol-1;
+                       }
+               }
+               else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
+                       ListBase *editnurb= ((Curve*)ob->data)->editnurb;
+                       Nurb *nu;
+
+                       if(editnurb) {
+                               for(nu= editnurb->first; nu; nu= nu->next)
+                                       if(isNurbsel(nu))
+                                               nu->mat_nr= nu->charidx= ob->actcol-1;
+                       }
+               }
+               else if(ob->type == OB_FONT) {
+                       EditFont *ef= ((Curve*)ob->data)->editfont;
+               int i, selstart, selend;
+
+                       if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
+                               for(i=selstart; i<=selend; i++)
+                                       ef->textbufinfo[i].mat_nr = ob->actcol-1;
+                       }
+               }
+       }
+
+    DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
+    WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
+       
+       return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Assign Material Slot";
+       ot->idname= "OBJECT_OT_material_slot_assign";
+       
+       /* api callbacks */
+       ot->exec= material_slot_assign_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int material_slot_de_select(bContext *C, int select)
+{
+       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+
+       if(!ob)
+               return OPERATOR_CANCELLED;
+
+       if(ob->type == OB_MESH) {
+               EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
+
+               if(em) {
+                       if(select)
+                               EM_select_by_material(em, ob->actcol-1);
+                       else
+                               EM_deselect_by_material(em, ob->actcol-1);
+               }
+       }
+       else if ELEM(ob->type, OB_CURVE, OB_SURF) {
+               ListBase *editnurb= ((Curve*)ob->data)->editnurb;
+               Nurb *nu;
+               BPoint *bp;
+               BezTriple *bezt;
+               int a;
+
+               for(nu= editnurb->first; nu; nu=nu->next) {
+                       if(nu->mat_nr==ob->actcol-1) {
+                               if(nu->bezt) {
+                                       a= nu->pntsu;
+                                       bezt= nu->bezt;
+                                       while(a--) {
+                                               if(bezt->hide==0) {
+                                                       if(select) {
+                                                               bezt->f1 |= SELECT;
+                                                               bezt->f2 |= SELECT;
+                                                               bezt->f3 |= SELECT;
+                                                       }
+                                                       else {
+                                                               bezt->f1 &= ~SELECT;
+                                                               bezt->f2 &= ~SELECT;
+                                                               bezt->f3 &= ~SELECT;
+                                                       }
+                                               }
+                                               bezt++;
+                                       }
+                               }
+                               else if(nu->bp) {
+                                       a= nu->pntsu*nu->pntsv;
+                                       bp= nu->bp;
+                                       while(a--) {
+                                               if(bp->hide==0) {
+                                                       if(select) bp->f1 |= SELECT;
+                                                       else bp->f1 &= ~SELECT;
+                                               }
+                                               bp++;
+                                       }
+                               }
+                       }
+               }
+       }
+
+    WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, ob);
+
+       return OPERATOR_FINISHED;
+}
+
+static int material_slot_select_exec(bContext *C, wmOperator *op)
+{
+       return material_slot_de_select(C, 1);
+}
+
+void OBJECT_OT_material_slot_select(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Select Material Slot";
+       ot->idname= "OBJECT_OT_material_slot_select";
+       
+       /* api callbacks */
+       ot->exec= material_slot_select_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int material_slot_deselect_exec(bContext *C, wmOperator *op)
+{
+       return material_slot_de_select(C, 0);
+}
+
+void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Deselect Material Slot";
+       ot->idname= "OBJECT_OT_material_slot_deselect";
+       
+       /* api callbacks */
+       ot->exec= material_slot_deselect_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
 /********************** new material operator *********************/
 
 static int new_material_exec(bContext *C, wmOperator *op)
 {
-       PointerRNA ptr;
-       Material *ma;
+       Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
        Object *ob;
+       PointerRNA ptr;
        int index;
 
        /* add or copy material */
-       ptr= CTX_data_pointer_get(C, "material");
-       ma= (RNA_struct_is_a(ptr.type, &RNA_Material))? ptr.data: NULL;
-
        if(ma)
                ma= copy_material(ma);
        else
@@ -70,9 +290,9 @@ static int new_material_exec(bContext *C, wmOperator *op)
        ma->id.us--; /* compensating for us++ in assign_material */
 
        /* attempt to assign to material slot */
-       ptr= CTX_data_pointer_get(C, "material_slot");
+       ptr= CTX_data_pointer_get_type(C, "material_slot", &RNA_MaterialSlot);
 
-       if(RNA_struct_is_a(ptr.type, &RNA_MaterialSlot)) {
+       if(ptr.data) {
                ob= ptr.id.data;
                index= (Material**)ptr.data - ob->mat;
 
@@ -80,6 +300,8 @@ static int new_material_exec(bContext *C, wmOperator *op)
 
                WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
        }
+
+       WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
        
        return OPERATOR_FINISHED;
 }
@@ -101,15 +323,12 @@ void MATERIAL_OT_new(wmOperatorType *ot)
 
 static int new_texture_exec(bContext *C, wmOperator *op)
 {
-       PointerRNA ptr;
+       Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
        ID *id;
-       Tex *tex;
        MTex *mtex;
+       PointerRNA ptr;
 
        /* add or copy texture */
-       ptr= CTX_data_pointer_get(C, "texture");
-       tex= (RNA_struct_is_a(ptr.type, &RNA_Texture))? ptr.data: NULL;
-
        if(tex)
                tex= copy_texture(tex);
        else
@@ -118,9 +337,9 @@ static int new_texture_exec(bContext *C, wmOperator *op)
        id_us_min(&tex->id);
 
        /* attempt to assign to texture slot */
-       ptr= CTX_data_pointer_get(C, "texture_slot");
+       ptr= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot);
 
-       if(RNA_struct_is_a(ptr.type, &RNA_TextureSlot)) {
+       if(ptr.data) {
                id= ptr.id.data;
                mtex= ptr.data;
 
@@ -133,6 +352,8 @@ static int new_texture_exec(bContext *C, wmOperator *op)
 
                /* XXX nodes, notifier .. */
        }
+
+       WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
        
        return OPERATOR_FINISHED;
 }
@@ -154,27 +375,21 @@ void TEXTURE_OT_new(wmOperatorType *ot)
 
 static int new_world_exec(bContext *C, wmOperator *op)
 {
-       PointerRNA ptr;
-       Scene *scene;
-       World *wo;
+       Scene *scene= CTX_data_scene(C);
+       World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
 
        /* add or copy world */
-       ptr= CTX_data_pointer_get(C, "world");
-       wo= (RNA_struct_is_a(ptr.type, &RNA_World))? ptr.data: NULL;
-
        if(wo)
                wo= copy_world(wo);
        else
                wo= add_world("World");
 
        /* assign to scene */
-       scene= CTX_data_scene(C);
-
        if(scene->world)
                id_us_min(&scene->world->id);
        scene->world= wo;
 
-       // XXX notifier
+       WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
        
        return OPERATOR_FINISHED;
 }
index e5d2215be299dfdffadcdb8c81fa7c4a721069f3..0e444d7f0b7a5456f6be0ae2574a3ed19fa458a3 100644 (file)
@@ -210,6 +210,12 @@ static void buttons_main_area_draw(const bContext *C, ARegion *ar)
 
 void buttons_operatortypes(void)
 {
+       WM_operatortype_append(OBJECT_OT_material_slot_add);
+       WM_operatortype_append(OBJECT_OT_material_slot_remove);
+       WM_operatortype_append(OBJECT_OT_material_slot_assign);
+       WM_operatortype_append(OBJECT_OT_material_slot_select);
+       WM_operatortype_append(OBJECT_OT_material_slot_deselect);
+
        WM_operatortype_append(MATERIAL_OT_new);
        WM_operatortype_append(TEXTURE_OT_new);
        WM_operatortype_append(WORLD_OT_new);