Passing a variable to the python script



  • I use the example from the Python Manual, to start a python script from C++.
    How can I pass additional variable to the python script.
    For example, a path?



  • Hi,

    I am not quite sure if do understand your question correctly. I assume with Python manual you are referring to this page. The example actually shows you how to expose two variable attributes (doc, op) and one constant attribute (__name__ ) in the dictionary of the executing module. So you could just copy the part for __name__ for your task of exposing a file name.

    If you were not referring to exposing an attribute in the dictionary of the module with "passing a variable to the script", you would have to elaborate what you exactly mean by that (at least for me).

    Cheers and stay safe,
    zipit



  • Yes, I am referring to that page.
    I saw that statement, but I assumed it was the main definition for the python plugin.

      // set __name__ = __main__
      scope.Add("__name__"_s, maxon::Data("__main__"_s)) iferr_return;
    

    If I understand you correctly, I can add different parameters with different values.
    For example:

      // set __param1__ = "c:/temp"
      scope.Add("param1"_s, maxon::Data("c:/temp"_s)) iferr_return;
      scope.Add("param2"_s, maxon::Data(220)) iferr_return;


  • And yes, it works.
    Thanks.



  • Hi,

    yeah like that. __name__ is just another module bound constant attribute. It is a dunder attribute, signaling a special importance, indicated by its double underscores, but that is only a convention and not a functionality. You can set __name__ to anything you like, just like you could set __author__ to your favourite ice cream brand. The convention dictates that __name__ tells the script in which context is being executed, where __main__ should signal that the module is executed directly. You could set it to my_cpp for example, so that your scripts could react differently to being executed from your C++ code.

    Cheers,
    zipit


Log in to reply