An FBX SDK iOS quick tutorial

I recently had a question on the FBX iOS SDK and while I had to use it on a ReCap Photo project, I wanted to share my experience with you. The SDK is the one used to write the FBXReview application on iOS. The FBX development team is working on an Android version right now and should come in a future release.

FbxReview

To minimize the efforts of writing our first iOS application using the FBX iOS SDK, we will reuse the ImportScene FBX sample and transpose it to iOS Cocoa vs a Windows or MacOS console application. When you install the FBX iOS SDK, the installer does not install any samples, documentation, etc… but you can install the MacOS version to get at leaast the source code even if there is no iOS Cocoa project coming with it.

Starting a New Project

We will create a blank project as the starting point for our FBX project.

  1. Install the FBX iOS SDK from http://www.autodesk.com/developfbx
    (latest release at the time of this post is 2015.1)
    The installer will copy files in a non-standard location /Applications/Autodesk/FBX SDK/2015.1
  2. Start Xcode
    (using version 5.3 in this example)
  3. Create a new iOS single view project (Universal)
    With this version of the FBX SDK (i.e. 2015.1) the simulator library does not contain the 64 bit version, so you need to change your Build Settings accordingly for ‘Architectures’ and ‘Other Linker Flags’
    1. For ‘Architectures’, press the + sign on Debug and Release to add different configuration for building simulator or a real device app, and change the default standard architecture to $(ARCHS_STANDARD_32_BIT) for the simulator configurations.
      Note this step will not be needed in future releases as this bug has been already fixed.

Architectures

    1. For ‘Other Linker Flags’, do almost the same thing but reference the libraries depending of the Architecture.

OtherLinkerFlags

  1. Add into the Project Framework dependency list the ‘libiconv.dylib’
  2. Next is to tell Xcode where to find the header files. Go in ‘User Header Search Paths’ and add the following path “/Applications/Autodesk/FBX SDK/2015.1/include” (do not forget the “). And set the ‘Always Search User Path’ to true.
  3. Open your ViewController.h and change the interfece to
    @interface ViewController : UITableViewController
  4. Rename your ViewController.m into ViewController.mm
  5. Open your ViewController.mm and add
    #import “fbxsdk.h”
  6. Press Cmd+B to build your project. It should compile and link fine.

Copy the existing ImportScene sample code into the project

For our example, I just copied an existing FBX sample and slightly modified it to my needs. There is no major modification and 99% of the code is working as is. The only modification made where to adapt the code from a console app to a Cocoa app. The modification are made in the main.cxx.

Few Tips

Because the FBX iOS SDK is a C++ API, you need to remember few things to avoid problems:

  • rename your Cocoa files to .mm vs .m, otherwise Xcode will compile the source as C code vs C++ and that will generate many errors.
  • strings are C++ strings (‘char *’) vs NSString, so you need to convert them back and force. You can use these techniques for example:
    • NSString *st =[[NSString alloc] initWithFormat:@”%s”, myString]
    • NSString *st =[[NSString alloc] initWithCString:myString encoding:NSUTF8StringEncoding]
    • FbxString st ([myString cStringUsingEncoding:[NSString defaultCStringEncoding]])
  • most of the FBX sample are console applications which aren’t valid app for iOS. For a quick port, you can trap all stdout and redirect them to an UITextView control. See code in GenericViewController.mm fct:viewDidLoad line #50 #80, 81, 82
    - (void)viewDidLoad {
    [super viewDidLoad] ;
    NSArray *paths =NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) ;
    NSString *documentsPath =[paths objectAtIndex:0] ;
    NSString *filename =[NSString stringWithFormat:@"%@/output.txt", documentsPath] ;
    FILE *fp =freopen ([filename cStringUsingEncoding:[NSString defaultCStringEncoding]], "w", stdout) ;
    switch ( __what ) {
    case 1: // Global Light Settings
    DisplayGlobalLightSettings (&(self._fbxObject->_scene)->GetGlobalSettings ());
    break ;
    case 2: // Global Camera Settings
    DisplayGlobalCameraSettings (&(self._fbxObject->_scene)->GetGlobalSettings ()) ;
    break ;
    case 3: // Global Time Settings
    DisplayGlobalTimeSettings (&(self._fbxObject->_scene)->GetGlobalSettings ()) ;
    break ;
    case 4: // Hierarchy
    DisplayHierarchy (self._fbxObject->_scene) ;
    break ;
    case 5: // Node Content
    DisplayContent (self._fbxObject->_scene) ;
    break ;
    case 6: // Pose
    DisplayPose (self._fbxObject->_scene) ;
    break ;
    case 7: // Animation
    DisplayAnimation (self._fbxObject->_scene) ;
    break ;
    case 8: // Generic Information
    DisplayGenericInfo (self._fbxObject->_scene) ;
    break ;
    }
    fclose (fp) ;
    std::ifstream txtFile ([filename cStringUsingEncoding:[NSString defaultCStringEncoding]]) ;
    std::string contents ((std::istreambuf_iterator(txtFile)), std::istreambuf_iterator()) ;
    _text.text =[NSString stringWithCString:contents.data () encoding:NSUTF8StringEncoding] ;
    }
    

 

The completed sample is posted on GitHub at this location


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading