4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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
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.
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.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * This is a new part of Blender.
28 * Contributor(s): Joilnen Leite
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_view3d_types.h"
35 #include "DNA_vfont_types.h"
37 #include "BKE_object.h"
38 #include "BDR_editobject.h"
45 extern PyObject *Curve_getName( BPy_Text3d * self );
46 extern PyObject *Curve_setName( BPy_Text3d * self, PyObject * args );
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 );
54 /*****************************************************************************/
55 /* Python BPy_Text3d methods declarations: */
56 /*****************************************************************************/
57 /*PyObject *Text3d_getType(BPy_Text3d *self);*/
59 /*****************************************************************************/
60 /* Python Text3d_Type callback function prototypes: */
61 /*****************************************************************************/
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 );
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 );
78 /*****************************************************************************/
79 /* Python BPy_Text3d methods table: */
80 /*****************************************************************************/
81 static char text2text3_doc[] = "(str) - set Text3d string";
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"},
95 /*****************************************************************************/
96 /* Python Text3d_Type structure definition: */
97 /*****************************************************************************/
98 PyTypeObject Text3d_Type = {
99 PyObject_HEAD_INIT( NULL )
101 "Text3d", /* tp_name */
102 sizeof( BPy_Text3d ), /* tp_basicsize */
105 ( destructor ) Text3dDeAlloc, /* tp_dealloc */
107 ( getattrfunc ) Text3dGetAttr, /* tp_getattr */
108 ( setattrfunc ) Text3dSetAttr, /* tp_setattr */
110 ( reprfunc ) Text3dRepr, /* tp_repr */
111 0, /* tp_as_number */
112 0, /* tp_as_sequence */
113 0, /* tp_as_mapping */
118 BPy_Text3d_methods, /* tp_methods */
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}
132 /*****************************************************************************/
133 /* Python BPy_Text3d methods declarations: */
134 /*****************************************************************************/
135 static PyObject *Text2Text3d( BPy_Text3d * self, PyObject * args );
140 * method to update display list for a Curve.
142 static PyObject *Text3d_update( BPy_Text3d * self )
144 freedisplist( &self->curve->disp );
146 Py_INCREF( Py_None );
151 /*****************************************************************************/
152 /* Function: M_Text3d_New */
153 /* Python equivalent: Blender.Text3d.New */
154 /*****************************************************************************/
156 PyObject *M_Text3d_New( PyObject * self, PyObject * args )
160 BPy_Text3d *pytext3d; /* for Curve Data object wrapper in Python */
161 Text3d *bltext3d = 0; /* for actual Curve Data we create in Blender */
163 if( !PyArg_ParseTuple( args, "|s", &name ) )
164 return ( EXPP_ReturnPyObjError
165 ( PyExc_AttributeError,
166 "expected string argument or no argument" ) );
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");
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" ) );
180 /* return user count to zero because add_curve() inc'd it */
182 /* create python wrapper obj */
183 pytext3d = ( BPy_Text3d * ) PyObject_NEW( BPy_Text3d, &Text3d_Type );
185 if( pytext3d == NULL )
186 return ( EXPP_ReturnPyObjError
188 "couldn't create Curve Data object" ) );
190 pytext3d->curve = bltext3d; /* link Python curve wrapper to Blender Curve */
192 PyOS_snprintf( buf, sizeof( buf ), "%s", name );
193 rename_id( &bltext3d->id, buf );
195 Text3d_update ( pytext3d );
196 return ( PyObject * ) pytext3d;
199 PyObject *M_Text3d_Get( PyObject * self, PyObject * args )
203 BPy_Text3d *wanted_curv;
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 */
211 curv_iter = G.main->curve.first;
213 while( ( curv_iter ) && ( wanted_curv == NULL ) ) {
215 if( strcmp( name, curv_iter->id.name + 2 ) == 0 ) {
216 wanted_curv = ( BPy_Text3d * )
217 PyObject_NEW( BPy_Text3d, &Text3d_Type );
219 wanted_curv->curve = curv_iter;
222 curv_iter = curv_iter->id.next;
225 if( wanted_curv == NULL ) { /* Requested curve doesn't exist */
227 PyOS_snprintf( error_msg, sizeof( error_msg ),
228 "Curve \"%s\" not found", name );
229 return ( EXPP_ReturnPyObjError
230 ( PyExc_NameError, error_msg ) );
233 return ( PyObject * ) wanted_curv;
234 } /* end of if(name) */
236 /* no name has been given; return a list of all curves by name. */
239 curv_iter = G.main->curve.first;
240 curvlist = PyList_New( 0 );
242 if( curvlist == NULL )
243 return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
244 "couldn't create PyList" ) );
246 while( curv_iter && curv_iter->vfont ) {
247 BPy_Text3d *found_text3d =
248 ( BPy_Text3d * ) PyObject_NEW( BPy_Text3d,
250 found_text3d->curve = curv_iter;
251 PyList_Append( curvlist, ( PyObject * ) found_text3d );
253 curv_iter = curv_iter->id.next;
259 PyObject *Text3d_Init( void )
263 Text3d_Type.ob_type = &PyType_Type;
265 submodule = Py_InitModule3( "Blender.Text3d", M_Text3d_methods, 0 );
266 return ( submodule );
269 void Text3dDeAlloc( BPy_Text3d * self )
271 PyObject_DEL( self );
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 */
279 /*****************************************************************************/
282 PyObject *Text3dGetAttr( BPy_Text3d * self, char *name )
284 return Py_FindMethod( BPy_Text3d_methods, ( PyObject * ) self, name );
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 /*****************************************************************************/
293 int Text3dSetAttr( BPy_Text3d * self, char *name, PyObject * value )
295 return 0; /* normal exit */
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 /*****************************************************************************/
304 PyObject *Text3dRepr( BPy_Text3d * self )
307 return PyString_FromString( str );
310 int Text3d_CheckPyObject( PyObject * py_obj )
312 return ( py_obj->ob_type == &Text3d_Type );
315 struct Text3d *Text3d_FromPyObject( PyObject * py_obj )
317 BPy_Text3d *blen_obj;
319 blen_obj = ( BPy_Text3d * ) py_obj;
320 return ((Text3d*) blen_obj->curve );
323 static PyObject *Text3d_getName( BPy_Text3d * self )
325 return Curve_getName( self );
328 static PyObject *Text3d_setName( BPy_Text3d * self, PyObject * args )
330 return Curve_setName( self,args );
333 static PyObject *Text3d_setText( BPy_Text3d * self, PyObject * args )
336 if( !PyArg_ParseTuple( args, "s", &text ) )
337 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
338 "expected string argument" ) );
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);
345 Py_INCREF( Py_None );
349 static PyObject *Text3d_getText( BPy_Text3d * self )
351 if ( strlen(self->curve->str) )
352 return PyString_FromString (self->curve->str);