Blender  V2.93
BPy_StrokeShader.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BPy_StrokeShader.h"
22 
23 #include "BPy_Convert.h"
24 #include "Interface1D/BPy_Stroke.h"
25 
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 using namespace Freestyle;
50 
52 
53 //-------------------MODULE INITIALIZATION--------------------------------
54 int StrokeShader_Init(PyObject *module)
55 {
56  if (module == nullptr) {
57  return -1;
58  }
59 
60  if (PyType_Ready(&StrokeShader_Type) < 0) {
61  return -1;
62  }
63  Py_INCREF(&StrokeShader_Type);
64  PyModule_AddObject(module, "StrokeShader", (PyObject *)&StrokeShader_Type);
65 
66  if (PyType_Ready(&BackboneStretcherShader_Type) < 0) {
67  return -1;
68  }
69  Py_INCREF(&BackboneStretcherShader_Type);
70  PyModule_AddObject(module, "BackboneStretcherShader", (PyObject *)&BackboneStretcherShader_Type);
71 
72  if (PyType_Ready(&BezierCurveShader_Type) < 0) {
73  return -1;
74  }
75  Py_INCREF(&BezierCurveShader_Type);
76  PyModule_AddObject(module, "BezierCurveShader", (PyObject *)&BezierCurveShader_Type);
77 
78  if (PyType_Ready(&BlenderTextureShader_Type) < 0) {
79  return -1;
80  }
81  Py_INCREF(&BlenderTextureShader_Type);
82  PyModule_AddObject(module, "BlenderTextureShader", (PyObject *)&BlenderTextureShader_Type);
83 
84  if (PyType_Ready(&CalligraphicShader_Type) < 0) {
85  return -1;
86  }
87  Py_INCREF(&CalligraphicShader_Type);
88  PyModule_AddObject(module, "CalligraphicShader", (PyObject *)&CalligraphicShader_Type);
89 
90  if (PyType_Ready(&ColorNoiseShader_Type) < 0) {
91  return -1;
92  }
93  Py_INCREF(&ColorNoiseShader_Type);
94  PyModule_AddObject(module, "ColorNoiseShader", (PyObject *)&ColorNoiseShader_Type);
95 
96  if (PyType_Ready(&ConstantColorShader_Type) < 0) {
97  return -1;
98  }
99  Py_INCREF(&ConstantColorShader_Type);
100  PyModule_AddObject(module, "ConstantColorShader", (PyObject *)&ConstantColorShader_Type);
101 
102  if (PyType_Ready(&ConstantThicknessShader_Type) < 0) {
103  return -1;
104  }
105  Py_INCREF(&ConstantThicknessShader_Type);
106  PyModule_AddObject(module, "ConstantThicknessShader", (PyObject *)&ConstantThicknessShader_Type);
107 
108  if (PyType_Ready(&ConstrainedIncreasingThicknessShader_Type) < 0) {
109  return -1;
110  }
112  PyModule_AddObject(module,
113  "ConstrainedIncreasingThicknessShader",
115 
116  if (PyType_Ready(&GuidingLinesShader_Type) < 0) {
117  return -1;
118  }
119  Py_INCREF(&GuidingLinesShader_Type);
120  PyModule_AddObject(module, "GuidingLinesShader", (PyObject *)&GuidingLinesShader_Type);
121 
122  if (PyType_Ready(&IncreasingColorShader_Type) < 0) {
123  return -1;
124  }
125  Py_INCREF(&IncreasingColorShader_Type);
126  PyModule_AddObject(module, "IncreasingColorShader", (PyObject *)&IncreasingColorShader_Type);
127 
128  if (PyType_Ready(&IncreasingThicknessShader_Type) < 0) {
129  return -1;
130  }
131  Py_INCREF(&IncreasingThicknessShader_Type);
132  PyModule_AddObject(
133  module, "IncreasingThicknessShader", (PyObject *)&IncreasingThicknessShader_Type);
134 
135  if (PyType_Ready(&PolygonalizationShader_Type) < 0) {
136  return -1;
137  }
138  Py_INCREF(&PolygonalizationShader_Type);
139  PyModule_AddObject(module, "PolygonalizationShader", (PyObject *)&PolygonalizationShader_Type);
140 
141  if (PyType_Ready(&SamplingShader_Type) < 0) {
142  return -1;
143  }
144  Py_INCREF(&SamplingShader_Type);
145  PyModule_AddObject(module, "SamplingShader", (PyObject *)&SamplingShader_Type);
146 
147  if (PyType_Ready(&SmoothingShader_Type) < 0) {
148  return -1;
149  }
150  Py_INCREF(&SmoothingShader_Type);
151  PyModule_AddObject(module, "SmoothingShader", (PyObject *)&SmoothingShader_Type);
152 
153  if (PyType_Ready(&SpatialNoiseShader_Type) < 0) {
154  return -1;
155  }
156  Py_INCREF(&SpatialNoiseShader_Type);
157  PyModule_AddObject(module, "SpatialNoiseShader", (PyObject *)&SpatialNoiseShader_Type);
158 
159  if (PyType_Ready(&StrokeTextureStepShader_Type) < 0) {
160  return -1;
161  }
162  Py_INCREF(&StrokeTextureStepShader_Type);
163  PyModule_AddObject(module, "StrokeTextureStepShader", (PyObject *)&StrokeTextureStepShader_Type);
164 
165  if (PyType_Ready(&ThicknessNoiseShader_Type) < 0) {
166  return -1;
167  }
168  Py_INCREF(&ThicknessNoiseShader_Type);
169  PyModule_AddObject(module, "ThicknessNoiseShader", (PyObject *)&ThicknessNoiseShader_Type);
170 
171  if (PyType_Ready(&TipRemoverShader_Type) < 0) {
172  return -1;
173  }
174  Py_INCREF(&TipRemoverShader_Type);
175  PyModule_AddObject(module, "TipRemoverShader", (PyObject *)&TipRemoverShader_Type);
176 
177  return 0;
178 }
179 
180 //------------------------INSTANCE METHODS ----------------------------------
181 
182 static char StrokeShader___doc__[] =
183  "Base class for stroke shaders. Any stroke shader must inherit from\n"
184  "this class and overload the shade() method. A StrokeShader is\n"
185  "designed to modify stroke attributes such as thickness, color,\n"
186  "geometry, texture, blending mode, and so on. The basic way for this\n"
187  "operation is to iterate over the stroke vertices of the :class:`Stroke`\n"
188  "and to modify the :class:`StrokeAttribute` of each vertex. Here is a\n"
189  "code example of such an iteration::\n"
190  "\n"
191  " it = ioStroke.strokeVerticesBegin()\n"
192  " while not it.is_end:\n"
193  " att = it.object.attribute\n"
194  " ## perform here any attribute modification\n"
195  " it.increment()\n"
196  "\n"
197  ".. method:: __init__()\n"
198  "\n"
199  " Default constructor.\n";
200 
201 static int StrokeShader___init__(BPy_StrokeShader *self, PyObject *args, PyObject *kwds)
202 {
203  static const char *kwlist[] = {nullptr};
204 
205  if (!PyArg_ParseTupleAndKeywords(args, kwds, "", (char **)kwlist)) {
206  return -1;
207  }
208  self->ss = new StrokeShader();
209  self->ss->py_ss = (PyObject *)self;
210  return 0;
211 }
212 
214 {
215  delete self->ss;
216  Py_TYPE(self)->tp_free((PyObject *)self);
217 }
218 
220 {
221  return PyUnicode_FromFormat("type: %s - address: %p", Py_TYPE(self)->tp_name, self->ss);
222 }
223 
225  ".. method:: shade(stroke)\n"
226  "\n"
227  " The shading method. Must be overloaded by inherited classes.\n"
228  "\n"
229  " :arg stroke: A Stroke object.\n"
230  " :type stroke: :class:`Stroke`\n";
231 
232 static PyObject *StrokeShader_shade(BPy_StrokeShader *self, PyObject *args, PyObject *kwds)
233 {
234  static const char *kwlist[] = {"stroke", nullptr};
235  PyObject *py_s = nullptr;
236 
237  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &Stroke_Type, &py_s)) {
238  return nullptr;
239  }
240 
241  if (typeid(*(self->ss)) == typeid(StrokeShader)) {
242  PyErr_SetString(PyExc_TypeError, "shade method not properly overridden");
243  return nullptr;
244  }
245  if (self->ss->shade(*(((BPy_Stroke *)py_s)->s)) < 0) {
246  if (!PyErr_Occurred()) {
247  string class_name(Py_TYPE(self)->tp_name);
248  PyErr_SetString(PyExc_RuntimeError, (class_name + " shade method failed").c_str());
249  }
250  return nullptr;
251  }
252  Py_RETURN_NONE;
253 }
254 
255 static PyMethodDef BPy_StrokeShader_methods[] = {
256  {"shade",
257  (PyCFunction)StrokeShader_shade,
258  METH_VARARGS | METH_KEYWORDS,
260  {nullptr, nullptr, 0, nullptr},
261 };
262 
263 /*----------------------StrokeShader get/setters ----------------------------*/
264 
265 PyDoc_STRVAR(StrokeShader_name_doc,
266  "The name of the stroke shader.\n"
267  "\n"
268  ":type: str");
269 
270 static PyObject *StrokeShader_name_get(BPy_StrokeShader *self, void *UNUSED(closure))
271 {
272  return PyUnicode_FromString(Py_TYPE(self)->tp_name);
273 }
274 
275 static PyGetSetDef BPy_StrokeShader_getseters[] = {
276  {"name", (getter)StrokeShader_name_get, (setter) nullptr, StrokeShader_name_doc, nullptr},
277  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
278 };
279 
280 /*-----------------------BPy_StrokeShader type definition ------------------------------*/
281 
282 PyTypeObject StrokeShader_Type = {
283  PyVarObject_HEAD_INIT(nullptr, 0) "StrokeShader", /* tp_name */
284  sizeof(BPy_StrokeShader), /* tp_basicsize */
285  0, /* tp_itemsize */
286  (destructor)StrokeShader___dealloc__, /* tp_dealloc */
287  0, /* tp_vectorcall_offset */
288  nullptr, /* tp_getattr */
289  nullptr, /* tp_setattr */
290  nullptr, /* tp_reserved */
291  (reprfunc)StrokeShader___repr__, /* tp_repr */
292  nullptr, /* tp_as_number */
293  nullptr, /* tp_as_sequence */
294  nullptr, /* tp_as_mapping */
295  nullptr, /* tp_hash */
296  nullptr, /* tp_call */
297  nullptr, /* tp_str */
298  nullptr, /* tp_getattro */
299  nullptr, /* tp_setattro */
300  nullptr, /* tp_as_buffer */
301  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
302  StrokeShader___doc__, /* tp_doc */
303  nullptr, /* tp_traverse */
304  nullptr, /* tp_clear */
305  nullptr, /* tp_richcompare */
306  0, /* tp_weaklistoffset */
307  nullptr, /* tp_iter */
308  nullptr, /* tp_iternext */
309  BPy_StrokeShader_methods, /* tp_methods */
310  nullptr, /* tp_members */
311  BPy_StrokeShader_getseters, /* tp_getset */
312  nullptr, /* tp_base */
313  nullptr, /* tp_dict */
314  nullptr, /* tp_descr_get */
315  nullptr, /* tp_descr_set */
316  0, /* tp_dictoffset */
317  (initproc)StrokeShader___init__, /* tp_init */
318  nullptr, /* tp_alloc */
319  PyType_GenericNew, /* tp_new */
320 };
321 
323 
324 #ifdef __cplusplus
325 }
326 #endif
#define UNUSED(x)
PyTypeObject BackboneStretcherShader_Type
PyTypeObject BezierCurveShader_Type
PyTypeObject BlenderTextureShader_Type
PyTypeObject CalligraphicShader_Type
PyTypeObject ColorNoiseShader_Type
PyTypeObject ConstantColorShader_Type
PyTypeObject ConstantThicknessShader_Type
PyTypeObject ConstrainedIncreasingThicknessShader_Type
PyTypeObject GuidingLinesShader_Type
PyTypeObject IncreasingColorShader_Type
PyTypeObject IncreasingThicknessShader_Type
PyTypeObject PolygonalizationShader_Type
PyTypeObject SamplingShader_Type
PyTypeObject SmoothingShader_Type
PyTypeObject SpatialNoiseShader_Type
PyTypeObject StrokeShader_Type
static PyGetSetDef BPy_StrokeShader_getseters[]
static PyObject * StrokeShader_shade(BPy_StrokeShader *self, PyObject *args, PyObject *kwds)
PyDoc_STRVAR(StrokeShader_name_doc, "The name of the stroke shader.\n" "\n" ":type: str")
static PyObject * StrokeShader___repr__(BPy_StrokeShader *self)
static PyObject * StrokeShader_name_get(BPy_StrokeShader *self, void *UNUSED(closure))
static int StrokeShader___init__(BPy_StrokeShader *self, PyObject *args, PyObject *kwds)
static char StrokeShader___doc__[]
static void StrokeShader___dealloc__(BPy_StrokeShader *self)
int StrokeShader_Init(PyObject *module)
static char StrokeShader_shade___doc__[]
static PyMethodDef BPy_StrokeShader_methods[]
PyTypeObject StrokeTextureStepShader_Type
PyTypeObject Stroke_Type
Definition: BPy_Stroke.cpp:501
PyTypeObject ThicknessNoiseShader_Type
PyTypeObject TipRemoverShader_Type
static struct PyModuleDef module
PyObject * self
Definition: bpy_driver.c:185
inherits from class Rep
Definition: AppCanvas.cpp:32