New Bpy type Text3d for accessing Blender's Font objects.
[blender.git] / source / blender / python / api2_2x / Text3d.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * This is a new part of Blender.
27  *
28  * Contributor(s): Joilnen Leite
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_view3d_types.h"
35 #include "DNA_vfont_types.h"
36
37 #include "BKE_object.h"
38 #include "BDR_editobject.h"
39
40 #include "blendef.h"
41 #include "Text3d.h"
42
43 #include "mydevice.h"
44
45 extern PyObject *Curve_getName( BPy_Text3d * self );
46 extern PyObject *Curve_setName( BPy_Text3d * self, PyObject * args );
47
48 /*****************************************************************************/
49 /* Python API function prototypes for the Effect module.                     */
50 /*****************************************************************************/
51 static PyObject *M_Text3d_New( PyObject * self, PyObject * args );
52 static PyObject *M_Text3d_Get( PyObject * self, PyObject * args );
53
54 /*****************************************************************************/
55 /* Python BPy_Text3d methods declarations:                                   */
56 /*****************************************************************************/
57 /*PyObject *Text3d_getType(BPy_Text3d *self);*/
58
59 /*****************************************************************************/
60 /* Python Text3d_Type callback function prototypes:                          */
61 /*****************************************************************************/
62
63 PyObject *Text3d_Init( void );
64 void Text3dDeAlloc( BPy_Text3d * msh );
65 /* int Text3dPrint (BPy_Text3d *msh, FILE *fp, int flags); */
66 int Text3dSetAttr( BPy_Text3d * msh, char *name, PyObject * v );
67 PyObject *Text3dGetAttr( BPy_Text3d * msh, char *name );
68 PyObject *Text3dRepr( BPy_Text3d * msh );
69 PyObject *Text3dCreatePyObject( Text3d *text3d );
70 int Text3dCheckPyObject( PyObject * py_obj );
71 struct Text3d *Text3dFromPyObject( PyObject * py_obj );
72
73 static PyObject *Text3d_getName( BPy_Text3d * self );
74 static PyObject *Text3d_setName( BPy_Text3d * self, PyObject * args );
75 static PyObject *Text3d_setText( BPy_Text3d * self, PyObject * args );
76 static PyObject *Text3d_getText( BPy_Text3d * self );
77
78 /*****************************************************************************/
79 /* Python BPy_Text3d methods table:                                            */
80 /*****************************************************************************/
81 static char text2text3_doc[] = "(str) - set Text3d string";
82
83 static PyMethodDef BPy_Text3d_methods[] = {
84         {"getName", ( PyCFunction ) Text3d_getName,
85          METH_NOARGS, "() - Return Text3d Data name"},
86         {"setName", ( PyCFunction ) Text3d_setName,
87          METH_VARARGS, "() - Sets Text3d Data name"},
88         {"setText", ( PyCFunction ) Text3d_setText,
89          METH_VARARGS, "() - Sets Text3d Data"},
90         {"getText", ( PyCFunction ) Text3d_getText,
91          METH_NOARGS, "() - Gets Text3d Data"}, 
92          {NULL, NULL, 0, NULL}
93 };
94
95 /*****************************************************************************/
96 /* Python Text3d_Type structure definition:                                  */
97 /*****************************************************************************/
98 PyTypeObject Text3d_Type = {
99         PyObject_HEAD_INIT( NULL )
100         0,              /* ob_size */
101         "Text3d",               /* tp_name */
102         sizeof( BPy_Text3d ),   /* tp_basicsize */
103         0,                      /* tp_itemsize */
104         /* methods */
105         ( destructor ) Text3dDeAlloc,   /* tp_dealloc */
106         0,                      /* tp_print */
107         ( getattrfunc ) Text3dGetAttr,  /* tp_getattr */
108         ( setattrfunc ) Text3dSetAttr,  /* tp_setattr */
109         0,                      /* tp_compare */
110         ( reprfunc ) Text3dRepr,        /* tp_repr */
111         0,                      /* tp_as_number */
112         0,                      /* tp_as_sequence */
113         0,                      /* tp_as_mapping */
114         0,                      /* tp_as_hash */
115         0, 0, 0, 0, 0, 0,
116         0,                      /* tp_doc */
117         0, 0, 0, 0, 0, 0,
118         BPy_Text3d_methods,     /* tp_methods */
119         0,                      /* tp_members */
120 };
121
122 /*****************************************************************************/
123 /* Python method structure definition for Blender.Text3d module:             */
124 /*****************************************************************************/
125 struct PyMethodDef M_Text3d_methods[] = {
126         {"New", ( PyCFunction ) M_Text3d_New, METH_VARARGS, NULL},
127         {"Get", M_Text3d_Get, METH_VARARGS, NULL},
128         {"get", M_Text3d_Get, METH_VARARGS, NULL},
129         {NULL, NULL, 0, NULL}
130 };
131
132 /*****************************************************************************/
133 /* Python BPy_Text3d methods declarations:                                   */
134 /*****************************************************************************/
135 static PyObject *Text2Text3d( BPy_Text3d * self, PyObject * args );
136
137
138 /* 
139  *   Text3d_update( )
140  *   method to update display list for a Curve.
141  */
142 static PyObject *Text3d_update( BPy_Text3d * self )
143 {
144         freedisplist( &self->curve->disp );
145
146         Py_INCREF( Py_None );
147         return Py_None;
148 }
149
150
151 /*****************************************************************************/
152 /* Function:              M_Text3d_New                                       */
153 /* Python equivalent:     Blender.Text3d.New                                 */
154 /*****************************************************************************/
155
156 PyObject *M_Text3d_New( PyObject * self, PyObject * args )
157 {
158         char buf[24];
159         char *name = NULL;
160         BPy_Text3d *pytext3d;   /* for Curve Data object wrapper in Python */
161         Text3d *bltext3d = 0;   /* for actual Curve Data we create in Blender */
162
163         if( !PyArg_ParseTuple( args, "|s", &name ) )
164                 return ( EXPP_ReturnPyObjError
165                          ( PyExc_AttributeError,
166                            "expected string argument or no argument" ) );
167
168         bltext3d = add_curve( OB_FONT );        /* first create the Curve Data in Blender */
169         bltext3d->vfont= get_builtin_font();
170         bltext3d->vfont->id.us++;
171         bltext3d->str= MEM_mallocN(12, "str");
172         strcpy(bltext3d->str, "Text");
173         bltext3d->pos= 4;
174         
175         if( bltext3d == NULL )  /* bail out if add_curve() failed */
176                 return ( EXPP_ReturnPyObjError
177                          ( PyExc_RuntimeError,
178                            "couldn't create Curve Data in Blender" ) );
179
180         /* return user count to zero because add_curve() inc'd it */
181         bltext3d->id.us = 0;
182         /* create python wrapper obj */
183         pytext3d = ( BPy_Text3d * ) PyObject_NEW( BPy_Text3d, &Text3d_Type );
184
185         if( pytext3d == NULL )
186                 return ( EXPP_ReturnPyObjError
187                          ( PyExc_MemoryError,
188                            "couldn't create Curve Data object" ) );
189
190         pytext3d->curve = bltext3d;     /* link Python curve wrapper to Blender Curve */
191         if( name ) {
192                 PyOS_snprintf( buf, sizeof( buf ), "%s", name );
193                 rename_id( &bltext3d->id, buf );
194         }
195         Text3d_update ( pytext3d );
196         return ( PyObject * ) pytext3d;
197 }
198
199 PyObject *M_Text3d_Get( PyObject * self, PyObject * args )
200 {
201         char *name = NULL;
202         Text3d *curv_iter;
203         BPy_Text3d *wanted_curv;
204
205         if( !PyArg_ParseTuple( args, "|s", &name ) )    /* expects nothing or a string */
206                 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
207                                                 "expected string argument" ) );
208         if( name ) {            /*a name has been given */
209                 /* Use the name to search for the curve requested */
210                 wanted_curv = NULL;
211                 curv_iter = G.main->curve.first;
212
213                 while( ( curv_iter ) && ( wanted_curv == NULL ) ) {
214
215                         if( strcmp( name, curv_iter->id.name + 2 ) == 0 ) {
216                                 wanted_curv = ( BPy_Text3d * )
217                                         PyObject_NEW( BPy_Text3d, &Text3d_Type );
218                                 if( wanted_curv )
219                                         wanted_curv->curve = curv_iter;
220                         }
221
222                         curv_iter = curv_iter->id.next;
223                 }
224
225                 if( wanted_curv == NULL ) {     /* Requested curve doesn't exist */
226                         char error_msg[64];
227                         PyOS_snprintf( error_msg, sizeof( error_msg ),
228                                        "Curve \"%s\" not found", name );
229                         return ( EXPP_ReturnPyObjError
230                                  ( PyExc_NameError, error_msg ) );
231                 }
232
233                 return ( PyObject * ) wanted_curv;
234         } /* end  of if(name) */
235         else {
236                 /* no name has been given; return a list of all curves by name.  */
237                 PyObject *curvlist;
238
239                 curv_iter = G.main->curve.first;
240                 curvlist = PyList_New( 0 );
241
242                 if( curvlist == NULL )
243                         return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
244                                                         "couldn't create PyList" ) );
245
246                 while( curv_iter && curv_iter->vfont ) {
247                         BPy_Text3d *found_text3d =
248                                 ( BPy_Text3d * ) PyObject_NEW( BPy_Text3d,
249                                                               &Text3d_Type );
250                         found_text3d->curve = curv_iter;
251                         PyList_Append( curvlist, ( PyObject * ) found_text3d );
252
253                         curv_iter = curv_iter->id.next;
254                 }
255                 return ( curvlist );
256         }
257 }
258
259 PyObject *Text3d_Init( void )
260 {
261         PyObject *submodule;
262
263         Text3d_Type.ob_type = &PyType_Type;
264
265         submodule = Py_InitModule3( "Blender.Text3d", M_Text3d_methods, 0 );
266         return ( submodule );
267 }
268
269 void Text3dDeAlloc( BPy_Text3d * self )
270 {
271         PyObject_DEL( self );
272 }
273
274 /*****************************************************************************/
275 /* Function:    Text3dGetAttr                                                */
276 /* Description: This is a callback function for the BPy_Text3d type. It is   */
277 /*              the function that accesses BPy_Text3d "member variables" and */
278 /*              methods.                                                     */
279 /*****************************************************************************/
280
281
282 PyObject *Text3dGetAttr( BPy_Text3d * self, char *name )
283 {
284         return Py_FindMethod( BPy_Text3d_methods, ( PyObject * ) self, name );
285 }
286
287 /*****************************************************************************/
288 /* Function:    EffectSetAttr                                                */
289 /* Description: This is a callback function for the BPy_Effect type. It   */
290 /*              sets Effect Data attributes (member variables). */
291 /*****************************************************************************/
292
293 int Text3dSetAttr( BPy_Text3d * self, char *name, PyObject * value )
294 {
295         return 0;               /* normal exit */
296 }
297
298 /*****************************************************************************/
299 /* Function:    Text3dRepr                                                   */
300 /* Description: This is a callback function for the BPy_Effect type. It      */
301 /*              builds a meaninful string to represent effcte objects.       */
302 /*****************************************************************************/
303
304 PyObject *Text3dRepr( BPy_Text3d * self )
305 {
306         char *str = "";
307         return PyString_FromString( str );
308 }
309
310 int Text3d_CheckPyObject( PyObject * py_obj )
311 {
312         return ( py_obj->ob_type == &Text3d_Type );
313 }
314
315 struct Text3d *Text3d_FromPyObject( PyObject * py_obj )
316 {
317         BPy_Text3d *blen_obj;
318
319         blen_obj = ( BPy_Text3d * ) py_obj;
320         return ((Text3d*) blen_obj->curve );
321 }
322
323 static PyObject *Text3d_getName( BPy_Text3d * self )
324 {
325         return Curve_getName( self );
326 }
327
328 static PyObject *Text3d_setName( BPy_Text3d * self, PyObject * args )
329 {
330         return Curve_setName( self,args );
331 }
332
333 static PyObject *Text3d_setText( BPy_Text3d * self, PyObject * args )
334 {
335         char *text;
336         if( !PyArg_ParseTuple( args, "s", &text  ) )
337                 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
338                                                 "expected string argument" ) );
339         if (self) {
340                 MEM_freeN (self->curve->str);
341                 self->curve->str= MEM_mallocN (strlen (text)+1, "str");
342                 strcpy (self->curve->str, text);
343                 self->curve->pos= strlen (text);
344         }
345         Py_INCREF( Py_None );
346         return Py_None;
347 }
348
349 static PyObject *Text3d_getText( BPy_Text3d * self )
350 {
351         if ( strlen(self->curve->str) )
352                 return PyString_FromString (self->curve->str);
353         else 
354                 return Py_None;
355 }
356