File: SoPyScript.cpp

package info (click to toggle)
pivy 0.6.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,216 kB
  • sloc: python: 36,331; cpp: 787; ansic: 733; makefile: 30; sh: 27; objc: 5
file content (674 lines) | stat: -rw-r--r-- 20,111 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
 * Copyright (c) 2002-2007 Systems in Motion
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <Python.h>
#include <assert.h>

#include <Inventor/SoInput.h>
#include <Inventor/C/tidbits.h>
#include <Inventor/actions/SoAudioRenderAction.h>
#include <Inventor/actions/SoCallbackAction.h>
#include <Inventor/actions/SoGetBoundingBoxAction.h>
#include <Inventor/actions/SoGetMatrixAction.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/actions/SoHandleEventAction.h>
#include <Inventor/actions/SoPickAction.h>
#include <Inventor/actions/SoGetPrimitiveCountAction.h>
#include <Inventor/actions/SoRayPickAction.h>
#include <Inventor/actions/SoSearchAction.h>
#include <Inventor/actions/SoWriteAction.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/errors/SoReadError.h>
#include <Inventor/lists/SbStringList.h>
#include <Inventor/sensors/SoOneShotSensor.h>

#include "swigpyrun.h"
#include "SoPyScript.h"

// Python code snippet to load in a URL through the urllib module
#define PYTHON_URLLIB_URLOPEN "\
import urllib\n\
try:\n\
  fd = urllib.urlopen(url.split()[0])\n\
  script = fd.read()\n\
  fd.close()\n\
except:\n\
  script = None\n\
del url"

class GlobalLock {
  public:
    GlobalLock(void) : state(PyGILState_Ensure()) {}
    ~GlobalLock() { PyGILState_Release(state); }
  private:
    PyGILState_STATE state;
};

class SoPyScriptP {
  public:
    SoPyScriptP(SoPyScript * master) :
      isReading(FALSE),
      oneshotSensor(new SoOneShotSensor(SoPyScript::eval_cb, master)),
      handler_registry_dict(PyDict_New()),
      local_module_dict(PyDict_New())
    {
      if (!global_module_dict) {
        Py_SetProgramName((char *)"SoPyScript");
        Py_Initialize();
        global_module_dict = PyModule_GetDict(PyImport_AddModule("__main__"));
        
        if (PyRun_SimpleString("from pivy.coin import *")) {
          SoDebugError::postWarning("SoPyScript::initClass",
                                    "\n*Yuk!* The box containing a fierce looking python snake to drive\n"
                                    "this node has arrived but was found to be empty! The Pivy module\n"
                                    "required for the Python Scripting Node could not be successfully\n"
                                    "imported! Check your setup and fix it so that the python snake can\n"
                                    "happily wiggle and byte you in the ass...");
        }
      }    
    }
    
    ~SoPyScriptP() {
      GlobalLock lock;
      delete this->oneshotSensor;
      Py_DECREF(handler_registry_dict);
      Py_DECREF(local_module_dict);
    }

    PyObject *
    createPySwigType(SbString typeVal, void * obj) {    
      typeVal += " *";
      swig_type_info * swig_type;
      if (!(swig_type = SWIG_TypeQuery(typeVal.getString()))) {
        // try again by prefixing the typename with So
        SbString soTypeVal("So");
        soTypeVal += typeVal;
        if (!(swig_type = SWIG_TypeQuery(soTypeVal.getString()))) {
          return NULL;
        }
      }

      return SWIG_NewPointerObj(obj, swig_type, 0);
    }

    SbBool isReading;
    SoOneShotSensor * oneshotSensor;
    PyObject * handler_registry_dict;
    PyObject * local_module_dict;
    static PyObject * global_module_dict;
    SoFieldList changedFields;
};

PyObject * SoPyScriptP::global_module_dict = NULL;

#define PRIVATE(_this_) (_this_)->pimpl

SoType SoPyScript::classTypeId;

