18
19
* You should have received a copy of the GNU General Public License
19
20
* 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
* 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.
23
24
* All rights reserved.
25
26
* This is a new part of Blender.
27
* Contributor(s): Michel Selten, Willian P. Germano
28
* Contributor(s): Michel Selten, Willian P. Germano, Alex Mole, Ken Hughes
29
30
* ***** END GPL/BL DUAL LICENSE BLOCK *****
33
#include <DNA_text_types.h>
34
#include <MEM_guardedalloc.h>
35
#include <BLI_blenlib.h>
36
#include <BIF_space.h>
32
38
#include "gen_utils.h"
33
39
#include "constant.h"
35
41
/*****************************************************************************/
36
/* Description: This function clamps an int to the given interval */
42
/* Description: This function clamps an int to the given interval */
38
44
/*****************************************************************************/
39
int EXPP_ClampInt (int value, int min, int max)
45
int EXPP_ClampInt( int value, int min, int max )
41
if (value < min) return min;
42
else if (value > max) return max;
49
else if( value > max )
46
54
/*****************************************************************************/
47
/* Description: This function clamps a float to the given interval */
55
/* Description: This function clamps a float to the given interval */
49
57
/*****************************************************************************/
50
float EXPP_ClampFloat (float value, float min, float max)
58
float EXPP_ClampFloat( float value, float min, float max )
52
if (value < min) return min;
53
else if (value > max) return max;
62
else if( value > max )
57
67
/*****************************************************************************/
58
68
/* Description: This function returns true if both given strings are equal, */
59
/* otherwise it returns false. */
69
/* otherwise it returns false. */
60
70
/*****************************************************************************/
61
int StringEqual (const char * string1, const char * string2)
71
int StringEqual( const char *string1, const char *string2 )
63
return (strcmp(string1, string2)==0);
73
return ( strcmp( string1, string2 ) == 0 );
66
76
/*****************************************************************************/
67
/* Description: This function returns the name of the given ID struct */
68
/* without the Object type identifying characters prepended. */
77
/* Description: This function returns the name of the given ID struct */
78
/* without the Object type identifying characters prepended. */
69
79
/*****************************************************************************/
70
char * GetIdName (ID *id)
80
char *GetIdName( ID * id )
72
return ((id->name)+2);
82
return ( ( id->name ) + 2 );
75
85
/*****************************************************************************/
76
86
/* Description: This function returns the ID of the object with given name */
77
/* from a given list. */
87
/* from a given list. */
78
88
/*****************************************************************************/
79
ID *GetIdFromList(ListBase *list, char *name)
89
ID *GetIdFromList( ListBase * list, char *name )
81
91
ID *id = list->first;
84
if(strcmp(name, id->name+2) == 0) break;
94
if( strcmp( name, id->name + 2 ) == 0 )
91
102
/*****************************************************************************/
92
103
/* Description: These functions set an internal string with the given type */
93
/* and error_msg arguments. */
104
/* and error_msg arguments. */
94
105
/*****************************************************************************/
95
PyObject * PythonReturnErrorObject (PyObject * type, char * error_msg)
97
PyErr_SetString (type, error_msg);
101
PyObject *EXPP_ReturnPyObjError (PyObject * type, char * error_msg)
102
{ /* same as above, just to change its name smoothly */
103
PyErr_SetString (type, error_msg);
107
PyObject *EXPP_ReturnPyObjError( PyObject * type, char *error_msg )
108
{ /* same as above, just to change its name smoothly */
109
PyErr_SetString( type, error_msg );
107
int EXPP_ReturnIntError (PyObject *type, char *error_msg)
113
int EXPP_ReturnIntError( PyObject * type, char *error_msg )
109
PyErr_SetString (type, error_msg);
115
PyErr_SetString( type, error_msg );
113
119
/*****************************************************************************/
114
/* Description: This function increments the reference count of the given */
115
/* Python object (usually Py_None) and returns it. */
116
/*****************************************************************************/
117
PyObject * PythonIncRef (PyObject *object)
123
PyObject *EXPP_incr_ret (PyObject *object)
128
/*****************************************************************************/
129
/* Description: This function maps the event identifier to a string. */
130
/*****************************************************************************/
131
char * event_to_name(short event)
135
case SCRIPT_FRAMECHANGED:
136
return "FrameChanged";
120
/* Description: This function increments the reference count of the given */
121
/* Python object (usually Py_None) and returns it. */
122
/*****************************************************************************/
124
PyObject *EXPP_incr_ret( PyObject * object )
130
/* return Py_False - to avoid warnings, we use the fact that
131
* 0 == False in Python: */
132
PyObject *EXPP_incr_ret_False()
134
return Py_BuildValue("i", 0);
137
/* return Py_True - to avoid warnings, we use the fact that
138
* 1 == True in Python: */
139
PyObject *EXPP_incr_ret_True()
141
return Py_BuildValue("i", 1);
144
/*****************************************************************************/
145
/* Description: This function maps the event identifier to a string. */
146
/*****************************************************************************/
147
char *event_to_name( short event )
150
case SCRIPT_FRAMECHANGED:
151
return "FrameChanged";
160
case SCRIPT_POSTRENDER:
146
167
/*****************************************************************************/
147
/* Description: Checks whether all objects in a PySequence are of a same */
148
/* given type. Returns 0 if not, 1 on success. */
168
/* Description: Checks whether all objects in a PySequence are of a same */
169
/* given type. Returns 0 if not, 1 on success. */
149
170
/*****************************************************************************/
150
int EXPP_check_sequence_consistency(PyObject *seq, PyTypeObject *against)
171
int EXPP_check_sequence_consistency( PyObject * seq, PyTypeObject * against )
153
int len = PySequence_Length(seq);
174
int len = PySequence_Length( seq );
156
for (i = 0; i < len; i++) {
157
ob = PySequence_GetItem(seq, i);
158
if (ob->ob_type != against) {
177
for( i = 0; i < len; i++ ) {
178
ob = PySequence_GetItem( seq, i );
181
else if( ob->ob_type != against ) {
187
return result; /* 1 if all of 'against' type, 2 if there are (also) Nones */
167
PyObject *EXPP_tuple_repr(PyObject *self, int size)
190
PyObject *EXPP_tuple_repr( PyObject * self, int size )
169
192
PyObject *repr, *item;
191
215
/****************************************************************************/
192
216
/* Description: searches through a map for a pair with a given name. If the */
193
/* pair is present, its ival is stored in *ival and nonzero is */
194
/* returned. If the pair is absent, zero is returned. */
217
/* pair is present, its ival is stored in *ival and nonzero is */
218
/* returned. If the pair is absent, zero is returned. */
195
219
/****************************************************************************/
196
int EXPP_map_getIntVal (const EXPP_map_pair *map, const char *sval, int *ival)
200
if (StringEqual(sval, map->sval))
220
int EXPP_map_getIntVal( const EXPP_map_pair * map, const char *sval,
224
if( StringEqual( sval, map->sval ) ) {
233
/* same as above, but string case is ignored */
234
int EXPP_map_case_getIntVal( const EXPP_map_pair * map, const char *sval,
238
if( !BLI_strcasecmp( sval, map->sval ) ) {
210
247
/****************************************************************************/
211
248
/* Description: searches through a map for a pair with a given name. If the */
212
/* pair is present, its ival is stored in *ival and nonzero is */
213
/* returned. If the pair is absent, zero is returned. */
214
/* note: this function is identical to EXPP_map_getIntVal except that the */
215
/* output is stored in a short value. */
249
/* pair is present, its ival is stored in *ival and nonzero is */
250
/* returned. If the pair is absent, zero is returned. */
251
/* note: this function is identical to EXPP_map_getIntVal except that the */
252
/* output is stored in a short value. */
216
253
/****************************************************************************/
217
int EXPP_map_getShortVal (const EXPP_map_pair *map,
218
const char *sval, short *ival)
254
int EXPP_map_getShortVal( const EXPP_map_pair * map,
255
const char *sval, short *ival )
222
if (StringEqual(sval, map->sval))
258
if( StringEqual( sval, map->sval ) ) {
232
267
/****************************************************************************/
233
268
/* Description: searches through a map for a pair with a given ival. If the */
234
/* pair is present, a pointer to its name is stored in *sval */
235
/* and nonzero is returned. If the pair is absent, zero is */
269
/* pair is present, a pointer to its name is stored in *sval */
270
/* and nonzero is returned. If the pair is absent, zero is */
237
272
/****************************************************************************/
238
int EXPP_map_getStrVal (const EXPP_map_pair *map, int ival, const char **sval)
242
if (ival == map->ival)
273
int EXPP_map_getStrVal( const EXPP_map_pair * map, int ival,
277
if( ival == map->ival ) {
286
/* Redraw wrappers */
288
/* this queues redraws if we're not in background mode: */
289
void EXPP_allqueue(unsigned short event, short val)
291
if (!G.background) allqueue(event, val);
294
/************************************************************************/
295
/* Scriptlink-related functions, used by scene, object, etc. bpyobjects */
296
/************************************************************************/
297
PyObject *EXPP_getScriptLinks( ScriptLink * slink, PyObject * args,
300
PyObject *list = NULL;
301
char *eventname = NULL;
304
/* actually !scriptlink shouldn't happen ... */
305
if( !slink || !slink->totscript )
306
return EXPP_incr_ret( Py_None );
308
if( !PyArg_ParseTuple( args, "s", &eventname ) )
309
return EXPP_ReturnPyObjError( PyExc_TypeError,
310
"expected event name (string) as argument" );
312
list = PyList_New( 0 );
314
return EXPP_ReturnPyObjError( PyExc_MemoryError,
315
"couldn't create PyList!" );
317
if( !strcmp( eventname, "FrameChanged" ) )
318
event = SCRIPT_FRAMECHANGED;
319
else if( !strcmp( eventname, "Redraw" ) )
320
event = SCRIPT_REDRAW;
321
else if( !strcmp( eventname, "Render" ) )
322
event = SCRIPT_RENDER;
323
else if( is_scene && !strcmp( eventname, "OnLoad" ) )
324
event = SCRIPT_ONLOAD;
325
else if( is_scene && !strcmp( eventname, "OnSave" ) )
326
event = SCRIPT_ONSAVE;
328
return EXPP_ReturnPyObjError( PyExc_AttributeError,
329
"invalid event name" );
331
for( i = 0; i < slink->totscript; i++ ) {
332
if( ( slink->flag[i] == event ) && slink->scripts[i] )
334
PyString_FromString( slink->scripts[i]->
341
PyObject *EXPP_clearScriptLinks( ScriptLink * slink, PyObject * args )
343
int i, j, totLinks, deleted = 0;
344
PyObject *seq = NULL;
348
/* check for an optional list of strings */
349
if( !PyArg_ParseTuple( args, "|O", &seq ) )
350
return ( EXPP_ReturnPyObjError
352
"expected no arguments or a list of strings" ) );
355
/* if there was a parameter, handle it */
357
/* check that parameter IS list of strings */
358
if ( !PyList_Check ( seq ) )
359
return ( EXPP_ReturnPyObjError
361
"expected a list of strings" ) );
363
totLinks = PyList_Size ( seq );
364
for ( i = 0 ; i < totLinks ; ++i ) {
365
if ( !PyString_Check ( PySequence_GetItem( seq, i ) ) )
366
return ( EXPP_ReturnPyObjError
368
"expected list to contain strings" ) );
372
parameters OK: now look for each script, and delete
373
its link as we find it (this handles multiple links)
375
for ( i = 0 ; i < totLinks ; ++i )
378
str = PyString_AsString ( PySequence_GetItem( seq, i ) );
379
for ( j = 0 ; j < slink->totscript ; ++j ) {
380
if ( slink->scripts[j] && !strcmp ( slink->scripts[j]->name+2, str ) ) {
381
slink->scripts[j] = NULL;
387
/* if no parameter, then delete all scripts */
389
deleted = slink->totscript;
393
if not all scripts deleted, create new lists and copy remaining
397
if ( slink->totscript > deleted ) {
398
slink->totscript -= deleted;
400
stmp = slink->scripts;
402
MEM_mallocN( sizeof( ID * ) * ( slink->totscript ),
407
MEM_mallocN( sizeof( short * ) * ( slink->totscript ),
410
for ( i = 0, j = 0 ; i < slink->totscript ; ++j ) {
411
if ( stmp[j] != NULL ) {
412
memcpy( slink->scripts+i, stmp+j, sizeof( ID * ) );
413
memcpy( slink->flag+i, ftmp+j, sizeof( short ) );
420
/*EXPP_allqueue (REDRAWBUTSSCRIPT, 0 );*/
421
slink->actscript = 1;
424
/* all scripts deleted, so delete entire list and free memory */
427
MEM_freeN( slink->scripts );
429
MEM_freeN( slink->flag );
431
slink->scripts = NULL;
433
slink->totscript = slink->actscript = 0;
436
return EXPP_incr_ret( Py_None );
440
PyObject *EXPP_addScriptLink(ScriptLink *slink, PyObject *args, int is_scene)
442
int event = 0, found_txt = 0;
443
void *stmp = NULL, *ftmp = NULL;
444
Text *bltxt = G.main->text.first;
445
char *textname = NULL;
446
char *eventname = NULL;
448
/* !scriptlink shouldn't happen ... */
450
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
451
"internal error: no scriptlink!" );
454
if( !PyArg_ParseTuple( args, "ss", &textname, &eventname ) )
455
return EXPP_ReturnPyObjError( PyExc_TypeError,
456
"expected two strings as arguments" );
459
if( !strcmp( bltxt->id.name + 2, textname ) ) {
463
bltxt = bltxt->id.next;
467
return EXPP_ReturnPyObjError( PyExc_AttributeError,
468
"no such Blender Text" );
470
if( !strcmp( eventname, "FrameChanged" ) )
471
event = SCRIPT_FRAMECHANGED;
472
else if( !strcmp( eventname, "Redraw" ) )
473
event = SCRIPT_REDRAW;
474
else if( !strcmp( eventname, "Render" ) )
475
event = SCRIPT_RENDER;
476
else if( is_scene && !strcmp( eventname, "OnLoad" ) )
477
event = SCRIPT_ONLOAD;
478
else if( is_scene && !strcmp( eventname, "OnSave" ) )
479
event = SCRIPT_ONSAVE;
481
return EXPP_ReturnPyObjError( PyExc_AttributeError,
482
"invalid event name" );
484
stmp = slink->scripts;
486
MEM_mallocN( sizeof( ID * ) * ( slink->totscript + 1 ),
491
MEM_mallocN( sizeof( short * ) * ( slink->totscript + 1 ),
494
if( slink->totscript ) {
495
memcpy( slink->scripts, stmp,
496
sizeof( ID * ) * ( slink->totscript ) );
499
memcpy( slink->flag, ftmp,
500
sizeof( short ) * ( slink->totscript ) );
504
slink->scripts[slink->totscript] = ( ID * ) bltxt;
505
slink->flag[slink->totscript] = event;
509
if( slink->actscript < 1 )
510
slink->actscript = 1;
512
return EXPP_incr_ret (Py_None); /* normal exit */