synched with trunk at revision 34793
[blender.git] / source / blender / editors / mesh / mesh_data.c
index 4eae2540fccc291638a27093d8c4b72694900871..d1c6ff4c4aee822688e840793fb7a28665a7e32f 100644 (file)
 #include "DNA_view3d_types.h"
 
 #include "BKE_context.h"
-#include "BKE_customdata.h"
 #include "BKE_depsgraph.h"
 #include "BKE_displist.h"
-#include "BKE_global.h"
 #include "BKE_image.h"
 #include "BKE_library.h"
 #include "BKE_material.h"
@@ -159,7 +157,7 @@ static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *la
        }
 }
 
-int ED_mesh_uv_texture_add(bContext *C, Scene *scene, Object *ob, Mesh *me)
+int ED_mesh_uv_texture_add(bContext *C, Scene *scene, Object *ob, Mesh *me, const char *name, int active_set)
 {
        EditMesh *em;
        int layernum;
@@ -169,22 +167,25 @@ int ED_mesh_uv_texture_add(bContext *C, Scene *scene, Object *ob, Mesh *me)
 
                layernum= CustomData_number_of_layers(&em->fdata, CD_MTFACE);
                if(layernum >= MAX_MTFACE)
-                       return OPERATOR_CANCELLED;
+                       return 0;
 
-               EM_add_data_layer(em, &em->fdata, CD_MTFACE);
-               CustomData_set_layer_active(&em->fdata, CD_MTFACE, layernum);
+               EM_add_data_layer(em, &em->fdata, CD_MTFACE, name);
+               if(active_set || layernum==0)
+                       CustomData_set_layer_active(&em->fdata, CD_MTFACE, layernum);
        }
        else {
                layernum= CustomData_number_of_layers(&me->fdata, CD_MTFACE);
                if(layernum >= MAX_MTFACE)
-                       return OPERATOR_CANCELLED;
+                       return 0;
 
                if(me->mtface)
-                       CustomData_add_layer(&me->fdata, CD_MTFACE, CD_DUPLICATE, me->mtface, me->totface);
+                       CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DUPLICATE, me->mtface, me->totface, name);
                else
-                       CustomData_add_layer(&me->fdata, CD_MTFACE, CD_DEFAULT, NULL, me->totface);
+                       CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DEFAULT, NULL, me->totface, name);
+
+               if(active_set || layernum==0)
+                       CustomData_set_layer_active(&me->fdata, CD_MTFACE, layernum);
 
-               CustomData_set_layer_active(&me->fdata, CD_MTFACE, layernum);
                mesh_update_customdata_pointers(me);
        }
 
@@ -200,7 +201,7 @@ int ED_mesh_uv_texture_remove(bContext *C, Object *ob, Mesh *me)
        CustomDataLayer *cdl;
        int index;
 
-        index= CustomData_get_active_layer_index(data, CD_MTFACE);
+       index= CustomData_get_active_layer_index(data, CD_MTFACE);
        cdl= (index == -1) ? NULL: &data->layers[index];
 
        if(!cdl)
@@ -213,7 +214,7 @@ int ED_mesh_uv_texture_remove(bContext *C, Object *ob, Mesh *me)
        return 1;
 }
 
-int ED_mesh_color_add(bContext *C, Scene *scene, Object *ob, Mesh *me)
+int ED_mesh_color_add(bContext *C, Scene *scene, Object *ob, Mesh *me, const char *name, int active_set)
 {
        EditMesh *em;
        MCol *mcol;
@@ -226,8 +227,9 @@ int ED_mesh_color_add(bContext *C, Scene *scene, Object *ob, Mesh *me)
                if(layernum >= MAX_MCOL)
                        return 0;
 
-               EM_add_data_layer(em, &em->fdata, CD_MCOL);
-               CustomData_set_layer_active(&em->fdata, CD_MCOL, layernum);
+               EM_add_data_layer(em, &em->fdata, CD_MCOL, name);
+               if(active_set || layernum==0)
+                       CustomData_set_layer_active(&em->fdata, CD_MCOL, layernum);
        }
        else {
                layernum= CustomData_number_of_layers(&me->fdata, CD_MCOL);
@@ -237,11 +239,13 @@ int ED_mesh_color_add(bContext *C, Scene *scene, Object *ob, Mesh *me)
                mcol= me->mcol;
 
                if(me->mcol)
-                       CustomData_add_layer(&me->fdata, CD_MCOL, CD_DUPLICATE, me->mcol, me->totface);
+                       CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DUPLICATE, me->mcol, me->totface, name);
                else
-                       CustomData_add_layer(&me->fdata, CD_MCOL, CD_DEFAULT, NULL, me->totface);
+                       CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DEFAULT, NULL, me->totface, name);
+
+               if(active_set || layernum==0)
+                       CustomData_set_layer_active(&me->fdata, CD_MCOL, layernum);
 
-               CustomData_set_layer_active(&me->fdata, CD_MCOL, layernum);
                mesh_update_customdata_pointers(me);
 
                if(!mcol)
