[Bf-blender-cvs] [6b4719e] particles_refactor: Added 'attributes' sequence property in the state type.

Lukas Tönne noreply at git.blender.org
Tue Apr 22 12:06:15 CEST 2014


Commit: 6b4719e1e0f475059fb2f1c2784e2054ae897cce
Author: Lukas Tönne
Date:   Thu Dec 19 18:20:02 2013 +0100
https://developer.blender.org/rB6b4719e1e0f475059fb2f1c2784e2054ae897cce

Added 'attributes' sequence property in the state type.

===================================================================

M	source/blender/blenkernel/BKE_nparticle.h
M	source/blender/blenkernel/intern/nparticle.c
M	source/blender/python/bparticles/bparticles_py_types.c
M	source/blender/python/bparticles/bparticles_py_types.h

===================================================================

diff --git a/source/blender/blenkernel/BKE_nparticle.h b/source/blender/blenkernel/BKE_nparticle.h
index f60d690..2502158 100644
--- a/source/blender/blenkernel/BKE_nparticle.h
+++ b/source/blender/blenkernel/BKE_nparticle.h
@@ -58,7 +58,21 @@ void BKE_nparticle_attribute_move(struct NParticleSystem *psys, int from_index,
 struct NParticleAttribute *BKE_nparticle_attribute_copy(struct NParticleSystem *to_psys,
                                                         struct NParticleSystem *from_psys, struct NParticleAttribute *from_attr);
 
+int BKE_nparticle_state_num_attributes(struct NParticleState *state);
 struct NParticleAttributeState *BKE_nparticle_state_find_attribute(struct NParticleState *state, const char *name);
+struct NParticleAttributeState *BKE_nparticle_state_get_attribute_by_index(struct NParticleState *state, int index);
+
+typedef struct NParticleAttributeStateIterator {
+	/* XXX for now is simply a pointer, using ListBase next/prev.
+	 * Eventually this will become a hash table iterator.
+	 */
+	struct NParticleAttributeState *attrstate;
+} NParticleAttributeStateIterator;
+
+void BKE_nparticle_state_attributes_begin(struct NParticleState *state, struct NParticleAttributeStateIterator *iter);
+bool BKE_nparticle_state_attribute_iter_valid(struct NParticleAttributeStateIterator *iter);
+void BKE_nparticle_state_attribute_iter_next(struct NParticleAttributeStateIterator *iter);
+void BKE_nparticle_state_attribute_iter_end(struct NParticleAttributeStateIterator *iter);
 
 
 int BKE_nparticle_find_index(struct NParticleState *state, NParticleID id);
diff --git a/source/blender/blenkernel/intern/nparticle.c b/source/blender/blenkernel/intern/nparticle.c
index f439cc2..1951687 100644
--- a/source/blender/blenkernel/intern/nparticle.c
+++ b/source/blender/blenkernel/intern/nparticle.c
@@ -235,6 +235,27 @@ void BKE_nparticle_state_free(NParticleState *state)
 }
 
 
