Enable / Disable button

On 31/12/2016 at 21:05, xxxxxxxx wrote:

Hello there,

How can I disable a button if the selected object not an editable object, the example below do that but not exactly what I want. In the example below if I select anther object without closing the dialog, nothing is happening.

I want to autoupdate the enabling or disabling of the button without closing and reopening the dialog if I select anther object

I hope that is clear.

import c4d, os  
from c4d import gui, documents, plugins  
  
PLUGIN_ID = 1234567  
  
class MyDLG( c4d.gui.GeDialog) :   
  
  def UpdateDlg(self, id) :  
      doc = documents.GetActiveDocument()  
      op = doc.GetActiveObjects(False)[0]  
      **if op.GetType() != 5100 :  
          self.Enable(1000,False)**  
            
  def CreateLayout(self) :          
      self.SetTitle("Texel Bake")          
      self.AddButton(1000, c4d.BFH_SCALEFIT, 0, 13, "Button")     
      return True  
    
  def InitValues(self) :  
      self.UpdateDlg(self.GetBool(1000))  
      return True  
  
class MyPlugin(c4d.plugins.CommandData) :      
  def Execute(self, doc) :      
      global RunDLG  
      RunDLG = MyDLG()  
      RunDLG.Open(dlgtype=c4d.DLG_TYPE_ASYNC, defaultw=0, defaulth=0)    
      c4d.EventAdd()  
      return True  
        
if __name__ == "__main__":   
  help = ""  
  icon = c4d.bitmaps.BaseBitmap()  
  dir, file = os.path.split(__file__)  
  fn = os.path.join(dir, "res", "icon.tif")  
  icon.InitWith(fn)  
  plugins.RegisterCommandPlugin(PLUGIN_ID, "My Plugin", 0, icon, help, MyPlugin())

Thanks.

On 01/01/2017 at 08:10, xxxxxxxx wrote:

Use the timer function of gedialog

import c4d, os
from c4d import gui, documents, plugins
  
PLUGIN_ID = 1234567
  
class MyDLG( c4d.gui.GeDialog) :         
            
    def CreateLayout(self) :
        self.SetTimer(250)    
        self.SetTitle("Texel Bake")        
        self.AddButton(1000, c4d.BFH_SCALEFIT, 0, 13, "Button")   
        return True
    
    def InitValues(self) :
        self.UpdateDlg(self.GetBool(1000))
        return True
    
    def UpdateDlg(self, id) :
        doc = documents.GetActiveDocument()
        op = doc.GetActiveObjects(False)
        if not op:
            self.Enable(1000,False)
            return
        
        if op[0].GetType() != 5100 :
            self.Enable(1000,False)
        else:
            self.Enable(1000,True)
            
    def Timer(self, msg) :
        self.UpdateDlg(self.GetBool(1000))
  
a = MyDLG()
a.Open(c4d.DLG_TYPE_ASYNC)

Moreover another tips never do op = doc.GetActiveObjects(False)[0]
Check if op is not None before 😉

On 02/01/2017 at 03:18, xxxxxxxx wrote:

Hi,

Using a timer works but I think catching EVMSG_CHANGE is a better solution:

class MyDLG( c4d.gui.GeDialog) :
    
    ...
    
    def CoreMessage(self, id, msg) :
        if id==c4d.EVMSG_CHANGE:
            self.UpdateDlg(self.GetBool(1000))
        return True

Also calling c4d.EventAdd() inside the Execute() of the command plugin is unnecessary.

On 02/01/2017 at 07:57, xxxxxxxx wrote:

Hi,
Thank you guys for your replies.