[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [44797] trunk/blender/source/blender: bmesh:

Campbell Barton ideasman42 at gmail.com
Sat Mar 10 21:41:26 CET 2012


Revision: 44797
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=44797
Author:   campbellbarton
Date:     2012-03-10 20:41:19 +0000 (Sat, 10 Mar 2012)
Log Message:
-----------
bmesh:
- moved mesh conversion functions into their own file.

bmesh py api:
- can now create a new empty bmesh without first creating mesh data.
- added function to copy bmesh data back to a mesh.
- bmesh.from_mesh() can now get a mesh which isnt in editmode.

Modified Paths:
--------------
    trunk/blender/source/blender/bmesh/CMakeLists.txt
    trunk/blender/source/blender/bmesh/bmesh.h
    trunk/blender/source/blender/bmesh/operators/bmo_mesh_conv.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_api.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_types.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_types.h

Added Paths:
-----------
    trunk/blender/source/blender/bmesh/intern/bmesh_mesh_conv.c
    trunk/blender/source/blender/bmesh/intern/bmesh_mesh_conv.h

Modified: trunk/blender/source/blender/bmesh/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/bmesh/CMakeLists.txt	2012-03-10 20:30:05 UTC (rev 44796)
+++ trunk/blender/source/blender/bmesh/CMakeLists.txt	2012-03-10 20:41:19 UTC (rev 44797)
@@ -65,6 +65,8 @@
 	intern/bmesh_marking.h
 	intern/bmesh_mesh.c
 	intern/bmesh_mesh.h
+	intern/bmesh_mesh_conv.c
+	intern/bmesh_mesh_conv.h	
 	intern/bmesh_mods.c
 	intern/bmesh_mods.h
 	intern/bmesh_opdefines.c

Modified: trunk/blender/source/blender/bmesh/bmesh.h
===================================================================
--- trunk/blender/source/blender/bmesh/bmesh.h	2012-03-10 20:30:05 UTC (rev 44796)
+++ trunk/blender/source/blender/bmesh/bmesh.h	2012-03-10 20:41:19 UTC (rev 44797)
@@ -286,6 +286,7 @@
 #include "intern/bmesh_iterators.h"
 #include "intern/bmesh_marking.h"
 #include "intern/bmesh_mesh.h"
+#include "intern/bmesh_mesh_conv.h"
 #include "intern/bmesh_mods.h"
 #include "intern/bmesh_operators.h"
 #include "intern/bmesh_polygon.h"

Added: trunk/blender/source/blender/bmesh/intern/bmesh_mesh_conv.c
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_mesh_conv.c	                        (rev 0)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_mesh_conv.c	2012-03-10 20:41:19 UTC (rev 44797)
@@ -0,0 +1,901 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Geoffrey Bantle.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/bmesh/intern/bmesh_mesh_conv.c
+ *  \ingroup bmesh
+ *
+ * BM mesh conversion functions.
+ */
+
+
+#include "bmesh.h"
+#include "intern/bmesh_private.h" /* for element checking */
+
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+#include "DNA_object_types.h"
+#include "DNA_modifier_types.h"
+#include "DNA_key_types.h"
+
+#include "BLI_listbase.h"
+#include "BLI_array.h"
+#include "BLI_math_vector.h"
+
+#include "BKE_mesh.h"
+#include "BKE_customdata.h"
+
+#include "BKE_global.h" /* ugh - for looping over all objects */
+#include "BKE_main.h"
+
+#include "MEM_guardedalloc.h"
+
+/* Mesh -> BMesh */
+void BM_mesh_to_bmesh(BMesh *bm, Mesh *me, int set_key, int act_key_nr)
+{
+	MVert *mvert;
+	BLI_array_declare(verts);
+	MEdge *medge;
+	MLoop *ml;
+	MPoly *mpoly;
+	KeyBlock *actkey, *block;
+	BMVert *v, **vt = NULL, **verts = NULL;
+	BMEdge *e, **fedges = NULL, **et = NULL;
+	BMFace *f;
+	BMLoop *l;
+	BLI_array_declare(fedges);
+	float (*keyco)[3] = NULL;
+	int *keyi;
+	int totuv, i, j;
+
+	if (!me || !me->totvert) {
+		return; /* sanity check */
+	}
+
+	vt = MEM_mallocN(sizeof(void **) * me->totvert, "mesh to bmesh vtable");
+
+	CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
+	CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
+	CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
+	CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
+
+	/* make sure uv layer names are consisten */
+	totuv = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
+	for (i = 0; i < totuv; i++) {
+		int li = CustomData_get_layer_index_n(&bm->pdata, CD_MTEXPOLY, i);
+		CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
+	}
+
+	if (!CustomData_has_layer(&bm->edata, CD_CREASE))
+		CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
+
+	if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
+		CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
+
+	if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
+		CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
+
+	if ((act_key_nr != 0) && (me->key != NULL)) {
+		actkey = BLI_findlink(&me->key->block, act_key_nr - 1);
+	}
+	else {
+		actkey = NULL;
+	}
+
+	if (actkey && actkey->totelem == me->totvert) {
+		CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
+
+		/* check if we need to generate unique ids for the shapekeys.
+		 * this also exists in the file reading code, but is here for
+		 * a sanity check */
+		if (!me->key->uidgen) {
+			fprintf(stderr,
+			        "%s had to generate shape key uid's in a situation we shouldn't need to! "
+			        "(bmesh internal error)\n",
+			        __func__);
+
+			me->key->uidgen = 1;
+			for (block = me->key->block.first; block; block = block->next) {
+				block->uid = me->key->uidgen++;
+			}
+		}
+
+		keyco = actkey->data;
+		bm->shapenr = act_key_nr;
+		for (i = 0, block = me->key->block.first; block; block = block->next, i++) {
+			CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY,
+			                           CD_ASSIGN, NULL, 0, block->name);
+
+			j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
+			bm->vdata.layers[j].uid = block->uid;
+		}
+	}
+	else if (actkey) {
+		printf("shapekey <-> mesh mismatch!\n");
+	}
+
+	CustomData_bmesh_init_pool(&bm->vdata, me->totvert, BM_VERT);
+	CustomData_bmesh_init_pool(&bm->edata, me->totedge, BM_EDGE);
+	CustomData_bmesh_init_pool(&bm->ldata, me->totloop, BM_LOOP);
+	CustomData_bmesh_init_pool(&bm->pdata, me->totpoly, BM_FACE);
+
+	for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) {
+		v = BM_vert_create(bm, keyco && set_key ? keyco[i] : mvert->co, NULL);
+		BM_elem_index_set(v, i); /* set_ok */
+		vt[i] = v;
+
+		/* transfer flag */
+		v->head.hflag = BM_vert_flag_from_mflag(mvert->flag);
+
+		/* this is necessary for selection counts to work properly */
+		if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
+			BM_vert_select_set(bm, v, TRUE);
+		}
+
+		normal_short_to_float_v3(v->no, mvert->no);
+
+		BM_elem_float_data_set(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
+
+		/* Copy Custom Dat */
+		CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
+
+		/* set shapekey data */
+		if (me->key) {
+			/* set shape key original index */
+			keyi = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_SHAPE_KEYINDEX);
+			if (keyi) {
+				*keyi = i;
+			}
+
+			for (block = me->key->block.first, j = 0; block; block = block->next, j++) {
+				float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, j);
+
+				if (co) {
+					copy_v3_v3(co, ((float *)block->data) + 3 * i);
+				}
+			}
+		}
+	}
+
+	bm->elem_index_dirty &= ~BM_VERT; /* added in order, clear dirty flag */
+
+	if (!me->totedge) {
+		MEM_freeN(vt);
+		return;
+	}
+
+	et = MEM_mallocN(sizeof(void **) * me->totedge, "mesh to bmesh etable");
+
+	medge = me->medge;
+	for (i = 0; i < me->totedge; i++, medge++) {
+		e = BM_edge_create(bm, vt[medge->v1], vt[medge->v2], NULL, FALSE);
+		BM_elem_index_set(e, i); /* set_ok */
+		et[i] = e;
+
+		/* transfer flags */
+		e->head.hflag = BM_edge_flag_from_mflag(medge->flag);
+
+		/* this is necessary for selection counts to work properly */
+		if (BM_elem_flag_test(e, BM_ELEM_SELECT)) BM_elem_select_set(bm, e, TRUE);
+
+		/* Copy Custom Data */
+		CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
+
+		BM_elem_float_data_set(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
+		BM_elem_float_data_set(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
+	}
+
+	bm->elem_index_dirty &= ~BM_EDGE; /* added in order, clear dirty flag */
+
+	mpoly = me->mpoly;
+	for (i = 0; i < me->totpoly; i++, mpoly++) {
+		BMIter iter;
+
+		BLI_array_empty(fedges);
+		BLI_array_empty(verts);
+
+		BLI_array_growitems(fedges, mpoly->totloop);
+		BLI_array_growitems(verts, mpoly->totloop);
+
+		for (j = 0; j < mpoly->totloop; j++) {
+			ml = &me->mloop[mpoly->loopstart + j];
+			v = vt[ml->v];
+			e = et[ml->e];
+
+			fedges[j] = e;
+			verts[j] = v;
+		}
+
+		/* not sure what this block is supposed to do,
+		 * but its unused. so commenting - campbell */
+#if 0
+		{
+			BMVert *v1, *v2;
+			v1 = vt[me->mloop[mpoly->loopstart].v];
+			v2 = vt[me->mloop[mpoly->loopstart + 1].v];
+
+			if (v1 == fedges[0]->v1) {
+				v2 = fedges[0]->v2;
+			}
+			else {
+				v1 = fedges[0]->v2;
+				v2 = fedges[0]->v1;
+			}
+		}
+#endif
+
+		f = BM_face_create(bm, verts, fedges, mpoly->totloop, FALSE);
+
+		if (!f) {
+			printf("%s: Warning! Bad face in mesh"
+			       " \"%s\" at index %d!, skipping\n",
+			       __func__, me->id.name + 2, i);
+			continue;
+		}
+
+		/* dont use 'i' since we may have skipped the face */
+		BM_elem_index_set(f, bm->totface - 1); /* set_ok */
+
+		/* transfer flag */
+		f->head.hflag = BM_face_flag_from_mflag(mpoly->flag);
+
+		/* this is necessary for selection counts to work properly */
+		if (BM_elem_flag_test(f, BM_ELEM_SELECT)) BM_elem_select_set(bm, f, TRUE);
+
+		f->mat_nr = mpoly->mat_nr;
+		if (i == me->act_face) bm->act_face = f;
+
+		j = 0;
+		BM_ITER_INDEX(l, &iter, bm, BM_LOOPS_OF_FACE, f, j) {
+			/* Save index of correspsonding MLoop */
+			BM_elem_index_set(l, mpoly->loopstart + j); /* set_loop */
+		}
+
+		/* Copy Custom Data */
+		CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
+	}
+
+	bm->elem_index_dirty &= ~BM_FACE; /* added in order, clear dirty flag */
+
+	{
+		BMIter fiter;
+		BMIter liter;
+
+		/* Copy over loop CustomData. Doing this in a separate loop isn't necessary
+		 * but is an optimization, to avoid copying a bunch of interpolated customdata
+		 * for each BMLoop (from previous BMLoops using the same edge), always followed
+		 * by freeing the interpolated data and overwriting it with data from the Mesh. */
+		BM_ITER(f, &fiter, bm, BM_FACES_OF_MESH, NULL) {
+			BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
+				int li = BM_elem_index_get(l);
+				CustomData_to_bmesh_block(&me->ldata, &bm->ldata, li, &l->head.data);
+				BM_elem_index_set(l, 0); /* set_loop */
+			}
+		}
+	}
+
+	if (me->mselect && me->totselect != 0) {
+		BMIter iter;
+		BMVert *vertex;
+		BMEdge *edge;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list