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

« back to all changes in this revision

Viewing changes to source/blender/nodes/intern/SHD_nodes/SHD_dynamic.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
/**
 
2
 * $Id: SHD_dynamic.c,v 1.4 2007/04/05 10:49:25 jesterking Exp $
 
3
 *
 
4
 * ***** BEGIN GPL 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. 
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software Foundation,
 
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 *
 
20
 * The Original Code is Copyright (C) 2007 Blender Foundation.
 
21
 * All rights reserved.
 
22
 *
 
23
 * The Original Code is: all of this file.
 
24
 *
 
25
 * Contributor(s): Nathan Letwory
 
26
 *
 
27
 * ***** END GPL LICENSE BLOCK *****
 
28
 */
 
29
 
 
30
#ifdef USE_PYNODES /* note: won't work without patch */
 
31
 
 
32
#include <Python.h>
 
33
#include <eval.h>
 
34
 
 
35
#include "DNA_text_types.h"
 
36
#include "BKE_text.h"
 
37
 
 
38
#include "api2_2x/Node.h"
 
39
#include "api2_2x/gen_utils.h"
 
40
#include "BPY_extern.h"
 
41
 
 
42
#include "../SHD_util.h"
 
43
 
 
44
static PyObject *init_dynamicdict(void) {
 
45
        PyObject *newscriptdict= PyDict_New();
 
46
        PyDict_SetItemString(newscriptdict, "__builtins__", PyEval_GetBuiltins());
 
47
        EXPP_dict_set_item_str(newscriptdict, "__name__", PyString_FromString("__main__"));
 
48
        return newscriptdict;
 
49
}
 
50
 
 
51
static void free_dynamicdict(PyObject *dict) {
 
52
        if(dict!=NULL) {
 
53
                Py_DECREF(dict);
 
54
        }
 
55
}
 
56
 
 
57
static void node_dynamic_init(bNode *node) {
 
58
        NodeScriptDict *nsd= MEM_callocN(sizeof(NodeScriptDict), "node script dictionary");
 
59
        int type= node->custom2;
 
60
        node->custom2= 0;
 
61
        node->storage= nsd;
 
62
        if(type>=NODE_DYNAMIC_MENU) {
 
63
                if(type==NODE_DYNAMIC_MENU) {
 
64
                        nodeMakeDynamicType(node);
 
65
                        node->custom1= SH_NODE_DYNAMIC_NEW;
 
66
                } else {
 
67
                        node->custom1= SH_NODE_DYNAMIC_ADDEXIST;
 
68
                }
 
69
                node->id= node->typeinfo->id;
 
70
                nodeDynamicParse(node);
 
71
        } else {
 
72
                if(node->custom1== SH_NODE_DYNAMIC_LOADED) {
 
73
                        nodeMakeDynamicType(node);
 
74
                        nodeDynamicParse(node);
 
75
                } else if(node->custom1== SH_NODE_DYNAMIC_ADDEXIST)
 
76
                        nodeDynamicParse(node);
 
77
        }
 
78
}
 
79
 
 
80
static void node_dynamic_free(bNode *node)
 
81
{
 
82
        NodeScriptDict *nsd= (NodeScriptDict *)(node->storage);
 
83
        BPy_Node *pynode= nsd->node;
 
84
        Py_XDECREF(pynode);
 
85
        free_dynamicdict((PyObject *)(nsd->dict));
 
86
        MEM_freeN(node->storage);
 
87
}
 
88
 
 
89
static void node_dynamic_copy(bNode *orig_node, bNode *new_node)
 
90
{
 
91
        NodeScriptDict *nsd= (NodeScriptDict *)(orig_node->storage);
 
92
        new_node->storage= MEM_dupallocN(orig_node->storage);
 
93
        if(nsd->node)
 
94
                Py_INCREF((PyObject *)(nsd->node));
 
95
        if(nsd->dict)
 
96
                Py_INCREF((PyObject *)(nsd->dict));
 
97
}
 
98
 
 
99
static void node_dynamic_exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out) {
 
100
        BPy_Node *mynode = NULL;
 
101
        NodeScriptDict *nsd = NULL;
 
102
        PyObject *pyresult = NULL;
 
103
        PyObject *args = NULL;
 
104
        ShadeInput *shi= ((ShaderCallData *)data)->shi;
 
105
 
 
106
        if(node->custom1==SH_NODE_DYNAMIC_NEW) {
 
107
                nodeDynamicParse(node);
 
108
                return;
 
109
        }
 
110
 
 
111
        if(node->custom2<0)
 
112
                return;
 
113
 
 
114
        if(node->custom1==SH_NODE_DYNAMIC_READY || node->custom1==SH_NODE_DYNAMIC_UPDATED) {
 
115
                if(node->custom1== SH_NODE_DYNAMIC_UPDATED)
 
116
                        node->custom1= SH_NODE_DYNAMIC_READY;
 
117
 
 
118
                nsd = (NodeScriptDict *)node->storage;
 
119
 
 
120
                mynode = (BPy_Node *)(nsd->node);
 
121
                if(mynode && PyCallable_Check((PyObject *)mynode)) {
 
122
                        mynode->node= node;
 
123
                        Node_SetStack(mynode, in, NODE_INPUTSTACK);
 
124
                        Node_SetStack(mynode, out, NODE_OUTPUTSTACK);
 
125
                        Node_SetShi(mynode, shi);
 
126
                        args=Py_BuildValue("()");
 
127
                        pyresult= PyObject_Call((PyObject *)mynode, args, NULL);
 
128
                        if(!pyresult) {
 
129
                                if(PyErr_Occurred()) {
 
130
                                        PyErr_Print();
 
131
                                        node->custom2= -1;
 
132
                                } else {
 
133
                                        printf("PyObject_Call __call__ failed\n");
 
134
                                }
 
135
                        }
 
136
                        Py_XDECREF(pyresult);
 
137
                        Py_DECREF(args);
 
138
                }
 
139
        }
 
140
}
 
