On 03/01/2017 at 03:33, xxxxxxxx wrote:
Hi Thomas,
To export an FBX from command line, SaveDocument() has to be called from another thread.
Here's the solution:
import c4d
from c4d import documents, threading
import sys
FBX_EXPORTER_ID = 1026370
class ExportThread(threading.C4DThread) :
def __init__(self, doc) :
self.doc = doc
self.status = False
def Main(self) :
# Export document to FBX
self.status = documents.SaveDocument(self.doc, DEST_FILE, c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST, FBX_EXPORTER_ID)
def GetStatus(self) :
return self.status
def ExportFBX() :
# Load document
load = documents.LoadFile(SOURCE_FILE)
if not load:
return False
# Get loaded document
doc = documents.GetActiveDocument()
if doc is None:
return False
# Clone document
clone = doc.GetClone(c4d.COPYFLAGS_DOCUMENT)
if clone is None:
return False
thread = ExportThread(clone)
thread.Start() # Start thread
thread.End() # Then end it but wait until it finishes
# Retrieve export status and return it
status = thread.GetStatus()
return status
def PluginMessage(id, data) :
if id==c4d.C4DPL_COMMANDLINEARGS:
if 'ExportFBX' in sys.argv:
print "FBX Export Started"
ret = ExportFBX()
if ret:
print "FBX Export Succeeded"
else:
print "FBX Export Failed!"
return True
return False
To test this code save it to a pyp file in your plugins directory. Then run Cinema 4D's in Command Line passing "ExportFBX" argument.
The thread solution also works well and is fast when Cinema 4D interface is running.