Get userdata Button state on object in Python tag



  • Hi
    I have a userdata button on a null and a python tag on that null. In the tag i try to get the state of the button on the object:

    import c4d
    #Welcome to the world of Python
    
    def message(id, data) :
      if id == 17:
          print "UserData-ID: ", data["descid"][1].id
    

    This only works if the userdata button is on the tag. How can i get the state of the userdata botton on the object from inside the python tag?

    Regards
    Bonsak


  • Global Moderator

    Hi @bonsak, there is a way but is not recommended and pretty experimental, so if a strange behavior occurs, we can't do anything. In the same time, it's the only solution.

    With that said we get AddEventNotification, which allow receiving messages from another object.
    Here is the code of the python tag

    import c4d
    
    def message(msg_type, data):
        if msg_type == c4d.MSG_NOTIFY_EVENT:
            event_data = data['event_data']
            if event_data['msg_id'] == c4d.MSG_DESCRIPTION_COMMAND:
                desc_id = event_data['msg_data']['id']
                if desc_id[1].id == 17: # The ID of the User Data
                    print "Button Pressed"
    
    def main():
        obj = op.GetObject()
        
        # Check if we already listen for message
        if not obj.FindEventNotification(doc, op, c4d.NOTIFY_EVENT_MESSAGE):
            obj.AddEventNotification(op, c4d.NOTIFY_EVENT_MESSAGE, 0, c4d.BaseContainer())
    

    Cheers,
    Maxime.



  • Thanks!
    I couldnt get it to work without modifying it like this:

    def message(msg_type, data):
        if msg_type == c4d.MSG_NOTIFY_EVENT:
            event_data = data['event_data']
            if event_data['msg_id'] == c4d.MSG_DESCRIPTION_COMMAND:
                #print event_data['msg_id']
                if event_data['msg_data']['id'][1].id == 18:
                    print "Button Pressed"
                #desc_id = event_data['msg_data']['id']
                #if desc_id[1].id == 17: # The ID of the User Data
                #    print "Button Pressed"
    
    def main():
        obj = op.GetObject()
        
        # Check if we already listen for message
        if not obj.FindEventNotification(doc, op, c4d.NOTIFY_EVENT_MESSAGE):
            obj.AddEventNotification(op, c4d.NOTIFY_EVENT_MESSAGE, 0, c4d.BaseContainer())
    

    Regards
    Bonsak
    EDIT m_adam: Fixed your code snippet


  • Global Moderator

    Which version do you use?
    Can you provide a scene?




  • Global Moderator

    Hi @bonsak,

    The code within the file and in the forum is not the same.
    The actual issue in your code posted on the forum is you are checking for if event_data['msg_id'] == 18: instead of if event_data['msg_data']['id'][1].id == 18:

    Moreover, in the scene you attached which use my code, the button ID is set to 2.
    So if you change if event_data['msg_id'] == 18: toif event_data['msg_id'] == 2: make it works.

    Cheers,
    Maxime.



  • Ah! So event_data['msg_data']['id'] is the id of the userdata.
    Didnt read your code comments :)
    Thanks alot!

    Regards
    Bonsak