3 #include "MEM_guardedalloc.h"
5 #include "DNA_listBase.h"
6 #include "DNA_customdata_types.h"
7 #include "DNA_mesh_types.h"
8 #include "DNA_meshdata_types.h"
9 #include "DNA_modifier_types.h"
10 #include "DNA_key_types.h"
11 #include "DNA_object_types.h"
12 #include "DNA_scene_types.h"
14 #include "BKE_customdata.h"
16 #include "BKE_global.h"
17 #include "BKE_DerivedMesh.h"
18 #include "BKE_cdderivedmesh.h"
22 #include "BLI_utildefines.h"
24 #include "BLI_blenlib.h"
25 #include "BLI_edgehash.h"
26 #include "BLI_editVert.h"
27 #include "BLI_scanfill.h"
28 #include "BLI_array.h"
29 #include "BLI_utildefines.h"
33 #include "mesh_intern.h"
35 #include "bmesh_private.h"
40 * This file contains functions
41 * for converting a Mesh
42 * into a Bmesh, and back again.
46 void mesh_to_bmesh_exec(BMesh *bm, BMOperator *op) {
47 Object *ob = BMO_Get_Pnt(op, "object");
48 Mesh *me = BMO_Get_Pnt(op, "mesh");
50 BLI_array_declare(verts);
54 KeyBlock *actkey, *block;
55 BMVert *v, **vt=NULL, **verts = NULL;
56 BMEdge *e, **fedges=NULL, **et = NULL;
58 BLI_array_declare(fedges);
59 float (*keyco)[3]= NULL;
61 int set_key = BMO_Get_Int(op, "set_shapekey");
62 int totuv, i, j, li, allocsize[4] = {512, 512, 2048, 512};
64 if (!me || !me->totvert) return; /*sanity check*/
67 vt = MEM_mallocN(sizeof(void**)*me->totvert, "mesh to bmesh vtable");
69 CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
70 CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
71 CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
72 CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
74 /*make sure uv layer names are consistent*/
75 totuv = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
76 for (i=0; i<totuv; i++) {
77 int li = CustomData_get_layer_index_n(&bm->pdata, CD_MTEXPOLY, i);
78 CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
81 if (!CustomData_has_layer(&bm->edata, CD_CREASE))
82 CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
84 if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
85 CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
87 if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
88 CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
91 if (me->key && ob->shapenr > me->key->totkey) {
92 ob->shapenr = me->key->totkey-1;
95 actkey = ob_get_keyblock(ob);
96 if(actkey && actkey->totelem == me->totvert) {
97 CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
99 /*check if we need to generate unique ids for the shapekeys.
100 this also exists in the file reading code, but is here for
102 if (!me->key->uidgen) {
103 printf("yeek! had to generate shape key uid's in a situation we shouldn't need to!\n");
105 for (block=me->key->block.first; block; block=block->next) {
106 block->uid = me->key->uidgen++;
111 bm->shapenr= ob->shapenr;
112 for (i=0, block=me->key->block.first; block; block=block->next, i++) {
113 CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY,
114 CD_ASSIGN, NULL, 0, block->name);
116 j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
117 bm->vdata.layers[j].uid = block->uid;
120 printf("shapekey<->mesh mismatch!\n");
123 CustomData_bmesh_init_pool(&bm->vdata, allocsize[0]);
124 CustomData_bmesh_init_pool(&bm->edata, allocsize[1]);
125 CustomData_bmesh_init_pool(&bm->ldata, allocsize[2]);
126 CustomData_bmesh_init_pool(&bm->pdata, allocsize[3]);
128 for (i=0; i<me->totvert; i++, mvert++) {
129 v = BM_Make_Vert(bm, keyco&&set_key ? keyco[i] : mvert->co, NULL);
130 normal_short_to_float_v3(v->no, mvert->no);
135 /*this is necassary for selection counts to work properly*/
136 if(v->head.flag & BM_SELECT) BM_Select_Vert(bm, v, 1);
139 v->head.flag = MEFlags_To_BMFlags(mvert->flag, BM_VERT);
140 BM_SetCDf(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
143 CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
145 /*set shapekey data*/
147 /*set shape key original index*/
148 keyi = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_SHAPE_KEYINDEX);
151 for (block=me->key->block.first, j=0; block; block=block->next, j++) {
152 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data,
155 VECCOPY(co, ((float*)block->data)+3*i);
165 et = MEM_mallocN(sizeof(void**)*me->totedge, "mesh to bmesh etable");
168 for (i=0; i<me->totedge; i++, medge++) {
169 e = BM_Make_Edge(bm, vt[medge->v1], vt[medge->v2], NULL, 0);
173 CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
175 BM_SetCDf(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
176 BM_SetCDf(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
178 /*this is necassary for selection counts to work properly*/
179 if (e->head.flag & BM_SELECT) BM_Select(bm, e, 1);
182 e->head.flag = MEFlags_To_BMFlags(medge->flag, BM_EDGE);
193 for (i=0; i<me->totpoly; i++, mpoly++) {
198 BLI_array_empty(fedges);
199 BLI_array_empty(verts);
200 for (j=0; j<mpoly->totloop; j++) {
201 ml = &me->mloop[mpoly->loopstart+j];
205 BLI_array_growone(fedges);
206 BLI_array_growone(verts);
212 v1 = vt[me->mloop[mpoly->loopstart].v];
213 v2 = vt[me->mloop[mpoly->loopstart+1].v];
215 if (v1 == fedges[0]->v1) v2 = fedges[0]->v2;
221 f = BM_Make_Face(bm, verts, fedges, mpoly->totloop);
224 printf("Warning! Bad face in mesh"
225 " \"%s\" at index %d!\n", me->id.name+2, i);
229 /*this is necassary for selection counts to work properly*/
230 if (f->head.flag & BM_SELECT) BM_Select(bm, f, 1);
233 f->head.flag = MEFlags_To_BMFlags(mpoly->flag, BM_FACE);
235 f->mat_nr = mpoly->mat_nr;
236 if (i == me->act_face) bm->act_face = f;
238 /*Copy over loop customdata*/
240 BM_ITER(l, &iter, bm, BM_LOOPS_OF_FACE, f) {
241 CustomData_to_bmesh_block(&me->ldata, &bm->ldata, mpoly->loopstart+j, &l->head.data);
247 CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
250 BLI_array_free(fedges);
251 BLI_array_free(verts);
258 static void loops_to_corners(BMesh *bm, Mesh *me, int findex,
259 BMFace *f, BMLoop *ls[3], int numTex, int numCol)
269 for(i=0; i < numTex; i++){
270 texface = CustomData_get_n(&me->fdata, CD_MTFACE, findex, i);
271 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->head.data, CD_MTEXPOLY, i);
273 texface->tpage = texpoly->tpage;
274 texface->flag = texpoly->flag;
275 texface->transp = texpoly->transp;
276 texface->mode = texpoly->mode;
277 texface->tile = texpoly->tile;
278 texface->unwrap = texpoly->unwrap;
280 for (j=0; j<3; j++) {
282 mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
283 texface->uv[j][0] = mloopuv->uv[0];
284 texface->uv[j][1] = mloopuv->uv[1];
288 for(i=0; i < numCol; i++){
289 mcol = CustomData_get_n(&me->fdata, CD_MCOL, findex, i);
291 for (j=0; j<3; j++) {
293 mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPCOL, i);
294 mcol[j].r = mloopcol->r;
295 mcol[j].g = mloopcol->g;
296 mcol[j].b = mloopcol->b;
297 mcol[j].a = mloopcol->a;
302 void object_load_bmesh_exec(BMesh *bm, BMOperator *op) {
303 Object *ob = BMO_Get_Pnt(op, "object");
304 /* Scene *scene = BMO_Get_Pnt(op, "scene"); */
307 BMO_CallOpf(bm, "bmesh_to_mesh mesh=%p object=%p", me, ob);
309 /*BMESH_TODO eventually we'll have to handle shapekeys here*/
312 void bmesh_to_mesh_exec(BMesh *bm, BMOperator *op) {
313 Mesh *me = BMO_Get_Pnt(op, "mesh");
314 /* Object *ob = BMO_Get_Pnt(op, "object"); */
318 MVert *mvert, *oldverts;
326 float *facenors = NULL;
327 int i, j, *keyi, ototvert, totloop, totface, numTex, numCol;
328 int dotess = !BMO_Get_Int(op, "notesselation");
330 numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
331 numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
333 ototvert = me->totvert;
335 /* new Vertex block */
336 if(bm->totvert==0) mvert= NULL;
337 else mvert= MEM_callocN(bm->totvert*sizeof(MVert), "loadeditbMesh vert");
340 if(bm->totedge==0) medge= NULL;
341 else medge= MEM_callocN(bm->totedge*sizeof(MEdge), "loadeditbMesh edge");
344 /* new Ngon Face block */
345 if(bm->totface==0) mpoly = NULL;
346 else mpoly= MEM_callocN(bm->totface*sizeof(MPoly), "loadeditbMesh poly");
348 /*find number of loops to allocate*/
350 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
354 if (totloop==0) mloop = NULL;
355 else mloop = MEM_callocN(totloop*sizeof(MLoop), "loadeditbMesh loop");
357 /* lets save the old verts just in case we are actually working on
358 * a key ... we now do processing of the keys at the end */
361 /* don't free this yet */
362 CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
364 /* free custom data */
365 CustomData_free(&me->vdata, me->totvert);
366 CustomData_free(&me->edata, me->totedge);
367 CustomData_free(&me->fdata, me->totface);
368 CustomData_free(&me->ldata, me->totloop);
369 CustomData_free(&me->pdata, me->totpoly);
371 /* add new custom data */
372 me->totvert= bm->totvert;
373 me->totedge= bm->totedge;
374 me->totloop= totloop;
375 me->totpoly= bm->totface;
377 CustomData_copy(&bm->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
378 CustomData_copy(&bm->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
379 CustomData_copy(&bm->ldata, &me->ldata, CD_MASK_MESH, CD_CALLOC, me->totloop);
380 CustomData_copy(&bm->pdata, &me->pdata, CD_MASK_MESH, CD_CALLOC, me->totpoly);
382 CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
383 CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
384 CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
385 CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
388 BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
389 float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
391 mvert->bweight = bweight ? (char)((*bweight)*255) : 0;
393 VECCOPY(mvert->co, v->co);
395 mvert->no[0] = (short) (v->no[0]*32767.0f);
396 mvert->no[1] = (short) (v->no[1]*32767.0f);
397 mvert->no[2] = (short) (v->no[2]*32767.0f);
399 mvert->flag = BMFlags_To_MEFlags(v);
403 /*copy over customdata*/
404 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
409 CHECK_ELEMENT(bm, v);
413 BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
414 float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
415 float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
417 medge->v1 = BM_GetIndex(e->v1);
418 medge->v2 = BM_GetIndex(e->v2);
419 medge->crease = crease ? (char)((*crease)*255) : 0;
420 medge->bweight = bweight ? (char)((*bweight)*255) : 0;
422 medge->flag = BMFlags_To_MEFlags(e);
426 /*copy over customdata*/
427 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
431 CHECK_ELEMENT(bm, e);
434 /*new scanfill tesselation code*/
436 /*first counter number of faces we'll need*/
438 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
439 EditVert *eve, *lasteve = NULL, *firsteve = NULL;
442 BLI_begin_edgefill();
444 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
445 eve = BLI_addfillvert(l->v->co);
451 BLI_addfilledge(lasteve, eve);
455 if (!firsteve) firsteve = eve;
460 BLI_addfilledge(lasteve, firsteve);
463 for (efa=fillfacebase.first; efa; efa=efa->next)
469 me->totface = totface;
471 /* new tess face block */
472 if(totface==0) mface= NULL;
474 mface= MEM_callocN(totface*sizeof(MFace), "loadeditbMesh face");
475 facenors = MEM_callocN(totface*sizeof(float)*3, "facenors");
478 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
479 CustomData_add_layer(&me->fdata, CD_NORMAL, CD_ASSIGN, facenors, me->totface);
480 CustomData_from_bmeshpoly(&me->fdata, &bm->pdata, &bm->ldata, totface);
482 mesh_update_customdata_pointers(me);
485 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
486 EditVert *eve, *lasteve = NULL, *firsteve = NULL;
490 BLI_begin_edgefill();
491 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
492 eve = BLI_addfillvert(l->v->co);
496 BLI_addfilledge(lasteve, eve);
500 if (!firsteve) firsteve = eve;
503 BLI_addfilledge(lasteve, firsteve);
506 for (efa=fillfacebase.first; efa; efa=efa->next) {
507 ls[0] = efa->v1->tmp.p;
508 ls[1] = efa->v2->tmp.p;
509 ls[2] = efa->v3->tmp.p;
511 /*ensure correct winding. I believe this is
512 analogous to bubble sort on three elements.*/
513 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
514 SWAP(BMLoop*, ls[0], ls[1]);
516 if (BM_GetIndex(ls[1]) > BM_GetIndex(ls[2])) {
517 SWAP(BMLoop*, ls[1], ls[2]);
519 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
520 SWAP(BMLoop*, ls[0], ls[1]);
523 mface->mat_nr = f->mat_nr;
524 mface->flag = BMFlags_To_MEFlags(f);
526 mface->v1 = BM_GetIndex(ls[0]->v);
527 mface->v2 = BM_GetIndex(ls[1]->v);
528 mface->v3 = BM_GetIndex(ls[2]->v);
530 test_index_face(mface, &me->fdata, i, 1);
532 loops_to_corners(bm, me, i, f, ls, numTex, numCol);
533 VECCOPY(facenors, ls[0]->f->no);
545 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
546 mpoly->loopstart = j;
547 mpoly->totloop = f->len;
548 mpoly->mat_nr = f->mat_nr;
549 mpoly->flag = BMFlags_To_MEFlags(f);
551 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
552 for ( ; l; l=BMIter_Step(&liter), j++, mloop++) {
553 mloop->e = BM_GetIndex(l->e);
554 mloop->v = BM_GetIndex(l->v);
556 /*copy over customdata*/
557 CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
558 CHECK_ELEMENT(bm, l);
559 CHECK_ELEMENT(bm, l->e);
560 CHECK_ELEMENT(bm, l->v);
563 if (f == bm->act_face) me->act_face = i;
565 /*copy over customdata*/
566 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
570 CHECK_ELEMENT(bm, f);
573 /* patch hook indices and vertex parents */
577 BMVert **vertMap = NULL;
580 for (ob=G.main->object.first; ob; ob=ob->id.next) {
581 if (ob->parent==ob && ELEM(ob->partype, PARVERT1,PARVERT3)) {
583 /* duplicate code from below, make it function later...? */
585 vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
587 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
588 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
589 if (*keyi != ORIGINDEX_NONE)
590 vertMap[*keyi] = eve;
593 if(ob->par1 < ototvert) {
594 eve = vertMap[ob->par1];
595 if(eve) ob->par1= BM_GetIndex(eve);
597 if(ob->par2 < ototvert) {
598 eve = vertMap[ob->par2];
599 if(eve) ob->par2= BM_GetIndex(eve);
601 if(ob->par3 < ototvert) {
602 eve = vertMap[ob->par3];
603 if(eve) ob->par3= BM_GetIndex(eve);
608 for (md=ob->modifiers.first; md; md=md->next) {
609 if (md->type==eModifierType_Hook) {
610 HookModifierData *hmd = (HookModifierData*) md;
613 vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
615 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
616 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
617 if (*keyi != ORIGINDEX_NONE)
618 vertMap[*keyi] = eve;
622 for (i=j=0; i<hmd->totindex; i++) {
623 if(hmd->indexar[i] < ototvert) {
624 eve = vertMap[hmd->indexar[i]];
627 hmd->indexar[j++] = BM_GetIndex(eve);
639 if (vertMap) MEM_freeN(vertMap);
642 mesh_update_customdata_pointers(me);
645 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
647 /*go through and find any shapekey customdata layers
648 that might not have corrusponding KeyBlocks, and add them if
651 for (i=0; i<bm->vdata.totlayer; i++) {
652 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
655 for (block=me->key->block.first; block; block=block->next) {
656 if (block->uid == bm->vdata.layers[i].uid)
661 block = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
662 block->type = KEY_LINEAR;
663 block->slidermin = 0.0f;
664 block->slidermax = 1.0f;
666 BLI_addtail(&me->key->block, block);
673 for (block=me->key->block.first; block; block=block->next) {
676 for (i=0; i<bm->vdata.totlayer; i++) {
677 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
680 if (block->uid == bm->vdata.layers[i].uid) {
684 MEM_freeN(block->data);
685 block->data = fp = MEM_mallocN(sizeof(float)*3*bm->totvert, "shape key data");
686 block->totelem = bm->totvert;
688 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
689 co = block==actkey ? eve->co : CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
700 /*if we didn't find a shapekey, tag the block to be reconstructed
701 via the old method below*/
702 if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
703 block->flag |= KEYBLOCK_MISSING;
708 /* old method of reconstructing keys via vertice's original key indices,
709 currently used if the new method above fails (which is theoretically
710 possible in certain cases of undo).*/
712 float *fp, *newkey, *oldkey;
714 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
716 /* Lets reorder the key data so that things line up roughly
717 * with the way things were before editmode */
718 currkey = me->key->block.first;
720 if (!(currkey->flag & KEYBLOCK_MISSING)) {
721 currkey = currkey->next;
725 printf("warning: had to hackishly reconstruct shape key \"%s\","
726 " it may not be correct anymore.\n", currkey->name);
728 currkey->flag &= ~KEYBLOCK_MISSING;
730 fp= newkey= MEM_callocN(me->key->elemsize*bm->totvert, "currkey->data");
731 oldkey = currkey->data;
733 eve= BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
738 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
739 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
740 if(currkey == actkey) {
741 if(actkey == me->key->refkey) {
742 VECCOPY(fp, mvert->co);
745 VECCOPY(fp, mvert->co);
747 VECCOPY(mvert->co, oldverts[*keyi].co);
753 VECCOPY(fp, oldkey + 3 * *keyi);
758 VECCOPY(fp, mvert->co);
763 eve= BMIter_Step(&iter);
765 currkey->totelem= bm->totvert;
766 if(currkey->data) MEM_freeN(currkey->data);
767 currkey->data = newkey;
769 currkey= currkey->next;
773 if(oldverts) MEM_freeN(oldverts);