Bitmap in UserArea



  • On 12/05/2013 at 02:21, xxxxxxxx wrote:

    Override the DrawMsg() method inside the GeUserArea. Do it there.

    Best,
    -Niklas



  • On 12/05/2013 at 03:10, xxxxxxxx wrote:

    I use this option yesterday without any results. I think that not load this class in the plugin code.

    This is the code of my class:

    class Area(c4d.gui.GeUserArea) :
        
        def DrawMsg(self, x1, y1, x2, y2, msg) : 
            path=c4d.storage.GeGetC4DPath(c4d.C4D_PATH_LIBRARY)
            archi= path + "\Joyeria\100.png"
            
            bmp=c4d.bitmaps.BaseBitmap()
            bmp.InitWith(path)        
            self.DrawBitmap(bmp,0,0,300,160,0,0,50,24,c4d.BMP_NORMAL)
            self.Redraw()

    In the dialog class:
     def __init__(self, area) :
            self.area = area

    In the CreateLayout, use AddUserArea y AttachUserArea for create the fields.

    In the CommadData class:

    class MyMenuPlugin(c4d.plugins.CommandData) :
        area = Area()
        dialog = None
        def Execute(self, doc) :
        # create the dialog
           if self.dialog is None:
              self.dialog = MiDialogo(self.area)
           return self.dialog.Open(dlgtype=c4d.DLG_TYPE_MODAL, pluginid=PLUGIN_ID,defaultw=200, defaulth=150, xpos=-1, ypos=-1)

    def RestoreLayout(self, sec_ref) :
        # manage the dialog
            if self.dialog is None:
                self.dialog = MiDialogo(self.area)
            return self.dialog.Restore(pluginid=PLUGIN_ID, secret=sec_ref)

    Only show this parts of the entire code becuase it have more than 300 lines.

    Thanks.



  • On 12/05/2013 at 03:13, xxxxxxxx wrote:

    Why do you call Redraw() in DrawMsg()? This will call DrawMsg() again. Also, load the bitmap once and
    draw it in DrawMsg(). The way you're doing it looks fine, try again with these changes.

    Best,
    -Niklas



  • On 12/05/2013 at 03:21, xxxxxxxx wrote:

    Maybe not load the bitmap becuse the coords of the UserArea and the DrawBitmap not the same?



  • On 12/05/2013 at 03:30, xxxxxxxx wrote:

    I resolve the problem. The AttachUserArea don´t have the AddUserArea ID.

    Now i have to scale the bitmap in the UserArea.

    Thanks for all.



  • On 12/05/2013 at 03:38, xxxxxxxx wrote:

    .. and hopefully load the bitmap once only instead of everytime it needs to be drawn! :)

    You're welcome.

    Best,
    -Niklas



  • On 12/05/2013 at 04:37, xxxxxxxx wrote:

    With this code:
     
         path=c4d.storage.GeGetC4DPath(c4d.C4D_PATH_LIBRARY)
         archi= path + "\Joyeria\100.png"
         bmp=c4d.bitmaps.BaseBitmap()
         bmp.InitWith(archi)
     
    The result path is: C:\PROGRAM FILES\MAXON\CINEMA 4D R14\library\Joyeria.bmp
     
    Why?



  • On 12/05/2013 at 05:29, xxxxxxxx wrote:

    You can't just use backslashes, since those are specific to the Windows OS. Try it like this:

      
    archi = path + "Joyeria" + "100.png"   
    

    That may seem strange at first glance, but since every separate string is auto-cast to Filename, it does make sense.



  • On 12/05/2013 at 05:46, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    You can't just use backslashes, since those are specific to the Windows OS. Try it like this:

     
    archi = path + "Joyeria" + "100.png"   
    

    That may seem strange at first glance, but since every separate string is auto-cast to Filename, it does make sense.

    as he is under windows he can, but there is the danger to trigger a control character if he uses
    the strings the way he does. the way jack posted should not work imho.

    archi= path + "\Joyeria\100.png" should be written in python as:

    archi= path + r"\Joyeria\100.png" or
    archi= path + "\\Joyeria\\100.png" or the best way
    archi= os.path.join(path,"\Joyeria\100.png")

    result = 'SomePath' + '\100' would become 'SomePath@' as \100 is the @ character.



  • On 12/05/2013 at 07:33, xxxxxxxx wrote:

    Thanks littledevil, problem solved.



  • On 12/05/2013 at 07:55, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    You can't just use backslashes, since those are specific to the Windows OS. Try it like this:

       
    archi = path + "Joyeria" + "100.png"   
    

    That may seem strange at first glance, but since every separate string is auto-cast to Filename, it does make sense.

    We're in Python here, there's no Filename class like in C++. :)

    archi = os.path.join(path, 'Joyeria', '100png')
    

    Best,
    -Niklas



  • On 12/05/2013 at 11:13, xxxxxxxx wrote:

    I'm having an issue related to using images in the UA.
    Rather than post a brand new thread. I think it would fit in here.

    I can load an image file into my UA just fine.
    But when I try to move the image when I press a button in the GeDialog class. The UA doesn't update properly.
    The image does move. But it also leaves behind a ghost of where it was originally. And also one of the GeDialog buttons also pops up in the corner of the UA!!???

    In other words. The UA freaks out. 😂
    How do we make the UA update properly if we move the image?
    Here is the entire plugin code:

    import  c4d,os  
    from c4d import gui, plugins  
      
    class MyUA(c4d.gui.GeUserArea) :  
      
      bmp = None  
      xValue = 0               #Where to position the image in the UA  
      yValue = 0      
      
      def __init__(self) :      
          dir, file = os.path.split(__file__)   #Gets the plugin's directory   
          path = os.path.join(dir, "res")       #Adds the res folder to the directory to target the res folder      
          image = path + "\myimage.png"         #Adds the image to the path  
            
          self.bmp = c4d.bitmaps.BaseBitmap()   #Create an instance of the BaseBitmap class  
          self.bmp.InitWith(image)              #Initialize it with the image      
      
      def DrawMsg(self, x1, y1, x2, y2, msg) :          
          self.OffScreenOn()                    #Helps to stabilize the bitmap drawing in the UA  
          w = self.bmp.GetBw()  
          h = self.bmp.GetBh()  
          self.DrawBitmap(self.bmp, self.xValue, self.yValue, w, h, 0, 0, w, h, c4d.BMP_ALLOWALPHA)  
          self.DrawBorder(c4d.BORDER_OUT, x1, y1, x2, y2)  
      
      def InputEvent(self, msg) :  
          return True  
      
    class MyDialog(c4d.gui.GeDialog) :  
      ua = MyUA()                #Create a local class instance of the UserArea  
      
      def CreateLayout(self) :  
          self.AddUserArea(2000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT)  
          self.AttachUserArea(self.ua, 2000)  
            
          self.AddButton(1000, c4d.BFH_CENTER, 80, 15, "Click Me")  
          self.AddButton(1001, c4d.BFH_CENTER, 80, 15, "Close")  
          return True  
            
      
      #This method initializes things when the plugin starts           
      def InitValues(self) :    
          return True  
      
      def Command(self, id, msg) :  
          if id==1000:  
              self.ua.xValue = 100       #Moves the Bitmap image to another location  
              self.ua.yValue = 100  
              self.ua.LayoutChanged()    #<---Trying to solve UA image display problems...Not Working!!  
              self.ua.Redraw()  
                
          elif id==1001:   
              self.Close()              
          return True          
            
      
            
    ###### This class is only used to register the plugin...Don't change!!!#######  
    class BitmapUaExample(c4d.plugins.CommandData) :  
      
      dlg = None  
      ua = None  
      PLUGIN_ID   = 10000090 # TESTING ID ONLY!!!  
      ICON = None  
      
      def Execute(self, doc) :          
          if not self.dlg: self.dlg = MyDialog()  
          self.dlg.Open(c4d.DLG_TYPE_ASYNC, self.PLUGIN_ID)  
          return True  
      
      def RestoreLayout(self, subid) :  
          if not self.dlg:  
              self.dlg  = MyDialog()  
          return self.dlg.Restore(self.PLUGIN_ID, subid)  
        
      
      @classmethod  
      def Register(myPlugin) :  
          data = {  
              "id":     myPlugin.PLUGIN_ID,  
              "icon":   myPlugin.ICON,  
              "str":    "Bitmap UA Example",  
              "help":   "Some text that shows up at the bottom of the C4D UI",  
              "info":   c4d.PLUGINFLAG_COMMAND_HOTKEY,  
              "dat":    myPlugin(),  
          }  
          c4d.plugins.RegisterCommandPlugin(**data)  
      
    if __name__ == "__main__":  
      BitmapUaExample.Register()
    

    -ScottA



  • On 12/05/2013 at 11:38, xxxxxxxx wrote:

    Hi Scott,

    just draw the background color onto the rectangle. ;-)

    * before drawing the bitmap of course

    self.DrawSetPen(c4d.COLOR_BG)
    self.DrawRectangle(x1, y1, x2, y1)
    

    Best,
    Niklas



  • On 12/05/2013 at 12:00, xxxxxxxx wrote:

    Still freaking out on me.

        def DrawMsg(self, x1, y1, x2, y2, msg) :          
          self.OffScreenOn()                    #Helps to stabilize the bitmap drawing in the UA  
          w = self.bmp.GetBw()  
          h = self.bmp.GetBh()  
            
          self.DrawSetPen(c4d.COLOR_BG)        #Doesn't do anything :-(  
          self.DrawRectangle(x1, y1, x2, y1)  
            
          self.DrawBitmap(self.bmp, self.xValue, self.yValue, w, h, 0, 0, w, h, c4d.BMP_ALLOWALPHA)  
          self.DrawBorder(c4d.BORDER_OUT, x1, y1, x2, y2)
    

    -ScottA



  • On 12/05/2013 at 12:49, xxxxxxxx wrote:

    Works fine here. Resize the dialog and the x position of the image will change.

    import c4d
    from c4d.gui import *
    from c4d.bitmaps import *
      
    class Area(GeUserArea) :
        def __init__(self) :
            super(Area, self).__init__()
            self.bmp = BaseBitmap()
            self.bmp.InitWith('C:/Users/niklas/Desktop/01.jpg')
            self.x = 0
            self.y = 0
            self.d = 1
      
        def DrawMsg(self, x1, y1, x2, y2, msg) :
            if self.x > 100:
                self.d = -1
            elif self.x <= 0:
                self.d = 1
            self.x += self.d
      
            self.DrawSetPen(c4d.COLOR_BG)
            self.DrawRectangle(x1, y1, x2, y2)
            w, h = self.bmp.GetSize()
            self.DrawBitmap(self.bmp, x1 + self.x, y1 + self.y,
                    w, h, 0, 0, w, h, c4d.BMP_ALLOWALPHA)
      
    class Dlg(GeDialog) :
        def CreateLayout(self) :
            self.ua = Area()
            self.AddUserArea(1000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT)
            self.AttachUserArea(self.ua, 1000)
            return True
      
    dlg = Dlg()
    dlg.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE)
    

    remark: relatively dirty code, please don't use as reference for coding style.

    Best,
    -Niklas



  • On 12/05/2013 at 13:23, xxxxxxxx wrote:

    Thanks Nik,

    Apparently. The UA doesn't work well with class member variables. Or at least not the way I'm using them.
    When I use a class member variable as the target variable for the DrawBitmap() method. The UA freaks out.

    Not sure why this happens. But there's probably a good reason for it.

    -ScottA



  • On 12/05/2013 at 13:26, xxxxxxxx wrote:

    Edit: Wait a sec, checking out the code you have posted. It freaks around here as well.



  • On 12/05/2013 at 13:34, xxxxxxxx wrote:

    Ok so first of all: You should always draw a background color if what you can not assume what
    you are drawing fills the complete rectangle. The buffer is not simply all set to zero (black) but
    is left the way it is. You must fill it with the desired background color yourself.

    Second is: You can actually draw outside of the area because the dialog itself is drawn on the same
    buffer. You can clip the drawing by using SetClippingRegion(x1, y1, x2, y2)

        def DrawMsg(self, x1, y1, x2, y2, msg) :
            self.DrawSetPen(c4d.COLOR_BG)
            self.DrawRectangle(x1, y1, x2, y2)
            self.SetClippingRegion(x1, y1, x2, y2)
            w = self.bmp.GetBw()
            h = self.bmp.GetBh()
            self.DrawBitmap(self.bmp, self.xValue, self.yValue, w, h, 0, 0, w, h, c4d.BMP_ALLOWALPHA)
            self.DrawBorder(c4d.BORDER_OUT, x1, y1, x2, y2)
    

    -Niklas



  • On 12/05/2013 at 14:24, xxxxxxxx wrote:

    No worries. I've got it all worked out now.
    It just didn't like the way I was using the class member variables.

    Here's the working version. Just in case anyone needs it. Or is curious about it.
    When the GeDialog button is clicked. The image moves to a new position properly now.
    I also threw in some mouse position checking code.

    import  c4d,os  
    from c4d import gui, plugins  
      
    class MyUA(c4d.gui.GeUserArea) :  
      
      xValue = 0           #These variables will set the position of the image  
      yValue = 0  
        
      def __init__(self) :      
          dir, file = os.path.split(__file__)   #Gets the plugin's directory   
          path = os.path.join(dir, "res")       #Adds the res folder to the directory to target the res folder      
          image = path + "\myimage.png"         #Adds the image to the path  
            
          self.bmp = c4d.bitmaps.BaseBitmap()   #Create an instance of the BaseBitmap class  
          self.bmp.InitWith(image)              #Initialize it with the image  
      
          self.xValue = 5  
          self.yValue = 5  
      
      def DrawMsg(self, x1, y1, x2, y2, msg) :   
            
          self.DrawSetPen(c4d.COLOR_BG)         #COLOR_BG is the image we load with DrawBitmap  
          self.DrawRectangle(x1, y1, x2, y2)    #Draws a rectangle and fills it with the image bitmap  
          self.SetClippingRegion(x1, y1, x2, y2)  
          w, h = self.bmp.GetSize()             #Gets the size values of the image  
          self.DrawBitmap(self.bmp, x1 + self.xValue, y1 + self.yValue, w, h, 0, 0, w, h, c4d.BMP_NORMAL | c4d.BMP_ALLOWALPHA)  
            
      
            
          self.DrawBorder(c4d.BORDER_ROUND, x1+5, y1+5, x2-5, y2-5)  #Draws a border around the UserArea          
      
      
      def InputEvent(self, msg) :  
        
          action = c4d.BaseContainer(c4d.BFM_ACTION)  
          action.SetLong(c4d.BFM_ACTION_ID, self.GetId())  
      
          while self.GetInputState(c4d.BFM_INPUT_MOUSE, c4d.BFM_INPUT_MOUSELEFT, msg) :    #While the left mouse button is held down  
              if msg.GetLong(c4d.BFM_INPUT_VALUE)==0: break  
        
              if msg[c4d.BFM_INPUT_DEVICE] == c4d.BFM_INPUT_MOUSE:  
                  x, y = msg[c4d.BFM_INPUT_X], msg[c4d.BFM_INPUT_Y]  
                  g2l  = self.Global2Local()  
                  x += g2l['x']  
                  y += g2l['y']  
      
              if msg[c4d.BFM_INPUT_CHANNEL] == c4d.BFM_INPUT_MOUSELEFT:  
                  print x, y  #The mouse's X & Y pos inside the user area  
                    
                  action.SetLong(c4d.BFM_ACTION_INDRAG, True)  
                  self.SendParentMessage(action)                    #Sends a message to the parent dialog that the mouse is dragging in the UA  
                  self.Redraw()                                     #Redraw while the left mouse button is held down  
      
          action.SetLong(c4d.BFM_ACTION_INDRAG, False)  
          self.SendParentMessage(action)                            #Sends a message to the parent dialog that the mouse has stopped dragging in the UA                  
      
          return True  
      
    class MyDialog(c4d.gui.GeDialog) :  
      ua = MyUA()                #Create a local class instance of the UserArea  
      
      def CreateLayout(self) :  
          self.AddUserArea(2000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT)  
          self.AttachUserArea(self.ua, 2000)  
            
          self.AddButton(1000, c4d.BFH_CENTER, 80, 15, "Click Me")  
          self.AddButton(1001, c4d.BFH_CENTER, 80, 15, "Close")  
          return True  
            
      
      #This method initializes things when the plugin starts           
      def InitValues(self) :    
          return True  
      
      def Command(self, id, msg) :  
          if id==1000:  
              self.ua.xValue = 100  
              self.ua.yValue = 100  
              self.ua.Redraw()  
                
          elif id==1001:   
              self.Close()              
          return True          
            
      
            
    ###### This class is only used to register the plugin...Don't change!!!#######  
    class BitmapUaExample(c4d.plugins.CommandData) :  
      
      dlg = None  
      ua = None  
      PLUGIN_ID   = 10000090 # TESTING ID ONLY!!!  
      ICON = None  
      
      def Execute(self, doc) :          
          if not self.dlg: self.dlg = MyDialog()  
          self.dlg.Open(c4d.DLG_TYPE_ASYNC, self.PLUGIN_ID, -1, -1, 400, 300)  
          return True  
      
      def RestoreLayout(self, subid) :  
          if not self.dlg:  
              self.dlg  = MyDialog()  
          return self.dlg.Restore(self.PLUGIN_ID, subid)  
        
      
      @classmethod  
      def Register(myPlugin) :  
          data = {  
              "id":     myPlugin.PLUGIN_ID,  
              "icon":   myPlugin.ICON,  
              "str":    "Bitmap UA Example",  
              "help":   "Some text that shows up at the bottom of the C4D UI",  
              "info":   c4d.PLUGINFLAG_COMMAND_HOTKEY,  
              "dat":    myPlugin(),  
          }  
          c4d.plugins.RegisterCommandPlugin(**data)  
      
    if __name__ == "__main__":  
      BitmapUaExample.Register()
    

    -ScottA


Log in to reply