How to read values from external python script

Forum Forums Visual Components 4.1 Python Programming in VC How to read values from external python script

Tagged: 

This topic contains 7 replies, has 4 voices, and was last updated by Avatar autohenrik 1 week, 2 days ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #16961
    Avatar
    autohenrik
    Participant

    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.

     

     

     

     

     

     

     

    #16979
    Johnny
    Johnny
    Participant

    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.

    #16980
    Keke
    Keke
    Moderator

    Hi,

    Try to use reload:

    -k

    #16988
    Avatar
    autohenrik
    Participant

    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. 😳

    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?

    #16990
    Keke
    Keke
    Moderator

    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

    Attachments:
    You must be logged in to view attached files.
    #17020
    Avatar
    autohenrik
    Participant

    Hi Keke,

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

    //

     

    #17948
    wwn_h
    wwn_h
    Participant

    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 ?

    #18439
    Avatar
    autohenrik
    Participant

    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.

    //

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.