SOLVED Simple Box Gui Creation

Hi,

I'm trying to create a GUI tool that lets me change the display color at will. Since we are talking about colors, I would like to the "buttons" to be colors rather than just a plain text button "Change to Green". You cans see an example attached in this post.

https://www.dropbox.com/s/j7k385dlx0obnb2/c4d070_UI_colorizer.JPG?dl=0

I tried looking up in the GUI documentation but nothing seems to fit my description.

Is there a way around this? Thank you for looking at my problem.

Hi,

may I ask you to please add tags to your post (see Read Before Posting)? For example I'm not sure, if you are looking for a C++ or Python solution.

In general the BitmapButtonCustomGui (C++, Python) should do the trick for you.

You can see the usage in our C++ examples:
In a parameter description: objectdata_descriptions.cpp
In a GeDialog: gedialog_gadgets.cpp

Cheers,
Andreas

Apologies for the confusion. I'm using a python. Just starting out

Thanks for the reference, but I'm not quite sure how to use it.

Here is what I have so far

import c4d
from c4d import gui 
 
Green        = 100

class Colorizer(gui.GeDialog):
    
    def CreateLayout(self):
        
        self.SetTitle('Colorizer')
        
        self.BitmapButtonCustomGui(BITMAPBUTTON_BACKCOLOR = c4d.Vector(0,1,0))
        #self.AddButton    (Green       , c4d.BFH_CENTER ,name='Green')
                         
def main():
    dialog = Colorizer()
    dialog.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, defaultw=300,defaulth=100)
     
if __name__=='__main__':
    main()

Currently, it returns an error of AttributeError: 'Colorizer' object has no attribute 'BitmapButtonCustomGui'.

Is there a way around this? Thank you

Hi,

thanks for the additional information and setting the tags.

The BitmapButton needs to be added to the dialog via AddCustomGui().
For example like so:

        # Prepare a red bitmap for the button.
        w = 50
        h = 50
        bmpRed = c4d.bitmaps.BaseBitmap()
        bmpRed.Init(w, h)
        for y in xrange(w):
            for x in xrange(h):
                bmpRed.SetPixel(x, y, 255, 0, 0)
        # BitmapButton configuration
        bcBitmapButton = c4d.BaseContainer()
        bcBitmapButton[c4d.BITMAPBUTTON_BUTTON] = True

        # Add a BitmapButton to the dialog.
        # _bitmapButton is a member variable of the dialog class
        buttonId = 2000
        _bitmapButton = self.AddCustomGui(buttonId, c4d.CUSTOMGUI_BITMAPBUTTON, "", c4d.BFH_CENTER|c4d.BFV_CENTER, w, h, bcBitmapButton)
        if _bitmapButton is None:
            print "Handle this error!"
        # Assign the image to the button.
        # Note: Let it create an internal copy as the created bitmap will be free'd, when scope is left.
        _bitmapButton.SetImage(bmpRed, True)

Cheers,
Andreas

Thank you for taking the time to make the code and make comments. Appreciate it alot.
The GUI creation is resolved but can I ask how do I connect the button (Red/Green bitmap) to the command.

I tried modifying and confirming the button ID and the command ID but does not seem to work.
You can see the work here (and sorry again for the trouble)

import c4d
from c4d import gui 
 
mainLayout = 1000
bnRed      = 1001
bnGreen    = 1002
bnBlue     = 1003

class Colorizer(gui.GeDialog):
    
    def CreateLayout(self):
        
        self.SetTitle('Colorizer')
        # Prepare a red bitmap for the button.
        self.GroupBegin   (mainLayout, c4d.BFV_CENTER, cols=2,rows=3)
        w = 25
        h = 25
        bmpRed = c4d.bitmaps.BaseBitmap()
        bmpRed.Init(w, h)
        for y in xrange(w):
            for x in xrange(h):
                bmpRed.SetPixel(x, y, 255, 0, 0)
        # BitmapButton configuration
        bcBitmapButton = c4d.BaseContainer()
        bcBitmapButton[c4d.BITMAPBUTTON_BUTTON] = True

        # Add a BitmapButton to the dialog.
        # _bitmapButton is a member variable of the dialog class
        #buttonId = 2000
        _bitmapButton = self.AddCustomGui(bnRed, c4d.CUSTOMGUI_BITMAPBUTTON, "", c4d.BFH_CENTER|c4d.BFV_CENTER, w, h, bcBitmapButton)
        if _bitmapButton is None:
            print "Handle this error!"
        # Assign the image to the button.
        # Note: Let it create an internal copy as the created bitmap will be free'd, when scope is left.
        _bitmapButton.SetImage(bmpRed, True)

        bmpGreen = c4d.bitmaps.BaseBitmap()
        bmpGreen.Init(w, h)
        for y in xrange(w):
            for x in xrange(h):
                bmpGreen.SetPixel(x, y, 0, 255, 0)
        
        bcBitmapButton = c4d.BaseContainer()
        bcBitmapButton[c4d.BITMAPBUTTON_BUTTON] = True

        _bitmapButton = self.AddCustomGui(bnGreen, c4d.CUSTOMGUI_BITMAPBUTTON, "", c4d.BFH_CENTER|c4d.BFV_CENTER, w, h, bcBitmapButton)
        if _bitmapButton is None:
            print "Handle this error!"
        
        _bitmapButton.SetImage(bmpGreen, True)

        self.GroupEnd()
        return True


    def colorRed (self,id,msg):
        if id == bnRed:
            op[c4d.ID_BASEOBJECT_USECOLOR] = 2
            op[c4d.ID_BASEOBJECT_COLOR] = c4d.Vector (1,0,0)
    
            c4d.EventAdd()
            self.Close()

        return True

    def colorGreen (self,id,msg):
        if id == bnGreen:
            op[c4d.ID_BASEOBJECT_USECOLOR] = 2
            op[c4d.ID_BASEOBJECT_COLOR] = c4d.Vector (1,0,0)
    
            c4d.EventAdd()
            self.Close()

        return True

                         
def main():
    dialog = Colorizer()
    dialog.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, defaultw=300,defaulth=100)
     
if __name__=='__main__':
    main()

Hi,

you have to react to the button in your GeDialog implementation, namely in the Command() function.
See for example these lines in the Py-TextureBaker example.

So picking up my example snippet from above, the Command function could look like so:

    def Command(self, id, msg):
        if id==2000: # corresponds to the button ID used in the code snippet in the first answer in this thread
            print "My Bitmap Button"
        return True

Cheers,
Andreas

Hi Andreas,

Thank you for the response and apologies for dragging this further.

The code you have given works only for "print" command. But when I added my block of commands, it doesn't work on anything.

The code in question is (please see comments)

   def Command (self,id,msg):
        if id == 2000:
          
            print "My Bitmap Button" # this code works
            op[c4d.ID_BASEOBJECT_USECOLOR] = 2 #However, the following two do not.
            op[c4d.ID_BASEOBJECT_COLOR] = c4d.Vector (1,0,0) #They work on a standalone script but on this button
    
        return True

Sorry for the trouble I'm just a newbie on scripting in general. Is there other prerequisite on executing the code?

Hi,

no worries, we'll get you there.

One recommendation: When doing Python development, have the Cinema 4D Console window open (Menu Script -> Console...). There you will be presented with errors.

I think, your problem will be, that you try to access op, probably intending to change parameters of the active object. In Script Manager doc and op are global variables predefined for convenience. In a plugin, you have to get these yourself using GetActiveDocument() and GetActiveObject().

Bye,
Andreas

@a_block

Case solved! Thanks again for your patience.