+void BKE_nparticle_state_attributes_begin(NParticleState *state, NParticleAttributeStateIterator *iter)
+{
+	iter->attrstate = state->attributes.first;
+}
+
+bool BKE_nparticle_state_attribute_iter_valid(NParticleAttributeStateIterator *iter)
+{
+	return (iter->attrstate != NULL);
+}
+
+void BKE_nparticle_state_attribute_iter_next(NParticleAttributeStateIterator *iter)
+{
+	iter->attrstate = iter->attrstate->next;
+}
+
+void BKE_nparticle_state_attribute_iter_end(NParticleAttributeStateIterator *iter)
+{
+	iter->attrstate = NULL;
+}
+
+
 NParticleAttribute *BKE_nparticle_attribute_find(NParticleSystem *psys, const char *name)
 {
 	NParticleAttribute *attr;
@@ -328,6 +349,11 @@ void BKE_nparticle_attribute_move(NParticleSystem *psys, int from_index, int to_
 }
 
 
+int BKE_nparticle_state_num_attributes(NParticleState *state)
+{
+	return BLI_countlist(&state->attributes);
+}
+
 NParticleAttributeState *BKE_nparticle_state_find_attribute(NParticleState *state, const char *name)
 {
 	int hashkey = BLI_ghashutil_strhash(name);
@@ -338,6 +364,11 @@ NParticleAttributeState *BKE_nparticle_state_find_attribute(NParticleState *stat
 	return NULL;
 }
 
+NParticleAttributeState *BKE_nparticle_state_get_attribute_by_index(NParticleState *state, int index)
+{
+	return BLI_findlink(&state->attributes, index);
+}
+
 
 int BKE_nparticle_find_index(NParticleState *state, NParticleID id)
 {
diff --git a/source/blender/python/bparticles/bparticles_py_types.c b/source/blender/python/bparticles/bparticles_py_types.c
index 1fd721d..468d2fb 100644
--- a/source/blender/python/bparticles/bparticles_py_types.c
+++ b/source/blender/python/bparticles/bparticles_py_types.c
@@ -50,6 +50,23 @@ PyDoc_STRVAR(bpy_bpar_attrstate_doc,
 "Particle attribute state data\n"
 );
 
+PyDoc_STRVAR(bpy_bpar_attrstateseq_doc,
+"Particle attribute state sequence\n"
+);
+
+PyDoc_STRVAR(bpy_bpar_attrstateiter_doc,
+"Iterator for looping over particle attribute states\n"
+);
+
+
+PyDoc_STRVAR(bpy_bpar_state_attributes_doc,
+"State attributes (read-only).\n\n:type: :class:`NParticleAttributeStateSeq`"
+);
+static PyObject *bpy_bpar_state_attributes_get(BPy_NParticleState *self, void *UNUSED(closure))
+{
+	return BPy_NParticleAttributeStateSeq_CreatePyObject(self->state);
+}
+
 PyDoc_STRVAR(bpy_bpar_attrstate_name_doc,
 "Attribute name\n"
 );
@@ -86,6 +103,7 @@ static PyObject *bpy_bpar_attrstate_repr(BPy_NParticleAttributeState *self)
 }
 
 static PyGetSetDef bpy_bpar_state_getseters[] = {
+	{(char *)"attributes", (getter)bpy_bpar_state_attributes_get, (setter)NULL, (char *)bpy_bpar_state_attributes_doc, NULL},
 	{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
 
@@ -112,6 +130,179 @@ static Py_hash_t bpy_bpar_attrstate_hash(PyObject *self)
 	return _Py_HashPointer(((BPy_NParticleAttributeState *)self)->attrstate);
 }
 
+
+static Py_ssize_t bpy_bpar_attrstateseq_length(BPy_NParticleAttributeStateSeq *self)
+{
+	return BKE_nparticle_state_num_attributes(self->state);
+}
+
+static PyObject *bpy_bpar_attrstateseq_subscript_int(BPy_NParticleAttributeStateSeq *self, int keynum)
+{
+	if (keynum < 0)
+		keynum += BKE_nparticle_state_num_attributes(self->state);
+	
+	if (keynum >= 0) {
+		NParticleAttributeState *attrstate = BKE_nparticle_state_get_attribute_by_index(self->state, keynum);
+		if (attrstate)
+			return BPy_NParticleAttributeState_CreatePyObject(self->state, attrstate);
+	}
+
+	PyErr_Format(PyExc_IndexError,
+	             "NParticleAttributeStateSeq[index]: index %d out of range", keynum);
+	return NULL;
+}
+
+static PyObject *bpy_bpar_attrstateseq_subscript_str(BPy_NParticleAttributeStateSeq *self, const char *keyname)
+{
+	NParticleAttributeState *attrstate = BKE_nparticle_state_find_attribute(self->state, keyname);
+	
+	if (attrstate)
+		return BPy_NParticleAttributeState_CreatePyObject(self->state, attrstate);
+
+	PyErr_Format(PyExc_KeyError, "NParticleAttributeStateSeq[key]: key \"%.200s\" not found", keyname);
+	return NULL;
+}
+
+static PyObject *bpy_bpar_attrstateseq_subscript_slice(BPy_NParticleAttributeStateSeq *self, Py_ssize_t start, Py_ssize_t stop)
+{
+	NParticleAttributeStateIterator iter;
+	int count;
+
+	PyObject *list;
+	PyObject *item;
+
+	list = PyList_New(0);
+
+	/* skip to start */
+	for (count = 0, BKE_nparticle_state_attributes_begin(self->state, &iter);
+	     count < start && BKE_nparticle_state_attribute_iter_valid(&iter);
+	     ++count, BKE_nparticle_state_attribute_iter_next(&iter))
+	{}
+	
+	/* add items until stop */
+	for (; count < stop && BKE_nparticle_state_attribute_iter_valid(&iter);
+	     ++count, BKE_nparticle_state_attribute_iter_next(&iter))
+	{
+		item = BPy_NParticleAttributeState_CreatePyObject(self->state, iter.attrstate);
+		PyList_Append(list, item);
+		Py_DECREF(item);
+	}
+	BKE_nparticle_state_attribute_iter_end(&iter);
+	
+	return list;
+}
+
+static int bpy_bpar_attrstateseq_contains(BPy_NParticleAttributeStateSeq *self, PyObject *value)
+{
+	if (BPy_NParticleAttributeState_Check(value)) {
+		BPy_NParticleAttributeState *attrstate_py = (BPy_NParticleAttributeState *)value;
+		NParticleAttributeState *attrstate = attrstate_py->attrstate;
+		if (BKE_nparticle_state_find_attribute(self->state, attrstate->desc.name) == attrstate)
+			return 1;
+	}
+	return 0;
+}
+
+static PyObject *bpy_bpar_attrstateseq_subscript(BPy_NParticleAttributeStateSeq *self, PyObject *key)
+{
+	if (PyUnicode_Check(key)) {
+		return bpy_bpar_attrstateseq_subscript_str(self, _PyUnicode_AsString(key));
+	}
+	else if (PyIndex_Check(key)) {
+		Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
+		if (i == -1 && PyErr_Occurred())
+			return NULL;
+		return bpy_bpar_attrstateseq_subscript_int(self, i);
+	}
+	else if (PySlice_Check(key)) {
+		PySliceObject *key_slice = (PySliceObject *)key;
+		Py_ssize_t step = 1;
+
+		if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
+			return NULL;
+		}
+		else if (step != 1) {
+			PyErr_SetString(PyExc_TypeError, "NParticleAttributeStateSeq[slice]: slice steps not supported");
+			return NULL;
+		}
+		else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
+			return bpy_bpar_attrstateseq_subscript_slice(self, 0, PY_SSIZE_T_MAX);
+		}
+		else {
+			Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
+
+			/* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
+			if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
+			if (key_slice->stop  != Py_None && !_PyEval_SliceIndex(key_slice->stop,  &stop))  return NULL;
+
+			if (start < 0 || stop < 0) {
+				/* only get the length for negative values */
+				Py_ssize_t len = (Py_ssize_t)BKE_nparticle_state_num_attributes(self->state);
+				if (start < 0) start += len;
+				if (stop  < 0) stop  += len;
+			}
+
+			if (stop - start <= 0) {
+				return PyList_New(0);
+			}
+			else {
+				return bpy_bpar_attrstateseq_subscript_slice(self, start, stop);
+			}
+		}
+	}
+	else {
+		PyErr_Format(PyExc_TypeError,
+		             "NParticleAttributeStateSeq[key]: invalid key, "
+		             "must be a string or an int, not %.200s",
+		             Py_TYPE(key)->tp_name);
+		return NULL;
+	}
+}
+
+static PySequenceMethods bpy_bpar_attrstateseq_as_sequence = {
+	(lenfunc)bpy_bpar_attrstateseq_length,              /* sq_length */
+	NULL,                                               /* sq_concat */
+	NULL,                                               /* sq_repeat */
+	(ssizeargfunc)bpy_bpar_attrstateseq_subscript_int,  /* sq_item */ /* Only set this so PySequence_Check() returns True */
+	NULL,                                               /* sq_slice */
+	(ssizeobjargproc)NULL,                              /* sq_ass_item */
+	NULL,                                               /* *was* sq_ass_slice */
+	(objobjproc)bpy_bpar_attrstateseq_contains,         /* sq_contains */
+	(binaryfunc) NULL,                                  /* sq_inplace_concat */
+	(ssizeargfunc) NULL,                                /* sq_inplace_repeat */
+};
+
+static PyMappingMethods bpy_bpar_attrstateseq_as_mapping = {
+	(lenfunc)bpy_bpar_attrstateseq_length,              /* mp_length */
+	(binaryfunc)bpy_bpar_attrstateseq_subscript,        /* mp_subscript */
+	(objobjargproc)NULL,                                /* mp_ass_subscript */
+};
+
+/* Iter

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list