d0580cb780d3e1e4d71187d4f7172570e2771a2a
[blender.git] / source / blender / python / api2_2x / Ipocurve.c
1 /*
2  *
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * This is a new part of Blender.
26  *
27  * Contributor(s): Jacques Guignot
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include "Ipocurve.h"
33
34 /*****************************************************************************/
35 /* Function:              M_IpoCurve_New                                          */
36 /* Python equivalent:     Blender.IpoCurve.New                                    */
37 /*****************************************************************************/
38 static PyObject *M_IpoCurve_New(PyObject *self, PyObject *args)
39 {
40
41
42         return 0;
43 }
44
45
46 /*****************************************************************************/
47 /* Function:              Ipo_Init                                           */
48 /*****************************************************************************/
49 PyObject *IpoCurve_Init (void)
50 {
51   PyObject  *submodule;
52
53   IpoCurve_Type.ob_type = &PyType_Type;
54
55   submodule = Py_InitModule3("Blender.IpoCurve", M_IpoCurve_methods, M_IpoCurve_doc);
56
57   return (submodule);
58 }
59
60 /*****************************************************************************/
61 /* Function:              M_IpoCurve_Get                                     */
62 /* Python equivalent:     Blender.IpoCurve.Get                               */
63 /* Description:           Receives a string and returns the ipo data obj     */
64 /*                        whose name matches the string.  If no argument is  */
65 /*                           passed in, a list of all ipo data names in the  */
66 /*                        current scene is returned.                         */
67 /*****************************************************************************/
68 static PyObject *M_IpoCurve_Get(PyObject *self, PyObject *args)
69 {
70         return 0;
71 }
72
73
74 /*****************************************************************************/
75 /* Python C_IpoCurve methods:                                                */
76 /*****************************************************************************/
77
78 static PyObject *IpoCurve_setInterpolation( C_IpoCurve * self, PyObject *args)
79 {
80         char*interpolationtype = 0;
81         int id = -1;
82   if (!PyArg_ParseTuple(args, "s", &interpolationtype))
83     return (EXPP_ReturnPyObjError (PyExc_TypeError,"expected string argument"));
84         if (!strcmp(interpolationtype,"Bezier"))id = IPO_BEZ;
85         if (!strcmp(interpolationtype,"Constant"))id = IPO_CONST;
86         if (!strcmp(interpolationtype,"Linear"))id = IPO_LIN;
87         if (id == -1)
88     return (EXPP_ReturnPyObjError (PyExc_TypeError,"bad interpolation ytpe"));
89   Py_INCREF(Py_None);
90   return Py_None;
91 }
92 static PyObject *IpoCurve_getInterpolation( C_IpoCurve * self)
93 {
94
95   Py_INCREF(Py_None);
96   return Py_None;
97 }
98 static PyObject *IpoCurve_setExtrapolation( C_IpoCurve * self, PyObject *args)
99 {
100
101   Py_INCREF(Py_None);
102   return Py_None;
103 }
104 static PyObject *IpoCurve_getExtrapolation( C_IpoCurve * self)
105 {
106
107   Py_INCREF(Py_None);
108   return Py_None;
109 }
110 static PyObject *IpoCurve_addBezier( C_IpoCurve * self, PyObject *args)
111 {
112 short MEM_freeN(void *vmemh)    ;
113         void *MEM_mallocN(unsigned int len, char *str);
114         char *str=0;
115         float x,y;
116         int npoints;
117         IpoCurve *icu;
118         BezTriple *bzt,*tmp;
119         static char name[10] = "mlml";
120         PyObject*popo = 0;
121   if (!PyArg_ParseTuple(args, "Os", &popo,&str))
122     return (EXPP_ReturnPyObjError (PyExc_TypeError,"expected list argument"));
123         x = PyFloat_AsDouble(PyTuple_GetItem(popo,0));
124         y = PyFloat_AsDouble(PyTuple_GetItem(popo,1));
125   icu = self->ipocurve;
126         npoints = icu->totvert;
127         tmp = icu->bezt;
128         icu->bezt = MEM_mallocN(sizeof(BezTriple)*(npoints+1),name);
129         if(tmp){
130         memmove(icu->bezt,tmp,sizeof(BezTriple)*npoints);
131         MEM_freeN(tmp);
132         }
133         memmove(icu->bezt+npoints,icu->bezt,sizeof(BezTriple));
134         icu->totvert++;
135         bzt = icu->bezt + npoints;
136         bzt->vec[0][0] = x-1;
137         bzt->vec[1][0] = x;
138         bzt->vec[2][0] = x+1;
139         bzt->vec[0][1] = y-1;
140         bzt->vec[1][1] = y;
141         bzt->vec[2][1] = y+1;
142   Py_INCREF(Py_None);
143   return Py_None;
144 }
145
146 static PyObject *IpoCurve_setName(C_IpoCurve *self, PyObject *args)
147 {
148         return 0;
149 }
150
151 static PyObject *IpoCurve_Recalc(C_IpoCurve *self)
152 {
153         void testhandles_ipocurve(IpoCurve *icu);
154         IpoCurve *icu = self->ipocurve;
155         testhandles_ipocurve(icu); 
156   Py_INCREF(Py_None);
157   return Py_None;
158 }
159
160 static PyObject* IpoCurve_getName (C_IpoCurve *self)
161 {
162         char * nametab[24] = {"LocX","LocY","LocZ","dLocX","dLocY","dLocZ","RotX","RotY","RotZ","dRotX","dRotY","dRotZ","SizeX","SizeY","SizeZ","dSizeX","dSizeY","dSizeZ","Layer","Time","ColR","ColG","ColB","ColA"};
163
164         //      printf("IpoCurve_getName %d\n",self->ipocurve->vartype);
165         if (self->ipocurve->adrcode <=0 )
166 return PyString_FromString("Index too small");
167         if (self->ipocurve->adrcode >= 25 )
168 return PyString_FromString("Index too big");
169
170 return PyString_FromString(nametab[self->ipocurve->adrcode-1]);
171
172 }
173
174
175
176 static void IpoCurveDeAlloc (C_IpoCurve *self)
177 {
178   PyObject_DEL (self);
179 }
180
181 static PyObject* IpoCurve_getPoints (C_IpoCurve *self)
182 {       
183 struct BezTriple *bezt;
184         PyObject* l = PyList_New(0);
185                                 int i;
186                                 for(i = 0;i<self->ipocurve->totvert;i++)
187                                         {
188                                                 bezt = self->ipocurve->bezt + i;
189   PyList_Append( l, BezTriple_CreatePyObject(bezt));
190                                         }
191   return l;
192 }
193
194
195
196 int IpoCurve_setPoints (C_IpoCurve *self, PyObject *value )
197 {       
198 struct BezTriple *bezt;
199         PyObject* l = PyList_New(0);
200                                 int i;
201                                 for(i = 0;i<self->ipocurve->totvert;i++)
202                                         {
203                                                 bezt = self->ipocurve->bezt + i;
204   PyList_Append( l, BezTriple_CreatePyObject(bezt));
205                                         }
206   return 0;
207 }
208
209
210 /*****************************************************************************/
211 /* Function:    IpoCurveGetAttr                                                   */
212 /* Description: This is a callback function for the C_IpoCurve type. It is        */
213 /*              the function that accesses C_IpoCurve "member variables" and      */
214 /*              methods.                                                     */
215 /*****************************************************************************/
216 static PyObject *IpoCurveGetAttr (C_IpoCurve *self, char *name)
217 {
218 if (strcmp (name, "bezierPoints") == 0)return IpoCurve_getPoints(self);
219 if (strcmp (name, "name") == 0)return IpoCurve_getName(self);
220   return Py_FindMethod(C_IpoCurve_methods, (PyObject *)self, name);
221 }
222
223 /*****************************************************************************/
224 /* Function:    IpoCurveSetAttr                                                */
225 /* Description: This is a callback function for the C_IpoCurve type. It is the */
226 /*              function that sets IpoCurve Data attributes (member variables).*/
227 /*****************************************************************************/
228 static int IpoCurveSetAttr (C_IpoCurve *self, char *name, PyObject *value)
229 {
230 if (strcmp (name, "bezierPoints") == 0)return IpoCurve_setPoints(self,value);
231   return 0; /* normal exit */
232 }
233
234 /*****************************************************************************/
235 /* Function:    IpoCurveRepr                                                      */
236 /* Description: This is a callback function for the C_IpoCurve type. It           */
237 /*              builds a meaninful string to represent ipo objects.          */
238 /*****************************************************************************/
239 static PyObject *IpoCurveRepr (C_IpoCurve *self)
240 {
241         char s[1024];   
242         sprintf(s,"IpoCurve %d %d %d %d %d %d %d %d \n",self->ipocurve->blocktype,self->ipocurve->adrcode,self->ipocurve->vartype,self->ipocurve->totvert,self->ipocurve->ipo,self->ipocurve->extrap,self->ipocurve->flag,self->ipocurve->rt);
243   return PyString_FromString(s);
244 }
245
246 /* Three Python IpoCurve_Type helper functions needed by the Object module: */
247
248 /*****************************************************************************/
249 /* Function:    IpoCurve_CreatePyObject                                           */
250 /* Description: This function will create a new C_IpoCurve from an existing       */
251 /*              Blender ipo structure.                                       */
252 /*****************************************************************************/
253 PyObject *IpoCurve_CreatePyObject (IpoCurve *ipo)
254 {
255         C_IpoCurve *pyipo;
256
257         pyipo = (C_IpoCurve *)PyObject_NEW (C_IpoCurve, &IpoCurve_Type);
258
259         if (!pyipo)
260                 return EXPP_ReturnPyObjError (PyExc_MemoryError,
261                                                 "couldn't create C_IpoCurve object");
262
263         pyipo->ipocurve = ipo;
264
265         return (PyObject *)pyipo;
266 }
267
268 /*****************************************************************************/
269 /* Function:    IpoCurve_CheckPyObject                                            */
270 /* Description: This function returns true when the given PyObject is of the */
271 /*              type IpoCurve. Otherwise it will return false.                    */
272 /*****************************************************************************/
273 int IpoCurve_CheckPyObject (PyObject *pyobj)
274 {
275         return (pyobj->ob_type == &IpoCurve_Type);
276 }
277
278 /*****************************************************************************/
279 /* Function:    IpoCurve_FromPyObject                                             */
280 /* Description: This function returns the Blender ipo from the given         */
281 /*              PyObject.                                                    */
282 /*****************************************************************************/
283 IpoCurve *IpoCurve_FromPyObject (PyObject *pyobj)
284 {
285         return ((C_IpoCurve *)pyobj)->ipocurve;
286 }