Add image to GUI [SOLVED]

On 06/01/2015 at 07:52, xxxxxxxx wrote:

Two questions regarding image in the UI.

1. Do I need to register every image I use in UI (I mean get it's IDs from pluginCafe)? As I am using only one image, can I use Plugins ID?
2. I am using this method of adding image. However, I feel like I am doing it wrong, as it looks like a button (gets highlighted once mouse over). What would be the correct way of adding image (not button) to the UI?

BITMAP_ID = 100001;   
buttonimage = c4d.bitmaps.BaseBitmap()                         #We need an instance of BaseBitmap class to use an image   
buttonimage.InitWith(os.path.join(dir, "res", "logo.png"))     #The location where the button image exists   
gui.RegisterIcon(BITMAP_ID, buttonimage)   
bc = c4d.BaseContainer()   
self.AddCustomGui(200, c4d.CUSTOMGUI_BITMAPBUTTON, "Image", c4d.BFH_CENTER | c4d.BFV_CENTER, 100, 100, bc)   

Million thanks.

On 06/01/2015 at 10:54, xxxxxxxx wrote:

I prefer using image files stored in my plugin's folders instead of registering them.
Mainly because there's a finite number of available registration numbers. And it feels like I'm wasting them if I use them for images.

    dir, file = os.path.split(__file__)           #Gets the plugin's directory   
  resFolder = os.path.join(dir, "res")          #Adds the res folder to the path  
  image = os.path.join(resFolder,'myimage.jpg') #The specific image in the res folder to use  
  bmp.InitWith(image)                           #Apply it to the button


On 06/01/2015 at 12:03, xxxxxxxx wrote:

Yes, that's a really good point Scott. In this case there's no need to register it? Right?

gui.RegisterIcon(BITMAP_ID, buttonimage)   

However, what about adding that image to GUI? Any thoughts on that?

On 06/01/2015 at 12:53, xxxxxxxx wrote:

There might be a case where registering the image is the only way.
But for things like bitmap buttons. You should be able to store them in your plugin folders and use file paths.

This example is not a plugin. It's a script  that points to an image on your desktop.
But it's the same principle as using your res folder (or any folder you want)  to store images in plugins:

import c4d,os  
from c4d import gui,bitmaps  
class MyDialog(c4d.gui.GeDialog) :  
  def CreateLayout(self) :  
      self.SetTitle("My Python Dialog")  
      self.GroupBegin(0, c4d.BFH_SCALEFIT|c4d.BFH_SCALEFIT, 1, 1, "Bitmap Example",0)  
      bc = c4d.BaseContainer()                            #Create a new container to store the button image   
      fn = #Gets the desktop path  
      path = os.path.join(fn,'myimage.jpg')               #The path to the image    
      bc.SetFilename(MY_BITMAP_BUTTON, path)              #Add this location info to the conatiner  
      self.myBitButton=self.AddCustomGui(MY_BITMAP_BUTTON, c4d.CUSTOMGUI_BITMAPBUTTON, "Bitmap Button", c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, 30, 30, bc)  
      self.myBitButton.SetImage(path, False)             #Add the image to the button   
      return True  
  #Do something when the button is pressed  
  def Command(self, id, msg=None) :  
      if id==MY_BITMAP_BUTTON:  
         print "Hello"    
      return True   
if __name__=='__main__':  
  dlg = MyDialog()  
  dlg.Open(c4d.DLG_TYPE_ASYNC, defaultw=100, defaulth=100)


On 06/01/2015 at 13:05, xxxxxxxx wrote:

this is how i did it with my buffer booster plugin:

class BANNER(c4d.gui.GeUserArea) :
    def __init__(self) :
        self.bmp = c4d.bitmaps.BaseBitmap()
    def GetMinSize(self) :
        self.width = 300 ####WIDTH OF YOUR IMAGE FILE
        self.height = 85 ####HEIGHT OF YOUR IMAGE FILE
        return (self.width, self.height)
    def DrawMsg(self, x1, y1, x2, y2, msg) :
        path = os.path.join(os.path.dirname(__file__), "res", "logo.png")
        result, ismovie = self.bmp.InitWith(path)
        x1 = 0
        y1 = 0
        x2 = self.bmp.GetBw()
        y2 = self.bmp.GetBh()
        if result == c4d.IMAGERESULT_OK:
            self.DrawBitmap(self.bmp, 0, 0, self.bmp.GetBw(), self.bmp.GetBh(), x1, y1, x2, y2, c4d.BMP_NORMALSCALED | c4d.BMP_ALLOWALPHA)
class bufferBoosterModal(gui.GeDialog) :
    def CreateLayout(self) :
        #logo group
        self.GroupBegin(0, c4d.BFH_CENTER | c4d.BFV_CENTER, cols=1)
        self.GroupBorderSpace(0, 0, 0, 20)
        self.AddUserArea(1, c4d.BFV_CENTER | c4d.BFV_CENTER)    
        self.AttachUserArea(self.LOGO, 1)
        # end logo group

On 07/01/2015 at 06:33, xxxxxxxx wrote:

as there is no GUI element with the sole purpose to display images, we'd also recommend to go the GeUserArea road. Thanks charlesR, for already posting a nice example.

I'd like to add two thoughts on the initial questions:

  1. If you want to register you images as icons, then, yes, all IDs need to be unique IDs to be retrieved from PluginCafe. A plugin ID used for something else may not be reused for an icon.

  2. The button behavior of a BITMAPBUTTON can be disabled. Like so (assuming the code from the first post) :

bc.SetBool(c4d.BITMAPBUTTON_BUTTON, False)

Note: This does not disable the "mouse over" highlighting. This could be worked around by having an image without transparency and button size matching the image size (the effect will still be there, but should not be visible).

On 07/01/2015 at 07:14, xxxxxxxx wrote:

Yes indeed, solution provided by charlesR is really useful. Thank you Charles.

Andreas, yes, I had image with transparency, that's why I was getting "mouseOver" effect. Disabling BITMAPBUTTON did not work in that case.

And thanks for clarifying the registration of image icons.

Thanks a bunch for all of you.

On 07/01/2015 at 07:18, xxxxxxxx wrote:

maybe I didn't put this clearly enough.
By setting BITMAPBUTTON_BUTTON to false you can disable the button behavior, not the mouse over. So when it is set to false, clicking the image will no longer have any effect.

On 07/01/2015 at 14:25, xxxxxxxx wrote:

No no, you are right. I understood you from the first try:) That's my English is getting my my way of communication:)

Anyways, All is working now, thanks to Charles.

On 07/01/2015 at 15:05, xxxxxxxx wrote:

YAY I helped! YAY! Glad i could help out Tomas.