Blender V5.0
BPy_FrsMaterial.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BPy_FrsMaterial.h"
10
11#include "BPy_Convert.h"
12
13#include "BLI_hash_mm2a.hh"
14#include "BLI_math_vector.h"
15
16using namespace Freestyle;
17
19
20//-------------------MODULE INITIALIZATION--------------------------------
22{
23 if (module == nullptr) {
24 return -1;
25 }
26
27 if (PyType_Ready(&FrsMaterial_Type) < 0) {
28 return -1;
29 }
30 PyModule_AddObjectRef(module, "Material", (PyObject *)&FrsMaterial_Type);
31
33
34 return 0;
35}
36
37//------------------------INSTANCE METHODS ----------------------------------
38
40 /* Wrap. */
41 FrsMaterial_doc,
42 "Class defining a material.\n"
43 "\n"
44 ".. method:: __init__()\n"
45 " __init__(brother)\n"
46 " __init__(line, diffuse, ambient, specular, emission, shininess, priority)\n"
47 "\n"
48 " Creates a :class:`FrsMaterial` using either default constructor,\n"
49 " copy constructor, or an overloaded constructor\n"
50 "\n"
51 " :arg brother: A Material object to be used as a copy constructor.\n"
52 " :type brother: :class:`Material`\n"
53 " :arg line: The line color.\n"
54 " :type line: :class:`mathutils.Vector` | tuple[float, float, float, float] | list[float]\n"
55 " :arg diffuse: The diffuse color.\n"
56 " :type diffuse: \n"
57 " :arg ambient: The ambient color.\n"
58 " :type ambient: :class:`mathutils.Vector` | tuple[float, float, float, float] | "
59 "list[float]\n"
60 " :arg specular: The specular color.\n"
61 " :type specular: :class:`mathutils.Vector` | tuple[float, float, float, float] | "
62 "list[float]\n"
63 " :arg emission: The emissive color.\n"
64 " :type emission: :class:`mathutils.Vector` | tuple[float, float, float, float] | "
65 "list[float]\n"
66 " :arg shininess: The shininess coefficient.\n"
67 " :type shininess: float\n"
68 " :arg priority: The line color priority.\n"
69 " :type priority: int\n");
70static int FrsMaterial_init(BPy_FrsMaterial *self, PyObject *args, PyObject *kwds)
71{
72 static const char *kwlist_1[] = {"brother", nullptr};
73 static const char *kwlist_2[] = {
74 "line", "diffuse", "ambient", "specular", "emission", "shininess", "priority", nullptr};
75 PyObject *brother = nullptr;
76 float line[4], diffuse[4], ambient[4], specular[4], emission[4], shininess;
77 int priority;
78
79 if (PyArg_ParseTupleAndKeywords(
80 args, kwds, "|O!", (char **)kwlist_1, &FrsMaterial_Type, &brother))
81 {
82 if (!brother) {
83 self->m = new FrsMaterial();
84 }
85 else {
86 FrsMaterial *m = ((BPy_FrsMaterial *)brother)->m;
87 if (!m) {
88 PyErr_SetString(PyExc_RuntimeError, "invalid Material object");
89 return -1;
90 }
91 self->m = new FrsMaterial(*m);
92 }
93 }
94 else if ((void)PyErr_Clear(),
95 PyArg_ParseTupleAndKeywords(args,
96 kwds,
97 "O&O&O&O&O&fi",
98 (char **)kwlist_2,
100 line,
102 diffuse,
104 ambient,
106 specular,
108 emission,
109 &shininess,
110 &priority))
111 {
112 self->m = new FrsMaterial(line, diffuse, ambient, specular, emission, shininess, priority);
113 }
114 else {
115 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
116 return -1;
117 }
118 return 0;
119}
120
122{
123 delete self->m;
124 Py_TYPE(self)->tp_free((PyObject *)self);
125}
126
128{
129 return PyUnicode_FromFormat("Material - address: %p", self->m);
130}
131
132/*----------------------mathutils callbacks ----------------------------*/
133
134/* subtype */
135#define MATHUTILS_SUBTYPE_DIFFUSE 1
136#define MATHUTILS_SUBTYPE_SPECULAR 2
137#define MATHUTILS_SUBTYPE_AMBIENT 3
138#define MATHUTILS_SUBTYPE_EMISSION 4
139#define MATHUTILS_SUBTYPE_LINE 5
140
142{
143 if (!BPy_FrsMaterial_Check(bmo->cb_user)) {
144 return -1;
145 }
146 return 0;
147}
148
149static int FrsMaterial_mathutils_get(BaseMathObject *bmo, int subtype)
150{
151 BPy_FrsMaterial *self = (BPy_FrsMaterial *)bmo->cb_user;
152 switch (subtype) {
154 bmo->data[0] = self->m->lineR();
155 bmo->data[1] = self->m->lineG();
156 bmo->data[2] = self->m->lineB();
157 bmo->data[3] = self->m->lineA();
158 break;
160 bmo->data[0] = self->m->diffuseR();
161 bmo->data[1] = self->m->diffuseG();
162 bmo->data[2] = self->m->diffuseB();
163 bmo->data[3] = self->m->diffuseA();
164 break;
166 bmo->data[0] = self->m->specularR();
167 bmo->data[1] = self->m->specularG();
168 bmo->data[2] = self->m->specularB();
169 bmo->data[3] = self->m->specularA();
170 break;
172 bmo->data[0] = self->m->ambientR();
173 bmo->data[1] = self->m->ambientG();
174 bmo->data[2] = self->m->ambientB();
175 bmo->data[3] = self->m->ambientA();
176 break;
178 bmo->data[0] = self->m->emissionR();
179 bmo->data[1] = self->m->emissionG();
180 bmo->data[2] = self->m->emissionB();
181 bmo->data[3] = self->m->emissionA();
182 break;
183 default:
184 return -1;
185 }
186 return 0;
187}
188
189static int FrsMaterial_mathutils_set(BaseMathObject *bmo, int subtype)
190{
191 BPy_FrsMaterial *self = (BPy_FrsMaterial *)bmo->cb_user;
192 switch (subtype) {
194 self->m->setLine(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
195 break;
197 self->m->setDiffuse(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
198 break;
200 self->m->setSpecular(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
201 break;
203 self->m->setAmbient(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
204 break;
206 self->m->setEmission(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
207 break;
208 default:
209 return -1;
210 }
211 return 0;
212}
213
214static int FrsMaterial_mathutils_get_index(BaseMathObject *bmo, int subtype, int index)
215{
216 BPy_FrsMaterial *self = (BPy_FrsMaterial *)bmo->cb_user;
217 switch (subtype) {
219 const float *color = self->m->line();
220 bmo->data[index] = color[index];
221 break;
222 }
224 const float *color = self->m->diffuse();
225 bmo->data[index] = color[index];
226 break;
227 }
229 const float *color = self->m->specular();
230 bmo->data[index] = color[index];
231 break;
232 }
234 const float *color = self->m->ambient();
235 bmo->data[index] = color[index];
236 break;
237 }
239 const float *color = self->m->emission();
240 bmo->data[index] = color[index];
241 break;
242 }
243 default:
244 return -1;
245 }
246 return 0;
247}
248
249static int FrsMaterial_mathutils_set_index(BaseMathObject *bmo, int subtype, int index)
250{
251 BPy_FrsMaterial *self = (BPy_FrsMaterial *)bmo->cb_user;
252 float color[4];
253 switch (subtype) {
255 copy_v4_v4(color, self->m->line());
256 color[index] = bmo->data[index];
257 self->m->setLine(color[0], color[1], color[2], color[3]);
258 break;
260 copy_v4_v4(color, self->m->diffuse());
261 color[index] = bmo->data[index];
262 self->m->setDiffuse(color[0], color[1], color[2], color[3]);
263 break;
265 copy_v4_v4(color, self->m->specular());
266 color[index] = bmo->data[index];
267 self->m->setSpecular(color[0], color[1], color[2], color[3]);
268 break;
270 copy_v4_v4(color, self->m->ambient());
271 color[index] = bmo->data[index];
272 self->m->setAmbient(color[0], color[1], color[2], color[3]);
273 break;
275 copy_v4_v4(color, self->m->emission());
276 color[index] = bmo->data[index];
277 self->m->setEmission(color[0], color[1], color[2], color[3]);
278 break;
279 default:
280 return -1;
281 }
282 return 0;
283}
284
292
294
299
300/*----------------------FrsMaterial get/setters ----------------------------*/
301
303 /* Wrap. */
304 FrsMaterial_line_doc,
305 "RGBA components of the line color of the material.\n"
306 "\n"
307 ":type: :class:`mathutils.Vector`\n");
308static PyObject *FrsMaterial_line_get(BPy_FrsMaterial *self, void * /*closure*/)
309{
312}
313
314static int FrsMaterial_line_set(BPy_FrsMaterial *self, PyObject *value, void * /*closure*/)
315{
316 float color[4];
317 if (mathutils_array_parse(color, 4, 4, value, "value must be a 4-dimensional vector") == -1) {
318 return -1;
319 }
320 self->m->setLine(color[0], color[1], color[2], color[3]);
321 return 0;
322}
323
325 /* Wrap. */
326 FrsMaterial_diffuse_doc,
327 "RGBA components of the diffuse color of the material.\n"
328 "\n"
329 ":type: :class:`mathutils.Vector`\n");
330static PyObject *FrsMaterial_diffuse_get(BPy_FrsMaterial *self, void * /*closure*/)
331{
334}
335
336static int FrsMaterial_diffuse_set(BPy_FrsMaterial *self, PyObject *value, void * /*closure*/)
337{
338 float color[4];
339 if (mathutils_array_parse(color, 4, 4, value, "value must be a 4-dimensional vector") == -1) {
340 return -1;
341 }
342 self->m->setDiffuse(color[0], color[1], color[2], color[3]);
343 return 0;
344}
345
347 /* Wrap. */
348 FrsMaterial_specular_doc,
349 "RGBA components of the specular color of the material.\n"
350 "\n"
351 ":type: :class:`mathutils.Vector`\n");
352static PyObject *FrsMaterial_specular_get(BPy_FrsMaterial *self, void * /*closure*/)
353{
356}
357
358static int FrsMaterial_specular_set(BPy_FrsMaterial *self, PyObject *value, void * /*closure*/)
359{
360 float color[4];
361 if (mathutils_array_parse(color, 4, 4, value, "value must be a 4-dimensional vector") == -1) {
362 return -1;
363 }
364 self->m->setSpecular(color[0], color[1], color[2], color[3]);
365 return 0;
366}
367
369 /* Wrap. */
370 FrsMaterial_ambient_doc,
371 "RGBA components of the ambient color of the material.\n"
372 "\n"
373 ":type: :class:`mathutils.Color`\n");
374static PyObject *FrsMaterial_ambient_get(BPy_FrsMaterial *self, void * /*closure*/)
375{
378}
379
380static int FrsMaterial_ambient_set(BPy_FrsMaterial *self, PyObject *value, void * /*closure*/)
381{
382 float color[4];
383 if (mathutils_array_parse(color, 4, 4, value, "value must be a 4-dimensional vector") == -1) {
384 return -1;
385 }
386 self->m->setAmbient(color[0], color[1], color[2], color[3]);
387 return 0;
388}
389
391 /* Wrap. */
392 FrsMaterial_emission_doc,
393 "RGBA components of the emissive color of the material.\n"
394 "\n"
395 ":type: :class:`mathutils.Color`\n");
396static PyObject *FrsMaterial_emission_get(BPy_FrsMaterial *self, void * /*closure*/)
397{
400}
401
402static int FrsMaterial_emission_set(BPy_FrsMaterial *self, PyObject *value, void * /*closure*/)
403{
404 float color[4];
405 if (mathutils_array_parse(color, 4, 4, value, "value must be a 4-dimensional vector") == -1) {
406 return -1;
407 }
408 self->m->setEmission(color[0], color[1], color[2], color[3]);
409 return 0;
410}
411
413 /* Wrap. */
414 FrsMaterial_shininess_doc,
415 "Shininess coefficient of the material.\n"
416 "\n"
417 ":type: float\n");
418static PyObject *FrsMaterial_shininess_get(BPy_FrsMaterial *self, void * /*closure*/)
419{
420 return PyFloat_FromDouble(self->m->shininess());
421}
422
423static int FrsMaterial_shininess_set(BPy_FrsMaterial *self, PyObject *value, void * /*closure*/)
424{
425 float scalar;
426 if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
427 /* parsed item not a number */
428 PyErr_SetString(PyExc_TypeError, "value must be a number");
429 return -1;
430 }
431 self->m->setShininess(scalar);
432 return 0;
433}
434
436 /* Wrap. */
437 FrsMaterial_priority_doc,
438 "Line color priority of the material.\n"
439 "\n"
440 ":type: int\n");
441static PyObject *FrsMaterial_priority_get(BPy_FrsMaterial *self, void * /*closure*/)
442{
443 return PyLong_FromLong(self->m->priority());
444}
445
446static int FrsMaterial_priority_set(BPy_FrsMaterial *self, PyObject *value, void * /*closure*/)
447{
448 int scalar;
449 if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
450 PyErr_SetString(PyExc_TypeError, "value must be an integer");
451 return -1;
452 }
453 self->m->setPriority(scalar);
454 return 0;
455}
456
457static PyGetSetDef BPy_FrsMaterial_getseters[] = {
458 {"line",
459 (getter)FrsMaterial_line_get,
460 (setter)FrsMaterial_line_set,
461 FrsMaterial_line_doc,
462 nullptr},
463 {"diffuse",
466 FrsMaterial_diffuse_doc,
467 nullptr},
468 {"specular",
471 FrsMaterial_specular_doc,
472 nullptr},
473 {"ambient",
476 FrsMaterial_ambient_doc,
477 nullptr},
478 {"emission",
481 FrsMaterial_emission_doc,
482 nullptr},
483 {"shininess",
486 FrsMaterial_shininess_doc,
487 nullptr},
488 {"priority",
491 FrsMaterial_priority_doc,
492 nullptr},
493 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
494};
495
496static PyObject *BPy_FrsMaterial_richcmpr(PyObject *objectA,
497 PyObject *objectB,
498 int comparison_type)
499{
500 const BPy_FrsMaterial *matA = nullptr, *matB = nullptr;
501 bool result = false;
502
503 if (!BPy_FrsMaterial_Check(objectA) || !BPy_FrsMaterial_Check(objectB)) {
504 if (comparison_type == Py_NE) {
505 Py_RETURN_TRUE;
506 }
507
508 Py_RETURN_FALSE;
509 }
510
511 matA = (BPy_FrsMaterial *)objectA;
512 matB = (BPy_FrsMaterial *)objectB;
513
514 switch (comparison_type) {
515 case Py_NE:
516 result = (*matA->m) != (*matB->m);
517 break;
518 case Py_EQ:
519 result = (*matA->m) == (*matB->m);
520 break;
521 default:
522 PyErr_SetString(PyExc_TypeError, "Material does not support this comparison type");
523 return nullptr;
524 }
525
526 if (result == true) {
527 Py_RETURN_TRUE;
528 }
529
530 Py_RETURN_FALSE;
531}
532
533static Py_hash_t FrsMaterial_hash(PyObject *self)
534{
535 return (Py_uhash_t)BLI_hash_mm2((const uchar *)self, sizeof(*self), 0);
536}
537/*-----------------------BPy_FrsMaterial type definition ------------------------------*/
538
539PyTypeObject FrsMaterial_Type = {
540 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
541 /*tp_name*/ "Material",
542 /*tp_basicsize*/ sizeof(BPy_FrsMaterial),
543 /*tp_itemsize*/ 0,
544 /*tp_dealloc*/ (destructor)FrsMaterial_dealloc,
545 /*tp_vectorcall_offset*/ 0,
546 /*tp_getattr*/ nullptr,
547 /*tp_setattr*/ nullptr,
548 /*tp_as_async*/ nullptr,
549 /*tp_repr*/ (reprfunc)FrsMaterial_repr,
550 /*tp_as_number*/ nullptr,
551 /*tp_as_sequence*/ nullptr,
552 /*tp_as_mapping*/ nullptr,
553 /*tp_hash*/ (hashfunc)FrsMaterial_hash,
554 /*tp_call*/ nullptr,
555 /*tp_str*/ nullptr,
556 /*tp_getattro*/ nullptr,
557 /*tp_setattro*/ nullptr,
558 /*tp_as_buffer*/ nullptr,
559 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
560 /*tp_doc*/ FrsMaterial_doc,
561 /*tp_traverse*/ nullptr,
562 /*tp_clear*/ nullptr,
563 /*tp_richcompare*/ (richcmpfunc)BPy_FrsMaterial_richcmpr,
564 /*tp_weaklistoffset*/ 0,
565 /*tp_iter*/ nullptr,
566 /*tp_iternext*/ nullptr,
567 /*tp_methods*/ nullptr,
568 /*tp_members*/ nullptr,
569 /*tp_getset*/ BPy_FrsMaterial_getseters,
570 /*tp_base*/ nullptr,
571 /*tp_dict*/ nullptr,
572 /*tp_descr_get*/ nullptr,
573 /*tp_descr_set*/ nullptr,
574 /*tp_dictoffset*/ 0,
575 /*tp_init*/ (initproc)FrsMaterial_init,
576 /*tp_alloc*/ nullptr,
577 /*tp_new*/ PyType_GenericNew,
578};
579
uint32_t BLI_hash_mm2(const unsigned char *data, size_t len, uint32_t seed)
Definition hash_mm2a.cc:100
MINLINE void copy_v4_v4(float r[4], const float a[4])
unsigned char uchar
int convert_v4(PyObject *obj, void *v)
void FrsMaterial_mathutils_register_callback()
int FrsMaterial_Init(PyObject *module)
static int FrsMaterial_specular_set(BPy_FrsMaterial *self, PyObject *value, void *)
#define MATHUTILS_SUBTYPE_DIFFUSE
static PyGetSetDef BPy_FrsMaterial_getseters[]
static int FrsMaterial_ambient_set(BPy_FrsMaterial *self, PyObject *value, void *)
static PyObject * FrsMaterial_specular_get(BPy_FrsMaterial *self, void *)
static void FrsMaterial_dealloc(BPy_FrsMaterial *self)
static int FrsMaterial_line_set(BPy_FrsMaterial *self, PyObject *value, void *)
static PyObject * BPy_FrsMaterial_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
static PyObject * FrsMaterial_line_get(BPy_FrsMaterial *self, void *)
static int FrsMaterial_mathutils_get_index(BaseMathObject *bmo, int subtype, int index)
static PyObject * FrsMaterial_emission_get(BPy_FrsMaterial *self, void *)
static PyObject * FrsMaterial_ambient_get(BPy_FrsMaterial *self, void *)
static int FrsMaterial_priority_set(BPy_FrsMaterial *self, PyObject *value, void *)
static int FrsMaterial_init(BPy_FrsMaterial *self, PyObject *args, PyObject *kwds)
#define MATHUTILS_SUBTYPE_EMISSION
#define MATHUTILS_SUBTYPE_LINE
static Mathutils_Callback FrsMaterial_mathutils_cb
PyTypeObject FrsMaterial_Type
static int FrsMaterial_emission_set(BPy_FrsMaterial *self, PyObject *value, void *)
static int FrsMaterial_diffuse_set(BPy_FrsMaterial *self, PyObject *value, void *)
static int FrsMaterial_mathutils_set(BaseMathObject *bmo, int subtype)
#define MATHUTILS_SUBTYPE_SPECULAR
static PyObject * FrsMaterial_shininess_get(BPy_FrsMaterial *self, void *)
static PyObject * FrsMaterial_repr(BPy_FrsMaterial *self)
#define MATHUTILS_SUBTYPE_AMBIENT
static int FrsMaterial_mathutils_get(BaseMathObject *bmo, int subtype)
PyDoc_STRVAR(FrsMaterial_doc, "Class defining a material.\n" "\n" ".. method:: __init__()\n" " __init__(brother)\n" " __init__(line, diffuse, ambient, specular, emission, shininess, priority)\n" "\n" " Creates a :class:`FrsMaterial` using either default constructor,\n" " copy constructor, or an overloaded constructor\n" "\n" " :arg brother: A Material object to be used as a copy constructor.\n" " :type brother: :class:`Material`\n" " :arg line: The line color.\n" " :type line: :class:`mathutils.Vector` | tuple[float, float, float, float] | list[float]\n" " :arg diffuse: The diffuse color.\n" " :type diffuse: \n" " :arg ambient: The ambient color.\n" " :type ambient: :class:`mathutils.Vector` | tuple[float, float, float, float] | " "list[float]\n" " :arg specular: The specular color.\n" " :type specular: :class:`mathutils.Vector` | tuple[float, float, float, float] | " "list[float]\n" " :arg emission: The emissive color.\n" " :type emission: :class:`mathutils.Vector` | tuple[float, float, float, float] | " "list[float]\n" " :arg shininess: The shininess coefficient.\n" " :type shininess: float\n" " :arg priority: The line color priority.\n" " :type priority: int\n")
static uchar FrsMaterial_mathutils_cb_index
static int FrsMaterial_mathutils_set_index(BaseMathObject *bmo, int subtype, int index)
static PyObject * FrsMaterial_priority_get(BPy_FrsMaterial *self, void *)
static PyObject * FrsMaterial_diffuse_get(BPy_FrsMaterial *self, void *)
static int FrsMaterial_shininess_set(BPy_FrsMaterial *self, PyObject *value, void *)
static int FrsMaterial_mathutils_check(BaseMathObject *bmo)
static Py_hash_t FrsMaterial_hash(PyObject *self)
#define BPy_FrsMaterial_Check(v)
PyObject * self
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:96
uchar Mathutils_RegisterCallback(Mathutils_Callback *cb)
Definition mathutils.cc:521
PyObject * Vector_CreatePyObject_cb(PyObject *cb_user, int vec_num, uchar cb_type, uchar cb_subtype)
inherits from class Rep
Definition AppCanvas.cpp:20
static struct PyModuleDef module
Definition python.cpp:796
PyObject_HEAD Freestyle::FrsMaterial * m