How to read values from external python script

Hi there,

I’m trying to read variables from an external python script within a VC-python script.

For example:

In externalscript.py:

X = 1

=====================

In VC Python Script:

import externalscript

print externalscript.X

=====================

This will print me “1”, but if I change the value of X with a python program outside VC, the VC-script will keep print “1” each time I call “print externalscript.X”. If I restart VC and call “print externalscript.X”, it will print me the new value that was assign from the external python.

Also, even if I delete the “import externalscript” row in VC-Script, and then call “print externalscript.X”, it will keep printing the value of X, till the point I restart VC. (I get the feeling here that the import command saves the stuff temporarely somewhere and this is why I keep getting the old values? Even a new import command will not give me the new value of X though?!)

However, if I assign externalscript.X a new value from the VC python script, and call “print externalscript.X”, it will print me the new that I assigned from the VC-script.

Is there a way to read the variable from a VC- script and keep updating the same variable with another python program, and get the new updated value from the VC-script?

It would really me help alot if there is a way to do it this way.

 

 

 

 

 

 

 

Hi autohendrik

Not sure why, but I think the external Python code is cached somewhere.

I am using an external library myself but everytime I make a change and want to test it, I have to reload the scene. Would be awesome to have the possibility to reload the Python context while a simulation is opened. I looked up the .NET API but couldn’t find anything regarding that.

Hi,

Try to use reload:

import externalscript
reload(externalscript)
print externalscript.X

-k

Hi, Thanks for the tip keke!

I do get the new value of X in the externalscript with the"reload()" command, as long as I edit the value of X in the externalscript manually.

When I let the python program outside VC update the value of X, I still get the old value of X in VC-python script. :oops:

maybe I approach this from the wrong angle. Is there another valid way, where I can read in variable values from an external source, with an update time of about 50ms? Maybe go at it by writing to a .txt-file, and read it back with VC-script? Or use sqlite3 if that might work to share variable values between the external python program and VC-python?

Maybe you could use socket communication?

I attach couple of test models which demo TCP and UDP socket communications. In both layouts blue robot is the master. There are two box components (client and server) with python scripts. One box reads blue robot’s joint values and sends them with a socket to the other box component. This reads the values and sets them in the white robot so that it follows the blue robot.

Here you have two scripts communicating inside VC model but the other python script could be external and it should work the same.

-k

SocketTest_TCP.vcmx (588 KB)

SocketTest_UDP.vcmx (558 KB)

1 Like

Hi Keke,

It works like a charm! So mighty thanks for your help!

//

 

Hi,

I implemented a layout in which two robots implement twin motion through udp communication.

However, communication between the two layouts or communication between one VC layout and external python scripts cannot make a communication.

What should I do to achieve it ?

Hi wwn_h,

It is hard to tell what kind of problem you have, send the code you have both in the VC script and the external python script so we can see what might be causing the problem.

//

Hey autohenrik,

 

I am trying to do something quite similar.

Can you share any experience and/or scripts?

I would be forever grateful as I have no relation with the subject whatsoever but I am expected to deliver a demonstrator…

Best

Konstantinos