SOLVED User Data button Python Tag r19 - avoid "index out of depth" console feedback

Hello, there!

The following code works(Python Tag). Whenever I press the User Data button, the global position coordinates from the object(cube) get stored in three separate User Data fields. Great!

The issue rises whenever the input in those three fields gets done manually, via keyboard, which is something totally valid for my purposes. Console says "index out of depth", and I just can't figure out how to avoid going that deep with the "if" comparisons, I mean, my goal is to stop passing the "if" tests whenever the messages come from inputs other than the User Data button.

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 != None: #Trying to avoid 'Index Out of Depth' console messages

                if desc_id[1].dtype == c4d.DTYPE_BUTTON:
                    #Gets Driver Object Global Matrix and stores its Global Position
                    o = doc.SearchObject("Drv")
                    if o:
                        m = o.GetMg()
                        pos = m.off
            
                    #Update Button Hit
                    if desc_id[1].id == 1:
                
                        op[c4d.ID_USERDATA,1][c4d.ID_USERDATA,2] = pos[0]
                        op[c4d.ID_USERDATA,1][c4d.ID_USERDATA,4] = pos[1]
                        op[c4d.ID_USERDATA,1][c4d.ID_USERDATA,3] = pos[2]
                            
                        c4d.EventAdd()
            
                    else:
                        return


def main():
    pass

Thanks for your Attention,

Leo

User Data Button test.c4d

This is the test .c4d file.

Once again, thanks for your time.

hello,

The error message make sense as you are trying to reach a level on the DescID that doesn't exist.

So to avoid that you can simply check the depth of the DescID using GetDepth() to check if it's superior to 1.

or

you can create a DescID that you know your button will have and simply compare equalisty

import c4d


myButtonID = 1
myByttonDescID = c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA , c4d.DTYPE_SUBCONTAINER ,0), c4d.DescLevel(myButtonID, c4d.DTYPE_BUTTON,0))



def message(id, data):

    if id == 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']
            #first solution check if the descid depth is more than one
            #if desc_id.GetDepth() > 1:
            
            #second solution, we compare the id with the descID we know our button will have.
            if desc_id == myByttonDescID:
                #Gets Driver Object Global Matrix and stores its Global Position
                o = doc.SearchObject("Drv")
                if o:
                    m = o.GetMg()
                    pos = m.off

                #Update Button Hit
                if desc_id[1].id == 1:

                    op[c4d.ID_USERDATA,1][c4d.ID_USERDATA,2] = pos[0]
                    op[c4d.ID_USERDATA,1][c4d.ID_USERDATA,4] = pos[1]
                    op[c4d.ID_USERDATA,1][c4d.ID_USERDATA,3] = pos[2]

                    c4d.EventAdd()

                else:
                    return


def main():
    pass

and this will allow you to have several button.

Cheers
Manuel

@m_magalhaes said in User Data button Python Tag r19 - avoid "index out of depth" console feedback:

if desc_id.GetDepth() > 1

Thanks a lot, Manuel! It's perfect now.