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

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

    General Talk
    2
    2
    584
    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.
    • S
      sakomaxo
      last edited by

      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...
      
      
      1 Reply Last reply Reply Quote 0
      • C4DSC
        C4DS
        last edited by

        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."

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