~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/blender/python/BPY_interface.c

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2007-05-17 11:47:59 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517114759-yp4ybrnhp2u7pk66
Tags: 2.44-1
* New upstream release.
* Drop debian/patches/01_64bits_stupidity, not needed anymore: as of this
  version blender is 64 bits safe again. Adjust README.Debian accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* 
2
 
 * $Id: BPY_interface.c,v 1.94 2007/02/09 18:27:20 elubie Exp $
 
2
 * $Id: BPY_interface.c,v 1.108 2007/04/18 14:40:00 campbellbarton Exp $
3
3
 *
4
4
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5
5
 *
43
43
#include "BKE_library.h"
44
44
#include "BKE_object.h"         /* during_scriptlink() */
45
45
#include "BKE_text.h"
 
46
 
46
47
#include "DNA_curve_types.h" /* for struct IpoDriver */
 
48
#include "DNA_ID.h" /* ipo driver */
 
49
#include "DNA_object_types.h" /* ipo driver */
 
50
 
47
51
#include "DNA_screen_types.h"
48
52
#include "DNA_userdef_types.h"  /* for U.pythondir */
49
53
#include "MEM_guardedalloc.h"
55
59
#include "api2_2x/EXPP_interface.h"
56
60
#include "api2_2x/constant.h"
57
61
#include "api2_2x/gen_utils.h"
 
62
#include "api2_2x/gen_library.h" /* GetPyObjectFromID */
58
63
#include "api2_2x/BGL.h" 
59
64
#include "api2_2x/Blender.h"
60
65
#include "api2_2x/Camera.h"
61
66
#include "api2_2x/Draw.h"
62
 
#include "api2_2x/Lamp.h"
63
 
#include "api2_2x/NMesh.h"
64
67
#include "api2_2x/Object.h"
65
68
#include "api2_2x/Registry.h"
66
 
#include "api2_2x/Scene.h"
67
 
#include "api2_2x/World.h"
 
69
#include "api2_2x/bpy.h" /* for the new "bpy" module */
 
70
 
 
71
/* for scriptlinks */
 
72
#include "DNA_lamp_types.h"
 
73
#include "DNA_camera_types.h"
 
74
#include "DNA_world_types.h"
 
75
#include "DNA_scene_types.h"
 
76
#include "DNA_material_types.h"
68
77
 
69
78
/* bpy_registryDict is declared in api2_2x/Registry.h and defined
70
79
 * in api2_2x/Registry.c
72
81
 * choose to preserve after they are executed, so user changes can be
73
82
 * restored next time the script is used.  Check the Blender.Registry module. 
74
83
 */
75
 
//#include "api2_2x/Registry.h"
 
84
/*#include "api2_2x/Registry.h" */
76
85
 
77
86
/* for pydrivers (ipo drivers defined by one-line Python expressions) */
78
87
PyObject *bpy_pydriver_Dict = NULL;
103
112
        return 1;
104
113
}
105
114
 
106
 
/*Declares the modules and their initialization functions
107
 
*These are TOP-LEVEL modules e.g. import `module` - there is no
108
 
*support for packages here e.g. import `package.module` */
 
115
/* Declares the modules and their initialization functions
 
116
 * These are TOP-LEVEL modules e.g. import `module` - there is no
 
117
 * support for packages here e.g. import `package.module` */
 
118
 
109
119
static struct _inittab BPy_Inittab_Modules[] = {
110
120
        {"Blender", M_Blender_Init},
 
121
        {"bpy", m_bpy_init},
111
122
        {NULL, NULL}
112
123
};
113
124
 
115
126
* Structure definitions 
116
127
**************************************************************************/
117
128
#define FILENAME_LENGTH 24
 
129
 
118
130
typedef struct _ScriptError {
119
131
        char filename[FILENAME_LENGTH];
120
132
        int lineno;
170
182
         * print an error if not found.  See init_syspath() for the
171
183
         * rest of our init msgs.
172
184
         */
173
 
        // Py_GetVersion() returns a ptr to astatic string
174
 
        printf( "Compiled with Python version %.3s.\n", Py_GetVersion() );
 
185
 
 
186
        /* print Python version
 
187
         * Py_GetVersion() returns a ptr to a static string "9.9.9 (aaaa..." 
 
188
         */
 
189
        {
 
190
                int count = 3;  /* a nice default for major.minor.  example 2.5 */
 
191
                const char *version = Py_GetVersion();
 
192
                /* we know a blank is there somewhere! */
 
193
                char *blank_ptr = strchr( version, ' '); 
 
194
                if(blank_ptr)
 
195
                        count = blank_ptr - version;
 
196
                
 
197
                printf( "Compiled with Python version %.*s.\n", count, version );
 
198
        }
 
199
 
175
200
 
176
201
        //Initialize the TOP-LEVEL modules
177
202
        PyImport_ExtendInittab(BPy_Inittab_Modules);
760
785
        case PYMENU_HELP:
761
786
        case PYMENU_RENDER:
762
787
        case PYMENU_WIZARDS:
 
788
        case PYMENU_SCRIPTTEMPLATE:
 
789
        case PYMENU_MESHFACEKEY:
763
790
                break;
764
791
 
765
792
        default:
1044
1071
        if (mod) {
1045
1072
                PyObject *fcn = PyObject_GetAttrString(mod, "Get");
1046
1073
                Py_DECREF(mod);
1047
 
                if (fcn)
 
1074
                if (fcn) {
1048
1075
                        PyDict_SetItemString(d, "ob", fcn);
 
1076
                        Py_DECREF(fcn);
 
1077
                }
1049
1078
        }
1050
1079
 
1051
1080
        /* me(meshname) == Blender.Mesh.Get(meshname) */
1053
1082
        if (mod) {
1054
1083
                PyObject *fcn = PyObject_GetAttrString(mod, "Get");
1055
1084
                Py_DECREF(mod);
1056
 
                if (fcn)
 
1085
                if (fcn) {
1057
1086
                        PyDict_SetItemString(d, "me", fcn);
 
1087
                        Py_DECREF(fcn);
 
1088
                }
1058
1089
        }
1059
1090
 
1060
1091
        /* ma(matname) == Blender.Material.Get(matname) */
1062
1093
        if (mod) {
1063
1094
                PyObject *fcn = PyObject_GetAttrString(mod, "Get");
1064
1095
                Py_DECREF(mod);
1065
 
                if (fcn)
 
1096
                if (fcn) {
1066
1097
                        PyDict_SetItemString(d, "ma", fcn);
 
1098
                        Py_DECREF(fcn);
 
1099
                }
1067
1100
        }
1068
1101
 
1069
1102
        return 0;
1144
1177
float BPY_pydriver_eval(IpoDriver *driver)
1145
1178
{
1146
1179
        char *expr = NULL;
1147
 
        PyObject *retval, *floatval;
 
1180
        PyObject *retval, *floatval, *bpy_ob = NULL;
1148
1181
        float result = 0.0f; /* default return */
 
1182
        int setitem_retval;
1149
1183
 
1150
1184
        if (!driver) return result;
1151
1185
 
1159
1193
                }
1160
1194
        }
