[Closed] Batch file import creates new layers
Hello,
Can anyone help me with a script line?
I want to batch import some files (it can be whatever, FBX, game asset, objects) and I need to add a line in a script where each each imported file creates a new layer. If the layer name gets the imported file name too, the better.
For example, I have this part of OFIO script where I can import multiple *oft files. But the files doesnt create a new layer for each when imported. But comes all as a bulk in the default layer.
group " \\ FRAGMENTS \\ OFT "
(
button btn_IV_FT_Import "Import Multiple" width:120 align:#center
button btn_IV_FT_Export "Export Single" width:120 align:#center
label margin_IV_FT "" height:0
)
on btn_IV_FT_Import pressed do
(
local input_oft_dialog = dotNetObject "System.Windows.Forms.OpenFileDialog" --create a OpenFileDialog
input_oft_dialog.title = "Select One Or More .oft Files" --set the title
input_oft_dialog.Multiselect = true --allow multiple files to be selected
input_oft_dialog.Filter = "OFT Files (*.oft)|*.oft|All Files (*.*)|*.*" --specify the filter
input_oft_dialog.FilterIndex = 1 --set the filter drop-down list to All Files
local input_oft_dialog_result = input_oft_dialog.showDialog() --display the dialog, get result into variable
input_oft_dialog_result.ToString() --when closed, convert the result to string
input_oft_dialog_result.Equals input_oft_dialog_result.OK --returns TRUE if OK was pressed, FALSE otherwise
input_oft_dialog_result.Equals input_oft_dialog_result.Cancel --returns TRUE if Cancel was pressed, FALSE otherwise
local input_oft_file_names = input_oft_dialog.fileNames --the selected filenames will be returned as an array
for input_oft_file_name in input_oft_file_names do
if (input_oft_file_name != undefined) and (getFileSize input_oft_file_name) > 0 then
(
with undo off
(
with redraw off
(
local oft = ofio_iv_oft()
ofioLog.start()
try (
local start_time = timeStamp()
ofioLog.print "reading input file"
oft.read(input_oft_file_name)
ofioLog.print "building"
oft.build()
ofioLog.print ("oft import succeed, elapsed " + ( ( (timeStamp() - start_time) / 1000.0 ) as String ) + " seconds")
gc()
) catch
(
ofutils.showException()
oft.cleanup()
ofioLog.print "IMPORT ERROR !"
)
ofioLog.finish()
oft = undefined
)
)
)
)
Something like this:
(
filesToImportArr = #(filePath1, filePath2, filePath3)
for f in filesToImportArr do
(
importFile f
fileName = getFilenameFile f
if layerManager.getLayerFromName fileName == undefined do
(
layerManager.newLayerFromName fileName
)
)
)
Hey miauu.
Thank you again for your help.
So basically I can add this lines at the end of any batch import script formulas?
You could simply create a layer and make it current before the import from particular file
for filename in filenames do
(
_layer = layerManager.newLayerFromName (GetLayerNameFromFilename filename)
if _layer != undefined then
(
_layer.current = true
... do import here.
)
else
(
throw "Duplicate layer name"
)
)
I’m a noob.
I don’t know much about how maxscript works. But with that, I blindly managed to complete some time consuming tasks by researching maxscript functions from different scripts or by asking great developers or people that knows how to do it.
I’m not a maxscript developer at all.
I will try to attach your lines to the sample script I posted.
The original OFIO script had single import file function. Meaning if you want to import 1000 *oft files , you need to do it manualy… one by one.
Here is the original part of the OFIO Script
group " \\ FRAGMENTS \\ OFT "
(
button btn_IV_FT_Import "Import Single" width:120 align:#center
button btn_IV_FT_Export "Export Single" width:120 align:#center
label margin_IV_FT "" height:0
)
on btn_IV_FT_Import pressed do
(
local input_oft_file_name = getOpenFileName caption:"Select GTA IV *.oft file" types:"gta iv open formats fragments (*.oft)|*oft"
if (input_oft_file_name != undefined) and (getFileSize input_oft_file_name) > 0 then
(
with undo off
(
with redraw off
(
local oft = ofio_iv_oft()
ofioLog.start()
try (
local start_time = timeStamp()
ofioLog.print "reading input file"
oft.read(input_oft_file_name)
ofioLog.print "building"
oft.build()
ofioLog.print ("oft import succeed, elapsed " + ( ( (timeStamp() - start_time) / 1000.0 ) as String ) + " seconds")
gc()
) catch
(
ofutils.showException()
oft.cleanup()
ofioLog.print "IMPORT ERROR !"
)
ofioLog.finish()
oft = undefined
)
)
)
)
So I managed to edit this to import from single to multiple *oft files (see the sample I posted in first post).
Now, I wanted to create a layer for each file is imported.
Using your lines, I managed to insert some of your lines without errors.
It looks like this.
on btn_IV_FT_Import pressed do
(
local input_oft_dialog = dotNetObject "System.Windows.Forms.OpenFileDialog" --create a OpenFileDialog
input_oft_dialog.title = "Select One Or More .oft Files" --set the title
input_oft_dialog.Multiselect = true --allow multiple files to be selected
input_oft_dialog.Filter = "OFT Files (*.oft)|*.oft|All Files (*.*)|*.*" --specify the filter
input_oft_dialog.FilterIndex = 1 --set the filter drop-down list to OFT Files
local input_oft_dialog_result = input_oft_dialog.showDialog() --display the dialog, get result into variable
input_oft_dialog_result.ToString() --when closed, convert the result to string
input_oft_dialog_result.Equals input_oft_dialog_result.OK --returns TRUE if OK was pressed, FALSE otherwise
input_oft_dialog_result.Equals input_oft_dialog_result.Cancel --returns TRUE if Cancel was pressed, FALSE otherwise
local input_oft_file_names = input_oft_dialog.fileNames --the selected filenames will be returned as an array
--local input_oft_file_name = getOpenFileName caption:"Select GTA IV *.oft file" types:"gta iv open formats fragments (*.oft)|*oft"
for input_oft_file_name in input_oft_file_names do
if (input_oft_file_name != undefined) and (getFileSize input_oft_file_name) > 0 then
(
if layerManager.getLayerFromName input_oft_file_name == undefined do
(
layerManager.newLayerFromName input_oft_file_name
)
with undo off
(
with redraw off
(
local oft = ofio_iv_oft()
ofioLog.start()
try (
local start_time = timeStamp()
ofioLog.print "reading input file"
oft.read(input_oft_file_name)
ofioLog.print "building"
oft.build()
ofioLog.print ("oft import succeed, elapsed " + ( ( (timeStamp() - start_time) / 1000.0 ) as String ) + " seconds")
gc()
) catch
(
ofutils.showException()
oft.cleanup()
ofioLog.print "IMPORT ERROR !"
)
ofioLog.finish()
oft = undefined
)
)
)
)
Now here the layers are created for each imported file. But with two problems:
- Imported objects remains in the default layer… and the new ones are created as empty (without the objects)
- The layer name takes “the *oft name along with full path where the *oft file is located”. Not just the *oft file name.
Also, I don’t think I inserted the lines in the right place.
One more thing. This script doesn’t use the Max basic import function. But an open file trough a dialog from Utilities Panel. (not sure if matters. I thought is good to be known). And I think uses another script to decompile the *oft into multiple objects.
Many thanks for your reply Serejah.
I will give it a shot.
So, for this particular example script… I managed to make it work with this lines:
(
if layerManager.getLayerFromName (filenameFromPath (getFilenameFile input_oft_file_name)) == undefined do
(
layerManager.newLayerFromName (filenameFromPath (getFilenameFile input_oft_file_name))
(LayerManager.getLayerFromName (filenameFromPath (getFilenameFile input_oft_file_name))).current = true
)