void
SoPyScript::initClass(void)
{
  if (SoType::fromName("SoPyScript").isBad()) {
    SoPyScript::classTypeId =
      SoType::createType(SoNode::getClassTypeId(),
                         SbName("SoPyScript"),
                         SoPyScript::createInstance,
                         SoNode::nextActionMethodIndex++);

#if 0 // FIXME: necessary or not necessary? 20040412 tamer.
    SoNode::setCompatibilityTypes(SoPyScript::getClassTypeId(),
                                  SoNode::COIN_2_0|SoNode::COIN_2_2|SoNode::COIN_2_3);
#endif

    SoAudioRenderAction::addMethod(SoPyScript::getClassTypeId(), SoNode::audioRenderS);
  }
}

SoPyScript::SoPyScript(void)
  : fielddata(NULL)
{
  PRIVATE(this) = new SoPyScriptP(this);
  this->isBuiltIn = FALSE;

  assert(SoPyScript::classTypeId != SoType::badType());

  this->script.setValue(NULL);
  this->script.setContainer(this);

  this->mustEvaluate.setValue(FALSE);
  this->mustEvaluate.setContainer(this);

  this->initFieldData();
}

// Doc in parent
SoType
SoPyScript::getClassTypeId(void)
{
  return SoPyScript::classTypeId;
}

// Doc in parent
SoType
SoPyScript::getTypeId(void) const
{
  return SoPyScript::classTypeId;
}

SoPyScript::~SoPyScript()
{
  delete PRIVATE(this);
  for (int i = 0; i < this->fielddata->getNumFields(); i++) {
    SoField * f = this->fielddata->getField(this, i);
    if (f != &this->script || f != &this->mustEvaluate) { delete f; }
  }
  delete this->fielddata;
}

// Doc in parent
void
SoPyScript::doPyAction(SoAction * action, const char * funcname)
{
  if (funcname && !script.isIgnored()) {
    GlobalLock lock;

    PyObject * func = PyDict_GetItemString(PRIVATE(this)->local_module_dict, funcname);

    if (func) {
      if (coin_getenv("PIVY_DEBUG")) {
        SoDebugError::postInfo("SoPyScript::doAction",
                               "%s called!", action->getTypeId().getName().getString());
      }
      
      // convert the action instance to a Python object
      SbString typeVal(action->getTypeId().getName().getString());
      
      PyObject * pyAction;
      if (!(pyAction = PRIVATE(this)->createPySwigType(typeVal, action))) {
        SoDebugError::post("SoPyScript::doAction",
                           "%s could not be created!",
                           typeVal.getString());
        inherited::doAction(action);
        return;
      }
      if (!PyCallable_Check(func)) {
        SbString errMsg(funcname);
        errMsg += " is not a callable object!";
        PyErr_SetString(PyExc_TypeError, errMsg.getString());
      } else {
        PyObject * argtuple = Py_BuildValue("(O)", pyAction);
        PyObject * result;
        if (!(result = PyObject_CallObject(func, argtuple))) {
          PyErr_Print();
        }
        Py_XDECREF(result);
        Py_DECREF(argtuple);
        Py_DECREF(pyAction);
      }
    }

    if (coin_getenv("PIVY_DEBUG")) {
      SoDebugError::postInfo("SoPyScript::doAction",
                             "funcname: %s, func: %p",
                             funcname, func);
    }
  }
  inherited::doAction(action);
}

// Doc in parent
void
SoPyScript::GLRender(SoGLRenderAction * action)
{
  SoPyScript::doPyAction(action, "GLRender");
  inherited::GLRender(action);
}

// Doc in parent
void
SoPyScript::GLRenderBelowPath(SoGLRenderAction * action)
{
  SoPyScript::doPyAction(action, "GLRenderBelowPath");
  inherited::GLRenderBelowPath(action);
}

// Doc in parent
void
SoPyScript::GLRenderInPath(SoGLRenderAction * action)
{
  SoPyScript::doPyAction(action, "GLRenderInPath");
  inherited::GLRenderInPath(action);
}

// Doc in parent
void
SoPyScript::GLRenderOffPath(SoGLRenderAction * action)
{
  SoPyScript::doPyAction(action, "GLRenderOffPath");
  inherited::GLRenderOffPath(action);
}

// Doc in parent
void
SoPyScript::callback(SoCallbackAction * action)
{
  SoPyScript::doPyAction(action, "callback");
  inherited::callback(action);
}

