Lock / unlock plugin from Plugins menu



  • Hi,
    Is it possible to lock a plugin in the Plugins menu using python. like image below:
    lock_plugin_gui.jpg
    I want to lock some of commands of my plugin depending on the selected object
    Thanks.



  • Override CommandData.GetState(doc) or ToolData.GetState(doc) to return a combination of
    CMD_ENABLED (enabled)
    CMD_VALUE (checked)
    or neither.



  • @Cairyn
    Thank you Cairyn,
    I had arrived at the result below :

    class MyCommand(c4d.plugins.CommandData):
        def __init__ (self):
            self.isActive = False
    
        def Execute(self, doc):
            ...
            return True
    
        def GetState(self, doc):
                if (self.isActive):
                    return c4d.CMD_ENABLED
                else:
                    return c4d.CMD_VALUE
    

    Now I'm searching , how to swap from disabled to enabled depending on the type of the selected object in Object Manager.



  • @mfersaoui
    I found this solution:

    def GetState(self, doc):
            op = doc.GetActiveObject()
            if op is not None and op.GetType() == OBJECT_ID:
                return c4d.CMD_ENABLED
            else :
                return False
                
    


  • Hi @mfersaoui while True, False is supported it's better to return the correct value from the symbols (CMD_ENABLE) since in any case your bool will be converted to an Int and in the future value may change while symbols will remain the same.

    Moreover in R20 GetState is also available for script in the Script Manager.

    Cheers,
    Maxime.



  • @m_adam
    Thank you Maxime, I had used the (return False) because this give a different result visual of the icon.
    preview:
    GetState_result.jpg
    Result (1) gived when use : return c4d.CMD_VALUE
    Result (2) gived when use : return False



  • @mfersaoui You misunderstand the return value.

    This is not a single true/false flag, but a set of flags. As I mentioned, CMD_ENABLED stands for the enabling of the command (greyed out if not set), and CMD_VALUE stands for the checked state (in case of icons, the background changes, as you saw in your test). These two flags are not mutually exclusive but can be used together! (use or to join).

    The actual value to use if you want neither flag set is simply 0. False works in this case because the numeric equivalent of false is 0.

    import c4d
    from c4d import gui
    
    def state():
        op = doc.GetActiveObject()
        if op is not None:
            if op.GetType() == c4d.Ocube :
                return c4d.CMD_ENABLED | c4d.CMD_VALUE
            elif op.GetType() == c4d.Ocylinder :
                return c4d.CMD_VALUE
            else :
                return c4d.CMD_ENABLED
        return 0
    
    # Main function
    def main():
        print "Triggered!"
    
    # Execute main()
    if __name__=='__main__':
        main()
    

    If you watch the above script as icon in a toolbar, you will see all four state combinations depending on what primitive you select. If nothing is selected, the icon is grayed out. If you select a cylinder, the icon is still grayed out and unselectable but the background changes to make clear it's checked. A cube will show both the checked and active states, and some other object will show only the active state.

    To be fair, the standard default script in R20 mentions False explicitly though.



  • @Cairyn Thank you for your detailed reply.


Log in to reply