c4d.documents.SaveDocument modifies python source code file !!!



  • Hello,
    im facing a weird behaviour in cinema4d programming in python.

    im executing on demand via python's execfile function a custom script also written in python.
    This custom script imports the c4d module and does cinema 4d specific operations.
    the thing is, each time i run my execfile function that executes my custom script, this custom script file gets weirdly changed!! The source code is turned into what seems to be some kind of compiled version. Here is an image of the resulting file after execution:0_1536237281452_cinema4d.PNG

    Ive managed to narrow the cause of this source code modification to the method:
    c4d.documents.SaveDocument(active_doc, file_path, c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT)

    if i comment this method call and leave the rest of my custom script untouched the source file remains unchanged. So i think its pretty clear this method is doing something weird under the hood.

    Can someone spot what's going on?

    Im using Cinema 4D R19.

    Thanks in advance



  • @jiceq What's the value of file_path? Looks like you overwrite your script with a C4D scene file.



  • arghhh you were right, somehow i was mixing my custom script filepath with the cinema4d scene filepath!! Strange behaviour of cinema 4d is that it lets you save a file with any custom extension....

    Thanks Niklas.



  • Wait a minute!

    the filepath im passing to the SaveDocument() method was being correctly retrieved.

    i do this:

    file_path = os.path.join(c4d.documents.GetActiveDocument().GetDocumentPath(),c4d.documents.GetActiveDocument().GetDocumentName())

    Ive been using this method so far to retrieve the current cinema 4d scene pathname.Then i changed the overall behaviour of my application to run this custom script through an execfile() python method.

    Why is GetDocumentName()/GetDocumentPath() returning the filepath of my script file instead of Cinema's current scene name?

    Im relatively new to Cinema 4D... Am I retrieving incorrectly the scene name? What should i use instead?


  • Global Moderator

    Hi,

    I'm not sure to be honest.
    Do you create the document with wrong path and name yourself?
    Can you maybe post some more code, so we can get a better idea, what's actually going on?

    Thanks,
    Andreas



  • Hi, thanks but i managed to fix it. It wasnt a Cinema4d related error. Apparently a bad argument to one of python methods was causing it to return the filepath of the script instead.