| Customize Help

Script-based user-defined MIL function



Besides C-based user-defined functions, the MIL Function Development module also allows you to create user-defined MIL functions based on functions/scripts that require an interpreter (for example, written in Python) or the .NET framework (for example, written in C#) to be present at runtime. These are referred to as script-based user-defined functions.

Script-based and C-based user-defined MIL functions have many similarities with regards to their implementation and manner of use. The following table gives a brief overview of the differences encountered:

Feature

C-based user-defined functions

Script-based user-defined functions

Parameter limitation

16 parameters

15 parameters

Compilation step

Compiled when the MIL application is compiled

Compiled at runtime

Function grouping within MIL

7 modules

64 functions per module

2 modules

64 functions per module

Supported languages

C, C++

C#, VB .NET, Python 2.7, 3.7

Modifiable during runtime

No

Yes

Much like the C-based user-defined functions, each script-based user-defined MIL function has a master and slave component to it. However, unlike a C-based user-defined function, you must allocate a MIL function context using MfuncAllocScript() instead of MfuncAlloc(). The slave portion of the script-based user-defined MIL function is the script you provide. Similar to C-based user-defined functions, you must register the parameters using MfuncParam(). By passing the function context identifier to MfuncCall(), you will execute the function specified in the call to MfuncAllocScript().

The script/function can be written in any Python language version or in a .NET languages, as long as an interpreter interface DLL is provided. Included in MIL are interpreter interface DLLs for the following languages (and language versions):

  • CPython 2.7.

  • CPython 3.7.

  • C#.

  • Visual Basic .NET.

The code required to develop and compile your own interpreter interface DLL is also provided so that you can use another language version of Python or a language supported by the .NET Framework, within the MIL Function Development module. This code can be found in the "\Matrox Imaging\MIL\Scripting\MilInterpCustom" directory of your MIL installation.

If you are using a language (or language version) for which an interpreter interface DLL is not provided, you will be required to develop your own interpreter interface DLL from the provided code, along with ensuring the presence of an appropriate MIL wrapper to unpack parameters in the slave script/function. This wrapper is required to associate objects in that language with MIL, and make other MIL calls such as unpacking your parameters.

Notable differences between the two types of user-defined functions are that you can register a maximum of 15 parameters for script-based user-defined functions, whereas you can register up to 16 parameters for C-based user-defined functions. Furthermore, you can organize your script-based user-defined functions into only two modules (M_SCRIPT_MODULE...); whereas, you can organize your C-based user-defined functions into 7 modules. However, like C-based user-defined MIL functions, you can store up to 64 functions per module.

If your script/function requires other DLLs to run, such as C# dependencies, use the M_ADD_SCRIPT_REFERENCE control type to specify a path to the file name of the DLL(s) that you need to include. Furthermore, using the M_COMPILE control type, you can set when to recompile .NET languages (into byte-code); this allows you to modify the script's code without having to stop the entire MIL application. You can set whether your script should be compiled once (M_ONCE) or if the script should be recompiled whenever the file on disk has been changed (M_MODIFIED).

For languages within the .NET framework, it is possible to specify whether debug information should be generated, using the M_DEBUG_INFORMATION control type. If you choose to have debug information generated, you need to set a path to the directory where the compiled version of the script (assembly file) will be created when the script/function is run. You must pass this path to the M_DEBUG_INFORMATION_PATH control type.

Much like with MfuncAlloc(), MIL function contexts allocated using MfuncAllocScript() can be set to have the slave function execute either locally or remotely. If the function can be executed remotely, it is important to include a copy of the DLL(s) and script file(s) on the remote computers in the cluster.

The following example shows how to create a script-based user-defined MIL function.