[Bf-python] Curve.string

Toni Alatalo antont at kyperjokki.fi
Tue Feb 8 11:09:14 CET 2005


hi,

did some work on getting&setting Curve.string .. well i guess a month ago, 
'cause some people needed to get the text of 3d font objects to their 
scripts (at least one used them to add information to game maps), and 
Bassed was planning a credit text making tool where he needed setting .. 
and anticipated similar needs for myself too, so gave it a shot.

never got to finish it, and hence not post about it, but as time is 
passing and now <pidhash> on irc was asking to see it, here comes

it was surprisingly complex:

1. there is the type OB_FONT in the internals, but was nowhere in the 
Python API. Actually i first mistakingly started making a Font module, 
before realizing they were actually just curves with special properties. 
Then the problem was that the type is association to the Object, not 
ObData such as Curve. On irc Ton figured that this text should still be 
about curves, so we made this check to see if a curve is a text which is 
similar to what blender internally does when checking for OB_FONTness. He 
said it would be ok as an 'undocumented convention'

2. for setting, on the GUI side there is the editmode dance, and i never 
get the mallocing/.. right when trying to replicate it for the API (looked 
at also how names etc. are set via the API). tried many things that are 
currently mostly commented out in my working copy, and hence this patch. 
best i could get done was that during the running of a script the string 
was set, and the new string then gotten ok, but after it there was garbage 
(other random string from the app) as the text -- a typical memory alloc. 
prob. i guess.

now busy with other work, perhaps someone else can take it on from here 
(otherwise i'll try returning to it when have time).

oh and the same diff also includes the is_cyclic() thing that did get to 
finish and post as a clean patch earlier (didn't check if it's committed 
yet)

couldn't make a complete diff, 'cause did this in a copy where also have 
LoneTech's Pyrex stuff and the stub Font module that started when doing 
this, but hopefully this helps a little anyhow

oh and the plan was to provide the getters&setters via the .string 
attribute (/property?) like .name etc. are but that's probably not done 
yet in this drafty patch.

~Toni
-------------- next part --------------
108a109
> static PyObject *Curve_isCyclic( BPy_Curve * self, PyObject * args);
121a123,127
> 
> /* trying to get Font/Text data */
> static PyObject *Curve_getString( BPy_Curve * self );
> static PyObject *Curve_setString( BPy_Curve * self, PyObject * args );
> 
206a213,214
> 	{"isCyclic", ( PyCFunction ) Curve_isCyclic,
> 	 METH_VARARGS, "() - returns 1 if curve is cyclic (closed), 0 otherwise."},
213a222,225
> 	{"getString", ( PyCFunction ) Curve_getString, METH_NOARGS, 
> 	 "() - Gets font string"},
> 	{"setString", ( PyCFunction ) Curve_setString, METH_VARARGS,
> 	 "( string ) - changes the text of a text/font curve object"},
1060a1073,1112
> /* trying to make a check for closedness (cyclic), following on isNurb (above) 
>    copy-pasting done by antont at kyperjokki.fi */
> 
> static PyObject *Curve_isCyclic( BPy_Curve * self, PyObject * args )
> {
> 	int curve_num = 0;	/* default value */
> 	int is_cyclic;
> 	Nurb *ptrnurb;
> 	PyObject *ret_val;
> 	int i;
> 
> 	/* parse and check input args */
> 	if( !PyArg_ParseTuple( args, "|i", &curve_num ) ) {
> 		return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
> 						"expected int argument" ) );
> 	}
> 	if( curve_num < 0 ) {
> 		return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
> 						"curve number must be non-negative" ) );
> 	}
> 
> 	ptrnurb = self->curve->nurb.first;
> 
> 	if( !ptrnurb )		/* no splines in this curve */
> 		return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
> 						"no splines in this Curve" ) );
> 
> 	for( i = 0; i < curve_num; i++ ) {
> 		ptrnurb = ptrnurb->next;
> 		if( !ptrnurb )	/* if zero, we ran just ran out of curves */
> 			return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
> 							"curve index out of range" ) );
> 	}
> 
> 	is_cyclic = ptrnurb->flagu & CU_CYCLIC;
> 	ret_val = PyInt_FromLong( ( long ) is_cyclic );
> 
> 	return (ret_val);
> }
> 
1283a1336,1387
> static PyObject *Curve_getString( BPy_Curve * self )
> {
>       /* self->type!=OB_FONT) -- self does not have type :/ how to get ob? 
>          Ton said that this is ok here ('an undocumented convention') */
>       if(!self->curve->str)
> 	return EXPP_ReturnPyObjError( PyExc_TypeError,
> 				      "this curve is not a text");
> 
>       return PyString_FromString( self->curve->str );
> }
> 
> static PyObject *Curve_setString( BPy_Curve * self, PyObject * args )
> {
> 	char *string;
> 	/*char buf[1000];*/
> 
> 	if(!self->curve->str)
> 	    return EXPP_ReturnPyObjError( PyExc_TypeError,
> 					  "this curve is not a text");
> 
> 	/* string = MEM_mallocN(1004, "texteditbuf"); */
> 	
> 	if( !PyArg_ParseTuple( args, "s", &( string ) ) )
> 	    return EXPP_ReturnPyObjError( PyExc_AttributeError,
> 					  "expected string argument" );
> 
> 	/*	PyOS_snprintf( buf, sizeof( buf ), "%s", string );*/
> 
> 	/* after reading make_editText() in src/editfont.c */
> 	/* MAXTEXT is 1000 in editfont.c */	
> 	/* BLI_strncpy(string, self->curve->str, 1000); */
> 
> 	/*strcpy(self->curve->str, buf);*/
> 	self->curve->str = string;
> 	self->curve->len = strlen(string);
> 	if(self->curve->pos > self->curve->len) self->curve->pos = self->curve->len;
> 	else printf("did not adjust pos when setting text.\n");
> 
> 	/* this does not update the view. it can be only done with an object pointer
> 	   (though the only need is that text_to_curve does type checking) 
> 	   *text_to_curve(Object *ob, int mode) is doing if(ob->type!=OB_FONT) */
> 
> 	/* if (text_to_curve(self, 0)) {
> 	   makeDispList(self); */
> 
> 	/* like in load_editText */
> 	/*MEM_freeN(string);
> 	  string=NULL;*/
> 
> 	/* returns the text that was set */
> 	return PyString_FromString( self->curve->str );
> }
1331a1436,1439
> 	if( strcmp( name, "string" ) == 0 )
> 		attr = PyString_FromString( self->curve->id.name + 2 );
> 
> 	  /* return Curve_getString( self ); */


More information about the Bf-python mailing list