30 #ifndef __PYTHONMETHODS_H__
31 #define __PYTHONMETHODS_H__
44 #if defined _DEBUG && ! defined HAVE_PYTHON_DEBUG_LIBRARY
45 #define _DEBUG_UNDEFED
59 #if defined(__APPLE__) && defined(__MACH__) && defined( HAVE_PYTHON_OSX_FRAMEWORK )
60 #include <Python/Python.h>
63 #ifdef _POSIX_C_SOURCE
64 #undef _POSIX_C_SOURCE
85 namespace PythonInternals {
86 #if PY_MAJOR_VERSION >= 3
87 PyMODINIT_FUNC PyInit_H3D(
void );
89 PyMODINIT_FUNC initH3D();
92 H3DAPI_API PyObject * initH3DInternal();
93 H3DAPI_API
void finishH3DInternal();
95 double pyObjectToDouble( PyObject *v );
97 void fieldDestructor(
void *f );
99 PyObject *fieldAsPythonObject( Field * f,
bool destruct =
false, PyNode * owner_pynode = NULL );
101 PyObject *pythonCreateField( PyObject *
self, PyObject *args );
103 bool pythonSetFieldValueFromObject( Field *field_ptr, PyObject *v );
105 PyObject *pythonFieldSetValue( PyObject *
self, PyObject *args );
107 PyObject *pythonFieldGetValue( PyObject *
self, PyObject *arg );
109 PyObject *pythonFieldSetAccessType( PyObject *
self, PyObject *arg );
111 PyObject *pythonFieldGetAccessType( PyObject *
self, PyObject *arg );
113 PyObject *pythonFieldSetAccessCheck( PyObject *
self, PyObject *arg );
115 PyObject *pythonFieldIsAccessCheckOn( PyObject *
self, PyObject *arg );
117 PyObject *pythonFieldRoute( PyObject *
self, PyObject *args );
119 PyObject *pythonFieldRouteNoEvent( PyObject *
self, PyObject *args );
121 PyObject *pythonFieldUnroute( PyObject *
self, PyObject *args );
123 PyObject *pythonFieldReplaceRoute( PyObject *
self, PyObject *args );
125 PyObject *pythonFieldReplaceRouteNoEvent( PyObject *
self, PyObject *args );
127 PyObject *pythonFieldUnrouteAll( PyObject *
self, PyObject *arg );
129 PyObject *pythonSFStringIsValidValue( PyObject *
self, PyObject *args );
131 PyObject *pythonSFStringGetValidValues( PyObject *
self, PyObject *args );
133 PyObject *pythonGetCPtr( PyObject *
self, PyObject *arg );
135 PyObject* pythonCreateX3DFromURL( PyObject *
self, PyObject *arg );
137 PyObject* pythonCreateX3DFromString( PyObject *
self, PyObject *arg );
139 PyObject* pythonCreateX3DNodeFromURL( PyObject *
self, PyObject *arg );
141 PyObject* pythonCreateX3DNodeFromString( PyObject *
self, PyObject *arg );
143 PyObject* pythonCreateVRMLFromURL( PyObject *
self, PyObject *arg );
145 PyObject* pythonCreateVRMLFromString( PyObject *
self, PyObject *arg );
147 PyObject* pythonWriteNodeAsX3D( PyObject *
self, PyObject *arg );
149 PyObject* pythonCreateVRMLNodeFromURL( PyObject *
self, PyObject *arg );
151 PyObject* pythonCreateVRMLNodeFromString( PyObject *
self, PyObject *arg );
153 PyObject* pythonFieldRoutesTo( PyObject *
self, PyObject *arg );
155 PyObject* pythonFieldHasRouteFrom( PyObject *
self, PyObject *arg );
157 PyObject* pythonFieldGetRoutesIn( PyObject *
self, PyObject *arg );
159 PyObject* pythonFieldGetRoutesOut( PyObject *
self, PyObject *arg );
161 PyObject* pythonGetCurrentScenes( PyObject *
self, PyObject *arg );
163 PyObject* pythonGetActiveDeviceInfo( PyObject *
self, PyObject *arg );
165 PyObject* pythonGetActiveBindableNode( PyObject *
self, PyObject *arg );
167 PyObject* pythonGetActiveViewpoint( PyObject *
self, PyObject *arg );
169 PyObject* pythonGetActiveNavigationInfo( PyObject *
self, PyObject *arg );
171 PyObject* pythonGetActiveStereoInfo( PyObject *
self, PyObject *arg );
173 PyObject* pythonGetActiveBackground( PyObject *
self, PyObject *arg );
175 PyObject* pythonGetActiveFog( PyObject *
self, PyObject *arg );
177 PyObject* pythonGetActiveGlobalSettings( PyObject *
self, PyObject *arg );
179 PyObject* pythonMFieldErase( PyObject *
self, PyObject *arg );
181 PyObject* pythonMFieldPushBack( PyObject *
self, PyObject *arg );
183 PyObject* pythonMFieldClear( PyObject *
self, PyObject *arg );
185 PyObject* pythonMFieldBack( PyObject *
self, PyObject *arg );
187 PyObject* pythonMFieldFront( PyObject *
self, PyObject *arg );
189 PyObject* pythonMFieldEmpty( PyObject *
self, PyObject *arg );
191 PyObject* pythonMFieldPopBack( PyObject *
self, PyObject *arg );
193 PyObject* pythonMFieldSize( PyObject *
self, PyObject *arg );
195 PyObject* pythonFieldTouch( PyObject *
self, PyObject *arg );
197 PyObject* pythonResolveURLAsFile( PyObject *
self, PyObject *arg );
199 PyObject* pythonResolveURLAsFolder( PyObject *
self, PyObject *arg );
201 PyObject* throwQuitAPIException( PyObject *
self, PyObject *arg );
203 PyObject* pythonCreateNode( PyObject*
self, PyObject* arg, PyObject* keywds );
205 PyObject* pythonGetHapticsDevice( PyObject *
self, PyObject *arg );
207 PyObject* pythonGetNrHapticsDevices( PyObject *
self, PyObject *arg );
209 PyObject* pythonGetNamedNode( PyObject *
self, PyObject *arg );
211 PyObject* pythonFieldSetName( PyObject *
self, PyObject *arg );
213 PyObject* pythonFieldGetName( PyObject *
self, PyObject *arg );
215 PyObject* pythonFieldGetFullName( PyObject *
self, PyObject *arg );
217 PyObject* pythonFieldGetTypeName( PyObject *
self, PyObject *arg );
219 PyObject* pythonFieldGetOwner( PyObject *
self, PyObject *arg );
221 PyObject* pythonFieldSetOwner( PyObject *
self, PyObject *arg );
223 PyObject* pythonFieldGetValueAsString( PyObject *
self, PyObject *arg );
225 PyObject* pythonFieldSetValueFromString( PyObject *
self, PyObject *arg );
227 PyObject* pythonFieldUpToDate( PyObject *
self, PyObject *arg );
229 PyObject* pythonFieldIsUpToDate( PyObject *
self, PyObject *arg );
243 PyObject* pythonFindNodes( PyObject *
self, PyObject *arg );
245 PyObject* pythonTakeScreenshot( PyObject *
self, PyObject *arg );
247 PyObject* pythonAddURNResolveRule( PyObject *
self, PyObject *arg );
249 PyObject* pythonExportGeometryAsSTL( PyObject *
self, PyObject *args );
259 struct PythonFieldBase {
260 PythonFieldBase(
void *_python_field ) :
261 python_field( _python_field ),
262 have_update( false ),
263 have_type_info( false ),
264 have_opt_type_info( false ) {
266 python_fields.insert(
this );
269 virtual ~PythonFieldBase() {
271 python_fields.erase(
this );
273 static H3DAPI_API std::set< PythonFieldBase * > python_fields;
286 bool have_opt_type_info;
290 struct PythonField :
public F, PythonFieldBase {
292 PythonField(
void *_python_field ) :
293 PythonFieldBase( _python_field ) {
296 virtual void update() {
298 if( Profiling::profile_python_fields ) {
299 H3DUtil::H3DTimer::stepBegin( getName().c_str(),
"PYTHON" );
304 PyGILState_STATE state = PyGILState_Ensure();
306 PyObject *python_update = PyObject_GetAttrString(
307 static_cast<PyObject *
>(python_field),
"update" );
309 if( python_update ) {
310 PyObject *args = PyTuple_New( 1 );
311 PyObject *f = PythonInternals::fieldAsPythonObject( this->event.ptr,
false );
312 PyTuple_SetItem( args, 0, f );
315 PyObject *r = PyObject_CallObject( python_update,
320 if( r == Py_None && this->getX3DType() != X3DTypes::SFNODE && this->getX3DType() != X3DTypes::MFNODE ) {
321 Console( LogLevel::Warning ) <<
"Warning: update()-function for Python defined field of type "
322 << this->getFullName() <<
" does not return a value. " << endl;
325 if( !PythonInternals::pythonSetFieldValueFromObject(
this, r ) ) {
326 std::stringstream ss;
327 ss <<
"Warning: invalid return value from update()-function"
328 <<
" for Python defined field of type "
329 << this->getFullName();
330 bool is_MField = (this->getX3DType() % 2 != 0);
331 if( is_MField && !PyList_Check( r ) ) {
332 ss <<
". The field is a MField which requires a list of values to be returned"
333 <<
" while the current return value is not a list.";
335 Console( LogLevel::Warning ) << ss.str() << std::endl;
336 PyObject* err = PyErr_Occurred();
343 PyObject* err = PyErr_Occurred();
348 Py_DECREF( python_update );
355 PyGILState_Release( state );
357 if( Profiling::profile_python_fields ) {
358 H3DUtil::H3DTimer::stepEnd( getName() );
370 void checkFieldType( Field *f,
int index ) {
371 if( have_type_info == 0 && have_opt_type_info == 0 ) {
372 F::checkFieldType( f, index );
377 PyGILState_STATE state = PyGILState_Ensure();
379 PyObject *python_typeinfo = PyObject_GetAttrString(
380 static_cast<PyObject *
>(python_field),
"__type_info__" );
384 if( python_typeinfo )
385 arg_size = (int)PyTuple_Size(
static_cast<PyObject *
>(python_typeinfo) );
387 if( index >= arg_size ) {
388 PyObject *python_opttypeinfo = PyObject_GetAttrString(
389 static_cast<PyObject *
>(python_field),
"__opt_type_info__" );
391 int opt_arg_size = -1;
392 if( python_opttypeinfo )
393 opt_arg_size = (int)PyTuple_Size(
static_cast<PyObject *
>(python_opttypeinfo) );
394 if( opt_arg_size > 0 ) {
395 PyObject *type_info =
396 PyTuple_GetItem(
static_cast<PyObject *
>(python_opttypeinfo),
398 PyObject *type_id = PyObject_GetAttrString( type_info,
"type" );
399 int type_int = PyInt_AsLong( type_id );
400 if( f->getTypeName().compare(
401 X3DTypes::typeToString( (X3DTypes::X3DType)type_int ) ) != 0 ) {
403 err <<
"Bad input, expected "
404 << X3DTypes::typeToString( (X3DTypes::X3DType)type_int )
405 <<
" got " << f->getTypeName() <<
" for route" << index;
406 PyGILState_Release( state );
412 err <<
"Too many inputs, expected " << arg_size + 1;
413 PyGILState_Release( state );
417 }
else if( python_typeinfo ) {
418 PyObject *type_info =
419 PyTuple_GetItem(
static_cast<PyObject *
>(python_typeinfo),
421 PyObject *type_id = PyObject_GetAttrString( type_info,
"type" );
422 int type_int = PyInt_AsLong( type_id );
429 if( f->getTypeName().compare(
430 X3DTypes::typeToString( (X3DTypes::X3DType)type_int ) ) != 0 &&
431 strcmp( X3DTypes::typeToString( (X3DTypes::X3DType)type_int ),
432 "UNKNOWN_X3D_TYPE" ) != 0 ) {
434 err <<
"Bad input, expected "
435 << X3DTypes::typeToString( (X3DTypes::X3DType)type_int )
436 <<
" got " << f->getTypeName() <<
" for route" << index;
441 PyGILState_Release( state );
Contains the Field class.
Defines and function for easy porting to Python 3.
PyObject * pythonAddProgramSetting(PyObject *self, PyObject *arg)
addProgramSettings( field, setting_name = "", section = "" )
Definition: PythonMethods.cpp:3110
Script node for python scripting.
This file contains functions for convertion from a string to a value of an X3D field type.
Header file containing useful functions for conversions of different representations of X3D types.
Header file containing all X3D types enumerated.
static bool inMainThread()
An exception thrown when a field is of the wrong type when it is checked.
Definition: PythonMethods.h:257
H3D_VALUE_EXCEPTION(string, InvalidType)
An exception thrown when a field is of the wrong type when it is checked.
H3D API namespace.
Definition: Anchor.h:38