// Doc in parent
void
SoPyScript::getBoundingBox(SoGetBoundingBoxAction * action)
{
  SoPyScript::doPyAction(action, "getBoundingBox");
  inherited::getBoundingBox(action);
}

// Doc in parent
void
SoPyScript::getMatrix(SoGetMatrixAction * action)
{
  SoPyScript::doPyAction(action, "getMatrix");
  inherited::getMatrix(action);
}

// Doc in parent
void
SoPyScript::handleEvent(SoHandleEventAction * action)
{
  SoPyScript::doPyAction(action, "handleEvent");
  inherited::handleEvent(action);
}

// Doc in parent
void
SoPyScript::pick(SoPickAction * action)
{
  SoPyScript::doPyAction(action, "pick");
  inherited::pick(action);
}

// Doc in parent
void
SoPyScript::rayPick(SoRayPickAction * action)
{
  SoPyScript::doPyAction(action, "rayPick");
  inherited::rayPick(action);
}

// Doc in parent
void
SoPyScript::search(SoSearchAction * action)
{
  SoPyScript::doPyAction(action, "search");
  inherited::search(action);
}

// Doc in parent
void
SoPyScript::write(SoWriteAction * action)
{
  SoPyScript::doPyAction(action, "write");
  inherited::write(action);
}

// Doc in parent
void
SoPyScript::audioRender(SoAudioRenderAction * action)
{
  SoPyScript::doPyAction(action, "audioRender");
  inherited::audioRender(action);
}

// Doc in parent
void
SoPyScript::getPrimitiveCount(SoGetPrimitiveCountAction * action)
{
  SoPyScript::doPyAction(action, "getPrimitiveCount");
  inherited::getPrimitiveCount(action);
}

// Doc in parent
void
SoPyScript::copyContents(const SoFieldContainer * from,
                         SbBool copyConn)
{
  assert(from->isOfType(SoPyScript::getClassTypeId()));

  this->initFieldData();

  const SoPyScript * fromnode = (SoPyScript*)from;
  const SoFieldData * src = from->getFieldData();

  for (int i = 0; i < src->getNumFields(); i++) {
    const SoField * f = src->getField(from, i);
    if (f != &fromnode->script &&
        f != &fromnode->mustEvaluate) {
      SoField * field = (SoField*)f->getTypeId().createInstance();
      SbString fieldname = src->getFieldName(i).getString();
      
      field->setContainer(this);
      this->fielddata->addField(this, fieldname.getString(), field);      
    }
  }
  inherited::copyContents(from, copyConn);
}

// Doc in parent
void 
SoPyScript::notify(SoNotList * list)
{
  if (!PRIVATE(this)->isReading) {
    SoField * f = list->getLastField();

    if (f == &this->mustEvaluate) {
      int pri = this->mustEvaluate.getValue() ? 
        0 : SoDelayQueueSensor::getDefaultPriority();
      PRIVATE(this)->oneshotSensor->setPriority(pri);
    }
    else if (f == &this->script) { this->executePyScript(); }
    else { 
      if (PRIVATE(this)->changedFields.find(f) == -1) {
        PRIVATE(this)->changedFields.append(f);
      }
      PRIVATE(this)->oneshotSensor->schedule();
    }
  }
  inherited::notify(list);
}

// Doc in parent
void *
SoPyScript::createInstance(void)
{
  return (void*)new SoPyScript;
}

