Cannot bind to Python function

Cannot bind to Python function

mischa_spelt
Advisor Advisor
323 Views
6 Replies
Message 1 of 7

Cannot bind to Python function

mischa_spelt
Advisor
Advisor

[ FlexSim 22.1.0 ]

The ability to connect FlexSim and Python looks really cool so I decided to make a simple test case. Rather than controlling FlexSim from Python I first decided to see if I can call a function from FlexSim.

I created a Test.fsm model and put a simple Python script in FlexSimPy\FlexSimPy.py. But when I try to call the Python function I get

exception: Code Binding Error: could not bind to function
Node: /Tools/UserCommands/Add/code
Binding string: external python "FlexSimPy/FlexSimPy" "add"
Windows Error Code : 126

As far as I can see code 126 means missing DLL, but according to the GitHub documentation,

The PyConnector project implements the ability to bind the evaluation of nodes in FlexSim's tree to the evaluation of python functions. The project is implemented to work with python versions 3.7, 3.8, 3.9 and 3.10. The output DLLs from this project are included in the installed version of FlexSim.

(emphasis mine) and I found the DLLs in program\python so I assumed this should work out of the box.

I set the Python version in Global Preferences and made sure that python.exe is on my path. What am I missing? 🙂

0 Likes
Accepted solutions (1)
324 Views
6 Replies
Replies (6)
Message 2 of 7

mischa_spelt
Advisor
Advisor
Hmm, it actually seems to work if I don't put the Python module in a subfolder of the model directory. Did I misread the docs that this should be possible?
0 Likes
Message 3 of 7

anthony_johnsonT83CM
Autodesk
Autodesk
Accepted solution

This is similar to this stack overflow question. First, you need to use dots instead of slashes as your path separators. So your module specifier should be "FlexSimPy.FlexSimPy" instead of "FlexSimPy/FlexSimPy". Second, in the folder where you put your FlexSimPy.py file, you need to have a blank file called __init__.py. Placing that file there signifies to python that this is a 'submodule' folder, so it can find the appropriate files.

I'll update the documentation to reflect this.

Message 4 of 7

mischa_spelt
Advisor
Advisor
Thanks, dot-instead-of-slash did the trick!

The __init__.py does not seem to be strictly necessary but I'll include it anyway as part of a proper Python module.

0 Likes
Message 5 of 7

guillem_f
Not applicable

I've followed all your steps, but I'm still having trouble binding my python function. I am getting the exact same error as Mischa, and neither the dots for subfolders nor the __init__.py seem to solve it.

The only thing I can't find is the "made sure that python.exe is on my path" part. May this be causing errors? Otherwise, am I missing something else, like downloading the github files?

Thanks in advance!


pymod.py

python-test.fsm


0 Likes
Message 6 of 7

jason_lightfootVL7B4
Autodesk
Autodesk
Isn't the path reference just to your systems environment variables?
Message 7 of 7

mischa_spelt
Advisor
Advisor

I am not sure if this was needed, but I did it anyway. To test if you have it on your PATH, open a Windows Command Prompt from the start menu and type python. If it says "'python' is not recognized as an internal or external command" you can add it to the path. I pulled Python from my Visual Studio installation and had to add the following folder to the path: "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64".