Python - Button Selection



  • On 02/06/2014 at 02:36, xxxxxxxx wrote:

    I created 4 buttons and want when the 1 button is pressed the other 3 will be 0 or False.

    I use this piece of code:

    import c4d
    from c4d import gui

    def main() :
        
        obj = doc.SearchObject("Null")
                
        if obj[c4d.ID_USERDATA,1] == 1:
                                   
           obj[c4d.ID_USERDATA,1] = 1
           obj[c4d.ID_USERDATA,2] = 0
           obj[c4d.ID_USERDATA,3] = 0
           obj[c4d.ID_USERDATA,4] = 0
                                     
        if obj[c4d.ID_USERDATA,2] == 1:
                                   
           obj[c4d.ID_USERDATA,1] = 0
           obj[c4d.ID_USERDATA,2] = 1
           obj[c4d.ID_USERDATA,3] = 0
           obj[c4d.ID_USERDATA,4] = 0
               
        if obj[c4d.ID_USERDATA,3] == 1:
                                   
           obj[c4d.ID_USERDATA,1] = 0
           obj[c4d.ID_USERDATA,2] = 0
           obj[c4d.ID_USERDATA,3] = 1
           obj[c4d.ID_USERDATA,4] = 0
                                     
        if obj[c4d.ID_USERDATA,4] == 1:
                                   
           obj[c4d.ID_USERDATA,1] = 0
           obj[c4d.ID_USERDATA,2] = 0
           obj[c4d.ID_USERDATA,3] = 0
           obj[c4d.ID_USERDATA,4] = 1
               
        c4d.EventAdd()

    if I press from the last one (4 to 1) its working but does not want to go back. It sticks to the one I pressed last.
    I tried putting it in a loop but still the same. _<_img src="http://www.c4dcafe.com/ipb/public/style_emoticons/default/ufogoldorak5.png" border="0" alt="😕" title="😕" /_>_

    Could there maybe someone who can help. THANKS &n;_<_img src="http://www.c4dcafe.com/ipb/public/style_emoticons/default/signthankspin.gif" border="0" alt=":signthankspin:" title=":signthankspin:" /_>_" />



  • On 03/06/2014 at 08:06, xxxxxxxx wrote:

    Remove the EventAdd() at the end. Apart from that I don't see what could be
    wrong with your code. Maybe share the scene.

    For semantic reasons, you might want to use if/elif and not if/if

    Cheers,
    -Niklas



  • On 03/06/2014 at 22:25, xxxxxxxx wrote:

    Related : https://plugincafe.maxon.net/topic/7914/10282_return-buttons-to-false-state

    Ah now I get your problem. Well it will set the value of button "4" to 0 when one of the buttons "1", "2" or "3" were enabled before. You can save the state of the last button and omitt it.

    import c4d
    from c4d import gui
      
    last = -1
    def main() :
        global last
        obj = doc.SearchObject("Null")
                
        if last != 1 and obj[c4d.ID_USERDATA,1] == 1:
            last = 1
            obj[c4d.ID_USERDATA,1] = 1
            obj[c4d.ID_USERDATA,2] = 0
            obj[c4d.ID_USERDATA,3] = 0
            obj[c4d.ID_USERDATA,4] = 0
                                     
        elif last != 2 and obj[c4d.ID_USERDATA,2] == 1:
            last = 2
            obj[c4d.ID_USERDATA,1] = 0
            obj[c4d.ID_USERDATA,2] = 1
            obj[c4d.ID_USERDATA,3] = 0
            obj[c4d.ID_USERDATA,4] = 0
               
        elif last != 3 and obj[c4d.ID_USERDATA,3] == 1:
            last = 3
            obj[c4d.ID_USERDATA,1] = 0
            obj[c4d.ID_USERDATA,2] = 0
            obj[c4d.ID_USERDATA,3] = 1
            obj[c4d.ID_USERDATA,4] = 0
                                     
        elif last != 4 and obj[c4d.ID_USERDATA,4] == 1:
            last = 4
            obj[c4d.ID_USERDATA,1] = 0
            obj[c4d.ID_USERDATA,2] = 0
            obj[c4d.ID_USERDATA,3] = 0
            obj[c4d.ID_USERDATA,4] = 1
    

    Or, even simpler:

    import c4d
    from c4d import gui
      
    last = -1
    cycle_ids = [1, 2, 3, 4]
      
    def reset(obj) :
        for id_ in cycle_ids:
            obj[c4d.ID_USERDATA, id_] = False
      
    def main() :
        global last
        obj = doc.SearchObject("Null")
                
        for id_ in cycle_ids:
            if last == id_:
                continue
            if obj[c4d.ID_USERDATA, id_]:
                last = id_
                reset(obj)
                obj[c4d.ID_USERDATA, id_] = True
    

    Best,
    -Niklas



  • On 03/06/2014 at 22:31, xxxxxxxx wrote:

    Thanks for the reply. I would love to upload a sample scene but don't see a 'upload file' option anywhere 😊



  • On 03/06/2014 at 22:33, xxxxxxxx wrote:

    Too slow again 😠 Thanks for the reply Niklas will give it a try👏



  • On 03/06/2014 at 22:50, xxxxxxxx wrote:

    Thanks Niklas, again you _<_img src="http://www.c4dcafe.com/ipb/public/style_emoticons/default/thisrocks.gif" border="0" alt=":thisrocks:" title=":thisrocks:" /_>_works perfectly 👏

    Now I just must figure out how to let it work in my Xpresso but pls no help on that. I want to figure it out on my own. Still learning 😊

    Thanks again for all your help.


Log in to reply