Skip to content

Commit

Permalink
Merge pull request #154 from Unity-Technologies/Uni-18845-complete-us…
Browse files Browse the repository at this point in the history
…e-case-node-hierarchy

add methods required to demo import node hierarchy
  • Loading branch information
inwoods authored Jun 3, 2017
2 parents aef7ab8 + 9231d18 commit 2a1f07a
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 129 deletions.
22 changes: 22 additions & 0 deletions examples/export/Assets/Editor/FbxExporter03.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)

if ( unityGo )
{
var unityGoIOSettings = unityGo.GetComponent<FbxSharp.IOSettings> ();
if (unityGoIOSettings)
{
if (unityGoIOSettings.SystemUnit == FbxSharp.SystemUnitType.m)
{
fbxSettings.SetSystemUnit(FbxSystemUnit.m);
}
if (unityGoIOSettings.AxisSystem == FbxSharp.AxisSystemType.Unity)
{
Debug.Log ("setting unity axis system");
fbxSettings.SetAxisSystem(UnityAxisSystem);
}
}

this.ExportComponents ( unityGo, fbxScene, fbxRootNode);
}
}
Expand Down Expand Up @@ -227,6 +241,14 @@ public void Dispose () { }
static string LastFilePath { get; set; }
const string Extension = "fbx";

FbxAxisSystem UnityAxisSystem
{
get { return new FbxAxisSystem (FbxAxisSystem.EUpVector.eYAxis,
FbxAxisSystem.EFrontVector.eParityOdd,
FbxAxisSystem.ECoordSystem.eLeftHanded);
}
}

/// <summary>
/// Get the GameObject
/// </summary>
Expand Down
110 changes: 48 additions & 62 deletions examples/import/Assets/Editor/FbxImporter01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,59 +98,41 @@ public int ImportAll (IEnumerable<UnityEngine.Object> unitySelectionSet)
return 0;
}

string msgLine = "";
MsgLine.Add( "Import Scene Report" );
MsgLine.Add( kBorderLine );

msgLine += kBorderLine;
msgLine += "File";
msgLine += kNewLine;
msgLine += kBorderLine;

msgLine += kPadding + string.Format ("FilePath: {0}", LastFilePath);
msgLine += kNewLine;
msgLine += kPadding + string.Format ("SDK version: {0}.{1}.{2}",
sdkMajor, sdkMinor, sdkRevision);
msgLine += kNewLine;
MsgLine.Add( kPadding + string.Format ("FilePath: {0}", LastFilePath));
MsgLine.Add( kPadding + string.Format ("SDK version: {0}.{1}.{2}",
sdkMajor, sdkMinor, sdkRevision));

if (!fbxImporter.IsFBX ())
{
Debug.LogError (string.Format ("file does not contain FBX data {0}", LastFilePath));
return 0;
}

msgLine += kPadding + string.Format ("File version: {0}.{1}.{2}",
fileMajor, fileMinor, fileRevision);
msgLine += kNewLine;
Debug.Log (msgLine);
MsgLine.Add( kPadding + string.Format ("File version: {0}.{1}.{2}",
fileMajor, fileMinor, fileRevision));

msgLine = kBorderLine;
msgLine += "Animation";
msgLine += kNewLine;
msgLine += kBorderLine;
MsgLine.Add( kBorderLine );
MsgLine.Add( "Animation" );
MsgLine.Add( kBorderLine );

int numAnimStack = fbxImporter.GetAnimStackCount ();

msgLine += kPadding + string.Format ("number of stacks: {0}", numAnimStack);
msgLine += kNewLine;
msgLine += kPadding + string.Format ("active animation stack: \"{0}\"\n", fbxImporter.GetActiveAnimStackName());
MsgLine.Add( kPadding + string.Format ("number of stacks: {0}", numAnimStack));
MsgLine.Add( kPadding + string.Format ("active animation stack: \"{0}\"\n", fbxImporter.GetActiveAnimStackName()));

for (int i = 0; i < numAnimStack; i++) {
#if UNI_18972
FbxTakeInfo fbxTakeInfo = fbxImporter.GetTakeInfo (i);
#endif
msgLine += kPadding + string.Format ("Animation Stack ({0})", i);
msgLine += kNewLine;
#if UNI_18972
msgLine += kPadding +string.Format ("name: \"{0}\"", fbxTakeInfo.mName) + string.kNewLine;
msgLine += kNewLine;
msgLine += kPadding +string.Format ("description: \"{0}\"", fbxTakeInfo.mDescription);
msgLine += kNewLine;
msgLine += kPadding +string.Format ("import name: \"{0}\"", fbxTakeInfo.mImportName);
msgLine += kNewLine;
msgLine += kPadding +string.Format ("import state: \"{0}\"", fbxTakeInfo.mSelect);
msgLine += kNewLine;
MsgLine.Add( kPadding + string.Format ("Animation Stack ({0})", i));
MsgLine.Add( kPadding +string.Format ("name: \"{0}\"", fbxTakeInfo.mName) + string.kNewLine);
MsgLine.Add( kPadding +string.Format ("description: \"{0}\"", fbxTakeInfo.mDescription));
MsgLine.Add( kPadding +string.Format ("import name: \"{0}\"", fbxTakeInfo.mImportName));
MsgLine.Add( kPadding +string.Format ("import state: \"{0}\"", fbxTakeInfo.mSelect));
#endif
}
Debug.Log (msgLine);

