SOLVED Python Plugin Unicode Convert

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

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

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

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.