Skip to content

Commit ae1c067

Browse files
committed
export parent constraint source translation + rotation offsets
- parent constraint's rotation offset is represented as radians in the fbx, so export accordingly
1 parent e548c3e commit ae1c067

File tree

2 files changed

+80
-39
lines changed

2 files changed

+80
-39
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,15 +1984,16 @@ public class UnityToMayaConvertSceneHelper
19841984
{
19851985
bool convertDistance = false;
19861986
bool convertLtoR = false;
1987+
bool convertToRadian = false;
19871988

19881989
float unitScaleFactor = 1f;
19891990

19901991
public UnityToMayaConvertSceneHelper(string uniPropertyName)
19911992
{
19921993
System.StringComparison cc = System.StringComparison.CurrentCulture;
19931994

1994-
bool partT = uniPropertyName.StartsWith ("m_LocalPosition.", cc) || uniPropertyName.StartsWith("m_TranslationOffset.", cc);
1995-
bool partTx = uniPropertyName.EndsWith ("Position.x", cc) || uniPropertyName.EndsWith ("T.x", cc) || uniPropertyName.EndsWith("TranslationOffset.x", cc);
1995+
bool partT = uniPropertyName.StartsWith("m_LocalPosition.", cc) || uniPropertyName.StartsWith("m_TranslationOffset", cc);
1996+
bool partTx = uniPropertyName.EndsWith("Position.x", cc) || uniPropertyName.EndsWith("T.x", cc) || (uniPropertyName.StartsWith("m_TranslationOffset") && uniPropertyName.EndsWith(".x", cc));
19961997
bool partRyz = uniPropertyName.StartsWith("m_RotationOffset", cc) && (uniPropertyName.EndsWith(".y") || uniPropertyName.EndsWith(".z"));
19971998

19981999
convertLtoR |= partTx;
@@ -2002,11 +2003,19 @@ public UnityToMayaConvertSceneHelper(string uniPropertyName)
20022003
convertDistance |= uniPropertyName.StartsWith ("m_Intensity", cc);
20032004
convertDistance |= uniPropertyName.ToLower().EndsWith("weight", cc);
20042005

2005-
if (convertDistance)
2006+
// The ParentConstraint's source Rotation Offsets are read in as radians, so make sure they are exported as radians
2007+
convertToRadian = uniPropertyName.StartsWith("m_RotationOffsets.Array.data", cc);
2008+
2009+
if (convertDistance)
20062010
unitScaleFactor = ModelExporter.UnitScaleFactor;
20072011

20082012
if (convertLtoR)
20092013
unitScaleFactor = -unitScaleFactor;
2014+
2015+
if (convertToRadian)
2016+
{
2017+
unitScaleFactor *= (Mathf.PI / 180);
2018+
}
20102019
}
20112020

20122021
public float Convert(float value)
@@ -2066,43 +2075,57 @@ struct FbxPropertyChannelPair {
20662075
{ "field of view", "FieldOfView" },
20672076
{ "m_Weight", "Weight" }
20682077
};
2078+
private static Dictionary<string, string> NullChannel = new Dictionary<string, string>() { { "", null } };
20692079
private static Dictionary<string, string> ConstraintSourceProperties = new Dictionary<string, string>()
20702080
{
2071-
{ "m_Sources\\.Array\\.data\\[(\\d+)\\]\\.weight", "{0}.Weight" },
2081+
{ "m_Sources\\.Array\\.data\\[(\\d+)\\]\\.weight", "{0}.Weight" }
2082+
};
2083+
private static Dictionary<string, string> ConstraintSourceTransformProperties = new Dictionary<string, string>()
2084+
{
20722085
{ "m_TranslationOffsets\\.Array\\.data\\[(\\d+)\\]", "{0}.Offset T" },
2073-
{ "m_RotationOffsets\\.Array\\.data\\[\\d+)\\]", "{0}.Offset R" }
2086+
{ "m_RotationOffsets\\.Array\\.data\\[(\\d+)\\]", "{0}.Offset R" }
20742087
};
20752088

20762089
public FbxPropertyChannelPair(string p, string c) : this() {
20772090
Property = p;
20782091
Channel = c;
20792092
}
20802093

2081-
private static bool TryGetChannelPairs(string uniPropertyName, string propFormat, Dictionary<string, string> properties, Dictionary<string, string> channels, ref FbxPropertyChannelPair[] channelPairs)
2094+
private static bool TryGetChannel(string uniPropertyName, string uniName, string propFormat, Dictionary<string, string> channels, out string outChannel)
20822095
{
2083-
System.StringComparison ct = System.StringComparison.CurrentCulture;
2096+
outChannel = null;
2097+
foreach (var channel in channels)
2098+
{
2099+
var uniChannel = channel.Key;
2100+
var fbxChannel = channel.Value;
2101+
if (uniPropertyName.EndsWith(string.Format(propFormat, uniName, uniChannel)))
2102+
{
2103+
outChannel = fbxChannel;
2104+
return true;
2105+
}
2106+
}
2107+
return false;
2108+
}
20842109

2110+
private static bool TryGetChannelPairs(string uniPropertyName, string propFormat, Dictionary<string, string> properties, Dictionary<string, string> channels, ref FbxPropertyChannelPair[] channelPairs)
2111+
{
20852112
foreach (var item in properties)
20862113
{
20872114
var uniName = item.Key;
20882115
var fbxName = item.Value;
2089-
foreach (var entry in channels)
2090-
{
2091-
var uniChannel = entry.Key;
2092-
var fbxChannel = entry.Value;
2093-
if (uniPropertyName.EndsWith(string.Format(propFormat, uniName, uniChannel), ct))
2094-
{
2095-
channelPairs = new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxName, fbxChannel) };
2096-
return true;
2097-
}
2116+
2117+
string channel;
2118+
if(TryGetChannel(uniPropertyName, uniName, propFormat, channels, out channel)) {
2119+
channelPairs = new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxName, channel) };
2120+
return true;
20982121
}
20992122
}
21002123
return false;
21012124
}
21022125

