From 3ca0870023bb71bc929925a8fc8d172c09df710f Mon Sep 17 00:00:00 2001 From: Tamito Kajiyama Date: Sun, 31 May 2015 17:46:58 +0900 Subject: Improvements to the Freestyle Python API (needed by the SVG Exporter) This patch adds some new functionality to the Freestyle Python API, notably: - MaterialBP1D, checks whether the supplied arguments have the same material - Fixes a potential crash in CurvePoint.fedge (due to NULL pointer) - Makes (error handling in) boolean predicates more robust - Adds a BoundingBox type, to make working with bounding boxes easier - Adds several new functions (get_object_name, get_strokes, is_poly_clockwise, material_from_fedge) - Adds a StrokeCollector StrokeShader, that collects all the strokes from a specific call to Operators.create() - Adds hashing and rich comparison to the FrsMaterial type These new features (most of them, anyway) are needed for making a more robust SVG exporter that supports holes in fills. Reviewers: kjym3, campbellbarton Subscribers: campbellbarton Projects: #bf_blender Differential Revision: https://developer.blender.org/D1245 --- .../freestyle/intern/python/BPy_FrsMaterial.cpp | 49 +++++++++++++++++++++- .../intern/python/Interface0D/BPy_CurvePoint.cpp | 5 ++- 2 files changed, 51 insertions(+), 3 deletions(-) (limited to 'source/blender/freestyle/intern/python') diff --git a/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp b/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp index 4d0d140474a..a36d446fcb7 100644 --- a/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp +++ b/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp @@ -30,6 +30,9 @@ extern "C" { #endif +#include "BLI_hash_mm2a.h" + + /////////////////////////////////////////////////////////////////////////////////////////// //-------------------MODULE INITIALIZATION-------------------------------- @@ -478,6 +481,48 @@ static PyGetSetDef BPy_FrsMaterial_getseters[] = { {NULL, NULL, NULL, NULL, NULL} /* Sentinel */ }; +static PyObject *BPy_FrsMaterial_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) +{ + const BPy_FrsMaterial *matA = NULL, *matB = NULL; + bool result = 0; + + if (!BPy_FrsMaterial_Check(objectA) || !BPy_FrsMaterial_Check(objectB)) { + if (comparison_type == Py_NE) { + Py_RETURN_TRUE; + } + else { + Py_RETURN_FALSE; + } + } + + matA = (BPy_FrsMaterial *)objectA; + matB = (BPy_FrsMaterial *)objectB; + + switch (comparison_type) { + case Py_NE: + result = (*matA->m) != (*matB->m); + break; + case Py_EQ: + result = (*matA->m) == (*matB->m); + break; + default: + PyErr_SetString(PyExc_TypeError, "Material does not support this comparison type"); + return NULL; + } + + if (result == true) { + Py_RETURN_TRUE; + } + else { + Py_RETURN_FALSE; + } +} + + +static Py_hash_t FrsMaterial_hash(PyObject *self) +{ + return (Py_uhash_t)BLI_hash_mm2((const unsigned char *)self, sizeof(*self), 0); +} /*-----------------------BPy_FrsMaterial type definition ------------------------------*/ PyTypeObject FrsMaterial_Type = { @@ -494,7 +539,7 @@ PyTypeObject FrsMaterial_Type = { 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ - 0, /* tp_hash */ + (hashfunc)FrsMaterial_hash, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ 0, /* tp_getattro */ @@ -504,7 +549,7 @@ PyTypeObject FrsMaterial_Type = { FrsMaterial_doc, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ - 0, /* tp_richcompare */ + (richcmpfunc)BPy_FrsMaterial_richcmpr, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ diff --git a/source/blender/freestyle/intern/python/Interface0D/BPy_CurvePoint.cpp b/source/blender/freestyle/intern/python/Interface0D/BPy_CurvePoint.cpp index 1ef29792d56..9f0660baa9b 100644 --- a/source/blender/freestyle/intern/python/Interface0D/BPy_CurvePoint.cpp +++ b/source/blender/freestyle/intern/python/Interface0D/BPy_CurvePoint.cpp @@ -188,7 +188,10 @@ static PyObject *CurvePoint_fedge_get(BPy_CurvePoint *self, void *UNUSED(closure { SVertex *A = self->cp->A(); Interface0D *B = (Interface0D *)self->cp->B(); - return Any_BPy_Interface1D_from_Interface1D(*(A->getFEdge(*B))); + // B can be NULL under certain circumstances + if (B) + return Any_BPy_Interface1D_from_Interface1D(*(A->getFEdge(*B))); + Py_RETURN_NONE; } PyDoc_STRVAR(CurvePoint_t2d_doc, -- cgit v1.2.3