141
 
 
142
void nodeDynamicParse(struct bNode *node)
 
143
{
 
144
        BPy_Node *pynode= NULL;
 
145
        PyObject *dict= NULL;
 
146
        PyObject *key= NULL;
 
147
        PyObject *value= NULL;
 
148
        PyObject *testinst= NULL;
 
149
        PyObject *args= NULL;
 
150
        int pos = 0;
 
151
        NodeScriptDict *nsd= NULL;
 
152
        PyObject *pyresult = NULL;
 
153
        PyObject *pycompiled = NULL;
 
154
        Text *txt = NULL;
 
155
        char *buf= NULL;
 
156
 
 
157
        if(! node->id) {
 
158
                return;
 
159
        }
 
160
 
 
161
        if(node->custom1!=SH_NODE_DYNAMIC_READY) {
 
162
                txt = (Text *)node->id;
 
163
                nsd = (NodeScriptDict *)node->storage;
 
164
 
 
165
                if(nsd->dict==NULL && (node->custom1==SH_NODE_DYNAMIC_NEW||node->custom1==SH_NODE_DYNAMIC_LOADED)) {
 
166
                        nsd->dict= init_dynamicdict();
 
167
                } else if(nsd->dict==NULL && node->custom1==SH_NODE_DYNAMIC_ADDEXIST) {
 
168
                        nsd->dict= node->typeinfo->pydict;
 
169
                        nsd->node= node->typeinfo->pynode;
 
170
                        Py_INCREF((PyObject *)(nsd->dict));
 
171
                        Py_INCREF((PyObject *)(nsd->node));
 
172
                        node->custom1= SH_NODE_DYNAMIC_READY;
 
173
                        return;
 
174
                }
 
175
                dict= (PyObject *)(nsd->dict);
 
176
 
 
177
                if(node->custom1!=SH_NODE_DYNAMIC_ADDEXIST) {
 
178
                        buf = txt_to_buf( txt );
 
179
                        /*printf("Running script (%s, %d)...", node->name, node->custom1);*/
 
180
                        pyresult = PyRun_String(buf, Py_file_input, dict, dict);
 
181
                        /*printf(" done\n");*/
 
182
 
 
183
                        MEM_freeN(buf);
 
184
 
 
185
                        if(!pyresult) {
 
186
                                if(PyErr_Occurred()) {
 
187
                                        PyErr_Print();
 
188
                                }
 
189
                                Py_XDECREF(pyresult);
 
190
                                return;
 
191
                        }
 
192
 
 
193
                        Py_DECREF(pyresult);
 
194
 
 
195
                        while(PyDict_Next( (PyObject *)(nsd->dict), &pos, &key, &value) ) {
 
196
                                if(PyObject_TypeCheck(value, &PyType_Type)==1) {
 
197
                                        BPy_DefinitionMap *outputdef= Node_CreateOutputDefMap(node);
 
198
                                        BPy_DefinitionMap *inputdef= Node_CreateInputDefMap(node);
 
199
 
 
200
                                        args= Py_BuildValue("(OO)", inputdef, outputdef);
 
201
                                        testinst= PyObject_Call(value, args, NULL);
 
202
 
 
203
                                        Py_DECREF(outputdef);
 
204
                                        Py_DECREF(inputdef);
 
205
                                        if(testinst && PyObject_TypeCheck(testinst, &Node_Type)==1) {
 
206
                                                Py_INCREF(testinst);
 
207
                                                Py_INCREF(dict);
 
208
                                                InitNode((BPy_Node *)(testinst), node);
 
209
                                                nsd->node= testinst;
 
210
                                                node->typeinfo->execfunc= node_dynamic_exec;
 
211
                                                if(node->custom1== SH_NODE_DYNAMIC_NEW || node->custom1== SH_NODE_DYNAMIC_LOADED) {
 
212
                                                        node->typeinfo->pynode= testinst;
 
213
                                                        node->typeinfo->pydict= nsd->dict;
 
214
                                                        node->typeinfo->id= node->id;
 
215
                                                        nodeAddSockets(node, node->typeinfo);
 
216
                                                        nodeRegisterType(&node_all_shaders, node->typeinfo);
 
217
                                                        node->custom1= SH_NODE_DYNAMIC_READY;
 
218
                                                }
 
219
                                                break;
 
220
                                        }
 
221
                                        Py_DECREF(args);
 
222
                                }
 
223
                        }
 
224
                }
 
225
        }
 
226
}
 
227
 
 
228
 
 
229
bNodeType sh_node_dynamic = {
 
230
        /* next, prev  */       NULL, NULL,
 
231
        /* type code   */       SH_NODE_DYNAMIC,
 
232
        /* name        */       "Dynamic",
 
233
        /* width+range */       150, 60, 300,
 
234
        /* class+opts  */       NODE_CLASS_OP_DYNAMIC, NODE_OPTIONS,
 
235
        /* input sock  */       NULL,
 
236
        /* output sock */       NULL,
 
237
        /* storage     */       "NodeScriptDict",
 
238
        /* execfunc    */       node_dynamic_exec,
 
239
        /* butfunc     */       NULL,
 
240
        /* initfunc    */       node_dynamic_init,
 
241
        /* freefunc    */       node_dynamic_free,
 
242
        /* copyfunc    */       node_dynamic_copy,
 
243
        /* id          */       NULL
 
244
};
 
245
 
 
246
#endif /* USE_PYNODES */
 
247