Fix for [#19847] Joined meshes fail to render
[blender.git] / source / blender / editors / mesh / meshtools.c
index 00893f10165228504fb7216c9ad195bb3bd750c3..18125207eca4b21018ed04a326be3218ec96b58b 100644 (file)
@@ -123,7 +123,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        KeyBlock *kb, *okb, *kbn;
        float imat[4][4], cmat[4][4], *fp1, *fp2, curpos;
        int a, b, totcol, totmat=0, totedge=0, totvert=0, totface=0, ok=0;
-       int vertofs, *matmap;
+       int vertofs, *matmap=NULL;
        int     i, j, index, haskey=0, edgeofs, faceofs;
        bDeformGroup *dg, *odg;
        MDeformVert *dvert;
@@ -168,7 +168,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        
        /* new material indices and material array */
        matar= MEM_callocN(sizeof(void*)*totmat, "join_mesh matar");
-       matmap= MEM_callocN(sizeof(int)*totmat, "join_mesh matmap");
+       if (totmat) matmap= MEM_callocN(sizeof(int)*totmat, "join_mesh matmap");
        totcol= ob->totcol;
        
        /* obact materials in new main array, is nicer start! */
@@ -435,7 +435,10 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                                        mface->v3+= vertofs;
                                        if(mface->v4) mface->v4+= vertofs;
                                        
-                                       mface->mat_nr= matmap[(int)mface->mat_nr];
+                                       if (matmap)
+                                               mface->mat_nr= matmap[(int)mface->mat_nr];
+                                       else 
+                                               mface->mat_nr= 0;
                                }
                                
                                faceofs += me->totface;
@@ -508,7 +511,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        ob->totcol= me->totcol= totcol;
        ob->colbits= 0;
 
-       MEM_freeN(matmap);
+       if (matmap) MEM_freeN(matmap);
        
        /* other mesh users */
        test_object_materials((ID *)me);
@@ -533,7 +536,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        DAG_scene_sort(scene);  // removed objects, need to rebuild dag before editmode call
        
        ED_object_enter_editmode(C, EM_WAITCURSOR);
-       ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR);
+       ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR|EM_DO_UNDO);
 
        WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
 
@@ -795,7 +798,7 @@ static void mesh_octree_add_nodes(MocNode **basetable, float *co, float *offs, f
        
 }
 
-static intptr_t mesh_octree_find_index(MocNode **bt, float (*orco)[3], MVert *mvert, float *co)
+static intptr_t mesh_octree_find_index(MocNode **bt, MVert *mvert, float *co)
 {
        float *vec;
        int a;
@@ -806,12 +809,7 @@ static intptr_t mesh_octree_find_index(MocNode **bt, float (*orco)[3], MVert *mv
        for(a=0; a<MOC_NODE_RES; a++) {
                if((*bt)->index[a]) {
                        /* does mesh verts and editmode, code looks potential dangerous, octree should really be filled OK! */
-                       if(orco) {
-                               vec= orco[(*bt)->index[a]-1];
-                               if(FloatCompare(vec, co, MOC_THRESH))
-                                       return (*bt)->index[a]-1;
-                       }
-                       else if(mvert) {
+                       if(mvert) {
                                vec= (mvert+(*bt)->index[a]-1)->co;
                                if(FloatCompare(vec, co, MOC_THRESH))
                                        return (*bt)->index[a]-1;
@@ -825,7 +823,7 @@ static intptr_t mesh_octree_find_index(MocNode **bt, float (*orco)[3], MVert *mv
                else return -1;
        }
        if( (*bt)->next)
-               return mesh_octree_find_index(&(*bt)->next, orco, mvert, co);
+               return mesh_octree_find_index(&(*bt)->next, mvert, co);
        
        return -1;
 }
@@ -833,9 +831,7 @@ static intptr_t mesh_octree_find_index(MocNode **bt, float (*orco)[3], MVert *mv
 static struct {
        MocNode **table;
        float offs[3], div[3];
-       float (*orco)[3];
-       float orcoloc[3];
-} MeshOctree = {NULL, {0, 0, 0}, {0, 0, 0}, NULL};
+} MeshOctree = {NULL, {0, 0, 0}, {0, 0, 0}};
 
 /* mode is 's' start, or 'e' end, or 'u' use */
 /* if end, ob can be NULL */
@@ -851,9 +847,9 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
                        Mesh *me= ob->data;
                        bt= MeshOctree.table + mesh_octree_get_base_offs(co, MeshOctree.offs, MeshOctree.div);
                        if(em)
-                               return mesh_octree_find_index(bt, NULL, NULL, co);
+                               return mesh_octree_find_index(bt, NULL, co);
                        else
-                               return mesh_octree_find_index(bt, MeshOctree.orco, me->mvert, co);
+                               return mesh_octree_find_index(bt, me->mvert, co);
                }
                return -1;
        }
@@ -873,16 +869,10 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
                }
                else {          
                        MVert *mvert;
-                       float *vco;
-                       int a, totvert;
-                       
-                       MeshOctree.orco= mesh_getRefKeyCos(me, &totvert);
-                       mesh_get_texspace(me, MeshOctree.orcoloc, NULL, NULL);
+                       int a;
                        
-                       for(a=0, mvert= me->mvert; a<me->totvert; a++, mvert++) {
-                               vco= (MeshOctree.orco)? MeshOctree.orco[a]: mvert->co;
-                               DO_MINMAX(vco, min, max);
-                       }
+                       for(a=0, mvert= me->mvert; a<me->totvert; a++, mvert++)
+                               DO_MINMAX(mvert->co, min, max);
                }
                
                /* for quick unit coordinate calculus */
@@ -915,13 +905,10 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
                }
                else {          
                        MVert *mvert;
-                       float *vco;
                        int a;
                        
-                       for(a=0, mvert= me->mvert; a<me->totvert; a++, mvert++) {
-                               vco= (MeshOctree.orco)? MeshOctree.orco[a]: mvert->co;
-                               mesh_octree_add_nodes(MeshOctree.table, vco, MeshOctree.offs, MeshOctree.div, a+1);
-                       }
+                       for(a=0, mvert= me->mvert; a<me->totvert; a++, mvert++)
+                               mesh_octree_add_nodes(MeshOctree.table, mvert->co, MeshOctree.offs, MeshOctree.div, a+1);
                }
        }
        else if(mode=='e') { /* end table */
@@ -934,10 +921,6 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
                        MEM_freeN(MeshOctree.table);
                        MeshOctree.table= NULL;
                }
-               if(MeshOctree.orco) {
-                       MEM_freeN(MeshOctree.orco);
-                       MeshOctree.orco= NULL;
-               }
        }
        return 0;
 }
@@ -948,19 +931,10 @@ int mesh_get_x_mirror_vert(Object *ob, int index)
        MVert *mvert;
        float vec[3];
        
-       if(MeshOctree.orco) {
-               float *loc= MeshOctree.orcoloc;
-
-               vec[0]= -(MeshOctree.orco[index][0] + loc[0]) - loc[0];
-               vec[1]= MeshOctree.orco[index][1];
-               vec[2]= MeshOctree.orco[index][2];
-       }
-       else {
-               mvert= me->mvert+index;
-               vec[0]= -mvert->co[0];
-               vec[1]= mvert->co[1];
-               vec[2]= mvert->co[2];
-       }
+       mvert= me->mvert+index;
+       vec[0]= -mvert->co[0];
+       vec[1]= mvert->co[1];
+       vec[2]= mvert->co[2];
        
        return mesh_octree_table(ob, NULL, vec, 'u');
 }