Class vtkMRMLLayerDMPythonUtil

Inheritance Relationships

Base Type

  • public vtkObject

Class Documentation

class vtkMRMLLayerDMPythonUtil : public vtkObject

Inheritance diagram for vtkMRMLLayerDMPythonUtil:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "2" [label="vtkObject" tooltip="vtkObject"] "1" [label="vtkMRMLLayerDMPythonUtil" tooltip="vtkMRMLLayerDMPythonUtil" fillcolor="#BFBFBF"] "1" -> "2" [dir=forward tooltip="public-inheritance"] }

Collaboration diagram for vtkMRMLLayerDMPythonUtil:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "2" [label="vtkObject" tooltip="vtkObject"] "1" [label="vtkMRMLLayerDMPythonUtil" tooltip="vtkMRMLLayerDMPythonUtil" fillcolor="#BFBFBF"] "1" -> "2" [dir=forward tooltip="public-inheritance"] }

Utility class for Python/C++ interoperability in VTK MRML Layer Displayable Manager.

This class provides a collection of static utility methods for converting between VTK C++ objects and Python objects, managing Python object lifecycles, and calling Python methods from C++. It serves as a bridge layer for Python integration in the Layer Displayable Manager module.

Public Functions

vtkTypeMacro(vtkMRMLLayerDMPythonUtil, vtkObject)

Public Static Functions

static vtkMRMLLayerDMPythonUtil *New()
static PyObject *ToPyObject(vtkObjectBase *obj)

Convert a VTK object to a Python object.

Parameters:

obj – VTK object base pointer to convert

Returns:

PyObject* Python object representation of the VTK object

static PyObject *ToPyObject(unsigned long value)

Convert an unsigned long value to a Python object.

Parameters:

value – Unsigned long value to convert

Returns:

PyObject* Python object representation of the value

static PyObject *ToPyObject(const std::string &value)

Convert a string value to a Python object.

Parameters:

value – string value to convert

Returns:

PyObject* Python object representation of the value

static PyObject *RawPtrToPython(void *ptr)

Convert a raw C pointer to a Python object.

Parameters:

ptr – Raw pointer to convert

Returns:

PyObject* Python object wrapping the pointer

static vtkSmartPyObject ToPyArgs(const std::vector<PyObject*> &pyObjs)

Create a Python tuple from a vector of Python objects.

Parameters:

pyObjs – Vector of Python objects to package as arguments

Returns:

vtkSmartPyObject Smart pointer to Python tuple containing the objects

static vtkSmartPyObject ToPyArgs(vtkObjectBase *obj)

Create a Python tuple from a single VTK object.

Parameters:

obj – VTK object base pointer to package as arguments

Returns:

vtkSmartPyObject Smart pointer to Python tuple containing the object

static vtkSmartPyObject ToPyArgs(vtkObject *obj, unsigned long eventId, void *callData)

Create a Python tuple for VTK event callback arguments.

Parameters:
  • obj – VTK object that triggered the event

  • eventId – Event identifier

  • callData – Additional event-specific data

Returns:

vtkSmartPyObject Smart pointer to Python tuple containing the event arguments

static PyObject *CastCallData(PyObject *object, int vtkType)

Cast call data to appropriate Python type based on VTK type.

Parameters:
  • object – Python object to cast

  • vtkType – VTK type identifier to cast to

Returns:

PyObject* Casted Python object

static PyObject *CallPythonMethod(PyObject *object, const vtkSmartPyObject &pyArgs, const std::string &fName)

Call a named method on a Python object with arguments.

Parameters:
  • object – Python object on which to call the method

  • pyArgs – Python tuple of arguments to pass to the method

  • fName – Name of the method to call

Returns:

PyObject* Return value from the Python method call

static PyObject *CallPythonObject(PyObject *object, const vtkSmartPyObject &pyArgs)

Call a Python callable object with arguments.

Parameters:
  • object – Python callable object to invoke

  • pyArgs – Python tuple of arguments to pass

Returns:

PyObject* Return value from the Python callable

static void SetPythonObject(PyObject **destObject, PyObject *object)

Safely set a Python object pointer with proper reference counting.

Parameters:
  • destObject – Pointer to destination PyObject pointer to update

  • object – Source Python object to assign

static void DeletePythonObject(PyObject **destObject)

Safely delete a Python object with proper reference counting.

Parameters:

destObject – Pointer to PyObject pointer to delete and set to nullptr

static bool IsValidPythonContext()

true is python is initialized and no error has occurred.

static std::string FormatExceptionTraceback()

returns the formatted exception string if any exception occurred. Can be used to warn on current errors without swallowing the exception.

static void PrintErrorTraceback(const vtkObject *object, const std::string &errorMsg)

Outputs a vtkErrorMacro containing the traceback in case it’s not empty.

Does nothing if the python environment is not initialized. Should be used by Methods that cannot properly forward Python results back to the interpreter to avoid silent errors happening in the Python layer (or errors being raised in unrelated parts of the executable).

This method doesn’t swallow the underlying python exception. Python level exceptions should be handled at the python level where they are raised to avoid silent errors from happening.

static std::string GetObjectStr(PyObject *object)

returns the input’s PyObject’s string representation as std::string. If the python environment is not initialized, returns empty string. If the input object is nullptr, returns “None” If the input object string representation fails, return “INVALID_OBJECT_STR” Calling this method doesn’t change the current python error stack.

Protected Functions

vtkMRMLLayerDMPythonUtil()
~vtkMRMLLayerDMPythonUtil() override