~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to source/blender/python/api2_2x/sceneTimeLine.c

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: sceneTimeLine.c 14444 2008-04-16 22:40:48Z hos $
 
2
 *
 
3
 * ***** BEGIN GPL 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.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software Foundation,
 
17
 * Inc., 59 Temple Place - Suite 330, Boston, MA        02111-1307, USA.
 
18
 *
 
19
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
20
 * All rights reserved.
 
21
 *
 
22
 * This is a new part of Blender.
 
23
 *
 
24
 * Contributor(s): Joilnen Leite
 
25
 *
 
26
 * ***** END GPL LICENSE BLOCK *****
 
27
*/
 
28
 
 
29
#include <BLI_blenlib.h>
 
30
#include "Scene.h"
 
31
 
 
32
#include <stdio.h>
 
33
#include <MEM_guardedalloc.h>   /* for MEM_callocN */
 
34
 
 
35
#include "gen_utils.h"
 
36
#include "sceneTimeLine.h"
 
37
 
 
38
// static PyObject *TimeLine_New (PyObject *self);
 
39
static PyObject *M_TimeLine_Get (PyObject *self, PyObject *args);
 
40
 
 
41
static char M_TimeLine_Get_doc[]= "Return the Scene.TimeLine.";
 
42
 
 
43
//----------------------Scene.TimeMarker subsubmodule method def----------------------------
 
44
struct PyMethodDef M_TimeLine_methods[]= {
 
45
//      {"New", (PyCFunction) M_TimeMarker_New, METH_NOVAR,
 
46
//       M_TimeLine_New_doc},
 
47
        {"Get", (PyCFunction) M_TimeLine_Get, METH_VARARGS,
 
48
         M_TimeLine_Get_doc},
 
49
        {NULL, NULL, 0, NULL}
 
50
};
 
51
 
 
52
static PyObject *TimeLine_addMarker (BPy_TimeLine *self, PyObject *args);
 
53
static PyObject *TimeLine_delMarker (BPy_TimeLine *self, PyObject *args);
 
54
static PyObject *TimeLine_setNameMarker (BPy_TimeLine *self, PyObject *args);
 
55
static PyObject *TimeLine_getNameMarker (BPy_TimeLine *self, PyObject *args);
 
56
static PyObject *TimeLine_getFramesMarked (BPy_TimeLine *self, PyObject *args);
 
57
 
 
58
static PyObject *TimeLine_repr (BPy_TimeLine *self) {
 
59
 
 
60
        return PyString_FromFormat ("[TimeLine]");
 
61
}
 
62
 
 
63
static PyMethodDef BPy_TimeLine_methods[] = {
 
64
        {"add", (PyCFunction) TimeLine_addMarker,
 
65
         METH_VARARGS,
 
66
         "() - Add timemarker"},
 
67
        {"delete", (PyCFunction) TimeLine_delMarker,
 
68
         METH_VARARGS,
 
69
         "() - delete timemarker"},
 
70
        {"setName", (PyCFunction) TimeLine_setNameMarker,
 
71
         METH_VARARGS,
 
72
         "() - Get timemarker name"},
 
73
        {"getName", (PyCFunction) TimeLine_getNameMarker,
 
74
         METH_VARARGS,
 
75
         "() - Set timemarker name"},
 
76
        {"getMarked", (PyCFunction) TimeLine_getFramesMarked,
 
77
         METH_VARARGS,
 
78
         "() - Get frames timemarked"},
 
79
        {NULL, NULL, 0, NULL}
 
80
};
 
81
 
 
82
/*-----------------------dealloc----------------------------------------*/
 
83
static void TimeLine_dealloc( BPy_TimeLine * self )
 
84
{
 
85
        PyObject_DEL( self );
 
86
}
 
87
 
 
88
/*-----------------------getattr----------------------------------------*/
 
89
static PyObject *TimeLine_getattr (BPy_TimeLine *self, char *name) {
 
90
        return Py_FindMethod( BPy_TimeLine_methods, ( PyObject * ) self, name );
 
91
}
 
92
 
 
93
/*-----------------------setattr----------------------------------------*/
 
94
static int TimeLine_setattr (BPy_TimeLine *self, char *name, PyObject *value) {
 
95
        PyObject *valtuple;
 
96
        PyObject *error= NULL;
 
97
 
 
98
        valtuple= Py_BuildValue ("(O)", value);
 
99
 
 
100
        if (!valtuple)
 
101
                return EXPP_ReturnIntError( PyExc_MemoryError,
 
102
                        "TimeLineSetAttr: couldn't create tuple" );
 
103
        if( strcmp( name, "name" ) == 0 )
 
104
                error = TimeLine_setNameMarker (self, valtuple);
 
105
        Py_DECREF (valtuple);
 
106
        if (error != Py_None)
 
107
                return -1;
 
108
 
 
109
        Py_DECREF (Py_None);
 
110
        return 0;       
 
111
}
 
112
 
 
113
//-----------------------BPy_Scene method def------------------------------
 
