PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/blender-2.63a/source/blender/bmesh/intern/bmesh_mesh_conv.c

#
C | 866 lines | 566 code | 158 blank | 142 comment | 161 complexity | 45d21e38dd8bb528f30d18a99ad11c98 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-3.0, BSD-2-Clause, Apache-2.0, AGPL-1.0
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Contributor(s): Geoffrey Bantle.
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/bmesh/intern/bmesh_mesh_conv.c
  23. * \ingroup bmesh
  24. *
  25. * BM mesh conversion functions.
  26. *
  27. * \section bm_mesh_conv_shapekey Converting Shape Keys
  28. *
  29. * When converting to/from a Mesh/BMesh you can optionally pass a shape key to edit.
  30. * This has the effect of editing the shape key-block rather then the original mesh vertex coords
  31. * (although additional geometry is still allowed and uses fallback locations on converting).
  32. *
  33. * While this works for any mesh/bmesh this is made use of by entering and exiting edit-mode.
  34. *
  35. * There are comments in code but this should help explain the general
  36. * intention as to how this works converting from/to bmesh.
  37. *
  38. *
  39. * \subsection user_pov User Perspective
  40. *
  41. * - Editmode operations when a shape key-block is active edits only that key-block.
  42. * - The first Basis key-block always matches the Mesh verts.
  43. * - Changing vertex locations of _any_ Basis will apply offsets to those shape keys using this as their Basis.
  44. *
  45. *
  46. * \subsection enter_editmode Entering EditMode - #BM_mesh_bm_from_me
  47. *
  48. * - the active key-block is used for BMesh vertex locations on entering edit-mode.
  49. * So obviously the meshes vertex locations remain unchanged and the shape key its self is not being edited directly.
  50. * Simply the #BMVert.co is a initialized from active shape key (when its set).
  51. * - all key-blocks are added as CustomData layers (read code for details).
  52. *
  53. *
  54. * \subsection exit_editmode Exiting EditMode - #BM_mesh_bm_to_me
  55. *
  56. * This is where the most confusing code is! Won't attempt to document the details here, for that read the code.
  57. * But basics are as follows.
  58. *
  59. * - Vertex locations (possibly modified from initial active key-block) are copied directly into #MVert.co
  60. * (special confusing note that these may be restored later, when editing the 'Basis', read on).
  61. * - if the 'Key' is relative, and the active key-block is the basis for ANY other key-blocks - get an array of offsets
  62. * between the new vertex locations and the original shape key (before entering edit-mode),
  63. * these offsets get applied later on to inactive key-blocks using the active one (which we are editing) as their Basis.
  64. *
  65. * Copying the locations back to the shape keys is quite confusing...
  66. * One main area of confusion is that when editing a 'Basis' key-block 'me->key->refkey'
  67. * The coords are written into the mesh, from the users perspective the Basis coords are written into the mesh
  68. * when exiting edit-mode.
  69. *
  70. * When _not_ editing the 'Basis', the original vertex locations (stored in the mesh and unchanged during edit-mode),
  71. * are copied back into the mesh.
  72. *
  73. * This has the effect from the users POV of leaving the mesh un-touched, and only editing the active shape key-block.
  74. *
  75. */
  76. #include "DNA_mesh_types.h"
  77. #include "DNA_meshdata_types.h"
  78. #include "DNA_object_types.h"
  79. #include "DNA_modifier_types.h"
  80. #include "DNA_key_types.h"
  81. #include "MEM_guardedalloc.h"
  82. #include "BLI_listbase.h"
  83. #include "BLI_array.h"
  84. #include "BLI_math_vector.h"
  85. #include "BKE_mesh.h"
  86. #include "BKE_customdata.h"
  87. #include "BKE_multires.h"
  88. #include "BKE_global.h" /* ugh - for looping over all objects */
  89. #include "BKE_main.h"
  90. #include "BKE_key.h"
  91. #include "bmesh.h"
  92. #include "intern/bmesh_private.h" /* for element checking */
  93. /* Mesh -> BMesh */
  94. void BM_mesh_bm_from_me(BMesh *bm, Mesh *me, int set_key, int act_key_nr)
  95. {
  96. MVert *mvert;
  97. BLI_array_declare(verts);
  98. MEdge *medge;
  99. MLoop *ml;
  100. MPoly *mpoly;
  101. KeyBlock *actkey, *block;
  102. BMVert *v, **vt = NULL, **verts = NULL;
  103. BMEdge *e, **fedges = NULL, **et = NULL;
  104. BMFace *f;
  105. BMLoop *l;
  106. BLI_array_declare(fedges);
  107. float (*keyco)[3] = NULL;
  108. int *keyi;
  109. int totuv, i, j;
  110. /* free custom data */
  111. /* this isnt needed in most cases but do just incase */
  112. CustomData_free(&bm->vdata, bm->totvert);
  113. CustomData_free(&bm->edata, bm->totedge);
  114. CustomData_free(&bm->ldata, bm->totloop);
  115. CustomData_free(&bm->pdata, bm->totface);
  116. if (!me || !me->totvert) {
  117. if (me) { /*no verts? still copy customdata layout*/
  118. CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_ASSIGN, 0);
  119. CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_ASSIGN, 0);
  120. CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_ASSIGN, 0);
  121. CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_ASSIGN, 0);
  122. CustomData_bmesh_init_pool(&bm->vdata, me->totvert, BM_VERT);
  123. CustomData_bmesh_init_pool(&bm->edata, me->totedge, BM_EDGE);
  124. CustomData_bmesh_init_pool(&bm->ldata, me->totloop, BM_LOOP);
  125. CustomData_bmesh_init_pool(&bm->pdata, me->totpoly, BM_FACE);
  126. }
  127. return; /* sanity check */
  128. }
  129. vt = MEM_mallocN(sizeof(void **) * me->totvert, "mesh to bmesh vtable");
  130. CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
  131. CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
  132. CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
  133. CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
  134. /* make sure uv layer names are consisten */
  135. totuv = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
  136. for (i = 0; i < totuv; i++) {
  137. int li = CustomData_get_layer_index_n(&bm->pdata, CD_MTEXPOLY, i);
  138. CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
  139. }
  140. if (!CustomData_has_layer(&bm->edata, CD_CREASE))
  141. CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
  142. if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
  143. CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
  144. if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
  145. CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
  146. if ((act_key_nr != 0) && (me->key != NULL)) {
  147. actkey = BLI_findlink(&me->key->block, act_key_nr - 1);
  148. }
  149. else {
  150. actkey = NULL;
  151. }
  152. if (me->key) {
  153. CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
  154. /* check if we need to generate unique ids for the shapekeys.
  155. * this also exists in the file reading code, but is here for
  156. * a sanity check */
  157. if (!me->key->uidgen) {
  158. fprintf(stderr,
  159. "%s had to generate shape key uid's in a situation we shouldn't need to! "
  160. "(bmesh internal error)\n",
  161. __func__);
  162. me->key->uidgen = 1;
  163. for (block = me->key->block.first; block; block = block->next) {
  164. block->uid = me->key->uidgen++;
  165. }
  166. }
  167. if (actkey && actkey->totelem == me->totvert) {
  168. keyco = actkey->data;
  169. bm->shapenr = act_key_nr;
  170. }
  171. for (i = 0, block = me->key->block.first; block; block = block->next, i++) {
  172. CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY,
  173. CD_ASSIGN, NULL, 0, block->name);
  174. j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
  175. bm->vdata.layers[j].uid = block->uid;
  176. }
  177. }
  178. CustomData_bmesh_init_pool(&bm->vdata, me->totvert, BM_VERT);
  179. CustomData_bmesh_init_pool(&bm->edata, me->totedge, BM_EDGE);
  180. CustomData_bmesh_init_pool(&bm->ldata, me->totloop, BM_LOOP);
  181. CustomData_bmesh_init_pool(&bm->pdata, me->totpoly, BM_FACE);
  182. for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) {
  183. v = BM_vert_create(bm, keyco && set_key ? keyco[i] : mvert->co, NULL);
  184. BM_elem_index_set(v, i); /* set_ok */
  185. vt[i] = v;
  186. /* transfer flag */
  187. v->head.hflag = BM_vert_flag_from_mflag(mvert->flag & ~SELECT);
  188. /* this is necessary for selection counts to work properly */
  189. if (mvert->flag & SELECT) {
  190. BM_vert_select_set(bm, v, TRUE);
  191. }
  192. normal_short_to_float_v3(v->no, mvert->no);
  193. BM_elem_float_data_set(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
  194. /* Copy Custom Dat */
  195. CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
  196. /* set shapekey data */
  197. if (me->key) {
  198. /* set shape key original index */
  199. keyi = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_SHAPE_KEYINDEX);
  200. if (keyi) {
  201. *keyi = i;
  202. }
  203. for (block = me->key->block.first, j = 0; block; block = block->next, j++) {
  204. float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, j);
  205. if (co) {
  206. copy_v3_v3(co, ((float *)block->data) + 3 * i);
  207. }
  208. }
  209. }
  210. }
  211. bm->elem_index_dirty &= ~BM_VERT; /* added in order, clear dirty flag */
  212. if (!me->totedge) {
  213. MEM_freeN(vt);
  214. return;
  215. }
  216. et = MEM_mallocN(sizeof(void **) * me->totedge, "mesh to bmesh etable");
  217. medge = me->medge;
  218. for (i = 0; i < me->totedge; i++, medge++) {
  219. e = BM_edge_create(bm, vt[medge->v1], vt[medge->v2], NULL, FALSE);
  220. BM_elem_index_set(e, i); /* set_ok */
  221. et[i] = e;
  222. /* transfer flags */
  223. e->head.hflag = BM_edge_flag_from_mflag(medge->flag & ~SELECT);
  224. /* this is necessary for selection counts to work properly */
  225. if (medge->flag & SELECT) {
  226. BM_edge_select_set(bm, e, TRUE);
  227. }
  228. /* Copy Custom Data */
  229. CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
  230. BM_elem_float_data_set(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
  231. BM_elem_float_data_set(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
  232. }
  233. bm->elem_index_dirty &= ~BM_EDGE; /* added in order, clear dirty flag */
  234. mpoly = me->mpoly;
  235. for (i = 0; i < me->totpoly; i++, mpoly++) {
  236. BMIter iter;
  237. BLI_array_empty(fedges);
  238. BLI_array_empty(verts);
  239. BLI_array_growitems(fedges, mpoly->totloop);
  240. BLI_array_growitems(verts, mpoly->totloop);
  241. for (j = 0; j < mpoly->totloop; j++) {
  242. ml = &me->mloop[mpoly->loopstart + j];
  243. v = vt[ml->v];
  244. e = et[ml->e];
  245. fedges[j] = e;
  246. verts[j] = v;
  247. }
  248. /* not sure what this block is supposed to do,
  249. * but its unused. so commenting - campbell */
  250. #if 0
  251. {
  252. BMVert *v1, *v2;
  253. v1 = vt[me->mloop[mpoly->loopstart].v];
  254. v2 = vt[me->mloop[mpoly->loopstart + 1].v];
  255. if (v1 == fedges[0]->v1) {
  256. v2 = fedges[0]->v2;
  257. }
  258. else {
  259. v1 = fedges[0]->v2;
  260. v2 = fedges[0]->v1;
  261. }
  262. }
  263. #endif
  264. f = BM_face_create(bm, verts, fedges, mpoly->totloop, FALSE);
  265. if (!f) {
  266. printf("%s: Warning! Bad face in mesh"
  267. " \"%s\" at index %d!, skipping\n",
  268. __func__, me->id.name + 2, i);
  269. continue;
  270. }
  271. /* don't use 'i' since we may have skipped the face */
  272. BM_elem_index_set(f, bm->totface - 1); /* set_ok */
  273. /* transfer flag */
  274. f->head.hflag = BM_face_flag_from_mflag(mpoly->flag & ~ME_FACE_SEL);
  275. /* this is necessary for selection counts to work properly */
  276. if (mpoly->flag & ME_FACE_SEL) {
  277. BM_face_select_set(bm, f, TRUE);
  278. }
  279. f->mat_nr = mpoly->mat_nr;
  280. if (i == me->act_face) bm->act_face = f;
  281. j = 0;
  282. BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, j) {
  283. /* Save index of correspsonding MLoop */
  284. BM_elem_index_set(l, mpoly->loopstart + j); /* set_loop */
  285. }
  286. /* Copy Custom Data */
  287. CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
  288. }
  289. bm->elem_index_dirty &= ~BM_FACE; /* added in order, clear dirty flag */
  290. {
  291. BMIter fiter;
  292. BMIter liter;
  293. /* Copy over loop CustomData. Doing this in a separate loop isn't necessary
  294. * but is an optimization, to avoid copying a bunch of interpolated customdata
  295. * for each BMLoop (from previous BMLoops using the same edge), always followed
  296. * by freeing the interpolated data and overwriting it with data from the Mesh. */
  297. BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
  298. BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
  299. int li = BM_elem_index_get(l);
  300. CustomData_to_bmesh_block(&me->ldata, &bm->ldata, li, &l->head.data);
  301. BM_elem_index_set(l, 0); /* set_loop */
  302. }
  303. }
  304. }
  305. if (me->mselect && me->totselect != 0) {
  306. BMVert **vert_array = MEM_callocN(sizeof(BMVert *) * bm->totvert,
  307. "Selection Conversion Vertex Pointer Array");
  308. BMEdge **edge_array = MEM_callocN(sizeof(BMEdge *) * bm->totedge,
  309. "Selection Conversion Edge Pointer Array");
  310. BMFace **face_array = MEM_callocN(sizeof(BMFace *) * bm->totface,
  311. "Selection Conversion Face Pointer Array");
  312. BMIter iter;
  313. BMVert *vert;
  314. BMEdge *edge;
  315. BMFace *face;
  316. MSelect *msel;
  317. BM_ITER_MESH_INDEX (vert, &iter, bm, BM_VERTS_OF_MESH, i) { vert_array[i] = vert; }
  318. BM_ITER_MESH_INDEX (edge, &iter, bm, BM_EDGES_OF_MESH, i) { edge_array[i] = edge; }
  319. BM_ITER_MESH_INDEX (face, &iter, bm, BM_FACES_OF_MESH, i) { face_array[i] = face; }
  320. for (i = 0, msel = me->mselect; i < me->totselect; i++, msel++) {
  321. switch (msel->type) {
  322. case ME_VSEL:
  323. BM_select_history_store(bm, (BMElem *)vert_array[msel->index]);
  324. break;
  325. case ME_ESEL:
  326. BM_select_history_store(bm, (BMElem *)edge_array[msel->index]);
  327. break;
  328. case ME_FSEL:
  329. BM_select_history_store(bm, (BMElem *)face_array[msel->index]);
  330. break;
  331. }
  332. }
  333. MEM_freeN(vert_array);
  334. MEM_freeN(edge_array);
  335. MEM_freeN(face_array);
  336. }
  337. else {
  338. me->totselect = 0;
  339. if (me->mselect) {
  340. MEM_freeN(me->mselect);
  341. me->mselect = NULL;
  342. }
  343. }
  344. BLI_array_free(fedges);
  345. BLI_array_free(verts);
  346. MEM_freeN(vt);
  347. MEM_freeN(et);
  348. }
  349. /* BMesh -> Mesh */
  350. static BMVert **bm_to_mesh_vertex_map(BMesh *bm, int ototvert)
  351. {
  352. BMVert **vertMap = NULL;
  353. BMVert *eve;
  354. int index;
  355. int i = 0;
  356. BMIter iter;
  357. /* caller needs to ensure this */
  358. BLI_assert(ototvert > 0);
  359. vertMap = MEM_callocN(sizeof(*vertMap) * ototvert, "vertMap");
  360. if (CustomData_has_layer(&bm->vdata, CD_SHAPE_KEYINDEX)) {
  361. int *keyi;
  362. BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
  363. keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
  364. if (keyi) {
  365. if (((index = *keyi) != ORIGINDEX_NONE) && (index < ototvert)) {
  366. vertMap[index] = eve;
  367. }
  368. }
  369. else {
  370. if (i < ototvert) {
  371. vertMap[i] = eve;
  372. }
  373. }
  374. i++;
  375. }
  376. }
  377. else {
  378. BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
  379. if (i < ototvert) {
  380. vertMap[i] = eve;
  381. }
  382. else {
  383. break;
  384. }
  385. i++;
  386. }
  387. }
  388. return vertMap;
  389. }
  390. /**
  391. * returns customdata shapekey index from a keyblock or -1
  392. * \note could split this out into a more generic function */
  393. static int bm_to_mesh_shape_layer_index_from_kb(BMesh *bm, KeyBlock *currkey)
  394. {
  395. int i;
  396. int j = 0;
  397. for (i = 0; i < bm->vdata.totlayer; i++) {
  398. if (bm->vdata.layers[i].type == CD_SHAPEKEY) {
  399. if (currkey->uid == bm->vdata.layers[i].uid) {
  400. return j;
  401. }
  402. j++;
  403. }
  404. }
  405. return -1;
  406. }
  407. BLI_INLINE void bmesh_quick_edgedraw_flag(MEdge *med, BMEdge *e)
  408. {
  409. /* this is a cheap way to set the edge draw, its not precise and will
  410. * pick the first 2 faces an edge uses.
  411. * The dot comparison is a little arbitrary, but set so that a 5 subd
  412. * IcoSphere won't vanish but subd 6 will (as with pre-bmesh blender) */
  413. if ( /* (med->flag & ME_EDGEDRAW) && */ /* assume to be true */
  414. (e->l && (e->l != e->l->radial_next)) &&
  415. (dot_v3v3(e->l->f->no, e->l->radial_next->f->no) > 0.9995f))
  416. {
  417. med->flag &= ~ME_EDGEDRAW;
  418. }
  419. }
  420. void BM_mesh_bm_to_me(BMesh *bm, Mesh *me, int dotess)
  421. {
  422. MLoop *mloop;
  423. MPoly *mpoly;
  424. MVert *mvert, *oldverts;
  425. MEdge *med, *medge;
  426. BMVert *v, *eve;
  427. BMEdge *e;
  428. BMLoop *l;
  429. BMFace *f;
  430. BMIter iter, liter;
  431. int i, j, ototvert;
  432. ototvert = me->totvert;
  433. /* new vertex block */
  434. if (bm->totvert == 0) mvert = NULL;
  435. else mvert = MEM_callocN(bm->totvert * sizeof(MVert), "loadeditbMesh vert");
  436. /* new edge block */
  437. if (bm->totedge == 0) medge = NULL;
  438. else medge = MEM_callocN(bm->totedge * sizeof(MEdge), "loadeditbMesh edge");
  439. /* new ngon face block */
  440. if (bm->totface == 0) mpoly = NULL;
  441. else mpoly = MEM_callocN(bm->totface * sizeof(MPoly), "loadeditbMesh poly");
  442. /* new loop block */
  443. if (bm->totloop == 0) mloop = NULL;
  444. else mloop = MEM_callocN(bm->totloop * sizeof(MLoop), "loadeditbMesh loop");
  445. /* lets save the old verts just in case we are actually working on
  446. * a key ... we now do processing of the keys at the end */
  447. oldverts = me->mvert;
  448. /* don't free this yet */
  449. CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
  450. /* free custom data */
  451. CustomData_free(&me->vdata, me->totvert);
  452. CustomData_free(&me->edata, me->totedge);
  453. CustomData_free(&me->fdata, me->totface);
  454. CustomData_free(&me->ldata, me->totloop);
  455. CustomData_free(&me->pdata, me->totpoly);
  456. /* add new custom data */
  457. me->totvert = bm->totvert;
  458. me->totedge = bm->totedge;
  459. me->totloop = bm->totloop;
  460. me->totpoly = bm->totface;
  461. /* will be overwritten with a valid value if 'dotess' is set, otherwise we
  462. * end up with 'me->totface' and me->mface == NULL which can crash [#28625]
  463. */
  464. me->totface = 0;
  465. CustomData_copy(&bm->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
  466. CustomData_copy(&bm->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
  467. CustomData_copy(&bm->ldata, &me->ldata, CD_MASK_MESH, CD_CALLOC, me->totloop);
  468. CustomData_copy(&bm->pdata, &me->pdata, CD_MASK_MESH, CD_CALLOC, me->totpoly);
  469. CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
  470. CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
  471. CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
  472. CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
  473. /* this is called again, 'dotess' arg is used there */
  474. mesh_update_customdata_pointers(me, 0);
  475. i = 0;
  476. BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
  477. float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
  478. mvert->bweight = bweight ? (char)((*bweight) * 255) : 0;
  479. copy_v3_v3(mvert->co, v->co);
  480. normal_float_to_short_v3(mvert->no, v->no);
  481. mvert->flag = BM_vert_flag_to_mflag(v);
  482. BM_elem_index_set(v, i); /* set_inline */
  483. /* copy over customdat */
  484. CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
  485. i++;
  486. mvert++;
  487. BM_CHECK_ELEMENT(v);
  488. }
  489. bm->elem_index_dirty &= ~BM_VERT;
  490. med = medge;
  491. i = 0;
  492. BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
  493. float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
  494. float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
  495. med->v1 = BM_elem_index_get(e->v1);
  496. med->v2 = BM_elem_index_get(e->v2);
  497. med->crease = crease ? (char)((*crease) * 255) : 0;
  498. med->bweight = bweight ? (char)((*bweight) * 255) : 0;
  499. med->flag = BM_edge_flag_to_mflag(e);
  500. BM_elem_index_set(e, i); /* set_inline */
  501. /* copy over customdata */
  502. CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
  503. bmesh_quick_edgedraw_flag(med, e);
  504. i++;
  505. med++;
  506. BM_CHECK_ELEMENT(e);
  507. }
  508. bm->elem_index_dirty &= ~BM_EDGE;
  509. i = 0;
  510. j = 0;
  511. BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
  512. mpoly->loopstart = j;
  513. mpoly->totloop = f->len;
  514. mpoly->mat_nr = f->mat_nr;
  515. mpoly->flag = BM_face_flag_to_mflag(f);
  516. l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, f);
  517. for ( ; l; l = BM_iter_step(&liter), j++, mloop++) {
  518. mloop->e = BM_elem_index_get(l->e);
  519. mloop->v = BM_elem_index_get(l->v);
  520. /* copy over customdat */
  521. CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
  522. BM_CHECK_ELEMENT(l);
  523. BM_CHECK_ELEMENT(l->e);
  524. BM_CHECK_ELEMENT(l->v);
  525. }
  526. if (f == bm->act_face) me->act_face = i;
  527. /* copy over customdata */
  528. CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
  529. i++;
  530. mpoly++;
  531. BM_CHECK_ELEMENT(f);
  532. }
  533. /* patch hook indices and vertex parents */
  534. if (ototvert > 0) {
  535. Object *ob;
  536. ModifierData *md;
  537. BMVert **vertMap = NULL;
  538. int i, j;
  539. for (ob = G.main->object.first; ob; ob = ob->id.next) {
  540. if ((ob->parent) && (ob->parent->data == me) && ELEM(ob->partype, PARVERT1, PARVERT3)) {
  541. if (vertMap == NULL) {
  542. vertMap = bm_to_mesh_vertex_map(bm, ototvert);
  543. }
  544. if (ob->par1 < ototvert) {
  545. eve = vertMap[ob->par1];
  546. if (eve) ob->par1 = BM_elem_index_get(eve);
  547. }
  548. if (ob->par2 < ototvert) {
  549. eve = vertMap[ob->par2];
  550. if (eve) ob->par2 = BM_elem_index_get(eve);
  551. }
  552. if (ob->par3 < ototvert) {
  553. eve = vertMap[ob->par3];
  554. if (eve) ob->par3 = BM_elem_index_get(eve);
  555. }
  556. }
  557. if (ob->data == me) {
  558. for (md = ob->modifiers.first; md; md = md->next) {
  559. if (md->type == eModifierType_Hook) {
  560. HookModifierData *hmd = (HookModifierData *) md;
  561. if (vertMap == NULL) {
  562. vertMap = bm_to_mesh_vertex_map(bm, ototvert);
  563. }
  564. for (i = j = 0; i < hmd->totindex; i++) {
  565. if (hmd->indexar[i] < ototvert) {
  566. eve = vertMap[hmd->indexar[i]];
  567. if (eve) {
  568. hmd->indexar[j++] = BM_elem_index_get(eve);
  569. }
  570. }
  571. else j++;
  572. }
  573. hmd->totindex = j;
  574. }
  575. }
  576. }
  577. }
  578. if (vertMap) MEM_freeN(vertMap);
  579. }
  580. if (dotess) {
  581. BKE_mesh_tessface_calc(me);
  582. }
  583. mesh_update_customdata_pointers(me, dotess);
  584. {
  585. BMEditSelection *selected;
  586. me->totselect = BLI_countlist(&(bm->selected));
  587. if (me->mselect) MEM_freeN(me->mselect);
  588. me->mselect = MEM_callocN(sizeof(MSelect) * me->totselect, "Mesh selection history");
  589. for (i = 0, selected = bm->selected.first; selected; i++, selected = selected->next) {
  590. if (selected->htype == BM_VERT) {
  591. me->mselect[i].type = ME_VSEL;
  592. }
  593. else if (selected->htype == BM_EDGE) {
  594. me->mselect[i].type = ME_ESEL;
  595. }
  596. else if (selected->htype == BM_FACE) {
  597. me->mselect[i].type = ME_FSEL;
  598. }
  599. me->mselect[i].index = BM_elem_index_get(selected->ele);
  600. }
  601. }
  602. /* see comment below, this logic is in twice */
  603. if (me->key) {
  604. KeyBlock *currkey;
  605. KeyBlock *actkey = BLI_findlink(&me->key->block, bm->shapenr - 1);
  606. float (*ofs)[3] = NULL;
  607. /* go through and find any shapekey customdata layers
  608. * that might not have corresponding KeyBlocks, and add them if
  609. * necessary */
  610. j = 0;
  611. for (i = 0; i < bm->vdata.totlayer; i++) {
  612. if (bm->vdata.layers[i].type != CD_SHAPEKEY)
  613. continue;
  614. for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
  615. if (currkey->uid == bm->vdata.layers[i].uid)
  616. break;
  617. }
  618. if (!currkey) {
  619. currkey = add_keyblock(me->key, bm->vdata.layers[i].name);
  620. currkey->uid = bm->vdata.layers[i].uid;
  621. }
  622. j++;
  623. }
  624. /* editing the base key should update others */
  625. if ((me->key->type == KEY_RELATIVE) && /* only need offsets for relative shape keys */
  626. (actkey != NULL) && /* unlikely, but the active key may not be valid if the
  627. * bmesh and the mesh are out of sync */
  628. (oldverts != NULL)) /* not used here, but 'oldverts' is used later for applying 'ofs' */
  629. {
  630. int act_is_basis = FALSE;
  631. /* find if this key is a basis for any others */
  632. for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
  633. if (bm->shapenr - 1 == currkey->relative) {
  634. act_is_basis = TRUE;
  635. break;
  636. }
  637. }
  638. if (act_is_basis) { /* active key is a base */
  639. float (*fp)[3] = actkey->data;
  640. int *keyi;
  641. ofs = MEM_callocN(sizeof(float) * 3 * bm->totvert, "currkey->data");
  642. mvert = me->mvert;
  643. BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
  644. keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
  645. if (keyi && *keyi != ORIGINDEX_NONE) {
  646. sub_v3_v3v3(ofs[i], mvert->co, fp[*keyi]);
  647. }
  648. mvert++;
  649. }
  650. }
  651. }
  652. for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
  653. int apply_offset = (ofs && (currkey != actkey) && (bm->shapenr - 1 == currkey->relative));
  654. int *keyi;
  655. float (*ofs_pt)[3] = ofs;
  656. float *newkey, *oldkey, *fp;
  657. j = bm_to_mesh_shape_layer_index_from_kb(bm, currkey);
  658. fp = newkey = MEM_callocN(me->key->elemsize * bm->totvert, "currkey->data");
  659. oldkey = currkey->data;
  660. mvert = me->mvert;
  661. BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
  662. if (currkey == actkey) {
  663. copy_v3_v3(fp, eve->co);
  664. if (actkey != me->key->refkey) { /* important see bug [#30771] */
  665. if (oldverts) {
  666. keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
  667. if (*keyi != ORIGINDEX_NONE && *keyi < currkey->totelem) { /* valid old vertex */
  668. copy_v3_v3(mvert->co, oldverts[*keyi].co);
  669. }
  670. }
  671. }
  672. }
  673. else if (j != -1) {
  674. /* in most cases this runs */
  675. copy_v3_v3(fp, CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j));
  676. }
  677. else if (oldkey &&
  678. (keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX)) &&
  679. (*keyi != ORIGINDEX_NONE && *keyi < currkey->totelem))
  680. {
  681. /* old method of reconstructing keys via vertice's original key indices,
  682. * currently used if the new method above fails (which is theoretically
  683. * possible in certain cases of undo) */
  684. copy_v3_v3(fp, &oldkey[3 * (*keyi)]);
  685. }
  686. else {
  687. /* fail! fill in with dummy value */
  688. copy_v3_v3(fp, mvert->co);
  689. }
  690. /* propagate edited basis offsets to other shapes */
  691. if (apply_offset) {
  692. add_v3_v3(fp, *ofs_pt++);
  693. }
  694. fp += 3;
  695. mvert++;
  696. }
  697. currkey->totelem = bm->totvert;
  698. if (currkey->data) {
  699. MEM_freeN(currkey->data);
  700. }
  701. currkey->data = newkey;
  702. }
  703. if (ofs) MEM_freeN(ofs);
  704. }
  705. if (oldverts) MEM_freeN(oldverts);
  706. /* topology could be changed, ensure mdisps are ok */
  707. multires_topology_changed(me);
  708. }