@@ -288,7 +292,7 @@ static int uv_texture_add_exec(bContext *C, wmOperator *op)
        Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
        Mesh *me= ob->data;
 
-       if(!ED_mesh_uv_texture_add(C, scene, ob, me))
+       if(!ED_mesh_uv_texture_add(C, scene, ob, me, NULL, TRUE))
                return OPERATOR_CANCELLED;
 
        return OPERATOR_FINISHED;
@@ -313,32 +317,34 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        Scene *scene= CTX_data_scene(C);
        Base *base= ED_view3d_give_base_under_cursor(C, event->mval);
-       Image *ima;
+       Image *ima= NULL;
        Mesh *me;
        Object *obedit;
        int exitmode= 0;
        char name[32];
        
+       /* Check context */
+       if(base==NULL || base->object->type!=OB_MESH) {
+               BKE_report(op->reports, RPT_ERROR, "Not an Object or Mesh");
+               return OPERATOR_CANCELLED;
+       }
+       
        /* check input variables */
-       if(RNA_property_is_set(op->ptr, "path")) {
+       if(RNA_property_is_set(op->ptr, "filepath")) {
                char path[FILE_MAX];
                
-               RNA_string_get(op->ptr, "path", path);
+               RNA_string_get(op->ptr, "filepath", path);
                ima= BKE_add_image_file(path, 
                                                                scene ? scene->r.cfra : 1);
-               
-               if(!ima) {
-                       BKE_report(op->reports, RPT_ERROR, "Not an Image.");
-                       return OPERATOR_CANCELLED;
-               }
        }
        else {
                RNA_string_get(op->ptr, "name", name);
                ima= (Image *)find_id("IM", name);
-               if(base==NULL || base->object->type!=OB_MESH || ima==NULL) {
-                       BKE_report(op->reports, RPT_ERROR, "Not a Mesh or no Image.");
-                       return OPERATOR_CANCELLED;
-               }
+       }
+       
+       if(!ima) {
+               BKE_report(op->reports, RPT_ERROR, "Not an Image.");
+               return OPERATOR_CANCELLED;
        }
        
        /* turn mesh in editmode */
@@ -383,7 +389,7 @@ void MESH_OT_drop_named_image(wmOperatorType *ot)
        
        /* properties */
        RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Image name to assign.");
-       RNA_def_string(ot->srna, "path", "Path", FILE_MAX, "Filepath", "Path to image file");
+       RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
 }
 
 static int uv_texture_remove_exec(bContext *C, wmOperator *op)
@@ -420,7 +426,7 @@ static int vertex_color_add_exec(bContext *C, wmOperator *op)
        Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
        Mesh *me= ob->data;
 
-       if(!ED_mesh_color_add(C, scene, ob, me))
+       if(!ED_mesh_color_add(C, scene, ob, me, NULL, TRUE))
                return OPERATOR_CANCELLED;
 
        return OPERATOR_FINISHED;
@@ -715,6 +721,7 @@ static void mesh_add_faces(Mesh *mesh, int len)
        mesh->totface= totface;
 }
 
+/*
 void ED_mesh_geometry_add(Mesh *mesh, ReportList *reports, int verts, int edges, int faces)
 {
        if(mesh->edit_mesh) {
@@ -729,33 +736,39 @@ void ED_mesh_geometry_add(Mesh *mesh, ReportList *reports, int verts, int edges,
        if(faces)
                mesh_add_faces(mesh, faces);
 }
+*/
 
-void ED_mesh_calc_normals(Mesh *me)
+void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
 {
-       mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
+       if(mesh->edit_mesh) {
+               BKE_report(reports, RPT_ERROR, "Can't add faces in edit mode.");
+               return;
+       }
+
+       mesh_add_faces(mesh, count);
 }
 
-void ED_mesh_material_add(Mesh *me, Material *ma)
+void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
 {
-       int i;
-       int totcol = me->totcol + 1;
-       Material **mat;
-
-       /* don't add if mesh already has it */
-       for(i = 0; i < me->totcol; i++)
-               if(me->mat[i] == ma)
-                       return;
-
-       mat= MEM_callocN(sizeof(void*)*totcol, "newmatar");
+       if(mesh->edit_mesh) {
+               BKE_report(reports, RPT_ERROR, "Can't add edges in edit mode.");
+               return;
+       }
 
-       if(me->totcol) memcpy(mat, me->mat, sizeof(void*) * me->totcol);
-       if(me->mat) MEM_freeN(me->mat);
+       mesh_add_edges(mesh, count);
+}
 
-       me->mat = mat;
-       me->mat[me->totcol++] = ma;
-       if(ma)
-               ma->id.us++;
+void ED_mesh_vertices_add(Mesh *mesh, ReportList *reports, int count)
+{
+       if(mesh->edit_mesh) {
+               BKE_report(reports, RPT_ERROR, "Can't add vertices in edit mode.");
+               return;
+       }
 
-       test_object_materials((ID*)me);
+       mesh_add_verts(mesh, count);
 }
 
+void ED_mesh_calc_normals(Mesh *me)
+{
+       mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
+}