SbBool
SoPyScript::readInstance(SoInput * in, unsigned short flags)
{
  // avoid triggering the eval cb while reading the file.
  PRIVATE(this)->isReading = TRUE;

  SbString name, typeVal;

  // read in the first string
  if (in->read(typeVal)) {
    if (typeVal != "fields") {
      in->putBack(typeVal.getString());
    } else if (in->read(typeVal) && typeVal == "[") {
      while (in->read(typeVal) && typeVal != "]") {
        SoType type = SoType::fromName(typeVal);
        
        /* if it denotes a valid type and is derived from SoField then
           read in the next string representing the name of the
           field */
        if (type != SoType::badType() &&
            type.isDerivedFrom(SoField::getClassTypeId()) &&
            in->read(name)) {
          // check for a comma at the end and strip it off
          const SbString fieldname = 
            (name[name.getLength()-1] == ',') ? name.getSubString(0, name.getLength()-2) : name;
          
          // skip the static fields
          if (fieldname == "script" || fieldname == "mustEvaluate") { continue; }
          
          /* instantiate the field and conduct similar actions as the
             SO_NODE_ADD_FIELD macro */
          SoField * field = (SoField *)type.createInstance();
          field->setContainer(this);
          this->fielddata->addField(this, fieldname.getString(), field);
        }
      }
    }
  }

  // and finally let the regular readInstance() method parse the rest
  SbBool ok = inherited::readInstance(in, flags);

  if (!ok) {
    // evaluate script
    PRIVATE(this)->oneshotSensor->schedule();
  }

  this->executePyScript();

  PRIVATE(this)->isReading = FALSE;

  return ok;
}

// Initializes the field data and adds the default fields.
void
SoPyScript::initFieldData(void)
{
  if (this->fielddata) delete this->fielddata;
  this->fielddata = new SoFieldData;
  this->fielddata->addField(this, "script", &this->script);
  this->fielddata->addField(this, "mustEvaluate", &this->mustEvaluate);
}

// Doc in parent
const SoFieldData *
SoPyScript::getFieldData(void) const
{
  return this->fielddata;
}

// loads and executes Python script contained in the script field
void
SoPyScript::executePyScript(void)
{
  // strip out possible \r's that could come from win32 line endings
  SbString src = script.getValue();
  SbString pyString;
  for (int i=0; i < src.getLength(); i++) {
    if (src[i] != '\r') { pyString += src[i]; }
  }

  GlobalLock lock;

  /* setup the local dictionary by creating a local copy of the global
     dictionary to use for executing the script */
  PyDict_Clear(PRIVATE(this)->local_module_dict);
  PyDict_Update(PRIVATE(this)->local_module_dict, PRIVATE(this)->global_module_dict);
  PyDict_Clear(PRIVATE(this)->handler_registry_dict);
      
  /* shovel the the node itself on to the Python interpreter as self
     instance */
  swig_type_info * swig_type = 0;

  if ((swig_type = SWIG_TypeQuery("SoNode *")) == 0) {
    SoDebugError::post("SoPyScript::executePyScript",
                       "SoNode type could not be found!");
  }

  // refcount ourselves for autoref'ing to work
  this->ref();

  // add the field to the global dict
  PyDict_SetItemString(PRIVATE(this)->local_module_dict, 
                       "self",
                       SWIG_NewPointerObj(this, swig_type, 0));
  
  // add the handler registry dict to the global dict
  PyDict_SetItemString(PRIVATE(this)->local_module_dict, 
                       "handler_registry",
                       PRIVATE(this)->handler_registry_dict);

  const SoFieldData * fields = this->getFieldData();
  const int n = fields->getNumFields();
  for (int i = 0; i < n; i++) {
    SoField * f = fields->getField(this, i);
    SbString typeName(f->getTypeId().getName());
    SbString fieldName(fields->getFieldName(i));
    
    // shovel the field instance on to the Python interpreter
    PyObject * pyField = NULL;
    if ((pyField = PRIVATE(this)->createPySwigType(typeName, f)) == NULL) {
      SoDebugError::post("SoPyScript::readInstance",
                         "field type %s could not be created!",
                          typeName.getString());            
      continue;
    }

    // add the field to the global dict
    PyDict_SetItemString(PRIVATE(this)->local_module_dict, 
                         fieldName.getString(),
                         pyField);

    SbString funcname("handle_");
    funcname += fieldName;

    // add the field handler to the handler registry
    PyDict_SetItemString(PRIVATE(this)->handler_registry_dict, 
                         fieldName.getString(),
#ifdef PY_2
                         PyString_FromString(funcname.getString());
#else
                         PyString_FromString(funcname.getString());
#endif
                         )
  }

  // check if the script denotes an URL or path
  /* FIXME: maybe, just maybe, we could do a little better error
     handling here??? throwing a Syntax Error at the user's face in
     case of a not resolving URL or non existent file for the reason
     of being to lame to catch a couple of exceptions is absolutely
     pathetic!  reconsider the whole approach as it smells like a
     lousy hack! 20041021 tamer. */
  if (src.getLength()) {
    // try to find a file relative to the current input directory stack
    SbStringList subdirs;
    SbString fullName = SoInput::searchForFile(pyString, SoInput::getDirectories(), subdirs);
    if (fullName != "") { pyString = fullName; }
    PyObject * url = 
#ifdef PY_2
      PyString_FromString(pyString.getString());
#else
      PyBytes_FromString(pyString.getString());
#endif

    // add the url to the global dict
    PyDict_SetItemString(PRIVATE(this)->local_module_dict, "url", url);

    PyObject * result = PyRun_String(PYTHON_URLLIB_URLOPEN,
                                     Py_file_input, 
                                     PRIVATE(this)->local_module_dict,
                                     PRIVATE(this)->local_module_dict);
    if (result) { Py_DECREF(result); }
    else { PyErr_Print(); }

    PyObject * script_new = PyDict_GetItemString(PRIVATE(this)->local_module_dict, "script");
    if (script_new != Py_None) {
      pyString.makeEmpty();
      pyString = 
#ifdef PY_2
        PyString_AsString(script_new);
#else
        PyBytes_AsString(script_new);
    }
    Py_DECREF(url);
  }

  PyObject * result = PyRun_String((char *)pyString.getString(),
                                   Py_file_input,
                                   PRIVATE(this)->local_module_dict,
                                   PRIVATE(this)->local_module_dict);
  if (result) { Py_DECREF(result); }
  else { PyErr_Print(); }

  PRIVATE(this)->changedFields.truncate(0);

  if (coin_getenv("PIVY_DEBUG")) {
    SoDebugError::postInfo("SoPyScript::executePyScript",
                           "script executed at full length!");
  }
}

