Python: GroupWeightsSave() is wrong



  • On 12/04/2013 at 07:59, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   13 
    Platform:   Windows  ;  Mac  ;  Mac OSX  ; 
    Language(s) :

    ---------
    Hi,

    There seems to be a problem with the Python version of GroupWeightsSave().
    It only allows one parameter(id). But it must have two params. (id, bc) for it to store the weights into the base container. The C++ version works properly.

    There's also a lot of mistakes in the Message() part of the sample code in the docs.
    Both typos and other errors.

    Here is an example of a complete working GeDialog plugin using weights on the gizmos.
    Except for the Message() method. Which does not save the weights data like it should due to the missing parameter bug:

    #This is an example of a GeDialog plugin using the weights option on the gizmos  
    #The weights option lets you change the height of each gizmo by dragging them with the mouse  
    #Note: The Message() method does not work. It does not save the weights properly because the docs are wrong and have errors!  
      
    import c4d,os  
    from c4d import plugins, gui, bitmaps, documents  
      
    Plugin_ID=1000009 # Testing id ONLY!!!!!!!   
      
    #enums  
    EDTEXT_1 = 1001  
    EDTEXT_2 = 1002  
    EDARROWS = 1003  
      
    GRP_MAIN = 2000  
    GRP_SUB1 = 2001  
      
    class MyDialog_Gui(gui.GeDialog) :  
      weights = c4d.BaseContainer()  
      weights_saved = False  
        
      def CreateLayout(self) :    
       
          self.GroupBegin(GRP_MAIN, c4d.BFH_SCALEFIT|c4d.BFV_SCALEFIT, 0, 0, "Main Group",0) #id, flags, columns, rows, grouptext, groupflags  
          self.GroupBorder(c4d.BORDER_BLACK)  
          self.GroupBorderSpace(5, 5, 5, 5) #Left, top, Right, Bottom   
        
          self.GroupBegin(GRP_SUB1, c4d.BFH_SCALEFIT|c4d.BFV_SCALEFIT, 0, 3, "Sub Group", c4d.BFV_GRIDGROUP_ALLOW_WEIGHTS)  
          self.GroupBorder(c4d.BORDER_BLACK)  
          self.AddEditText(EDTEXT_1,c4d.BFV_SCALEFIT,100,15,0) #id, flags, height, width, password     
          self.AddEditText(EDTEXT_2,c4d.BFV_SCALEFIT,100,15,0) #id, flags, height, width, password  
          self.AddEditNumberArrows(EDARROWS, c4d.BFV_SCALEFIT)  
        
          if not self.weights_saved:  
              self.weights.SetLong(c4d.GROUPWEIGHTS_PERCENT_H_CNT,3)       #number of rows - has to be equal to the given layout      
              self.weights.SetReal(c4d.GROUPWEIGHTS_PERCENT_H_VAL+0, 1.5)  #weight for row 1  
              self.weights.SetReal(c4d.GROUPWEIGHTS_PERCENT_H_VAL+1, 3.0)  #weight for row 2   
              self.weights.SetReal(c4d.GROUPWEIGHTS_PERCENT_H_VAL+2, 1.5)  #weight for row 3   
              self.weights_saved = True      
          self.GroupEnd()  #end of sub group      
      
          self.GroupWeightsLoad(GRP_SUB1, self.weights)  
      
          self.GroupEnd()  #end of main group    
          return True   
      
       
      def InitValues(self) :   
          self.SetReal(EDARROWS, 0, 0,100,1) #Sets the gizmo's value to 0... With a range of 0-100... Step=1  
          return True  
      
        
      def Command(self, id, msg) :  
          doc = documents.GetActiveDocument()   
          op = doc.GetActiveObject()   
          if id == EDARROWS:  
              print self.GetReal(EDARROWS)   #prints the value of the gizmo  
      
          c4d.EventAdd()    
          return True  
      
      
      def Message(self, msg, result) :  
        
          #This code will save the changed weights when the user changes the gizmo's elastic borders by hand  
          #This prevents the weights from resetting back to defaults when the GeDialog is closed  
          #The weights will only re-set when C4D is closed  
          #But the example code in the docs have many errors in it!  
          #Even when the errors are fixed..It still doesn't save the weights!          
        
          if msg.GetId()==c4d.BFM_WEIGHTS_CHANGED:         #<--Docs are wrong!  
              if msg.GetLong(c4d.BFM_WEIGHTS_CHANGED)==GRP_SUB1:  
                      self.GroupWeightsSave(GRP_SUB1)      #<--Docs are wrong!  
          return gui.GeDialog.Message(self, msg, result)   #<--Docs are wrong!  
            
        
    #---------------------------------------------------------------  
    #   MyDialog_Main --- Where the plugin stuff happens--Don't edit  
    #---------------------------------------------------------------  
    class myDialog_Main(plugins.CommandData) :  
      dialog = None  
        
      def Execute(self, doc) :  
        #create the dialog  
          if self.dialog is None:  
              self.dialog = MyDialog_Gui()  
          return self.dialog.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=Plugin_ID, defaultw=150, defaulth=250, xpos=-1, ypos=-1)  
            
      def RestoreLayout(self, sec_ref) :  
          #manage nonmodal dialog  
          if self.dialog is None:  
             self.dialog = MyDialog_Gui()  
          return self.dialog.Restore(pluginid=Plugin_ID, secret=sec_ref)  
       
      
    if __name__ == "__main__":  
     path, fn = os.path.split(__file__)  
     bmp = bitmaps.BaseBitmap()  
     bmp.InitWith(os.path.join(path, "res/icons/", "None"))  
     plugins.RegisterCommandPlugin(Plugin_ID, "Weights Example",0,None,"", myDialog_Main())
    

    -ScottA



  • On 13/04/2013 at 00:58, xxxxxxxx wrote:

    Hi Scott,

    I can confirm the example in the documentation is wrong. But the correct usage is easily derivable
    from the rest of the documentation. GroupWeightsSave() returns a BaseContainer instead of
    accepting one as parameter.

        def InitValues(self) :
            if self.weights:
                self.GroupWeightsLoad(GRP_SUB1, self.weights)
            return True
      
        def Message(self, msg, result) :
            if msg.GetId() = =c4d.BFM_WEIGHTS_CHANGED:
                if msg.GetLong(c4d.BFM_WEIGHTS_CHANGED)==GRP_SUB1:
                        self.weights = self.GroupWeightsSave(GRP_SUB1)
            return gui.GeDialog.Message(self, msg, result)
    

    -Niklas



  • On 13/04/2013 at 07:50, xxxxxxxx wrote:

    Thanks Nik.

    But this is radically different from what the docs say and show.
    They need to be changed.

    -ScottA



  • On 13/04/2013 at 07:54, xxxxxxxx wrote:

    Hi Scott,

    yes it is, I agree. But it is not always end of the world, because at least the function parameters
    and the return-value allowed conclude this solution.

    Best,
    -Niklas



  • On 13/04/2013 at 11:18, xxxxxxxx wrote:

    I respectfully disagree.
    It is a bug in the documentation. Hence....It is very serious.

    -ScottA


Log in to reply