Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    How to set global from python plugin

    Scheduled Pinned Locked Moved
    Plugins for DWC and DSF
    2
    2
    207
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • achrnundefined
      achrn
      last edited by

      What is the 'right ' way to set a global variable from a python routine running on the SBC?

      At the moment I'm using something equivalent to

      from dsf.connections import CommandConnection
      cmd_con = CommandConnection(debug=True)
      cmd_con.connect()
      
      newvalue = 37
      res = cmd_con.perform_simple_code('set global.foobar = ' + newvalue, async_exec=True)
      

      Which works, but I wondered if there's a better way to get to write to the object model directly.

      I note that there is patch_object_model(key: str, patch: str) in the dsf-python source, but it "May be used only in non-SPI mode", and there's set_object_model(property_path: str, value: str) but it is to "Set an atomic property in the object model" and I both don't actually know what it means by that, and can't make it do what I want -

      cmd_con.lock_object_model()
      cmd_con.set_object_model('global.foobar','123')
      cmd_con.unlock_object_model()
      

      for example gives me (inter alia)

      send: {"command":"SetObjectModel","PropertyPath":"global:foobar","Value":"123"}
      recv: {"result":false,"success":true}
      

      which I think means the command successfully sent, but didn't do anything (the variable doesn't get the value).

      I should probably confess that I'm using dsf-python-3.5.0rc2 on a 3.5.1 with bookworm DuetPi, so I might possibly be jumping the gun a bit.

      chrishammundefined 1 Reply Last reply Reply Quote 0
      • chrishammundefined
        chrishamm administrators @achrn
        last edited by

        @achrn Your first approach is fine. Instead of a simple code you can send a regular DSF G-code as well. SetObjectModel is reserved for the DuetPiManagementPlugin and for non-SPI mode (which I haven't tested in a while), so don't use that.

        Duet software engineer

        1 Reply Last reply Reply Quote 1
        • First post
          Last post
        Unless otherwise noted, all forum content is licensed under CC-BY-SA