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 Plugin Unicode Convert

    General Talk
    python
    3
    4
    688
    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.
    • G
      geese780
      last edited by

      Hey Y'all,

      I'm running into an issue with a JSON encoding bit, I'm getting a type error of this

      TypeError: unable to convert unicode to @net.maxon.interface.url-C

      I am trying to read a JSON file into cinema and make it a path in the plugin, below is the current code I have

      #----------------------------------------------------------------------------------------------------------------------------------------
      #  IMPORT OBJECTS
      #----------------------------------------------------------------------------------------------------------------------------------------
      import os
      from os import listdir
      from os.path import isfile, join
      def import_objs(OBJ_LIST):
          global SETTINGS
          LIBPATH         = SETTINGS["SETTING_ASSET_LIBRARY_FULLPATH"]
          FILES           = [f for f in listdir(LIBPATH) if isfile(join(LIBPATH, f))]
          for OBJ in OBJ_LIST:
              OBJ_FILENAME    = OBJ[0] + ".c4d"
              if OBJ_FILENAME in FILES:
                  FILEPATH = os.path.join(LIBPATH,OBJ_FILENAME)
                  c4d.documents.MergeDocument(doc, FILEPATH, c4d.SCENEFILTER_OBJECTS | c4d.SCENEFILTER_MATERIALS, None)
                  c4d.EventAdd()
          return True
      

      Any thoughts?

      Cheers!

      MattG

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

        Hi,

        with the information provided here it is rather hard to give you an answer (at least for me). You should provide:

        1. The full trace-back. I assume the exception is raised on c4d.documents.MergeDocument(...?
        2. Provide the full code or explain the environment if that is not possible.

        No offense intended, but you also might want to stick to naming conventions, upper case symbols are universally reserved for global constants in all languages.

        With that out of the way:

        1. My initial association for the given exception was that you wrote a file name as a string into a c4d.BaseContainer and Cinema is complaining about that (use BaseContainer.SetFilename instead of __setitem__ for that case).
        2. You also should check if FILEPATH forms an existing and valid path (LIBPATH could be malformed).

        Cheers,
        zipit

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 1
        • G
          geese780
          last edited by

          Hey Zipit!

          Thanks for the response, I actually completely overlooked the fact that I had to convert it to a string......(insert hammer on head here)

          Note to self hahaha!

          Stay safe out there!

          Cheers!

          MattG

          1 Reply Last reply Reply Quote 0
          • M
            m_adam
            last edited by

            Just to add on top,
            os.path.listdir and os.path.join in python 2 are context-specific, so if you input a Unicode string it will output a Unicode string, if you input a regular string it will output a regular string.
            Now the culprit is on c4d.documents.MergeDocument because normally this method accepts a C++ Filename object (which is the Classic API that under the hood uses the new MaxonAPI maxon::Url). Unfortunately, the Python parser is not able to convert from a Python Unicode string to a C++ Filename, but only from a Python ASCII string to a filename.

            So indeed casting from Unicode string to ASCII is the solution.
            Cheers,
            Maxime.

            MAXON SDK Specialist

            Development Blog, MAXON Registered Developer

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