Class PythonScriptOperation

  • All Implemented Interfaces:
    Operation

    public class PythonScriptOperation
    extends java.lang.Object
    implements Operation
    A class that implements an operation using Jython. This enables users to write plugins for the application as Python scripts. Python script plugins should have global lists of SocketHints called "inputs" and "outputs" that declare what parameters the script accepts and what outputs in produces. For example,
    
        import edu.wpi.grip.core as grip
        import java.lang.Integer
        inputs = [
            grip.SocketHint("a", java.lang.Integer, grip.SocketHint.View.SLIDER, (0, 100), 75),
            grip.SocketHint("b", java.lang.Integer, grip.SocketHint.View.SLIDER, (0, 100), 25),
        ]
        outputs = [
            grip.SocketHint("c", java.lang.Integer),
        ]
     
    The script should also define a function "perform", which takes the same number of parameters as there are inputs and returns the values for the outputs. It can return a single value if there's one output, or a sequence type for any number of values.
    
     def perform(a, b):
     return a + b
     
    Lastly, the script can optionally have global "name" and "summary" strings to provide the user with more information about what the operation does.
    • Method Detail

      • descriptionFor

        public static OperationDescription descriptionFor​(PythonScriptFile pythonScriptFile)
        Parameters:
        pythonScriptFile - The file to create the description for.
        Returns:
        An OperationDescription that describes this PythonScriptFile.
      • getInputSockets

        public java.util.List<InputSocket> getInputSockets()
        Specified by:
        getInputSockets in interface Operation
        Returns:
        An array of Sockets, based on the global "inputs" list in the Python script.
      • getOutputSockets

        public java.util.List<OutputSocket> getOutputSockets()
        Specified by:
        getOutputSockets in interface Operation
        Returns:
        An array of Sockets, based on the global "outputs" list in the Python script.
      • perform

        public void perform()
        Perform the operation by calling a function in the Python script. This method adapts each of the inputs into Python objects, calls the Python function, and then converts the outputs of the function back into Java objects and assigns them to the outputs array. The Python function should return a tuple, list, or other sequence containing the outputs. If there is only one output, it can just return a value. Either way, the number of inputs and outputs should match up with the number of parameters and return values of the function.
        Specified by:
        perform in interface Operation