a36d446fcb77ba7f202612973544f72941256109
[blender-staging.git] / source / blender / freestyle / intern / python / BPy_FrsMaterial.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp
22  *  \ingroup freestyle
23  */
24
25 #include "BPy_FrsMaterial.h"
26
27 #include "BPy_Convert.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #include "BLI_hash_mm2a.h"
34
35
36 ///////////////////////////////////////////////////////////////////////////////////////////
37
38 //-------------------MODULE INITIALIZATION--------------------------------
39 int FrsMaterial_Init(PyObject *module)
40 {
41         if (module == NULL)
42                 return -1;
43
44         if (PyType_Ready(&FrsMaterial_Type) < 0)
45                 return -1;
46         Py_INCREF(&FrsMaterial_Type);
47         PyModule_AddObject(module, "Material", (PyObject *)&FrsMaterial_Type);
48
49         FrsMaterial_mathutils_register_callback();
50
51         return 0;
52 }
53
54 //------------------------INSTANCE METHODS ----------------------------------
55
56 PyDoc_STRVAR(FrsMaterial_doc,
57 "Class defining a material.\n"
58 "\n"
59 ".. method:: __init__()\n"
60 "\n"
61 "   Default constructor.\n"
62 "\n"
63 ".. method:: __init__(brother)\n"
64 "\n"
65 "   Copy constructor.\n"
66 "\n"
67 "   :arg brother: A Material object.\n"
68 "   :type brother: :class:`Material`\n"
69 "\n"
70 ".. method:: __init__(line, diffuse, ambient, specular, emission, shininess, priority)\n"
71 "\n"
72 "   Builds a Material from its line, diffuse, ambient, specular, emissive\n"
73 "   colors, a shininess coefficient and line color priority.\n"
74 "\n"
75 "   :arg line: The line color.\n"
76 "   :type line: :class:`mathutils.Vector`, list or tuple of 4 float values\n"
77 "   :arg diffuse: The diffuse color.\n"
78 "   :type diffuse: :class:`mathutils.Vector`, list or tuple of 4 float values\n"
79 "   :arg ambient: The ambient color.\n"
80 "   :type ambient: :class:`mathutils.Vector`, list or tuple of 4 float values\n"
81 "   :arg specular: The specular color.\n"
82 "   :type specular: :class:`mathutils.Vector`, list or tuple of 4 float values\n"
83 "   :arg emission: The emissive color.\n"
84 "   :type emission: :class:`mathutils.Vector`, list or tuple of 4 float values\n"
85 "   :arg shininess: The shininess coefficient.\n"
86 "   :type shininess: float\n"
87 "   :arg priority: The line color priority.\n"
88 "   :type priority: int");
89
90 static int FrsMaterial_init(BPy_FrsMaterial *self, PyObject *args, PyObject *kwds)
91 {
92         static const char *kwlist_1[] = {"brother", NULL};
93         static const char *kwlist_2[] = {"line", "diffuse", "ambient", "specular", "emission", "shininess", "priority", NULL};
94         PyObject *brother = 0;
95         float line[4], diffuse[4], ambient[4], specular[4], emission[4], shininess;
96         int priority;
97
98         if (PyArg_ParseTupleAndKeywords(args, kwds, "|O!", (char **)kwlist_1, &FrsMaterial_Type, &brother)) {
99                 if (!brother) {
100                         self->m = new FrsMaterial();
101                 }
102                 else {
103                         FrsMaterial *m = ((BPy_FrsMaterial *)brother)->m;
104                         if (!m) {
105                                 PyErr_SetString(PyExc_RuntimeError, "invalid Material object");
106                                 return -1;
107                         }
108                         self->m = new FrsMaterial(*m);
109                 }
110         }
111         else if (PyErr_Clear(),
112                  PyArg_ParseTupleAndKeywords(args, kwds, "O&O&O&O&O&fi", (char **)kwlist_2,
113                                              convert_v4, line,
114                                              convert_v4, diffuse,
115                                              convert_v4, ambient,
116                                              convert_v4, specular,
117                                              convert_v4, emission,
118                                              &shininess, &priority))
119         {
120                 self->m = new FrsMaterial(line, diffuse, ambient, specular, emission, shininess, priority);
121         }
122         else {
123                 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
124                 return -1;
125         }
126         return 0;
127 }
128
129 static void FrsMaterial_dealloc(BPy_FrsMaterial *self)
130 {
131         delete self->m;
132         Py_TYPE(self)->tp_free((PyObject *)self);
133 }
134
135 static PyObject *FrsMaterial_repr(BPy_FrsMaterial *self)
136 {
137         return PyUnicode_FromFormat("Material - address: %p", self->m);
138 }
139
140 /*----------------------mathutils callbacks ----------------------------*/
141
142 /* subtype */
143 #define MATHUTILS_SUBTYPE_DIFFUSE   1
144 #define MATHUTILS_SUBTYPE_SPECULAR  2
145 #define MATHUTILS_SUBTYPE_AMBIENT   3
146 #define MATHUTILS_SUBTYPE_EMISSION  4
147 #define MATHUTILS_SUBTYPE_LINE      5
148
149 static int FrsMaterial_mathutils_check(BaseMathObject *bmo)
150 {
151         if (!BPy_FrsMaterial_Check(bmo->cb_user))
152                 return -1;
153         return 0;
154 }
155
156 static int FrsMaterial_mathutils_get(BaseMathObject *bmo, int subtype)
157 {
158         BPy_FrsMaterial *self = (BPy_FrsMaterial *)bmo->cb_user;
159         switch (subtype) {
160         case MATHUTILS_SUBTYPE_LINE:
161                 bmo->data[0] = self->m->lineR();
162                 bmo->data[1] = self->m->lineG();
163                 bmo->data[2] = self->m->lineB();
164                 bmo->data[3] = self->m->lineA();
165                 break;
166         case MATHUTILS_SUBTYPE_DIFFUSE:
167                 bmo->data[0] = self->m->diffuseR();
168                 bmo->data[1] = self->m->diffuseG();
169                 bmo->data[2] = self->m->diffuseB();
170                 bmo->data[3] = self->m->diffuseA();
171                 break;
172         case MATHUTILS_SUBTYPE_SPECULAR:
173                 bmo->data[0] = self->m->specularR();
174                 bmo->data[1] = self->m->specularG();
175                 bmo->data[2] = self->m->specularB();
176                 bmo->data[3] = self->m->specularA();
177                 break;
178         case MATHUTILS_SUBTYPE_AMBIENT:
179                 bmo->data[0] = self->m->ambientR();
180                 bmo->data[1] = self->m->ambientG();
181                 bmo->data[2] = self->m->ambientB();
182                 bmo->data[3] = self->m->ambientA();
183                 break;
184         case MATHUTILS_SUBTYPE_EMISSION:
185                 bmo->data[0] = self->m->emissionR();
186                 bmo->data[1] = self->m->emissionG();
187                 bmo->data[2] = self->m->emissionB();
188                 bmo->data[3] = self->m->emissionA();
189                 break;
190         default:
191                 return -1;
192         }
193         return 0;
194 }
195
196 static int FrsMaterial_mathutils_set(BaseMathObject *bmo, int subtype)
197 {
198         BPy_FrsMaterial *self = (BPy_FrsMaterial *)bmo->cb_user;
199         switch (subtype) {
200         case MATHUTILS_SUBTYPE_LINE:
201                 self->m->setLine(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
202                 break;
203         case MATHUTILS_SUBTYPE_DIFFUSE:
204                 self->m->setDiffuse(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
205                 break;
206         case MATHUTILS_SUBTYPE_SPECULAR:
207                 self->m->setSpecular(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
208                 break;
209         case MATHUTILS_SUBTYPE_AMBIENT:
210                 self->m->setAmbient(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
211                 break;
212         case MATHUTILS_SUBTYPE_EMISSION:
213                 self->m->setEmission(bmo->data[0], bmo->data[1], bmo->data[2], bmo->data[3]);
214                 break;
215         default:
216                 return -1;
217         }
218         return 0;
219 }
220
221 static int FrsMaterial_mathutils_get_index(BaseMathObject *bmo, int subtype, int index)
222 {
223         BPy_FrsMaterial *self = (BPy_FrsMaterial *)bmo->cb_user;
224         switch (subtype) {
225         case MATHUTILS_SUBTYPE_LINE:
226         {
227                 const float *color = self->m->line();
228                 bmo->data[index] = color[index];
229         }
230                 break;
231         case MATHUTILS_SUBTYPE_DIFFUSE:
232                 {
233                         const float *color = self->m->diffuse();
234                         bmo->data[index] = color[index];
235                 }
236                 break;
237         case MATHUTILS_SUBTYPE_SPECULAR:
238                 {
239                         const float *color = self->m->specular();
240                         bmo->data[index] = color[index];
241                 }
242                 break;
243         case MATHUTILS_SUBTYPE_AMBIENT:
244                 {
245                         const float *color = self->m->ambient();
246                         bmo->data[index] = color[index];
247                 }
248                 break;
249         case MATHUTILS_SUBTYPE_EMISSION:
250                 {
251                         const float *color = self->m->emission();
252                         bmo->data[index] = color[index];
253                 }
254                 break;
255         default:
256                 return -1;
257         }
258         return 0;
259 }
260
261 static int FrsMaterial_mathutils_set_index(BaseMathObject *bmo, int subtype, int index)
262 {
263         BPy_FrsMaterial *self = (BPy_FrsMaterial *)bmo->cb_user;
264         float color[4];
265         switch (subtype) {
266         case MATHUTILS_SUBTYPE_LINE:
267                 copy_v4_v4(color, self->m->line());
268                 color[index] = bmo->data[index];
269                 self->m->setLine(color[0], color[1], color[2], color[3]);
270                 break;
271         case MATHUTILS_SUBTYPE_DIFFUSE:
272                 copy_v4_v4(color, self->m->diffuse());
273                 color[index] = bmo->data[index];
274                 self->m->setDiffuse(color[0], color[1], color[2], color[3]);
275                 break;
276         case MATHUTILS_SUBTYPE_SPECULAR:
277                 copy_v4_v4(color, self->m->specular());
278                 color[index] = bmo->data[index];
279                 self->m->setSpecular(color[0], color[1], color[2], color[3]);
280                 break;
281         case MATHUTILS_SUBTYPE_AMBIENT:
282                 copy_v4_v4(color, self->m->ambient());
283                 color[index] = bmo->data[index];
284                 self->m->setAmbient(color[0], color[1], color[2], color[3]);
285                 break;
286         case MATHUTILS_SUBTYPE_EMISSION:
287                 copy_v4_v4(color, self->m->emission());
288                 color[index] = bmo->data[index];
289                 self->m->setEmission(color[0], color[1], color[2], color[3]);
290                 break;
291         default:
292                 return -1;
293         }
294         return 0;
295 }
296
297 static Mathutils_Callback FrsMaterial_mathutils_cb = {
298         FrsMaterial_mathutils_check,
299         FrsMaterial_mathutils_get,
300         FrsMaterial_mathutils_set,
301         FrsMaterial_mathutils_get_index,
302         FrsMaterial_mathutils_set_index
303 };
304
305 static unsigned char FrsMaterial_mathutils_cb_index = -1;
306
307 void FrsMaterial_mathutils_register_callback()
308 {
309         FrsMaterial_mathutils_cb_index = Mathutils_RegisterCallback(&FrsMaterial_mathutils_cb);
310 }
311
312 /*----------------------FrsMaterial get/setters ----------------------------*/
313
314 PyDoc_STRVAR(FrsMaterial_line_doc,
315 "RGBA components of the line color of the material.\n"
316 "\n"
317 ":type: :class:`mathutils.Vector`");
318
319 static PyObject *FrsMaterial_line_get(BPy_FrsMaterial *self, void *UNUSED(closure))
320 {
321         return Vector_CreatePyObject_cb((PyObject *)self, 4, FrsMaterial_mathutils_cb_index, MATHUTILS_SUBTYPE_LINE);
322 }
323
324 static int FrsMaterial_line_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
325 {
326         float color[4];
327         if (mathutils_array_parse(color, 4, 4, value,
328                                   "value must be a 4-dimensional vector") == -1)
329         {
330                 return -1;
331         }
332         self->m->setLine(color[0], color[1], color[2], color[3]);
333         return 0;
334 }
335
336 PyDoc_STRVAR(FrsMaterial_diffuse_doc,
337 "RGBA components of the diffuse color of the material.\n"
338 "\n"
339 ":type: :class:`mathutils.Vector`");
340
341 static PyObject *FrsMaterial_diffuse_get(BPy_FrsMaterial *self, void *UNUSED(closure))
342 {
343         return Vector_CreatePyObject_cb((PyObject *)self, 4, FrsMaterial_mathutils_cb_index, MATHUTILS_SUBTYPE_DIFFUSE);
344 }
345
346 static int FrsMaterial_diffuse_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
347 {
348         float color[4];
349         if (mathutils_array_parse(color, 4, 4, value,
350                                   "value must be a 4-dimensional vector") == -1)
351         {
352                 return -1;
353         }
354         self->m->setDiffuse(color[0], color[1], color[2], color[3]);
355         return 0;
356 }
357
358 PyDoc_STRVAR(FrsMaterial_specular_doc,
359 "RGBA components of the specular color of the material.\n"
360 "\n"
361 ":type: :class:`mathutils.Vector`");
362
363 static PyObject *FrsMaterial_specular_get(BPy_FrsMaterial *self, void *UNUSED(closure))
364 {
365         return Vector_CreatePyObject_cb((PyObject *)self, 4, FrsMaterial_mathutils_cb_index, MATHUTILS_SUBTYPE_SPECULAR);
366 }
367
368 static int FrsMaterial_specular_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
369 {
370         float color[4];
371         if (mathutils_array_parse(color, 4, 4, value,
372                                   "value must be a 4-dimensional vector") == -1)
373         {
374                 return -1;
375         }
376         self->m->setSpecular(color[0], color[1], color[2], color[3]);
377         return 0;
378 }
379
380 PyDoc_STRVAR(FrsMaterial_ambient_doc,
381 "RGBA components of the ambient color of the material.\n"
382 "\n"
383 ":type: :class:`mathutils.Color`");
384
385 static PyObject *FrsMaterial_ambient_get(BPy_FrsMaterial *self, void *UNUSED(closure))
386 {
387         return Vector_CreatePyObject_cb((PyObject *)self, 4, FrsMaterial_mathutils_cb_index, MATHUTILS_SUBTYPE_AMBIENT);
388 }
389
390 static int FrsMaterial_ambient_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
391 {
392         float color[4];
393         if (mathutils_array_parse(color, 4, 4, value,
394                                   "value must be a 4-dimensional vector") == -1)
395         {
396                 return -1;
397         }
398         self->m->setAmbient(color[0], color[1], color[2], color[3]);
399         return 0;
400 }
401
402 PyDoc_STRVAR(FrsMaterial_emission_doc,
403 "RGBA components of the emissive color of the material.\n"
404 "\n"
405 ":type: :class:`mathutils.Color`");
406
407 static PyObject *FrsMaterial_emission_get(BPy_FrsMaterial *self, void *UNUSED(closure))
408 {
409         return Vector_CreatePyObject_cb((PyObject *)self, 4, FrsMaterial_mathutils_cb_index, MATHUTILS_SUBTYPE_EMISSION);
410 }
411
412 static int FrsMaterial_emission_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
413 {
414         float color[4];
415         if (mathutils_array_parse(color, 4, 4, value,
416                                   "value must be a 4-dimensional vector") == -1)
417         {
418                 return -1;
419         }
420         self->m->setEmission(color[0], color[1], color[2], color[3]);
421         return 0;
422 }
423
424 PyDoc_STRVAR(FrsMaterial_shininess_doc,
425 "Shininess coefficient of the material.\n"
426 "\n"
427 ":type: float");
428
429 static PyObject *FrsMaterial_shininess_get(BPy_FrsMaterial *self, void *UNUSED(closure))
430 {
431         return PyFloat_FromDouble(self->m->shininess());
432 }
433
434 static int FrsMaterial_shininess_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
435 {
436         float scalar;
437         if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) { /* parsed item not a number */
438                 PyErr_SetString(PyExc_TypeError, "value must be a number");
439                 return -1;
440         }
441         self->m->setShininess(scalar);
442         return 0;
443 }
444
445 PyDoc_STRVAR(FrsMaterial_priority_doc,
446 "Line color priority of the material.\n"
447 "\n"
448 ":type: int");
449
450 static PyObject *FrsMaterial_priority_get(BPy_FrsMaterial *self, void *UNUSED(closure))
451 {
452         return PyLong_FromLong(self->m->priority());
453 }
454
455 static int FrsMaterial_priority_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
456 {
457         int scalar;
458         if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
459                 PyErr_SetString(PyExc_TypeError, "value must be an integer");
460                 return -1;
461         }
462         self->m->setPriority(scalar);
463         return 0;
464 }
465
466 static PyGetSetDef BPy_FrsMaterial_getseters[] = {
467         {(char *)"line", (getter)FrsMaterial_line_get, (setter)FrsMaterial_line_set,
468                          (char *)FrsMaterial_line_doc, NULL},
469         {(char *)"diffuse", (getter)FrsMaterial_diffuse_get, (setter)FrsMaterial_diffuse_set,
470                             (char *)FrsMaterial_diffuse_doc, NULL},
471         {(char *)"specular", (getter)FrsMaterial_specular_get, (setter)FrsMaterial_specular_set,
472                              (char *)FrsMaterial_specular_doc, NULL},
473         {(char *)"ambient", (getter)FrsMaterial_ambient_get, (setter)FrsMaterial_ambient_set,
474                             (char *)FrsMaterial_ambient_doc, NULL},
475         {(char *)"emission", (getter)FrsMaterial_emission_get, (setter)FrsMaterial_emission_set,
476                              (char *)FrsMaterial_emission_doc, NULL},
477         {(char *)"shininess", (getter)FrsMaterial_shininess_get, (setter)FrsMaterial_shininess_set,
478                               (char *)FrsMaterial_shininess_doc, NULL},
479         {(char *)"priority", (getter)FrsMaterial_priority_get, (setter)FrsMaterial_priority_set,
480                              (char *)FrsMaterial_priority_doc, NULL},
481         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
482 };
483
484 static PyObject *BPy_FrsMaterial_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
485 {
486         const BPy_FrsMaterial *matA = NULL, *matB = NULL;
487         bool result = 0;
488
489         if (!BPy_FrsMaterial_Check(objectA) || !BPy_FrsMaterial_Check(objectB)) {
490                 if (comparison_type == Py_NE) {
491                         Py_RETURN_TRUE;
492                 }
493                 else {
494                         Py_RETURN_FALSE;
495                 }
496         }
497
498         matA = (BPy_FrsMaterial *)objectA;
499         matB = (BPy_FrsMaterial *)objectB;
500
501         switch (comparison_type) {
502                 case Py_NE:
503                         result = (*matA->m) != (*matB->m);
504                         break;
505                 case Py_EQ:
506                         result = (*matA->m) == (*matB->m);
507                         break;
508                 default:
509                         PyErr_SetString(PyExc_TypeError, "Material does not support this comparison type");
510                         return NULL;
511         }
512
513         if (result == true) {
514                 Py_RETURN_TRUE;
515         }
516         else {
517                 Py_RETURN_FALSE;
518         }
519 }
520
521
522 static Py_hash_t FrsMaterial_hash(PyObject *self)
523 {
524         return (Py_uhash_t)BLI_hash_mm2((const unsigned char *)self, sizeof(*self), 0);
525 }
526 /*-----------------------BPy_FrsMaterial type definition ------------------------------*/
527
528 PyTypeObject FrsMaterial_Type = {
529         PyVarObject_HEAD_INIT(NULL, 0)
530         "Material",                     /* tp_name */
531         sizeof(BPy_FrsMaterial),        /* tp_basicsize */
532         0,                              /* tp_itemsize */
533         (destructor)FrsMaterial_dealloc, /* tp_dealloc */
534         0,                              /* tp_print */
535         0,                              /* tp_getattr */
536         0,                              /* tp_setattr */
537         0,                              /* tp_reserved */
538         (reprfunc)FrsMaterial_repr,     /* tp_repr */
539         0,                              /* tp_as_number */
540         0,                              /* tp_as_sequence */
541         0,                              /* tp_as_mapping */
542         (hashfunc)FrsMaterial_hash,     /* tp_hash  */
543         0,                              /* tp_call */
544         0,                              /* tp_str */
545         0,                              /* tp_getattro */
546         0,                              /* tp_setattro */
547         0,                              /* tp_as_buffer */
548         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
549         FrsMaterial_doc,                /* tp_doc */
550         0,                              /* tp_traverse */
551         0,                              /* tp_clear */
552         (richcmpfunc)BPy_FrsMaterial_richcmpr, /* tp_richcompare */
553         0,                              /* tp_weaklistoffset */
554         0,                              /* tp_iter */
555         0,                              /* tp_iternext */
556         0,                              /* tp_methods */
557         0,                              /* tp_members */
558         BPy_FrsMaterial_getseters,      /* tp_getset */
559         0,                              /* tp_base */
560         0,                              /* tp_dict */
561         0,                              /* tp_descr_get */
562         0,                              /* tp_descr_set */
563         0,                              /* tp_dictoffset */
564         (initproc)FrsMaterial_init,     /* tp_init */
565         0,                              /* tp_alloc */
566         PyType_GenericNew,              /* tp_new */
567 };
568
569 ///////////////////////////////////////////////////////////////////////////////////////////
570
571 #ifdef __cplusplus
572 }
573 #endif