Solved How to load a file with python and turn it into the active document ?

Hi!

I am a total beginner in python ... so this might be easy :)

I am using C4D Release 20 and looking for a way to import files and then work with the files as if they where actually in my C4D Editor. With that I mean I want to work with those CallCommands etc. I know that´s not the pro way but... I am not a pro yet ;) Down below you se the code so far.

The issue now is that this line:

c4d.documents.LoadDocument(sk_doc, loadflags = 0)

...does not really load the file into a state where I can work with it. At this point my editor still shows my empty "BaseDocument" and if I try to "GetActiveDocument" I get a FALSE boolean return. So for python I still have loaded nothing it seams.

I allready tried all sorts of other "loading" methods as you can see here:

https://miro.com/app/board/o9J_kjIM1Ko=/

So somehow I think I need to get the loaded file out of some obscure hidden "position/state" into the state of the "ActiveDocument". So I added those lines here:

c4d.documents.InsertBaseDocument(sk_doc)
        c4d.documents.SetActiveDocument(sk_doc)

But with them I get a "TypeError: argument 1 must be c4d.documents.BaseDocument, not str"

And I have no idea how to change the file URL into a "c4d.documents.BaseDocument"-Type...

Any idea anybody?

Thanks!

Sascha

import c4d
import os
from c4d import gui
from c4d import documents

# Main function
def main():

    def tool():
        return c4d.plugins.FindPlugin(doc.GetAction(), c4d.PLUGINTYPE_TOOL)

    def object():
        return doc.GetActiveObject()

    def tag():
        return doc.GetActiveTag()

    def renderdata():
        return doc.GetActiveRenderData()

    def prefs(id):
        return c4d.plugins.FindPlugin(id, c4d.PLUGINTYPE_PREFS)

    # Set Variables for pathes with dialog
    c4d.gui.MessageDialog("Please select the folder with the BVH files.", type = 0)
    sk_sourcefolder = c4d.storage.LoadDialog(flags = 2)
    c4d.gui.MessageDialog("Please select the destination folder for the converted .c4d files.", type = 0)
    sk_exportfolder = c4d.storage.LoadDialog(flags = 2)

    # Listing and filtering all .bvh files in directory
    sk_filteredfiles = filter(lambda x: x.endswith(".bvh"), os.listdir(sk_sourcefolder))

    # Loop through selected directory
    for file in sk_filteredfiles:

        # Load and activate current project from list
        sk_doc = file
        print sk_doc
        c4d.documents.LoadDocument(sk_doc, loadflags = 0)
        c4d.documents.InsertBaseDocument(sk_doc)
        c4d.documents.SetActiveDocument(sk_doc)

        # Set Render Settings
        c4d.CallCommand(600000098) # Projectname_As_Rendername
        c4d.CallCommand(12161) # Edit Render Settings...
        renderdata()[c4d.RDATA_RENDERENGINE] = 300001061 # Use Hardware OpenGL
        renderdata()[c4d.RDATA_XRES] = 400 # X Render Resolution
        renderdata()[c4d.RDATA_YRES] = 400 # Y Render Resolution
        renderdata()[c4d.RDATA_FRAMERATE] = doc[c4d.DOCUMENT_FPS]
        renderdata()[c4d.RDATA_FRAMESEQUENCE] = 3 # Preview Range als Render Range

... much more but irrelevant code for the current issue...

I have replied your post at C4DCafe.

"The c4d.documents.LoadDocument returns you a BaseDocument, which you then need as argument for the InsertBaseDocument and SetActiveDocument."