2103-
private static bool TryGetConstraintSourceChannelPairs(string uniPropertyName, FbxConstraint constraint, ref FbxPropertyChannelPair[] channelPairs)
2126+
private static bool TryGetConstraintSourceChannelPairs(string uniPropertyName, FbxConstraint constraint, Dictionary<string, string> properties, Dictionary<string, string> channels, ref FbxPropertyChannelPair[] channelPairs)
21042127
{
2105-
foreach (var prop in ConstraintSourceProperties)
2128+
foreach (var prop in properties)
21062129
{
21072130
var match = System.Text.RegularExpressions.Regex.Match(uniPropertyName, prop.Key);
21082131
if (match.Success && match.Groups.Count > 0)
@@ -2115,8 +2138,12 @@ private static bool TryGetConstraintSourceChannelPairs(string uniPropertyName, F
21152138
}
21162139
var source = constraint.GetConstraintSource(index);
21172140
var fbxName = string.Format(prop.Value, source.GetName());
2118-
channelPairs = new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxName, null) };
2119-
return true;
2141+
string channel;
2142+
// we've already matched with the property name, just get the channel
2143+
if(TryGetChannel(uniPropertyName, "", "{1}", channels, out channel)){
2144+
channelPairs = new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxName, channel) };
2145+
return true;
2146+
}
21202147
}
21212148
}
21222149
return false;
@@ -2131,9 +2158,31 @@ public static bool TryGetValue(string uniPropertyName, out FbxPropertyChannelPai
21312158
System.StringComparison ct = System.StringComparison.CurrentCulture;
21322159

21332160
prop = new FbxPropertyChannelPair[] { };
2161+
var propFormat = "{0}.{1}";
2162+
2163+
if (constraint != null)
2164+
{
2165+
// Aim constraint shares the RotationOffset property with RotationConstraint, so make sure that the correct FBX property is returned
2166+
if (constraint.GetConstraintType() == FbxConstraint.EType.eAim)
2167+
{
2168+
if (TryGetChannelPairs(uniPropertyName, propFormat, AimConstraintProperties, TransformChannels, ref prop))
2169+
{
2170+
return true;
2171+
}
2172+
}
2173+
2174+
if (TryGetConstraintSourceChannelPairs(uniPropertyName, constraint, ConstraintSourceProperties, NullChannel, ref prop))
2175+
{
2176+
return true;
2177+
}
2178+
2179+
if(TryGetConstraintSourceChannelPairs(uniPropertyName, constraint, ConstraintSourceTransformProperties, TransformChannels, ref prop))
2180+
{
2181+
return true;
2182+
}
2183+
}
21342184

21352185
// Transform Properties
2136-
var propFormat = "{0}.{1}";
21372186
if(TryGetChannelPairs(uniPropertyName, propFormat, TransformProperties, TransformChannels, ref prop))
21382187
{
21392188
return true;
@@ -2146,7 +2195,7 @@ public static bool TryGetValue(string uniPropertyName, out FbxPropertyChannelPai
21462195
}
21472196

21482197
// Other Properties
2149-
if(TryGetChannelPairs(uniPropertyName, "{0}", OtherProperties, new Dictionary<string, string>() { { "", null } }, ref prop))
2198+
if(TryGetChannelPairs(uniPropertyName, "{0}", OtherProperties, NullChannel, ref prop))
21502199
{
21512200
return true;
21522201
}
@@ -2161,23 +2210,6 @@ public static bool TryGetValue(string uniPropertyName, out FbxPropertyChannelPai
21612210
return true;
21622211
}
21632212

2164-
if(constraint != null)
2165-
{
2166-
// Aim constraint shares the RotationOffset property with RotationConstraint, so make sure that the correct FBX property is returned
2167-
if (constraint.GetConstraintType() == FbxConstraint.EType.eAim)
2168-
{
2169-
if (TryGetChannelPairs(uniPropertyName, propFormat, AimConstraintProperties, TransformChannels, ref prop))
2170-
{
2171-
return true;
2172-
}
2173-
}
2174-
2175-
if (TryGetConstraintSourceChannelPairs(uniPropertyName, constraint, ref prop))
2176-
{
2177-
return true;
2178-
}
2179-
}
2180-
21812213
return false;
21822214
}
21832215
}