// Import options. Determine what kind of data is to be imported.
// The default is true, but here we set the options explictly.
Expand Down Expand Up @@ -201,30 +183,24 @@ public void ProcessScene (FbxScene fbxScene, IEnumerable<UnityEngine.Object> uni

if (sceneInfo != null)
{
string msgLine = "";

msgLine += kBorderLine;
msgLine += "Scene Meta-Data";
msgLine += kNewLine;
msgLine += kBorderLine;

msgLine += kPadding + string.Format ("Title: \"{0}\"", sceneInfo.mTitle);
msgLine += kNewLine;
msgLine += kPadding + string.Format ("Subject: \"{0}\"", sceneInfo.mSubject);
msgLine += kNewLine;
msgLine += kPadding + string.Format ("Author: \"{0}\"", sceneInfo.mAuthor);
msgLine += kNewLine;
msgLine += kPadding + string.Format ("Keywords: \"{0}\"", sceneInfo.mKeywords);
msgLine += kNewLine;
msgLine += kPadding + string.Format ("Revision: \"{0}\"", sceneInfo.mRevision);
msgLine += kNewLine;
msgLine += kPadding + string.Format ("Comment: \"{0}\"", sceneInfo.mComment);
msgLine += kNewLine;

Debug.Log (msgLine);
MsgLine.Add( kBorderLine );
MsgLine.Add( "Scene Meta-Data" );
MsgLine.Add( kBorderLine );

MsgLine.Add( kPadding + string.Format ("Title: \"{0}\"", sceneInfo.mTitle));
MsgLine.Add( kPadding + string.Format ("Subject: \"{0}\"", sceneInfo.mSubject));
MsgLine.Add( kPadding + string.Format ("Author: \"{0}\"", sceneInfo.mAuthor));
MsgLine.Add( kPadding + string.Format ("Keywords: \"{0}\"", sceneInfo.mKeywords));
MsgLine.Add( kPadding + string.Format ("Revision: \"{0}\"", sceneInfo.mRevision));
MsgLine.Add( kPadding + string.Format ("Comment: \"{0}\"", sceneInfo.mComment));
}

var fbxSettings = fbxScene.GetGlobalSettings ();

MsgLine.Add( kBorderLine );
MsgLine.Add( "Global Settings" );
MsgLine.Add( kBorderLine );

FbxSystemUnit fbxSystemUnit = fbxSettings.GetSystemUnit ();

if (fbxSystemUnit != UnitySystemUnit)
Expand All @@ -233,8 +209,9 @@ public void ProcessScene (FbxScene fbxScene, IEnumerable<UnityEngine.Object> uni
UnitySystemUnit.ToString(), fbxSystemUnit.ToString()));
}

// The Unity axis system has Y up, Z forward, X to the right.
MsgLine.Add (kPadding + string.Format ("SystemUnits: {0}", fbxSystemUnit.ToString()));

// The Unity axis system has Y up, Z forward, X to the right.
FbxAxisSystem fbxAxisSystem = fbxSettings.GetAxisSystem();

if (fbxAxisSystem != UnityAxisSystem)
Expand All @@ -247,6 +224,10 @@ public void ProcessScene (FbxScene fbxScene, IEnumerable<UnityEngine.Object> uni
fbxAxisSystem.GetFrontVector().ToString (),
fbxAxisSystem.GetCoorSystem().ToString ()));
}
MsgLine.Add (kPadding + string.Format ("AxisSystem: {0}", AxisSystemToString(fbxAxisSystem)));

// print report
Debug.Log(string.Join (kNewLine, MsgLine.ToArray ()));

return;
}
Expand Down Expand Up @@ -290,14 +271,11 @@ public void Dispose () { }
/// </summary>
static string LastFilePath { get; set; }
const string kExtension = "fbx";
const string kBorderLine = "--------------------\n";
const string kBorderLine = "--------------------";
const string kNewLine = "\n";
const string kPadding = " ";

private static string MakeFileName(string basename = "test", string extension = "fbx")
{
return basename + "." + extension;
}
List<string> MsgLine = new List<string> ();

