py/bmesh api - add support for single item buffers (odd feature but used quite a bit with bmesh operators).

also add utility functions BMO_slot_buffer_from_single(), BMO_slot_buffer_get_single()
This commit is contained in:
2012-11-27 09:21:57 +00:00
parent 0cd26e6066
commit a9855c227e
8 changed files with 140 additions and 96 deletions

View File

@@ -1546,7 +1546,7 @@ static BMOpDefine bmo_slide_vert_def = {
"slide_vert",
/* slots_in */
{{"vert", BMO_OP_SLOT_ELEMENT_BUF, {BM_VERT | BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE}},
{"edge", BMO_OP_SLOT_ELEMENT_BUF, {BM_EDGE | BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE}},
{"edges", BMO_OP_SLOT_ELEMENT_BUF, {BM_EDGE}},
{"distance_t", BMO_OP_SLOT_FLT},
{{'\0'}},
},

View File

@@ -417,6 +417,10 @@ void BMO_slot_buffer_from_disabled_hflag(BMesh *bm, BMOperator *op,
BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name,
const char htype, const char hflag);
void BMO_slot_buffer_from_single(BMOperator *op, BMOpSlot *slot, BMHeader *ele);
void *BMO_slot_buffer_get_single(BMOpSlot *slot);
/* counts number of elements inside a slot array. */
int BMO_slot_buffer_count(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name);
int BMO_slot_map_count(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name);

View File

@@ -867,6 +867,29 @@ void BMO_slot_buffer_from_disabled_hflag(BMesh *bm, BMOperator *op,
bmo_slot_buffer_from_hflag(bm, op, slot_args, slot_name, htype, hflag, FALSE);
}
void BMO_slot_buffer_from_single(BMOperator *op, BMOpSlot *slot, BMHeader *ele)
{
BMO_ASSERT_SLOT_IN_OP(slot, op);
BLI_assert(slot->slot_type == BMO_OP_SLOT_ELEMENT_BUF);
BLI_assert(slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE);
BLI_assert(slot->len == 0 || slot->len == 1);
BLI_assert(slot->slot_subtype.elem & ele->htype);
slot->data.buf = BLI_memarena_alloc(op->arena, sizeof(void *) * 4); /* XXX, why 'x4' ? */
slot->len = 1;
*slot->data.buf = ele;
}
void *BMO_slot_buffer_get_single(BMOpSlot *slot)
{
BLI_assert(slot->slot_type == BMO_OP_SLOT_ELEMENT_BUF);
BLI_assert(slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE);
BLI_assert(slot->len == 0 || slot->len == 1);
return slot->len ? (BMHeader *)slot->data.buf[0] : NULL;
}
/**
* Copies the values from another slot to the end of the output slot.
*/
@@ -1580,12 +1603,11 @@ int BMO_op_vinitf(BMesh *bm, BMOperator *op, const int flag, const char *_fmt, v
}
case 'e':
{
/* XXX we have 'e' but no equivalent for verts/faces - why? we could use (V/E/P)*/
BMHeader *ele = va_arg(vlist, void *);
BMOpSlot *slot = BMO_slot_get(op->slots_in, slot_name);
slot->data.buf = BLI_memarena_alloc(op->arena, sizeof(void *) * 4);
slot->len = 1;
*slot->data.buf = ele;
BMO_slot_buffer_from_single(op, slot, ele);
state = 1;
break;

View File

@@ -263,7 +263,7 @@ void bmo_pointmerge_facedata_exec(BMesh *bm, BMOperator *op)
float fac;
int i, tot;
snapv = BMO_iter_new(&siter, op->slots_in, "snapv", BM_VERT);
snapv = BMO_slot_buffer_get_single(BMO_slot_get(op->slots_in, "snapv"));
tot = BM_vert_face_count(snapv);
if (!tot)

View File

@@ -56,8 +56,7 @@ void bmo_slide_vert_exec(BMesh *bm, BMOperator *op)
const float distance_t = BMO_slot_float_get(op->slots_in, "distance_t");
/* Get start vertex */
vertex = BMO_iter_new(&oiter, op->slots_in, "vert", BM_VERT);
vertex = BMO_slot_buffer_get_single(BMO_slot_get(op->slots_in, "vert"));
if (!vertex) {
if (G.debug & G_DEBUG) {
@@ -67,15 +66,13 @@ void bmo_slide_vert_exec(BMesh *bm, BMOperator *op)
return;
}
/* BMESH_TODO - this is odd, it only uses one edge, why take a list at all? */
/* Count selected edges */
BMO_ITER (h, &oiter, op->slots_in, "edge", BM_VERT | BM_EDGE) {
switch (h->htype) {
case BM_EDGE:
selected_edges++;
/* Mark all selected edges (cast BMHeader->BMEdge) */
BMO_elem_flag_enable(bm, (BMElemF *)h, EDGE_MARK);
break;
}
BMO_ITER (h, &oiter, op->slots_in, "edges", BM_EDGE) {
selected_edges++;
/* Mark all selected edges (cast BMHeader->BMEdge) */
BMO_elem_flag_enable(bm, (BMElemF *)h, EDGE_MARK);
break;
}
/* Only allow sliding if an edge is selected */

View File

@@ -701,9 +701,8 @@ typedef struct ElemNode {
void bmo_shortest_path_exec(BMesh *bm, BMOperator *op)
{
BMOIter vs_iter /* , vs2_iter */; /* selected verts iterator */
BMIter v_iter; /* mesh verts iterator */
BMVert *vs, *sv, *ev; /* starting vertex, ending vertex */
BMVert *sv, *ev; /* starting vertex, ending vertex */
BMVert *v; /* mesh vertex */
Heap *h = NULL;
@@ -712,13 +711,8 @@ void bmo_shortest_path_exec(BMesh *bm, BMOperator *op)
int num_total = 0 /*, num_sels = 0 */, i = 0;
const int type = BMO_slot_int_get(op->slots_in, "type");
/* BMESH_TODO use BMO_slot_buffer_elem_first here? */
BMO_ITER (vs, &vs_iter, op->slots_in, "startv", BM_VERT) {
sv = vs;
}
BMO_ITER (vs, &vs_iter, op->slots_in, "endv", BM_VERT) {
ev = vs;
}
sv = BMO_slot_buffer_get_single(BMO_slot_get(op->slots_in, "startv"));
ev = BMO_slot_buffer_get_single(BMO_slot_get(op->slots_in, "endv"));
num_total = BM_mesh_elem_count(bm, BM_VERT);

View File

@@ -729,7 +729,7 @@ static int edbm_vertex_slide_exec_ex(bContext *C, wmOperator *op, const int do_u
/* Prepare operator */
if (!EDBM_op_init(em, &bmop, op,
"slide_vert vert=%e edge=%hev distance_t=%f",
"slide_vert vert=%e edges=%he distance_t=%f",
start_vert, BM_ELEM_SELECT, distance_t))
{
return OPERATOR_CANCELLED;
@@ -738,10 +738,10 @@ static int edbm_vertex_slide_exec_ex(bContext *C, wmOperator *op, const int do_u
BMO_op_exec(bm, &bmop);
/* Deselect the input edges */
BMO_slot_buffer_hflag_disable(bm, bmop.slots_in, "edge", BM_ALL_NOLOOP, BM_ELEM_SELECT, TRUE);
BMO_slot_buffer_hflag_disable(bm, bmop.slots_in, "edges", BM_EDGE, BM_ELEM_SELECT, TRUE);
/* Select the output vert */
BMO_slot_buffer_hflag_enable(bm, bmop.slots_out, "verts.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, TRUE);
BMO_slot_buffer_hflag_enable(bm, bmop.slots_out, "verts.out", BM_VERT, BM_ELEM_SELECT, TRUE);
/* Flush the select buffers */
EDBM_selectmode_flush(em);

View File

@@ -224,15 +224,36 @@ static PyObject *pyrna_op_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *
}
case BMO_OP_SLOT_ELEMENT_BUF:
{
/* there are many ways we could interpret arguments, for now...
* - verts/edges/faces from the mesh direct,
* this way the operator takes every item.
* - `TODO` a plain python sequence (list) of elements.
* - `TODO` an iterator. eg.
* face.verts
* - `TODO` (type, flag) pair, eg.
* ('VERT', {'TAG'})
*/
if (slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE) {
if (!BPy_BMElem_Check(value) ||
!(((BPy_BMElem *)value)->ele->head.htype & slot->slot_subtype.elem))
{
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected a %.200s not *.200s",
self->opname, slot_name,
BPy_BMElem_StringFromHType(slot->slot_subtype.elem & BM_ALL_NOLOOP),
Py_TYPE(value)->tp_name);
return NULL;
}
else if (((BPy_BMElem *)value)->bm == NULL) {
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" invalidated element",
self->opname, slot_name);
return NULL;
}
BMO_slot_buffer_from_single(&bmop, slot, &((BPy_BMElem *)value)->ele->head);
}
else {
/* there are many ways we could interpret arguments, for now...
* - verts/edges/faces from the mesh direct,
* this way the operator takes every item.
* - `TODO` a plain python sequence (list) of elements.
* - `TODO` an iterator. eg.
* face.verts
* - `TODO` (type, flag) pair, eg.
* ('VERT', {'TAG'})
*/
#define BPY_BM_GENERIC_MESH_TEST(type_string) \
if (((BPy_BMGeneric *)value)->bm != bm) { \
@@ -248,76 +269,76 @@ static PyObject *pyrna_op_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *
"%.200s: keyword \"%.200s\" expected " \
"a list of %.200s not " type_string, \
self->opname, slot_name, \
BPy_BMElem_StringFromHType(slot->slot_subtype.elem & BM_ALL)); \
BPy_BMElem_StringFromHType(slot->slot_subtype.elem & BM_ALL_NOLOOP)); \
return NULL; \
} (void)0
if (BPy_BMVertSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("verts");
BPY_BM_ELEM_TYPE_TEST("verts");
if (BPy_BMVertSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("verts");
BPY_BM_ELEM_TYPE_TEST("verts");
BMO_slot_buffer_from_all(bm, &bmop, bmop.slots_in, slot_name, BM_VERT);
}
else if (BPy_BMEdgeSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("edges");
BPY_BM_ELEM_TYPE_TEST("edges");
BMO_slot_buffer_from_all(bm, &bmop, bmop.slots_in, slot_name, BM_EDGE);
}
else if (BPy_BMFaceSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("faces");
BPY_BM_ELEM_TYPE_TEST("faces");
BMO_slot_buffer_from_all(bm, &bmop, bmop.slots_in, slot_name, BM_FACE);
}
BMO_slot_buffer_from_all(bm, &bmop, bmop.slots_in, slot_name, BM_VERT);
}
else if (BPy_BMEdgeSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("edges");
BPY_BM_ELEM_TYPE_TEST("edges");
BMO_slot_buffer_from_all(bm, &bmop, bmop.slots_in, slot_name, BM_EDGE);
}
else if (BPy_BMFaceSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("faces");
BPY_BM_ELEM_TYPE_TEST("faces");
BMO_slot_buffer_from_all(bm, &bmop, bmop.slots_in, slot_name, BM_FACE);
}
#undef BPY_BM_ELEM_TYPE_TEST
else if (BPy_BMElemSeq_Check(value)) {
BMIter iter;
BMHeader *ele;
int tot;
unsigned int i;
else if (BPy_BMElemSeq_Check(value)) {
BMIter iter;
BMHeader *ele;
int tot;
unsigned int i;
BPY_BM_GENERIC_MESH_TEST("elements");
BPY_BM_GENERIC_MESH_TEST("elements");
/* this will loop over all elements which is a shame but
* we need to know this before alloc */
/* calls bpy_bmelemseq_length() */
tot = Py_TYPE(value)->tp_as_sequence->sq_length((PyObject *)self);
/* this will loop over all elements which is a shame but
* we need to know this before alloc */
/* calls bpy_bmelemseq_length() */
tot = Py_TYPE(value)->tp_as_sequence->sq_length((PyObject *)self);
BMO_slot_buffer_alloc(&bmop, bmop.slots_in, slot_name, tot);
BMO_slot_buffer_alloc(&bmop, bmop.slots_in, slot_name, tot);
i = 0;
BM_ITER_BPY_BM_SEQ (ele, &iter, ((BPy_BMElemSeq *)value)) {
slot->data.buf[i] = ele;
i++;
i = 0;
BM_ITER_BPY_BM_SEQ (ele, &iter, ((BPy_BMElemSeq *)value)) {
slot->data.buf[i] = ele;
i++;
}
}
}
/* keep this last */
else if (PySequence_Check(value)) {
BMElem **elem_array = NULL;
Py_ssize_t elem_array_len;
/* keep this last */
else if (PySequence_Check(value)) {
BMElem **elem_array = NULL;
Py_ssize_t elem_array_len;
elem_array = BPy_BMElem_PySeq_As_Array(&bm, value, 0, PY_SSIZE_T_MAX,
&elem_array_len, (slot->slot_subtype.elem & BM_ALL_NOLOOP),
TRUE, TRUE, slot_name);
elem_array = BPy_BMElem_PySeq_As_Array(&bm, value, 0, PY_SSIZE_T_MAX,
&elem_array_len, (slot->slot_subtype.elem & BM_ALL_NOLOOP),
TRUE, TRUE, slot_name);
/* error is set above */
if (elem_array == NULL) {
/* error is set above */
if (elem_array == NULL) {
return NULL;
}
BMO_slot_buffer_alloc(&bmop, bmop.slots_in, slot_name, elem_array_len);
memcpy(slot->data.buf, elem_array, sizeof(void *) * elem_array_len);
PyMem_FREE(elem_array);
}
else {
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected "
"a bmesh sequence, list, (htype, flag) pair, not %.200s",
self->opname, slot_name, Py_TYPE(value)->tp_name);
return NULL;
}
BMO_slot_buffer_alloc(&bmop, bmop.slots_in, slot_name, elem_array_len);
memcpy(slot->data.buf, elem_array, sizeof(void *) * elem_array_len);
PyMem_FREE(elem_array);
}
else {
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected "
"a bmesh sequence, list, (htype, flag) pair, not %.200s",
self->opname, slot_name, Py_TYPE(value)->tp_name);
return NULL;
}
#undef BPY_BM_GENERIC_MESH_TEST
break;
@@ -548,14 +569,20 @@ static PyObject *pyrna_op_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *
break;
case BMO_OP_SLOT_ELEMENT_BUF:
{
const int size = slot->len;
void **buffer = BMO_SLOT_AS_BUFFER(slot);
int j;
if (slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE) {
BMHeader *ele = BMO_slot_buffer_get_single(slot);
item = ele ? BPy_BMElem_CreatePyObject(bm, ele) : (Py_INCREF(Py_None), Py_None);
}
else {
const int size = slot->len;
void **buffer = BMO_SLOT_AS_BUFFER(slot);
int j;
item = PyList_New(size);
for (j = 0; j < size; j++) {
BMHeader *ele = buffer[i];
PyList_SET_ITEM(item, j, ele ? BPy_BMElem_CreatePyObject(bm, ele) : (Py_INCREF(Py_None), Py_None));
item = PyList_New(size);
for (j = 0; j < size; j++) {
BMHeader *ele = buffer[i];
PyList_SET_ITEM(item, j, BPy_BMElem_CreatePyObject(bm, ele));
}
}
break;
}