Using Python Object in LabVIEW Python Node

Updated Oct 25, 2023

Environment

Software

  • LabVIEW

Programming Language

  • Python

Python is one of the fastest-growing programming languages in the world. To ensure LabVIEW users are able to integrate Python object into the LabVIEW application, LabVIEW's Python Node officially supports the use of Python object in LabVIEW version 2022 Q3 and above!

This article will demonstrate how to use Python object in a LabVIEW environment.

  1. Ensure LabVIEW 2022 Q3 and above is installed.
  2. Ensure the Python version compatible with the installed LabVIEW is installed with its Path added into the system environment variable.
  3. Prepare a Python script with object implemented. For easier understanding of the reader, the following Python script will be used throughout this article. 
    class IncrementClass:
        def __init__(self, i):
            self.i = i
        def increment(self, a):
            res = a + self.i
            return res
    
    def NumberForIncrement(obj, a):
        s = obj.increment(a)
        return s
    
    def ObjInitialize(i):
        obj = IncrementClass(i)
        return obj
  4. Save the script as pyClass.py.
  5. In an empty VI, use Open Python Session Function to open a Python session with the Python version installed.
  6. Call ObjInitialize function by Python Node. Connect the return type terminal with a Python Object Refnum and connect one numeric input parameter.
  7. Call NumberForIncrement function using another Python Node. The first input parameter should be the returned Python Object Refnum from the previous Python Node while the second input parameter should be a numeric data type.
  8. Create a numeric indicator for the return value of second Python Node.
  9. Close the Python session using Close Python Session function.
  10. Close any opened session using Close Reference function.

VI below illustrates the outcome of the instructions:
pyclass.png
Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a 
snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
The result obtained is the summation of both numeric values inputted into the Python Node.