# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Script description:
# Shows how to specify namespace of importation for Load/Merge of
# native MB file or for FBX file generated from a FBX plugin 
#
# Topic: FBApplication, FBFbxManager, FBFbxManager.LoadBegin, FBFbxOptions
#

from pyfbsdk import *

import os.path

app = FBApplication()

# This function used the FbxManager to load a native MB file
def loadNativeFromFbxManager(f, isMerge, namespace):
    mgr = FBFbxManager()
    if mgr.LoadBegin(f, isMerge):
        # Here you can set any of the multiple options of 
        # a Load/Merge

        # For now we only set our namespace of importation
        mgr.CustomImportNamespace = namespace

        # Effective Load
        mgr.Load()
    # Clean up
    mgr.LoadEnd()

#Utility function to get the name of a file without extension
def getFileName(f):
    d, filename = os.path.split(f)
    if filename:
        return os.path.splitext(filename)[0]
    else:
        return "unknown"

# some constants for better clarity
NO_LOAD_UI_DIALOG = False
APPEND = False
MERGE = True
OPTIONS_FOR_LOAD = True

#### These tests are design to test the load/Merge
#### of Native MB scene. It shows how to specify a namespace
#### of importation for all objects in the scene.
nativeFile = r"C:\Users\phaneus\work\testfiles\cube.fbx"

# set the namespace of importation to the name of the scene
options = FBFbxOptions(OPTIONS_FOR_LOAD)
options.CustomImportNamespace = getFileName(nativeFile)

# Open a file with a custom namespace appended to all objects
app.FileOpen( nativeFile, NO_LOAD_UI_DIALOG, options )

# Append the same files with the same namespace appended. The renaming mecanims
# will rename the namespace instead of the objects.
app.FileAppend( nativeFile, NO_LOAD_UI_DIALOG, options )

# We want to merge so change the FBElementAction of all options.
options.SetAll(FBElementAction.kFBElementActionMerge, True)

# Merge with the objects loaded with the FileOpen
app.FileMerge( nativeFile, NO_LOAD_UI_DIALOG, options )

# Use the FBXManager loading mecanims to append to the current scene
loadNativeFromFbxManager(nativeFile, APPEND, getFileName(nativeFile))

# Use the FBXManager loading mecanims to merge with the object from the first FileOpen
loadNativeFromFbxManager(nativeFile, MERGE, getFileName(nativeFile))


#### Test for loading/merging from FBX-import, FBX-Merge
#### This is used to load scene created with a FBX Plugin (generally
#### from another application). We can also specify a namespace of importation

fbxFile = r"C:\Users\phaneus\work\testfiles\export_cube.fbx"
options.SetAll(FBElementAction.kFBElementActionAppend, True)
options.CustomImportNamespace = getFileName(fbxFile)

# Use FBX Import on a file and append a namespace to all objects
app.FBXFileOpen( fbxFile, options )

# Use FBX Append on a file and append a namespace to all objects
app.FBXFileAppend( fbxFile, options )

# Use FBX Merge with a namespace
options.SetAll(FBElementAction.kFBElementActionMerge, True)
app.FBXFileMerge( fbxFile, options )