H3D API  2.4.1
PythonScript.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API 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 H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __PYTHONSCRIPT_H__
30 #define __PYTHONSCRIPT_H__
31 
32 #include <H3DUtil/Exception.h>
33 #include <H3D/H3DScriptNode.h>
34 #include <H3D/MFNode.h>
35 #include <H3D/SFString.h>
36 
37 #ifdef HAVE_PYTHON
38 
39 #ifndef PyObject_HEAD
40 struct _object;
41 typedef _object PyObject;
42 #endif
43 
44 namespace H3D {
45 
54  struct H3DAPI_API PythonScript : public H3DScriptNode {
56  PythonScript( Inst< MFString > _url = 0,
57  Inst< MFNode > _references = 0,
58  Inst< SFString > _moduleName = 0 );
59 
60  virtual Field * getField( const string& _name) const {
61  Field *f = H3DScriptNode::getField( _name );
62  if( f ) return f;
63  if ( _name == "references" )
64  return references.get();
65  else
66  return lookupField( _name );
67  }
68 
70  ~PythonScript();
71 
75  virtual Field *lookupField( const string &_name ) const;
76 
81  virtual PyObject *getPythonAttribute( const string &_name );
82 
85  virtual void getTopLevelFields( vector< pair< string, Field * > > &fields );
86 
91  virtual void traverseSG( TraverseInfo &ti );
92 
93  virtual bool initialiseParser();
94 
95  virtual string getInstanceName() {
96  ostringstream namestr;
97  namestr << "PythonScript_" << this;
98  return namestr.str();
99  }
100 
101  virtual void loadScript( const string &script_filename, const string &script_content= "" );
102 
105  std::string execute ( const std::string& _command );
106 
107  static void setargv( int _argc, char *_argv[] );
108  static void setargv( int _argc, wchar_t *_argv[] );
118  static bool initPythonHome( const std::string& exe_with_full_path );
119 
122  virtual void initialize();
123 
129  H3DUniquePtr< MFNode > references;
130 
139  H3DUniquePtr< SFString > moduleName;
140 
143 
164  static void allowMainThreadPython();
165 
169  static void disallowMainThreadPython();
170 
173  static bool mainThreadPythonAllowed();
174 
177  inline PyObject *getPythonModule() {
178  return static_cast< PyObject *>(module);
179  }
180 
181  private:
182  void * module;
183  void * module_dict;
184  static int argc;
185  // Set the argv parameter in Python from the PythonScript::argv or PythonScript::w_argv
186  // depending on which is in use.
187  static void pythonSetargv();
188 
190  void initializePython();
191 
192  // argv or w_argv is the program argv parameter. Only one will be set to non-NULL. Which
193  // on depends if the main function is the wide chars version or not.
194  static char **argv;
195  static wchar_t **w_argv;
196  string module_name;
197  static bool python_h3d_initialized;
198  };
199 }
200 
201 #endif // HAVE_PYTHON
202 #endif
Base class for all scripting node interfaces.
Contains the MFNode class.
Contains the SFString field class.
The Field class.
Definition: Field.h:46
virtual Field * getField(const string &_name) const
return a pointer to the field specified by name within this instance
Definition: Node.cpp:217
TraverseInfo is a structure that is passed along when traversing the scene graph.
Definition: TraverseInfo.h:57
H3D API namespace.
Definition: Anchor.h:38
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194
The base class for script nodes.
Definition: H3DScriptNode.h:41
Interface to the dynamic Python library for executing script code as part of the API.
Definition: PythonScript.h:54
virtual Field * getField(const string &_name) const
return a pointer to the field specified by name within this instance
Definition: PythonScript.h:60
PyObject * getPythonModule()
Returns a borrowed reference to the PyModule object use by the PythonScript node.
Definition: PythonScript.h:177
H3DUniquePtr< SFString > moduleName
Specify a specific moduleName for the pythonscript.
Definition: PythonScript.h:139
H3DUniquePtr< MFNode > references
Through this field one can pass references to Nodes in the scene graph that can then be access throug...
Definition: PythonScript.h:129
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: PythonScript.h:142