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