114
PyTypeObject TimeLine_Type = {
 
115
        PyObject_HEAD_INIT (NULL) 0,    /* ob_size */
 
116
        "TimeLine",                     /* tp_name */
 
117
        sizeof (BPy_TimeLine),  /* tp_basicsize */
 
118
        0,                      /* tp_itemsize */
 
119
        /* methods */
 
120
        ( destructor ) TimeLine_dealloc,        /* tp_dealloc */
 
121
        (printfunc) 0,  /* tp_print */
 
122
        (getattrfunc) TimeLine_getattr, /* tp_getattr */
 
123
        (setattrfunc) TimeLine_setattr, /* tp_setattr */
 
124
        0,
 
125
        (reprfunc) TimeLine_repr,       /* tp_repr */
 
126
        0,                      /* tp_as_number */
 
127
        0,                      /* tp_as_sequence */
 
128
        0,                      /* tp_as_mapping */
 
129
        0,                      /* tp_hash */
 
130
        0,                      /* tp_as_number */
 
131
        0,                      /* tp_as_sequence */
 
132
        0,                      /* tp_as_mapping */
 
133
        0,                      /* tp_hash */
 
134
        0,0,0,0,0,0,0,0,0,
 
135
        BPy_TimeLine_methods,
 
136
        0,
 
137
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 
138
};
 
139
 
 
140
PyObject *TimeLine_Init (void) 
 
141
{
 
142
        PyObject *submodule;
 
143
 
 
144
        if (PyType_Ready (&TimeLine_Type) < 0)
 
145
                return NULL;
 
146
        submodule= Py_InitModule3 ("Blender.Scene.TimeLine", M_TimeLine_methods,
 
147
                        "The Blender TimeLine subsubmodule");
 
148
 
 
149
        return submodule;
 
150
}
 
151
 
 
152
PyObject *TimeLine_CreatePyObject (BPy_TimeLine *tl) {
 
153
        BPy_TimeLine *bl_tl;
 
154
 
 
155
        bl_tl= (BPy_TimeLine *) PyObject_NEW (BPy_TimeLine, &TimeLine_Type);
 
156
 
 
157
        return (( PyObject * ) bl_tl);
 
158
}
 
159
 
 
160
 
 
161
PyObject *M_TimeLine_Get (PyObject *self, PyObject *args) {
 
162
 
 
163
        return EXPP_incr_ret (Py_None);
 
164
}
 
165
 
 
166
static PyObject *TimeLine_getFramesMarked (BPy_TimeLine *self, PyObject *args) {
 
167
 
 
168
        PyObject *marker_dict= NULL;
 
169
        TimeMarker *marker_it= NULL;
 
170
        PyObject *tmarker= NULL, *pyo= NULL, *tmpstr;
 
171
 
 
172
        if (!PyArg_ParseTuple (args, "|O", &tmarker))
 
173
                return EXPP_ReturnPyObjError (PyExc_AttributeError,
 
174
                                              "expected nothing, string or int as arguments.");
 
175
        if (tmarker) {
 
176
                char s[64];
 
177
                int frm= 0;
 
178
 
 
179
                if (PyString_Check (tmarker) && (BLI_strncpy(s, PyString_AsString (tmarker), 64)) ) {
 
180
                        for (marker_it= self->marker_list->first; marker_it; marker_it= marker_it->next)
 
181
                                if (!strcmp (marker_it->name, s)) {
 
182
                                        frm= (int)marker_it->frame;
 
183
                                        break;
 
184
                                }
 
185
                }
 
186
                else if (PyInt_Check (tmarker))
 
187
                        frm= (int)PyInt_AS_LONG (tmarker);
 
188
                else
 
189
                        return EXPP_ReturnPyObjError (PyExc_AttributeError,
 
190
                                              "expected nothing, string or int as arguments.");
 
191
                if (frm>0) {
 
192
                        marker_dict= PyDict_New ();
 
193
                        for (marker_it= self->marker_list->first; marker_it; marker_it= marker_it->next){
 
194
                                if (marker_it->frame==frm) {
 
195
                                        pyo= PyDict_GetItem ((PyObject*)marker_dict, PyInt_FromLong ((long int)marker_it->frame));
 
196
                                        tmpstr = PyString_FromString(marker_it->name);
 
197
                                        if (pyo) {
 
198
                                                PyList_Append (pyo, tmpstr);
 
199
                                                Py_INCREF(pyo);
 
200
                                        }else{
 
201
                                                pyo = PyList_New(0);
 
202
                                                PyList_Append (pyo, tmpstr);
 
203
                                        }
 
204
                                        Py_DECREF(tmpstr);
 
205
                                        
 
206
                                        PyDict_SetItem (marker_dict, PyInt_FromLong ((long int)marker_it->frame), pyo); 
 
207
                                        if (pyo) { 
 
208
                                                Py_DECREF (pyo); 
 
209
                                                pyo= NULL; 
 
210
                                        }
 
211
                                }
 
212
                        }
 
213
                }
 
214
 
 
215
        }else {
 
216
                marker_dict= PyDict_New ();
 
217
                for (marker_it= self->marker_list->first; marker_it; marker_it= marker_it->next) {
 
218
                        pyo=PyDict_GetItem ((PyObject*)marker_dict, PyInt_FromLong ((long int)marker_it->frame));
 
219
                        tmpstr = PyString_FromString(marker_it->name);
 
220
                        if (pyo) {
 
221
                                PyList_Append (pyo, tmpstr);
 
222
                                Py_INCREF (pyo);
 
223
                        }else{ 
 
224
                                pyo= PyList_New (0);
 
225
                                PyList_Append (pyo, tmpstr);
 
226
                        }
 
227
                        Py_DECREF(tmpstr);
 
228
                        
 
229
                        PyDict_SetItem (marker_dict, PyInt_FromLong ((long int)marker_it->frame), pyo); 
 
230
                        if (pyo) { 
 
231
                                Py_DECREF (pyo); 
 
232
                                pyo= NULL; 
 
233
                        }
 
234
                }
 
235
        }
 
236
 
 
237
        return marker_dict;
 
238
}
 
