Creating Cube Using Python



  • On 07/03/2018 at 17:37, xxxxxxxx wrote:

    Hi,

    I'm trying to create a simple gui to create a cube. Currently, it looks like this
    https://www.dropbox.com/s/upcc1wykyall32a/c4d057_createCube02.jpg?dl=0

    Based on the image above, my current problem is adding the labels in the side. And adding a cm to the number boxes.

    Is there a way I can do that?

    I tried using the .AddStaticText but it breaks the code and removes all the input boxes in the GUI.

    Here is the working script:

    import c4d
    from c4d import gui
    #Welcome to the world of Python
     
     
    nameBox = 1001
    sliderBox = 1002
    numberBox = 1003
    Create = 1004
     
    class CreateCube(gui.GeDialog) :
         
        def CreateLayout(self) :
            self.SetTitle('Create Cube')
            self.AddEditText(nameBox, c4d.BFH_SCALEFIT)
            self.AddEditSlider(sliderBox, c4d.BFH_SCALEFIT)
            self.AddEditNumber(numberBox, c4d.BFH_SCALEFIT)
            self.AddButton( Create, c4d.BFH_SCALEFIT , name='Create Cube')
             
            self.GroupEnd()
            return True
             
        def Command (self, id, msg) :
            if id==Create:
                cube  = c4d.BaseObject(c4d.Ocube)
                text_value = self.GetString(nameBox)
                slider_value = self.GetInt32(sliderBox)
                number_value = self.GetInt32(numberBox)
                
                cube[c4d.ID_BASELIST_NAME]=text_value
                cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_X]=slider_value
                cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_Y]=slider_value
                cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_Z]=slider_value
                cube[c4d.PRIM_CUBE_SUBX]=number_value
                cube[c4d.PRIM_CUBE_SUBY]=number_value
                cube[c4d.PRIM_CUBE_SUBZ]=number_value
                
                self.cube = cube
                self.Close()
                 
            return True
                     
    def main() :
        dialog = CreateCube()
        dialog.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, defaultw=200,defaulth=200)
        
        cube = dialog.cube
        doc.InsertObject(cube)
        c4d.EventAdd()
         
    if __name__=='__main__':
        main()
    

    Thank you for your time.



  • On 08/03/2018 at 02:46, xxxxxxxx wrote:

    Hey Ben,

    You can use AddStaticText like this below, also and example of setting floats as types:

      
    import c4d
    from c4d import gui 
     
    nameBox       = 1001
    sliderBox     = 1002
    numberBox     = 1003
    Create        = 1004
      
    name_TextID   = 1005
    slider_TextID = 1006
    number_TextID = 1007
      
    class CreateCube(gui.GeDialog) :
        
        
        def CreateLayout(self) :
            self.SetTitle('Create Cube')
      
            self.AddStaticText(name_TextID  , c4d.BFH_LEFT     ,initw=200, name="Input cube name : ", borderstyle=c4d.BORDER_NONE)
            self.AddEditText  (nameBox      , c4d.BFH_SCALEFIT)
            self.AddStaticText(slider_TextID, c4d.BFH_LEFT     ,initw=200, name="Cube size: ", borderstyle=c4d.BORDER_NONE)
            self.AddEditSlider(sliderBox    , c4d.BFH_SCALEFIT)
            self.AddStaticText(number_TextID, c4d.BFH_LEFT     ,initw=200, name="Cube subdivisions : ", borderstyle=c4d.BORDER_NONE)
            self.AddEditNumber(numberBox    , c4d.BFH_SCALEFIT)
            self.AddButton    (Create       , c4d.BFH_SCALEFIT ,name='Create Cube')
            
            self.cube = None # This is so we can check it's existance later on so we don't get an error if we close the window without pressing the create button
            self.GroupEnd()
            return True
      
        def InitValues(self) :
            
            self.SetString(nameBox,'Enter cube name here')  
            self.SetFloat(sliderBox,200,format=c4d.FORMAT_METER)#Or you can use .SetMeter() instead of SetFloat    
            self.SetInt32(numberBox,1,min=1)
            
            return True
            
        def Command (self, id, msg) :
            if id==Create:
      
                text_value   = self.GetString(nameBox)
                slider_value = self.GetInt32(sliderBox)
                number_value = self.GetInt32(numberBox)
                
                cube  = c4d.BaseObject(c4d.Ocube)
                cube[c4d.ID_BASELIST_NAME]=text_value
                
                cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_X]=slider_value
                cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_Y]=slider_value
                cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_Z]=slider_value
                cube[c4d.PRIM_CUBE_SUBX]=number_value
                cube[c4d.PRIM_CUBE_SUBY]=number_value
                cube[c4d.PRIM_CUBE_SUBZ]=number_value
                
                self.cube = cube # Changing self.cube from None to the cube we just made
                self.Close()
                 
            return True
                     
    def main() :
        dialog = CreateCube()
        dialog.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, defaultw=200,defaulth=200)
        
        if not dialog.cube: return 
        cube = dialog.cube
        doc.InsertObject(cube)
        c4d.EventAdd()
         
    if __name__=='__main__':
        main()
      
    
    

    Cheers,

    Adam



  • On 08/03/2018 at 08:55, xxxxxxxx wrote:

    Hi,

    welcome to the Plugin Café forums 🙂 (or are you just using another account and are actually btbentraje?)

    Anyway, Adam already answered your questions.

    In CreateLayout() you are calling GroupEnd() without actually having begun a group. That's redundant and might (at least) lead to confusing situations, when extending the code later on.

    I'd rather handle both the slider and the edit field completely as float (also using GetFloat() on read) and then round later on. Also Adam accidentally just set the unit for the slider, you will want that for the numberBox as well.

    Links to the docs: SetFloat(), SetMeter()
    And although in our C++ documentation, the GeDialog manual might be an interesting read, too.



  • On 08/03/2018 at 20:01, xxxxxxxx wrote:

    Thanks Adam and Andreas for the response!  
    It works as expected.

    Yes, I am also btbentraje. At that time I forgot this account. Hahaha You can delete the btbentraje account if you want.

    Thank you again.


Log in to reply