Solved Export selected objects to separate files

Hello guys,

I wrote small script and got one problem. I can't export selected objects to separate files, after export operation i got obj files with duplicate meshes inside. Which the command do i must use for it?

import c4d
import os
from os.path import join, normpath

dirpath = "c:/usr/temp/mayablender/"

def main():
    doc = c4d.documents.GetActiveDocument()
    selected = doc.GetActiveObjects(0)

 
    for ob in selected:
        ob.DelBit(c4d.BIT_ACTIVE) 
        doc.SetActiveObject(ob)        
                   
        
        full_dirpath = normpath(join(dirpath + ob.GetName() + ".obj"))

        # export to obj
        c4d.documents.SaveDocument(doc,full_dirpath,c4d.SAVEDOCUMENTFLAGS_0,1030178)
        


if __name__=='__main__':
    main()

Whos knows any solution for it?

Hi @klints2, first of all, welcome in the plugincafe community.

I would like to point you some rules on this forum regarding how to post questions in order to help us to help you!

Then additionally it's not necessary to bump a topic to put more pressure on us, we monitor this forum and try to answers as soon as we can due to our workload.
Don't worry it's your first post you couldn't know, so I've setup your topic correctly. :wink:

Regarding your issue. Cinema 4D Obj exporter and all Cinema 4D exporters, does not offer a way to export only a list of objects.
So you have to copy all the stuff needed into a temporary document and then export this temporary document
the function c4d.documents.IsolateObjects does exactly that an returns the temporary BaseDocument.

Then in order to properly setup, the obj exported I invite you to read the export_OBJ in our GitHub repository.

If you have any questions, please let me know.
Cheers,
Maxime.

@klints2
Try to use IsolateObjects

A helper routine to copy the objects of document to a new document (returned). All materials associated are also copied over and the links are corrected.

Checkout my python tutorials, plugins, scripts, xpresso presets and more
https://mikeudin.net

@mikeudin Thanks, but don't undestand how works this command, i was tried to get list from isolated = c4d.documents.IsolateObjects(doc, selected) but it's give me nothing and i can't use for for it.
How do i can exported only selected list to obj, without dialog windows?

Sorry this is my first script for c4d, i'm maya user and little confused. Thanks

Well, I think found it.
Don't perfect but its works

import c4d
import os
from os.path import join, normpath
from c4d import documents

def export():

    dirpath = "c:/usr/temp/"  # temp directory
    global tdoc
    
    sobj = doc.GetActiveObjects(0)
    # isolate objects
    tdoc = c4d.documents.IsolateObjects(doc, sobj)
   
    # get path with object name
    full_dirpath = normpath(join(dirpath + (doc.GetActiveObjects(0)[0]).GetName() + ".obj"))
   
    # export to obj
    documents.SaveDocument(tdoc,full_dirpath,c4d.SAVEDOCUMENTFLAGS_0,1030178)        

if __name__=='__main__':
    export()

    documents.KillDocument(tdoc)

Hi @klints2, may I ask you why this is not perfect? Moreover here just a few corrections in your code.

  • In Cinema 4D (and in general), try to avoid as much as possible global variables. In your case you can call documents.KillDocument(tdoc) just after the SaveDocument.
  • Make use of join as the function should behave.
dirpath = "c:/usr/temp/"  # temp directory
full_dirpath = normpath(join(dirpath, (doc.GetActiveObjects(0)[0]).GetName() + ".obj"))
  • Always check, you get object selected or you will get some errors in the console.
sobj = doc.GetActiveObjects(0)
if not sobj: return

Cheers,
Maxime.

Hi @m_adam, thanks for you correction and advice, it's really usefull for me. I'm learn python just couple month and not sure in my code yet.

  • yeah i'm missed this mistake
    full_dirpath = normpath(join(dirpath, (doc.GetActiveObjects(0)[0]).GetName() + ".obj"))

Thanks one more time, always good to have help and support!