Code: Select all
class MyCommand:
def __init__(self):
self.flag_something = True
Code: Select all
FreeCADGui.addCommand('My_Command', MyCommand())
Thanks again in advance. m.
Code: Select all
class MyCommand:
def __init__(self):
self.flag_something = True
Code: Select all
FreeCADGui.addCommand('My_Command', MyCommand())
What do you want to do?
Code: Select all
FreeCADGui.runCommand('My_Command')
I really have to wrap my head arround scopes once again and refresh this topic ... I was mislead by myself.That variable only exists when you actually run that command.
I might be completly wrong, but shouldn't there by just one instance of that command?Or do you mean you want to have a persistent "global" variable for all instances?
OK, will do. Thanks. m.See how the commands of the Draft Workbench are defined,
Sorry, forgot to answer. Tried to have a variable that "belongs" to that command for status information. This should be "globaly" available. But I see my lack of understanding. Will have to do some reading.What do you want to do?
This is a very simple example how you can do it:
Code: Select all
import __main__
class MyCommand:
def __init__(self):
print ("Create command")
self.flag_something = True
__main__.link_from_command = self
def __del__(self):
print ("Delete command")
def Activated(self):
self.flag_something = False
print ("Activate command")
def GetResources(self):
return {}
FreeCADGui.addCommand('My_Command', MyCommand())
__main__.link_from_command.flag_something
FreeCADGui.runCommand('My_Command')
__main__.link_from_command.flag_something
Yes, FreeCAD internally has a std::map where a key is unique. If you repeat FreeCADGui.addCommand('My_Command', MyCommand()) a few times only the last added command is used.I might be completly wrong, but shouldn't there by just one instance of that command?
I don't know the internals of FreeCAD to be able to answer that exactly, but my empirical experience tells me every time you run a command, you essentially instantiate a new command "object", just like you would call the class in code.
Code: Select all
one = MyCommand() # it will call __init__ and then Activate
two = MyCommand() # again
three = MyCommand() # again
The common way to solve this is by using parameters. See the Parameter editor. Essentially, you define a parameter which can be a string, an integer, a float, a boolean, etc., and you can read and write to it....Tried to have a variable that "belongs" to that command for status information. This should be "globaly" available. But I see my lack of understanding. Will have to do some reading.
Code: Select all
class MyCommand:
def Activate(self):
param_group = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Something")
value = param_group.GetInt("Line_thickness", 4) # Default is 4 if the parameter does not exist, that is, on first run
# ... do something
param_group.SetInt("Line_thickness", 3) # Writes the parameter value, it creates the parameter on first run
Great, got it and learned something. This is quite hackish I think, but it was only intended for a quick fix anyway.This is a very simple example how you can do it:
The module __main__ keeps a dict of the global variables.
No, FreeCAD keeps a reference to the Python command object passed with FreeCADGui.addCommand. Internally a C++ wrapper is instantiated that is derived from the C++ class Gui::Command and this wrapper invokes the appropriate functions of the Python object.
Instead of registering a command with
Code: Select all
FreeCADGui.addCommand('My_Command', MyCommand())
Code: Select all
link_to_command = MyCommand()
FreeCADGui.addCommand('My_Command', link_to_command)
Yes, but does the wrapper call __init__ again, or only Activated? I feel it still calls the entire constructor when you run the command.
No, it doesn't call __init__, why should it? Here is the implementation of PythonCommand that acts as a wrapper of the Python command object:
OK, thanks. But it is good to see it can be done with the __main__-module, as well. Doesn't seem to be very prevalent knowledge.wmayer wrote: ↑Mon May 25, 2020 8:48 pm you can do it this wayThis is the cleaner and more direct way.Code: Select all
link_to_command = MyCommand() FreeCADGui.addCommand('My_Command', link_to_command)
OK, good to know.vocx wrote: ↑Mon May 25, 2020 5:04 pm The common way to solve this is by using parameters. See the Parameter editor. Essentially, you define a parameter which can be a string, an integer, a float, a boolean, etc., and you can read and write to it.