// callback for oneshotSensor
void 
SoPyScript::eval_cb(void * data, SoSensor *)
{
  SoPyScript * self = (SoPyScript*)data;

  if (coin_getenv("PIVY_DEBUG")) {
    SoDebugError::postInfo("SoPyScript::eval_cb",
                           "eval_cb called!");
  }

  for (int i = 0; i < PRIVATE(self)->changedFields.getLength(); i++) {
    SoField * f = PRIVATE(self)->changedFields[i];
    SbName fieldname;
    if (self->getFieldName(f, fieldname)) {
      GlobalLock lock;

      // look up the function name in the handler registry
      PyObject * funcname = PyDict_GetItemString(PRIVATE(self)->handler_registry_dict,
                                                 fieldname.getString());
      if (!funcname) { continue; }

      // check if it is a string
#ifdef PY_2
      if (PyString_Check(funcname)) {
#else
      if (PyBytes_Check(funcname)) {
#endif
        // get the function handle in the global module dictionary if available
        PyObject * func = PyDict_GetItemString(PRIVATE(self)->local_module_dict,
#ifdef PY_2
                                               PyString_AsString(funcname));
#else
                                               PyBytes_AsString(funcname));
#endif
        if (!func) { continue; }

        if (coin_getenv("PIVY_DEBUG")) {
          SoDebugError::postInfo("SoPyScript::eval_cb",
                                 "fieldname: %s, funcname: %s, func: %p",
                                 fieldname.getString(),
#ifdef PY_2
                                 PyString_AsString(funcname),
#else
                                 PyBytes_AsString(funcname),
#endif
                                 func);
        }

        if (!PyCallable_Check(func)) {
#ifdef PY_2
          SbString errMsg(PyString_AsString(funcname));
#else
          SbString errMsg(PyBytes_AsString(funcname));
#endif
          errMsg += " is not a callable object!";
          PyErr_SetString(PyExc_TypeError, errMsg.getString());
        } else {
          PyObject * result;
          if (!(result = PyObject_CallObject(func, NULL))) {
            PyErr_Print();
          }
          Py_XDECREF(result);
        }
      } // if (self->getFieldName(...)) - lock released here
    }
  }

  PRIVATE(self)->changedFields.truncate(0);
}

#undef PRIVATE