Changing the syntax from "SetString" to "Brackets" type



  • Hi,

    DISCLAIMER: This might be a trivial matter but I hope you'll hear me out.

    I'm trying to add a userdata with a dropdown list. Much like this interface in Maya (https://www.dropbox.com/s/8d7n77nb7imtnuh/c4d087_enum_list_attribute.jpg?dl=0)

    I managed to do that with the help of this blog. I modified it and it works as expected but my concern is it is using a "SetString" type rather than "Brackets" type. See illustration below.

    So instead of
    bc[c4d.DESC_NAME] = "Name"
    It uses
    bc.SetString(c4d.DESC_NAME, "Name")

    I would prefer the former as my previous codes are written like that.

    In the code below, I inserted a # comment line for the things that I'd like to be revised.

        con = c4d.BaseObject(5181)
        doc.InsertObject(con)
    
        bc = c4d.GetCustomDataTypeDefault(c4d.DTYPE_LONG)
        bc[c4d.DESC_NAME] = "spaceSwitch"
        bc[c4d.DESC_CUSTOMGUI]=c4d.CUSTOMGUI_CYCLE
        
        
        # how to change to bracket type
        test = c4d.BaseContainer()
        test.SetString(1, "head")
        test.SetString(2, "chest")
        bc.SetContainer(c4d.DESC_CYCLE, test)
        # how to change to bracket type
        
        descID = con.AddUserData(bc)
        con[c4d.ID_USERDATA, descID[1].id]=1
        
        c4d.EventAdd()
    

    Thank you for looking at my problem.



  • Hi @bentraje,

    [] bracket operator in a case of a BaseList2D is an alias for Set/GetParameter.
    [] bracket operator in a case of BaseContainer is an alias for SetData / GetData, see __setitem__ and __getitem__.

    So you can use them directly.

    test = c4d.BaseContainer()
    test[1] = "head"
    test[2] = "chest"
    bc[c4d.DESC_CYCLE] = test
    

    Hope it make sense.
    Cheers,
    Maxime.



  • @m_adam

    Interesting. I never knew about the bracket operator being different on a BaseList2D and the BaseContainer.

    Thank you for the response. Works as expected. Have a great day ahead!


Log in to reply