* Better support for vector-like objects in method arguments.
[blender.git] / source / blender / freestyle / intern / python / Interface1D / FEdge / BPy_FEdgeSharp.cpp
1 #include "BPy_FEdgeSharp.h"
2
3 #include "../../BPy_Convert.h"
4 #include "../../Interface0D/BPy_SVertex.h"
5
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9
10 ///////////////////////////////////////////////////////////////////////////////////////////
11
12 /*---------------  Python API function prototypes for FEdgeSharp instance  -----------*/
13 static int FEdgeSharp___init__(BPy_FEdgeSharp *self, PyObject *args, PyObject *kwds);
14
15 static PyObject * FEdgeSharp_normalA( BPy_FEdgeSharp *self ) ;
16 static PyObject * FEdgeSharp_normalB( BPy_FEdgeSharp *self );
17 static PyObject * FEdgeSharp_aMaterialIndex( BPy_FEdgeSharp *self ) ;
18 static PyObject * FEdgeSharp_bMaterialIndex( BPy_FEdgeSharp *self );
19 static PyObject * FEdgeSharp_aMaterial( BPy_FEdgeSharp *self );
20 static PyObject * FEdgeSharp_bMaterial( BPy_FEdgeSharp *self );
21 static PyObject * FEdgeSharp_setNormalA( BPy_FEdgeSharp *self, PyObject *args );
22 static PyObject * FEdgeSharp_setNormalB( BPy_FEdgeSharp *self, PyObject *args );
23 static PyObject * FEdgeSharp_setaMaterialIndex( BPy_FEdgeSharp *self, PyObject *args );
24 static PyObject * FEdgeSharp_setbMaterialIndex( BPy_FEdgeSharp *self, PyObject *args );
25
26 /*----------------------FEdgeSharp instance definitions ----------------------------*/
27 static PyMethodDef BPy_FEdgeSharp_methods[] = { 
28         {"normalA", ( PyCFunction ) FEdgeSharp_normalA, METH_NOARGS, "() Returns the normal to the face lying on the right of the FEdge. If this FEdge is a border, it has no Face on its right and therefore, no normal."},
29         {"normalB", ( PyCFunction ) FEdgeSharp_normalB, METH_NOARGS, "() Returns the normal to the face lying on the left of the FEdge."},
30         {"aMaterialIndex", ( PyCFunction ) FEdgeSharp_aMaterialIndex, METH_NOARGS, "() Returns the index of the material of the face lying on the right of the FEdge. If this FEdge is a border, it has no Face on its right and therefore, no material. "},
31         {"bMaterialIndex", ( PyCFunction ) FEdgeSharp_bMaterialIndex, METH_NOARGS, "() Returns the material of the face lying on the left of the FEdge. "},
32         {"aMaterial", ( PyCFunction ) FEdgeSharp_aMaterial, METH_NOARGS, "() Returns the material of the face lying on the right of the FEdge. If this FEdge is a border, it has no Face on its right and therefore, no material."},
33         {"bMaterial", ( PyCFunction ) FEdgeSharp_bMaterial, METH_NOARGS, "() Returns the material of the face lying on the left of the FEdge."},
34         {"setNormalA", ( PyCFunction ) FEdgeSharp_setNormalA, METH_VARARGS, "([x,y,z]) Sets the normal to the face lying on the right of the FEdge."},
35         {"setNormalB", ( PyCFunction ) FEdgeSharp_setNormalB, METH_VARARGS, "([x,y,z]) Sets the normal to the face lying on the left of the FEdge. "},
36         {"setaMaterialIndex", ( PyCFunction ) FEdgeSharp_setaMaterialIndex, METH_VARARGS, "(unsigned int i) Sets the index of the material lying on the right of the FEdge. "},
37         {"setbMaterialIndex", ( PyCFunction ) FEdgeSharp_setbMaterialIndex, METH_VARARGS, "(unsigned int i) Sets the index of the material lying on the left of the FEdge. "},
38         {NULL, NULL, 0, NULL}
39 };
40
41 /*-----------------------BPy_FEdgeSharp type definition ------------------------------*/
42
43 PyTypeObject FEdgeSharp_Type = {
44         PyObject_HEAD_INIT( NULL ) 
45         0,                                                      /* ob_size */
46         "FEdgeSharp",                           /* tp_name */
47         sizeof( BPy_FEdgeSharp ),       /* tp_basicsize */
48         0,                                                      /* tp_itemsize */
49         
50         /* methods */
51         NULL,   /* tp_dealloc */
52         NULL,                                                   /* printfunc tp_print; */
53         NULL,                                                   /* getattrfunc tp_getattr; */
54         NULL,                                                   /* setattrfunc tp_setattr; */
55         NULL,                                                                           /* tp_compare */
56         NULL,                                   /* tp_repr */
57
58         /* Method suites for standard classes */
59
60         NULL,                       /* PyNumberMethods *tp_as_number; */
61         NULL,                       /* PySequenceMethods *tp_as_sequence; */
62         NULL,                       /* PyMappingMethods *tp_as_mapping; */
63
64         /* More standard operations (here for binary compatibility) */
65
66         NULL,                                           /* hashfunc tp_hash; */
67         NULL,                       /* ternaryfunc tp_call; */
68         NULL,                       /* reprfunc tp_str; */
69         NULL,                       /* getattrofunc tp_getattro; */
70         NULL,                       /* setattrofunc tp_setattro; */
71
72         /* Functions to access object as input/output buffer */
73         NULL,                       /* PyBufferProcs *tp_as_buffer; */
74
75   /*** Flags to define presence of optional/expanded features ***/
76         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,               /* long tp_flags; */
77
78         NULL,                       /*  char *tp_doc;  Documentation string */
79   /*** Assigned meaning in release 2.0 ***/
80         /* call function for all accessible objects */
81         NULL,                       /* traverseproc tp_traverse; */
82
83         /* delete references to contained objects */
84         NULL,                       /* inquiry tp_clear; */
85
86   /***  Assigned meaning in release 2.1 ***/
87   /*** rich comparisons ***/
88         NULL,                       /* richcmpfunc tp_richcompare; */
89
90   /***  weak reference enabler ***/
91         0,                          /* long tp_weaklistoffset; */
92
93   /*** Added in release 2.2 ***/
94         /*   Iterators */
95         NULL,                       /* getiterfunc tp_iter; */
96         NULL,                       /* iternextfunc tp_iternext; */
97
98   /*** Attribute descriptor and subclassing stuff ***/
99         BPy_FEdgeSharp_methods, /* struct PyMethodDef *tp_methods; */
100         NULL,                           /* struct PyMemberDef *tp_members; */
101         NULL,                                           /* struct PyGetSetDef *tp_getset; */
102         &FEdge_Type,                            /* struct _typeobject *tp_base; */
103         NULL,                                                   /* PyObject *tp_dict; */
104         NULL,                                                   /* descrgetfunc tp_descr_get; */
105         NULL,                                                   /* descrsetfunc tp_descr_set; */
106         0,                              /* long tp_dictoffset; */
107         (initproc)FEdgeSharp___init__,                          /* initproc tp_init; */
108         NULL,                                                   /* allocfunc tp_alloc; */
109         NULL,           /* newfunc tp_new; */
110         
111         /*  Low-level free-memory routine */
112         NULL,                       /* freefunc tp_free;  */
113         
114         /* For PyObject_IS_GC */
115         NULL,                       /* inquiry tp_is_gc;  */
116         NULL,                       /* PyObject *tp_bases; */
117         
118         /* method resolution order */
119         NULL,                       /* PyObject *tp_mro;  */
120         NULL,                       /* PyObject *tp_cache; */
121         NULL,                       /* PyObject *tp_subclasses; */
122         NULL,                       /* PyObject *tp_weaklist; */
123         NULL
124 };
125
126 //------------------------INSTANCE METHODS ----------------------------------
127
128 int FEdgeSharp___init__(BPy_FEdgeSharp *self, PyObject *args, PyObject *kwds)
129 {
130         PyObject *obj1 = 0, *obj2 = 0;
131
132     if (! PyArg_ParseTuple(args, "|OO", &obj1, &obj2) )
133         return -1;
134
135         if( !obj1 ){
136                 self->fes = new FEdgeSharp();
137                 
138         } else if( BPy_FEdgeSharp_Check(obj1) ) {
139                 self->fes = new FEdgeSharp(*( ((BPy_FEdgeSharp *) obj1)->fes ));
140                 
141         } else if( BPy_SVertex_Check(obj1) && BPy_SVertex_Check(obj2) ) {
142                 self->fes = new FEdgeSharp( ((BPy_SVertex *) obj1)->sv, ((BPy_SVertex *) obj2)->sv );
143
144         } else {
145                 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
146                 return -1;
147         }
148
149         self->py_fe.fe = self->fes;
150         self->py_fe.py_if1D.if1D = self->fes;
151
152         return 0;
153 }
154
155
156 PyObject * FEdgeSharp_normalA( BPy_FEdgeSharp *self ) {
157         Vec3r v( self->fes->normalA() );
158         return Vector_from_Vec3r( v );
159 }
160
161 PyObject * FEdgeSharp_normalB( BPy_FEdgeSharp *self ) {
162         Vec3r v( self->fes->normalB() );
163         return Vector_from_Vec3r( v );
164 }
165
166 PyObject * FEdgeSharp_aMaterialIndex( BPy_FEdgeSharp *self ) {
167         return PyInt_FromLong( self->fes->aFrsMaterialIndex() );
168 }
169
170 PyObject * FEdgeSharp_bMaterialIndex( BPy_FEdgeSharp *self ) {
171         return PyInt_FromLong( self->fes->bFrsMaterialIndex() );
172 }
173
174 PyObject * FEdgeSharp_aMaterial( BPy_FEdgeSharp *self ) {
175         FrsMaterial m( self->fes->aFrsMaterial() );
176         return BPy_FrsMaterial_from_FrsMaterial(m);
177 }
178
179 PyObject * FEdgeSharp_bMaterial( BPy_FEdgeSharp *self ) {
180         FrsMaterial m( self->fes->aFrsMaterial() );
181         return BPy_FrsMaterial_from_FrsMaterial(m);
182 }
183
184 PyObject * FEdgeSharp_setNormalA( BPy_FEdgeSharp *self, PyObject *args ) {
185         PyObject *obj = 0;
186
187         if(!( PyArg_ParseTuple(args, "O", &obj) ))
188                 return NULL;
189         Vec3r *v = Vec3r_ptr_from_PyObject(obj);
190         if( !v ) {
191                 PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
192                 return NULL;
193         }
194         self->fes->setNormalA( *v );
195         delete v;
196
197         Py_RETURN_NONE;
198 }
199
200 PyObject * FEdgeSharp_setNormalB( BPy_FEdgeSharp *self, PyObject *args ) {
201         PyObject *obj = 0;
202
203         if(!( PyArg_ParseTuple(args, "O", &obj) ))
204                 return NULL;
205         Vec3r *v = Vec3r_ptr_from_PyObject(obj);
206         if( !v ) {
207                 PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
208                 return NULL;
209         }
210         self->fes->setNormalB( *v );
211         delete v;
212
213         Py_RETURN_NONE;
214 }
215
216 PyObject * FEdgeSharp_setaMaterialIndex( BPy_FEdgeSharp *self, PyObject *args ) {
217         unsigned int i;
218
219         if(!( PyArg_ParseTuple(args, "I", &i) ))
220                 return NULL;
221         
222         self->fes->setaFrsMaterialIndex( i );
223
224         Py_RETURN_NONE;
225 }
226
227 PyObject * FEdgeSharp_setbMaterialIndex( BPy_FEdgeSharp *self, PyObject *args ) {
228         unsigned int i;
229
230         if(!( PyArg_ParseTuple(args, "I", &i) ))
231                 return NULL;
232         
233         self->fes->setbFrsMaterialIndex( i );
234
235         Py_RETURN_NONE;
236 }
237
238 ///////////////////////////////////////////////////////////////////////////////////////////
239
240 #ifdef __cplusplus
241 }
242 #endif