PYTHON - GetCustomDataTypeDefault(--- integer ---)



  • Hello, there!

    My goal here is to create temporary Userdata.

    ...
    bContainer = c4d.GetCustomDataTypeDefault(myType)
    bContainer[c4d.DESC_NAME] = "TempData"
    myID = obj.AddUserData(bContainer)
    ...
    

    It turns out myType is not an integer, which is what GetCustomDataTypeDefault expects, it's a <type 'float'> passed along from a source somewhere else. Yet, another function could make it a vector instead... Is there a way to cross check myType with c4d.DTYPE_REAL, c4d.DTYPE_VECTOR, and so on, or simply get the proper integer value based on type dynamically?

    Or should I drop this idea altogether and get Basecontainers from different sources with GetClone so I could feed them to the AddUserData method whenever necessary?

    Thanks for your time,

    Leo



  • Hi,

    I am not sure if I did understand your question correctly. Type checking in Cinema in general is encouraged to be done symbol based. For complex types there are c4d.C4DAtom.CheckType() and .GetType() for that purpose. For atomic types there are no such methods, since they are mostly not defined by Cinema. You can find here all DTYPE symbols. So just define a hash table and write a little function for it?

    DTYPE_TABLE = {
        c4d.DTYPE_LONG: (int, some_other_type_u_wanna_map),
        c4d.DTYPE_REAL: float,
        #...
    }
    
    def get_dtype(value):
        """ Returns the DTYPE for value.
        """
        for did, types in DTYPE_TABLE.items():
            if isinstance(value, types):
                return did
        return c4d.NOTOK
    
    dtype = get_dtype(op[some_id])
    bc = c4d.GetCustomDataTypeDefault(dtype) if dtype != c4d.NOTOK else None
    

    FYI: It does not matter here, but never type check with type() since you usually want to take polymorphy into account.

    Cheers
    zipit



  • @zipit Hey, thanks for your quick reply.

    It's definitely a solution, but it forces me to pass the value, which means more code tweaking than what I intended to go for.

    Thanks again!



  • @Leo_Saramago said in PYTHON - GetCustomDataTypeDefault(--- integer ---):

    @zipit Hey, thanks for your quick reply.

    It's definitely a solution, but it forces me to pass the value, which means more code tweaking than what I intended to go for.

    Thanks again!

    Hi,

    you could obviously make the conversion type based.

    def type_to_dtype(_type):
        """ Returns the DTYPE for _type.
        """
        for did, types in DTYPE_TABLE.items():
            if not isinstance(types, tuple):
                types = (types)
            if _type in types:
                return did
        return c4d.NOTOK
    
    dtype = type_to_dtype(my_type)
    bc = c4d.GetCustomDataTypeDefault(dtype) if dtype != c4d.NOTOK else None
    

    But as I mentioned in the FYI in my previous posting, doing type checks via type() is a bad habit, that will rather sooner than later bite you in the ass.

    Cheers
    zipit



  • Hello, @zipit ! I ended up using GetClone() to solve the AddUserData issue.

    A little after that, I found myself in a corner again: unfortunately, there's no way to retrieve Userdata selection in the Attributes Manager - that's why I narrow down my Userdata tracks to only those that will be manipulated by using GetCTracks. Among Userdata, Vectors show up with their components, though, and this means matching by name becomes impossible without slicing. Checking the type allows me to know when slicing is needed. This time, it was much easier to pass values as an argument, they were at hand, so I did apply your first solution, effortlessly.

    Polymorphism... I haven't heard of it since my C++ days... about 25 years ago, when I thought I'd be a real coder.

    This script I'm about to finish will speed up my 3D workflow tremendously.

    Once again, thanks a lot!



  • Hello,

    nothing to add, thanks @zipit for the answer :)

    @Leo_Saramago don't forget to mark your thread to solved.

    Cheers,
    Manuel


Log in to reply