Assets/FbxExporters/Editor/UnitTests/FbxConstraintTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public static IEnumerable TestCases
4747
yield return new TestCaseData(typeof(ScaleConstraint), new float[] { 1f, 2f, 3f }, new float[] { 10f, 180f, 10f }, "m_ScaleOffset.x").Returns(1);
4848
yield return new TestCaseData(typeof(ScaleConstraint), new float[] { 4f, 5f, 6f }, new float[] { 1f, 18f, 50f }, "m_ScaleOffset.y").Returns(1);
4949
yield return new TestCaseData(typeof(ScaleConstraint), new float[] { 1f, 2f, 3f }, new float[] { 2f, 23f, 4f }, "m_ScaleOffset.z").Returns(1);
50+
51+
/* Test Parent */
52+
yield return new TestCaseData(typeof(ParentConstraint), new float[] { 1, 3, 35 }, new float[] { 45f, 34.7f, 56f }, "m_TranslationOffsets.Array.data[0].x").Returns(1);
53+
yield return new TestCaseData(typeof(ParentConstraint), new float[] { 4, 7, 75 }, new float[] { 3f, 65.67f, -34f }, "m_TranslationOffsets.Array.data[0].y").Returns(1);
54+
yield return new TestCaseData(typeof(ParentConstraint), new float[] { 3, 4, 32 }, new float[] { -223f, 56.7f, 0.2f }, "m_TranslationOffsets.Array.data[0].z").Returns(1);
55+
56+
yield return new TestCaseData(typeof(ParentConstraint), new float[] { 1, 3, 35 }, new float[] { 45f, 34.7f, -56f }, "m_RotationOffsets.Array.data[0].x").Returns(1);
57+
yield return new TestCaseData(typeof(ParentConstraint), new float[] { 4, 7, 75 }, new float[] { 3f, 65.67f, 180f }, "m_RotationOffsets.Array.data[0].y").Returns(1);
58+
yield return new TestCaseData(typeof(ParentConstraint), new float[] { 3, 4, 32 }, new float[] { -223f, 56.7f, 0.2f }, "m_RotationOffsets.Array.data[0].z").Returns(1);
5059
}
5160
}
5261
}

0 commit comments

Comments
 (0)