1161
1195
 
1162
 
/* sds*/
 
1196
        if (driver->ob)
 
1197
                bpy_ob = Object_CreatePyObject(driver->ob);
 
1198
 
 
1199
        if (!bpy_ob) {
 
1200
                Py_INCREF(Py_None);
 
1201
                bpy_ob = Py_None;
 
1202
        }
 
1203
 
 
1204
        setitem_retval = EXPP_dict_set_item_str(bpy_pydriver_Dict, "self", bpy_ob);
 
1205
 
1163
1206
        if( !setup_armature_weakrefs()){
1164
1207
                fprintf( stderr, "Oops - weakref dict setup\n");
1165
1208
                return result;
1171
1214
        if (retval == NULL) {
1172
1215
                return pydriver_error(driver);
1173
1216
        }
1174
 
        else {
1175
 
                floatval = PyNumber_Float(retval);
1176
 
                Py_DECREF(retval);
1177
 
        }
 
1217
 
 
1218
        floatval = PyNumber_Float(retval);
 
1219
        Py_DECREF(retval);
1178
1220
 
1179
1221
        if (floatval == NULL) 
1180
1222
                return pydriver_error(driver);
1181
 
        else {
1182
 
                result = (float)PyFloat_AsDouble(floatval);
1183
 
                Py_DECREF(floatval);
1184
 
        }
 
1223
 
 
1224
        result = (float)PyFloat_AsDouble(floatval);
 
1225
        Py_DECREF(floatval);
 
1226
 
 
1227
        /* remove 'self', since this dict is also used by py buttons */
 
1228
        if (setitem_retval == 0) PyDict_DelItemString(bpy_pydriver_Dict, "self");
1185
1229
 
1186
1230
        /* all fine, make sure the "invalid expression" flag is cleared */
1187
1231
        driver->flag &= ~IPO_DRIVER_FLAG_INVALID;
1235
1279
                }
1236
1280
        }
1237
1281
 
1238
 
/* sds*/
 
1282
 
1239
1283
        if( !setup_armature_weakrefs()){
1240
1284
                fprintf(stderr, "Oops - weakref dict\n");
1241
1285
                return -1;
1333
1377
        }
1334
1378
}
1335
1379
 
1336
 
static PyObject *ID_asPyObject( ID * id )
1337
 
{
1338
 
        switch ( MAKE_ID2( id->name[0], id->name[1] ) ) {
1339
 
        case ID_OB:
1340
 
                return Object_CreatePyObject( ( Object * ) id );
1341
 
        case ID_LA:
1342
 
                return Lamp_CreatePyObject( ( Lamp * ) id );
1343
 
        case ID_CA:
1344
 
                return Camera_CreatePyObject( ( Camera * ) id );
1345
 
        case ID_MA:
1346
 
                return Material_CreatePyObject( ( Material * ) id );
1347
 
        case ID_WO:
1348
 
                return World_CreatePyObject( ( World * ) id );
1349
 
        case ID_SCE:
1350
 
                return Scene_CreatePyObject( ( Scene * ) id );
1351
 
        default:
1352
 
                Py_INCREF( Py_None );
1353
 
                return Py_None;
1354
 
        }
1355
 
}
1356
 
 
1357
1380
int BPY_has_onload_script( void )
1358
1381
{
1359
1382
        ScriptLink *slink = &G.scene->scriptlink;
1397
1420
                /* set globals in Blender module to identify scriptlink */
1398
1421
                EXPP_dict_set_item_str( g_blenderdict, "bylink", EXPP_incr_ret_True() );
1399
1422
                EXPP_dict_set_item_str( g_blenderdict, "link",
1400
 
                                      ID_asPyObject( id ) );
 
1423
                                      GetPyObjectFromID( id ) );
1401
1424
                EXPP_dict_set_item_str( g_blenderdict, "event",
1402
1425
                                      PyString_FromString( event_to_name
1403
1426
                                                           ( event ) ) );
1982
2005
 
1983
2006
        m = PyImport_AddModule( "__builtin__" );
1984
2007
        d = PyModule_GetDict( m );
 
2008
        
1985
2009
        EXPP_dict_set_item_str( d, "__import__", import );
1986
2010
}
1987
2011