Start a new topic

Script to batch convert FLT to OBJ

Hi,

Has anyone tried to batch convert FLT files to OBJ?  I have to convert loads of FLT files to be used in Unity.


Thanks


Rob


Hey Rob, are you using Creator or just the OpenFlight API? And what version?

Hi Steve, I don't know, whichever is easier. I have Anaconda Python installed on my PC and Creator 17, 18 and 19. I'm new to Python and never tried using the OpenFlight API yet. I've run a python script in Creator before but that is just on a single model.

Lots of options Rob, that's why I asked about your environemnt. Let's start simple. Here is a script that wil run in Creator and export the current db (top window in Creator) to an OBJ file in the same folder as the original OpenFlight file. This will also run in Batch Run Script if you want to process more than one file at a time. If you have any questions let us know.

import os

def MakeOBJName(db):
	'''return file name/path for obj file exported from this db'''
	# get the file name of this db (OpenFlight file)
	dbFilename = mgRec2Filename(db)
	# keep base name, change suffix to 'obj'
	# if you want the OBJ files to go somewhere else, change the code here.
	baseName,ext = os.path.splitext(dbFilename)
	objFilename = baseName + ".obj"
	return objFilename
	
def ExportOBJ(db):
	objFilename = MakeOBJName(db)

	# "D5A7C26C-C6CF-43A9-9786-F7C9C2A91DFA" is the GUID that identifies the Wavefront
	# OBJ exporter. If you want to export a different format, you need to find the GUID
	# for that exporter. These are listed in Creator Script Snippet Wizard (in OpenFlight
	# script editor). Some exporters have additional parameters you will need to set up
	# as well (FBX for example). The Creator Script Snippet Wizard will show you.
	ok = mgExportDb (db, objFilename, "D5A7C26C-C6CF-43A9-9786-F7C9C2A91DFA", None, None)
	if (ok == MG_TRUE):
		mgSendMessage(MMSG_STATUS, objFilename + " exported successfully")
	else:
		mgSendMessage(MMSG_ERROR, "Error exporting " + objFilename)

db = mgGetCurrentDb()
# the OBJ exporter only exports the current LOD, so if you have more than one LOD in 
# your OpenFlight you need to make sure the LOD you want is ON. If you wanted the lowest
# LOD, you'd call mgLeastDetail(db) here.
mgMostDetail(db)
ExportOBJ(db)

 

Oh and that script will run in either Creator 17, 18 or 19 but won't work in any previous versions.

Thanks Steve. How would I run it to batch process lots of files?

Hey Rob, 

If you have Creator Pro, use Scripts>Batch Run Script. In that tool you specify a list of OpenFlight files and a list of scripts to run on those files. In your case the list of scripts to run would be just this one script. If you don't have Creator Pro, look at the code snippet in the OpenFlight Script Editor called Edit>Insert Code Snippet>Batch Database Processor. You'd have to juggle the code around a bit but all pieces are there. Let us know if you get stuck.

Steve, Thanks, ill give it a try on Monday UK time.

Kinda slow this afternoon here so I took the liberty of writing for you the Batch Database Processor version I suggested above. You still run this inside of Creator. Instead of processing the "top" database on the desktop, this script prompts you to select a list of OpenFlight files. For each file you select, the script opens the db, exports it and then closes the db.  Extra credit if you try this yourself  by inserting the snippet Edit>Insert Code Snippet>Batch Database Processor and juggling the code before just grabbing my script ;-)  Either way it's all good. Let me know if you have any questions or problems.


import os

def MakeOBJName(db):
	'''return file name/path for obj file exported from this db'''
	# get the file name of this db (OpenFlight file)
	dbFilename = mgRec2Filename(db)
	# keep base name, change suffix to 'obj'
	# if you want the OBJ files to go somewhere else, change the code here.
	baseName,ext = os.path.splitext(dbFilename)
	objFilename = baseName + ".obj"
	return objFilename
	
def ExportOBJ(db):
	objFilename = MakeOBJName(db)

	# "D5A7C26C-C6CF-43A9-9786-F7C9C2A91DFA" is the GUID that identifies the Wavefront
	# OBJ exporter. If you want to export a different format, you need to find the GUID
	# for that exporter. These are listed in Creator Script Snippet Wizard (in OpenFlight
	# script editor). Some exporters have additional parameters you will need to set up
	# as well (FBX for example). The Creator Script Snippet Wizard will show you.
	ok = mgExportDb (db, objFilename, "D5A7C26C-C6CF-43A9-9786-F7C9C2A91DFA", None, None)
	if (ok == MG_TRUE):
		mgSendMessage(MMSG_STATUS, objFilename + " exported successfully")
	else:
		mgSendMessage(MMSG_ERROR, "Error exporting " + objFilename)

def ProcessOneDb(db):
	# the OBJ exporter only exports the current LOD, so if you have more than one LOD in 
	# your OpenFlight you need to make sure the LOD you want is ON. If you wanted the lowest
	# LOD, you'd call mgLeastDetail(db) here.
	mgMostDetail(db)
	ExportOBJ(db)
	# exporting the db does not modify it so we shouldn't force a "write" on this db. 
	# if you did do something here to modify db, you should return True, doing so will
	# trigger mgWriteDb in the caller.
	return False

outs = mgPromptDialogFile(None,
		MPFM_OPEN, MPFA_FLAGS, MPFF_FILEMUSTEXIST|MPFF_MULTISELECT,
		MPFA_PATTERN, "OpenFlight|*.flt",
		MPFA_TITLE, "Select OpenFlight files")

status = outs[0]
if (MSTAT_ISOK(status)):
	numFiles = outs[1]
	fileNames = outs[2:len(outs)]
	for fileName in fileNames:
		db = mgOpenDb(fileName)
		modified = ProcessOneDb(db)
		if (modified):
			mgWriteDb(db)
		mgCloseDb(db)

 

Steve, that's amazing!  Thank you so much. I really wouldn't have got that done myself.  Works perfectly.


Now on to my next problem, how to export the light point attributes into an xml file.


Login to post a comment