239
 
 
240
static PyObject *TimeLine_addMarker (BPy_TimeLine *self, PyObject *args) {
 
241
        int frame= 0;
 
242
        TimeMarker *marker= NULL, *marker_it= NULL;
 
243
 
 
244
        if (!PyArg_ParseTuple( args, "i", &frame ))
 
245
                return EXPP_ReturnPyObjError (PyExc_TypeError,
 
246
                      "expected int as argument.");
 
247
        /* two markers can't be at the same place */
 
248
        for (marker_it= self->marker_list->first; marker_it; marker_it= marker_it->next) {
 
249
                if (marker_it->frame==frame)
 
250
                        return EXPP_incr_ret (Py_None);
 
251
        }
 
252
        if (frame<self->sfra || frame>self->efra)
 
253
                return EXPP_ReturnPyObjError (PyExc_TypeError, "frame out of range.");
 
254
        marker= MEM_callocN (sizeof(TimeMarker), "TimeMarker");
 
255
        if (!marker) return EXPP_incr_ret (Py_None); 
 
256
        marker->frame= frame;
 
257
        BLI_addtail (self->marker_list, marker);
 
258
        return EXPP_incr_ret (Py_None);
 
259
}
 
260
 
 
261
static PyObject *TimeLine_delMarker (BPy_TimeLine *self, PyObject *args) {
 
262
        int frame= 0;
 
263
        TimeMarker *marker= NULL;
 
264
        
 
265
        if (!PyArg_ParseTuple (args, "|i", &frame))
 
266
                return EXPP_ReturnPyObjError (PyExc_TypeError,
 
267
                                "expected int as argument.");
 
268
 
 
269
        for (marker= self->marker_list->first; marker; marker= marker->next) {
 
270
                if (!frame)
 
271
                        BLI_freelinkN (self->marker_list, marker);
 
272
                else if (marker->frame == frame) {
 
273
                        BLI_freelinkN (self->marker_list, marker);
 
274
                        return EXPP_incr_ret (Py_None);
 
275
                }
 
276
        }
 
277
 
 
278
        return EXPP_incr_ret (Py_None);
 
279
}
 
280
 
 
281
static PyObject *TimeLine_setNameMarker (BPy_TimeLine *self, PyObject *args) {
 
282
        char *buf;
 
283
        char name[64];
 
284
        int frame= 0;
 
285
        TimeMarker *marker= NULL;
 
286
        
 
287
        if (!PyArg_ParseTuple( args, "is", &frame, &buf))
 
288
                return EXPP_ReturnPyObjError (PyExc_TypeError,
 
289
                                              "expected int as argument.");
 
290
        PyOS_snprintf (name, sizeof (name), "%s", buf);
 
291
        for (marker= self->marker_list->first; marker; marker= marker->next) {
 
292
                if (marker->frame == frame) {
 
293
                        BLI_strncpy(marker->name, name, sizeof(marker->name));
 
294
                        return EXPP_incr_ret (Py_None);
 
295
                }
 
296
        }
 
297
 
 
298
        return EXPP_ReturnPyObjError (PyExc_TypeError, "frame not marked.");
 
299
}
 
300
 
 
301
static PyObject *TimeLine_getNameMarker (BPy_TimeLine *self, PyObject *args) {
 
302
        int frame= 0;
 
303
        TimeMarker *marker;
 
304
 
 
305
        if (!PyArg_ParseTuple (args, "i", &frame))
 
306
                return EXPP_ReturnPyObjError (PyExc_TypeError, "expected int as argument.");
 
307
        
 
308
        for (marker= self->marker_list->first; marker; marker= marker->next) {
 
309
                if (marker->frame == frame)
 
310
                        return PyString_FromString (marker->name);
 
311
        }
 
312
 
 
313
        return EXPP_ReturnPyObjError (PyExc_TypeError, "frame not marked.");
 
314
}
 
315
 
 
316