Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Recent
    • Tags
    • Users
    • Login

    Python - Button Selection

    Scheduled Pinned Locked Moved PYTHON Development
    6 Posts 0 Posters 558 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      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:" /_>_" />

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        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

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

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

          Related : https://developers.maxon.net/forum/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

          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            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 ConfusedConfused😊

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

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

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

              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post