private FbxSystemUnit UnitySystemUnit { get { return FbxSystemUnit.m; } }

Expand All @@ -307,6 +285,14 @@ private FbxAxisSystem UnityAxisSystem {
FbxAxisSystem.ECoordSystem.eLeftHanded); }
}

private static string AxisSystemToString (FbxAxisSystem fbxAxisSystem)
{
return string.Format ("[{0}, {1}, {2}]",
fbxAxisSystem.GetUpVector ().ToString (),
fbxAxisSystem.GetFrontVector ().ToString (),
fbxAxisSystem.GetCoorSystem ().ToString ());
}

// use the SaveFile panel to allow user to enter a file name
private static void OnImport()
{
Expand Down
138 changes: 71 additions & 67 deletions examples/import/Assets/Editor/FbxImporter02.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//#define UNI_18892
//#define UNI_18844
// ***********************************************************************
// Copyright (c) 2017 Unity Technologies. All rights reserved.
Expand Down Expand Up @@ -50,7 +49,7 @@ public class FbxImporter02 : System.IDisposable
/// <summary>
/// Process fbx scene by doing nothing
/// </summary>
public void ProcessNode (FbxNode fbxNode, GameObject unityParentObj = null)
public void ProcessNode (FbxNode fbxNode, GameObject unityParentObj = null, bool constructTransform = false)
{
string name = fbxNode.GetName ();

Expand All @@ -61,68 +60,80 @@ public void ProcessNode (FbxNode fbxNode, GameObject unityParentObj = null)
unityGo.transform.parent = unityParentObj.transform;
}

FbxVector4 lclTrs = new FbxVector4();
FbxQuaternion lclRot = new FbxQuaternion();
FbxVector4 lclScl = new FbxVector4(1.0f, 1.0f, 1.0f);
FbxAMatrix fbxTransform = null;

if (constructTransform) {
#if UNI_18844
// Construct rotation matrices
FbxVector4 fbxRotation = new FbxVector4 (fbxNode.LclRotation.Get ());
FbxAMatrix fbxRotationM = new FbxAMatrix ();
fbxRotationM.SetR(fbxRotation);

FbxVector4 fbxPreRotation = new FbxVector4 (fbxNode.PreRotation.Get ());
FbxAMatrix fbxPreRotationM = new FbxAMatrix ();
fbxPreRotationM.SetR(fbxPreRotation);

FbxVector4 fbxPostRotation = new FbxVector4 (fbxNode.PostRotation.Get ());
FbxAMatrix fbxPostRotationM = new FbxAMatrix ();
fbxPostRotationM.SetR(fbxPostRotation);

// Construct translation matrix
FbxAMatrix fbxTranslationM = new FbxAMatrix ();
FbxVector4 fbxTranslation = new FbxVector4 (fbxNode.LclTranslation.Get ());
fbxTranslationM.SetT(fbxTranslation);

// Construct scaling matrix
FbxAMatrix fbxScalingM = new FbxAMatrix ();
FbxVector4 fbxScaling = new FbxVector4 (fbxNode.LclScaling.Get ());
fbxScalingM.SetS(fbxScaling);

// Construct offset and pivot matrices
FbxAMatrix fbxRotationOffsetM = new FbxAMatrix ();
FbxVector4 fbxRotationOffset = fbxNode.RotationOffset.Get ();
fbxRotationOffsetM.SetT(fbxRotationOffset);

FbxAMatrix fbxRotationPivotM = new FbxAMatrix ();
FbxVector4 fbxRotationPivot = fbxNode.RotationPivot.Get ();
fbxRotationPivotM.SetT(fbxRotationPivot);

FbxAMatrix fbxScalingOffsetM = new FbxAMatrix ();
FbxVector4 fbxScalingOffset = fbxNode.ScalingOffset.Get ();
fbxScalingOffsetM.SetT(fbxScalingOffset);

FbxAMatrix fbxScalingPivotM = new FbxAMatrix ();
FbxVector4 fbxScalingPivot = fbxNode.ScalingPivot.Get ();
fbxScalingPivotM.SetT(fbxScalingPivot);

FbxAMatrix fbxTransform =
fbxTranslationM *
fbxRotationOffsetM *
fbxRotationPivotM *
fbxPreRotationM *
fbxRotationM *
fbxPostRotationM *
fbxRotationPivotM.Inverse () *
fbxScalingOffsetM *
fbxScalingPivotM *
fbxScalingM *
fbxScalingPivotM.Inverse ();

// Construct rotation matrices
FbxVector4 fbxRotation = new FbxVector4 (fbxNode.LclRotation.Get ());
FbxAMatrix fbxRotationM = new FbxAMatrix ();
fbxRotationM.SetR(fbxRotation);

FbxVector4 fbxPreRotation = new FbxVector4 (fbxNode.PreRotation.Get ());
FbxAMatrix fbxPreRotationM = new FbxAMatrix ();
fbxPreRotationM.SetR(fbxPreRotation);

FbxVector4 fbxPostRotation = new FbxVector4 (fbxNode.PostRotation.Get ());
FbxAMatrix fbxPostRotationM = new FbxAMatrix ();
fbxPostRotationM.SetR(fbxPostRotation);

// Construct translation matrix
FbxAMatrix fbxTranslationM = new FbxAMatrix ();
FbxVector4 fbxTranslation = new FbxVector4 (fbxNode.LclTranslation.Get ());
fbxTranslationM.SetT(fbxTranslation);

// Construct scaling matrix
FbxAMatrix fbxScalingM = new FbxAMatrix ();
FbxVector4 fbxScaling = new FbxVector4 (fbxNode.LclScaling.Get ());
fbxScalingM.SetS(fbxScaling);

// Construct offset and pivot matrices
FbxAMatrix fbxRotationOffsetM = new FbxAMatrix ();
FbxVector4 fbxRotationOffset = fbxNode.RotationOffset.Get();
fbxRotationOffsetM.SetT(fbxRotationOffset);

FbxAMatrix fbxRotationPivotM = new FbxAMatrix ();
FbxVector4 fbxRotationPivot = fbxNode.RotationPivot.Get();
fbxRotationPivotM.SetT(fbxRotationPivot);

FbxAMatrix fbxScalingOffsetM = new FbxAMatrix ();
FbxVector4 fbxScalingOffset = fbxNode.ScalingOffset.Get ();
fbxScalingOffsetM.SetT(fbxScalingOffset);

FbxAMatrix fbxScalingPivotM = new FbxAMatrix ();
FbxVector4 fbxScalingPivot = fbxNode.ScalingPivot.Get ();
fbxScalingPivotM.SetT(fbxScalingPivot);

fbxTransform =
fbxTranslationM *
fbxRotationOffsetM *
fbxRotationPivotM *
fbxPreRotationM *
fbxRotationM *
fbxPostRotationM *
fbxRotationPivotM.Inverse () *
fbxScalingOffsetM *
fbxScalingPivotM *
fbxScalingM *
fbxScalingPivotM.Inverse ();

lclTrs = fbxTransform.GetT ();
lclRot = fbxTransform.GetQ ();
lclScl = fbxTransform.GetS ();
#endif
} else {
fbxTransform = fbxNode.EvaluateLocalTransform ();
}

if (fbxTransform == null)
{
Debug.LogError (string.Format ("failed to retrieve transform for node : {0}", fbxNode.GetName()));
return;
}

FbxVector4 lclTrs = fbxTransform.GetT ();
FbxQuaternion lclRot = fbxTransform.GetQ ();
FbxVector4 lclScl = fbxTransform.GetS ();
#endif

Debug.Log (string.Format ("processing {3} Lcl : T({0}) R({1}) S({2})",
lclTrs.ToString (),
Expand Down Expand Up @@ -255,10 +266,8 @@ public int ImportAll (IEnumerable<Object> unitySelectionSet)

// Get the version number of the FBX file format.
int fileMajor = -1, fileMinor = -1, fileRevision = -1;
fbxImporter.GetFileVersion (out fileMajor, out fileMinor, out fileRevision);

#if UNI_18892
fbxImporter.GetFileVersion (fileMajor, fileMinor, fileRevision);
#endif
// Check that initialization of the fbxImporter was successful
if (!status)
{
Expand All @@ -278,15 +287,10 @@ public int ImportAll (IEnumerable<Object> unitySelectionSet)

// Import options. Determine what kind of data is to be imported.
// The default is true, but here we set the options explictly.
#if UNI_18892
fbxIOSettings.SetBoolProp(Globals.IMP_FBX_MATERIAL, false);
fbxIOSettings.SetBoolProp(Globals.IMP_FBX_TEXTURE, false);
fbxIOSettings.SetBoolProp(Globals.IMP_FBX_LINK, false);
fbxIOSettings.SetBoolProp(Globals.IMP_FBX_SHAPE, false);
fbxIOSettings.SetBoolProp(Globals.IMP_FBX_GOBO, false);
fbxIOSettings.SetBoolProp(Globals.IMP_FBX_ANIMATION, false);
fbxIOSettings.SetBoolProp(Globals.IMP_FBX_GLOBAL_SETTINGS, true);
#endif

// Create a scene
var fbxScene = FbxScene.Create (fbxManager, "Scene");
Expand Down
Loading

0 comments on commit 2a1f07a

Please sign in to comment.