Open Reality Reference Guide
FBPython Class Reference

FBPython is for python users to run python script and communicate with MotionBuilder internal python facility. More...

#include <fbpython.h>

Static Public Member Functions

static const char * GetVersion ()
 Get the version of the Python interpreter. More...
 
static const char * GetPlatform ()
 Get the target platform of the Python interpreter. More...
 
static const char * GetStdout (FBPythonContext *pPythonContext=NULL)
 Get the value of stdout. More...
 
static const char * GetStderr (FBPythonContext *pPythonContext=NULL)
 Get the value of stderr. More...
 
static bool ParseLine (const char *pLine, FBPythonContext *pPythonContext=NULL)
 Submit a line of code for parsing. More...
 
static bool EvalLine (FBPythonContext *pPythonContext=NULL)
 Submit the last line (or last few lines) of code parsed for evaluation. More...
 
static bool EvalLines (const char *pScript, FBPythonContext *pPythonContext=NULL)
 Submit a script for parsing and execution. More...
 
static bool EvalFile (const char *pFilename, FBPythonContext *pPythonContext=NULL)
 Execute a python script file within the given context. More...
 
static bool ExecuteScript (const char *pFilename, FBPythonContext *pPythonContext=nullptr)
 Put a python script file in the execution queue to be executed on the next UI thread idle callback. More...
 
static bool ClearContext (FBPythonContext *pPythonContext=NULL)
 Clears the python context. More...
 

Detailed Description

FBPython is for python users to run python script and communicate with MotionBuilder internal python facility.

A user FBPythonContext object should be created to make sure the stdout/stderr streams can be retrieved properly. If no user FBPythonContext object is passed to the various methods, the default Python Editor context will be used. Using the default context is not recommended with this class as the user might end up losing some stdout and stderr outputs.

Warning: When the user is calling the methods of FBPython in another thread other than MotionBuilder main thread, the user needs to create new thread state because Python internally prevents from illegal access to its internal Python objects if the current thread is not created by Python or has no thread state. In order to address this issue, the user could use the following workaround.

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
FBPython::ParseLine("import sys");
FBPython::EvalLine(); // MotionBuilder would crash if PyGILState_Ensure is not called before it.
PyGILState_Release(gstate);
static bool EvalLine(FBPythonContext *pPythonContext=NULL)
Submit the last line (or last few lines) of code parsed for evaluation.
static bool ParseLine(const char *pLine, FBPythonContext *pPythonContext=NULL)
Submit a line of code for parsing.

PyGILState_Ensure will create a new thread state for current calling thread.

// This example shows how to call some of the FBPython API.
bool returnValue;
std::string str;
FBPythonContext myContext( "myPluginContext" );
FBPython::ParseLine( "import os", &myContext );
returnValue = FBPython::EvalLine( &myContext );
if( returnValue == false )
str = FBPython::GetStderr( &myContext );
FBPython::ParseLine( "def myPrintFunc( pMyVar ):", &myContext );
FBPython::ParseLine( " print( pMyVar )", &myContext );
FBPython::ParseLine( "", &myContext ); // This line is required to terminate the block
returnValue = FBPython::EvalLine( &myContext );
if( returnValue == false )
str = FBPython::GetStderr( &myContext );
returnValue = FBPython::EvalLines( "myPrintFunc( 1 )", &myContext );
if( returnValue == false )
str = FBPython::GetStderr( &myContext );
else
str = FBPython::GetStdout( &myContext );
returnValue = FBPython::EvalLines( "def myPrintFunc2( pMyVar ):\n print( pMyVar )", &myContext );
if( returnValue == false )
str = FBPython::GetStderr( &myContext );
returnValue = FBPython::EvalLines( "myPrintFunc2( 10 )", &myContext );
if( returnValue == false )
str = FBPython::GetStderr( &myContext );
else
str = FBPython::GetStdout( &myContext );
returnValue = FBPython::EvalFile( "d:\\myScript.py", &myContext );
if( returnValue == false )
str = FBPython::GetStderr( &myContext );
else
str = FBPython::GetStdout( &myContext );
returnValue = FBPython::EvalLines( "myPrintFuncFromFile( 100 )", &myContext );
if( returnValue == false )
str = FBPython::GetStderr( &myContext );
else
str = FBPython::GetStdout( &myContext );
static bool EvalFile(const char *pFilename, FBPythonContext *pPythonContext=NULL)
Execute a python script file within the given context.
static const char * GetStderr(FBPythonContext *pPythonContext=NULL)
Get the value of stderr.
static const char * GetStdout(FBPythonContext *pPythonContext=NULL)
Get the value of stdout.
static bool EvalLines(const char *pScript, FBPythonContext *pPythonContext=NULL)
Submit a script for parsing and execution.

Definition at line 150 of file fbpython.h.

Member Function Documentation

◆ ClearContext()

static bool ClearContext ( FBPythonContext pPythonContext = NULL)
static

Clears the python context.

Parameters
pPythonContextContext to clear.
Returns
false If there was no interpreter.
true If there was a Python interpreter.

◆ EvalFile()

static bool EvalFile ( const char *  pFilename,
FBPythonContext pPythonContext = NULL 
)
static

Execute a python script file within the given context.

Parameters
pFilenameThe script file to execute.
pPythonContextContext in which the execution will occur.
Returns
false If there was no interpreter or there were errors/unhandled exceptions. (consult stderr for more info)
true If there was a Python interpreter and the code ran successfully. (consult stdout for output, if any)
Remarks
This function can only be used in the UI thread.

◆ EvalLine()

static bool EvalLine ( FBPythonContext pPythonContext = NULL)
static

Submit the last line (or last few lines) of code parsed for evaluation.

Should only be called once the ParseLine method returns true.

Parameters
pPythonContextContext in which the execution will occur.
Returns
false If there was no interpreter or there were errors/unhandled exceptions. (consult stderr for more info)
true If there was a Python interpreter and the code ran successfully. (consult stdout for output, if any)

◆ EvalLines()

static bool EvalLines ( const char *  pScript,
FBPythonContext pPythonContext = NULL 
)
static

Submit a script for parsing and execution.

Parameters
pScriptString to evaluate.
pPythonContextContext in which the execution will occur.
Returns
false If there was no interpreter or there were errors/unhandled exceptions. (consult stderr for more info)
true If there was a Python interpreter and the code ran successfully. (consult stdout for output, if any)

◆ ExecuteScript()

static bool ExecuteScript ( const char *  pFilename,
FBPythonContext pPythonContext = nullptr 
)
static

Put a python script file in the execution queue to be executed on the next UI thread idle callback.

Clears the current context.

Parameters
pFilenameThe script file to execute.
pPythonContextOptional. Context in which the execution will occur. Make sure the context is still available when the next UI thread idle callback will occur.
Returns
True if the script will be executed on the next UI thread idle callback, false otherwise.
Remarks
This function can be used in any thread.
Once the script is executed, consult stdout for output (or stderr for more info).

◆ GetPlatform()

static const char * GetPlatform ( )
static

Get the target platform of the Python interpreter.

Returns
A string indicating the platform for which the interpreter was compiled.

◆ GetStderr()

static const char * GetStderr ( FBPythonContext pPythonContext = NULL)
static

Get the value of stderr.

Parameters
pPythonContextContext to get the stderr from.
Returns
A string containing the last stderr output.

◆ GetStdout()

static const char * GetStdout ( FBPythonContext pPythonContext = NULL)
static

Get the value of stdout.

Parameters
pPythonContextContext to get the stdout from.
Returns
A string containing the last stdout output.

◆ GetVersion()

static const char * GetVersion ( )
static

Get the version of the Python interpreter.

Returns
A string indicating the version of the interpreter.

◆ ParseLine()

static bool ParseLine ( const char *  pLine,
FBPythonContext pPythonContext = NULL 
)
static

Submit a line of code for parsing.

Parameters
pLineLine of code that the Python interpreter should parse.
pPythonContextContext in which the parse will occur.
Returns
true If there was a Python interpreter and the line was valid.
false If there was no interpreter or the line should not yet be evaluated with EvalLine.

The documentation for this class was generated from the following file: