Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware 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
    • Unread
    • Recent
    • Tags
    • Users
    • Login

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

    Cinema 4D SDK
    3
    6
    942
    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.
    • Leo_SaramagoL
      Leo_Saramago
      last edited by Leo_Saramago

      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

      "The idea dictates everything."

      by David Lynch

      1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand
        last edited by ferdinand

        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

        MAXON SDK Specialist
        developers.maxon.net

        Leo_SaramagoL 1 Reply Last reply Reply Quote 1
        • Leo_SaramagoL
          Leo_Saramago @ferdinand
          last edited by

          @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!

          "The idea dictates everything."

          by David Lynch

          ferdinandF 1 Reply Last reply Reply Quote 0
          • ferdinandF
            ferdinand @Leo_Saramago
            last edited by ferdinand

            @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

            MAXON SDK Specialist
            developers.maxon.net

            1 Reply Last reply Reply Quote 0
            • Leo_SaramagoL
              Leo_Saramago
              last edited by Leo_Saramago

              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!

              "The idea dictates everything."

              by David Lynch

              1 Reply Last reply Reply Quote 0
              • ManuelM
                Manuel
                last edited by

                Hello,

                nothing to add, thanks @zipit for the answer 🙂

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

                Cheers,
                Manuel

                MAXON SDK Specialist

                MAXON Registered Developer

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