Skip to content

Commit abe07f8

Browse files
committed
move getting the fbx property and fbx channel to different functions
- simplifies the workflow and allows constraints and other properties to reuse more of the code
1 parent 28b6c30 commit abe07f8

File tree

1 file changed

+65
-91
lines changed

1 file changed

+65
-91
lines changed

Assets/FbxExporters/Editor/FbxPropertyChannelPair.cs

Lines changed: 65 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -169,93 +169,85 @@ private static UnityPropertyChannelPair GetUnityPropertyChannelPair(string fullP
169169
return new UnityPropertyChannelPair(property, channel);
170170
}
171171

172-
private static FbxPropertyChannelPair[] GetChannelPairs(string uniPropertyName, PropertyChannelMap propertyChannelMap)
172+
private static string GetFbxProperty(string uniProperty, Dictionary<string, string> propertyMap)
173173
{
174-
// Unity property name is of the format "property.channel". Split by the last '.' and search for the property in the property dict, and channel in the channel dict.
175-
// If the property name is just "property" then the channel is null.
176-
177-
var properties = propertyChannelMap.MapUnityPropToFbxProp;
178-
var channels = propertyChannelMap.MapUnityChannelToFbxChannel;
174+
string fbxProperty;
175+
if(!propertyMap.TryGetValue(uniProperty, out fbxProperty)){
176+
return null;
177+
}
178+
return fbxProperty;
179+
}
179180

180-
// First handle case where there's no channels.
181-
if (channels == null)
181+
private static string GetFbxConstraintSourceProperty(string uniProperty, FbxConstraint constraint, Dictionary<string, string> propertyMap)
182+
{
183+
foreach (var prop in propertyMap)
182184
{
183-
string fbxProperty;
184-
if (properties.TryGetValue(uniPropertyName, out fbxProperty))
185+
var match = System.Text.RegularExpressions.Regex.Match(uniProperty, prop.Key);
186+
if (match.Success && match.Groups.Count > 0)
185187
{
186-
return new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxProperty, null) };
188+
var matchedStr = match.Groups[1].Value;
189+
int index;
190+
if (!int.TryParse(matchedStr, out index))
191+
{
192+
continue;
193+
}
194+
var source = constraint.GetConstraintSource(index);
195+
return string.Format(prop.Value, source.GetName());
187196
}
188-
return null;
189197
}
198+
return null;
199+
}
190200

191-
var uniPropChannelPair = GetUnityPropertyChannelPair(uniPropertyName);
192-
if(uniPropChannelPair.channel == null)
201+
private static string GetFbxChannel(string uniChannel, Dictionary<string, string> channelMap)
202+
{
203+
string fbxChannel;
204+
if(!channelMap.TryGetValue(uniChannel, out fbxChannel))
193205
{
194-
// We've already checked the case where there are no channels
195206
return null;
196207
}
208+
return fbxChannel;
209+
}
197210

198-
var property = uniPropChannelPair.property;
199-
var channel = uniPropChannelPair.channel;
211+
private static FbxPropertyChannelPair[] GetChannelPairs(string uniPropertyName, PropertyChannelMap propertyChannelMap, FbxConstraint constraint = null)
212+
{
213+
// Unity property name is of the format "property.channel" or "property". Handle both cases.
214+
var possibleUniPropChannelPairs = new List<UnityPropertyChannelPair>();
200215

201-
string fbxProp;
202-
if(!properties.TryGetValue(property, out fbxProp))
216+
// could give same result as already in the list, avoid checking this case twice
217+
var propChannelPair = GetUnityPropertyChannelPair(uniPropertyName);
218+
possibleUniPropChannelPairs.Add(propChannelPair);
219+
if (propChannelPair.property != uniPropertyName)
203220
{
204-
return null;
221+
possibleUniPropChannelPairs.Add(new UnityPropertyChannelPair(uniPropertyName, null));
205222
}
206223

207-
string fbxChannel;
208-
if(!channels.TryGetValue(channel, out fbxChannel))
224+
foreach (var uniPropChannelPair in possibleUniPropChannelPairs)
209225
{
210-
return null;
211-
}
212-
213-
return new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxProp, fbxChannel) };
214-
}
215-
216-
private static FbxPropertyChannelPair[] GetConstraintSourceChannelPairs(string uniPropertyName, FbxConstraint constraint, PropertyChannelMap propertyChannelMap)
217-
{
218-
var properties = propertyChannelMap.MapUnityPropToFbxProp;
219-
var channels = propertyChannelMap.MapUnityChannelToFbxChannel;
226+
// try to match property
227+
var fbxProperty = GetFbxProperty(uniPropChannelPair.property, propertyChannelMap.MapUnityPropToFbxProp);
228+
if (string.IsNullOrEmpty(fbxProperty) && constraint != null)
229+
{
230+
// check if it's a constraint source property
231+
fbxProperty = GetFbxConstraintSourceProperty(uniPropChannelPair.property, constraint, propertyChannelMap.MapUnityPropToFbxProp);
232+
}
233+
if (string.IsNullOrEmpty(fbxProperty))
234+
{
235+
continue;
236+
}
220237

221-
foreach(var prop in properties)
222-
{
223-
var match = System.Text.RegularExpressions.Regex.Match(uniPropertyName, prop.Key);
224-
if (match.Success && match.Groups.Count > 0)
238+
// matched property, now try to match channel
239+
string fbxChannel = null;
240+
if(!string.IsNullOrEmpty(uniPropChannelPair.channel) && propertyChannelMap.MapUnityChannelToFbxChannel != null)
225241
{
226-
var matchedStr = match.Groups[1].Value;
227-
int index;
228-
if (!int.TryParse(matchedStr, out index))
242+
fbxChannel = GetFbxChannel(uniPropChannelPair.channel, propertyChannelMap.MapUnityChannelToFbxChannel);
243+
if (string.IsNullOrEmpty(fbxChannel))
229244
{
245+
// couldn't match the Unity channel to the fbx channel
230246
continue;
231247
}
232-
var source = constraint.GetConstraintSource(index);
233-
var fbxName = string.Format(prop.Value, source.GetName());
234-
235-
// Have the fbx name, now need the channel
236-
if(channels == null)
237-
{
238-
// no channel, we have what we need
239-
return new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxName, null) };
240-
}
241-
242-
var uniPropChannelPair = GetUnityPropertyChannelPair(uniPropertyName);
243-
if (uniPropChannelPair.channel == null)
244-
{
245-
// We've already checked the case where there are no channels
246-
return null;
247-
}
248-
249-
var channel = uniPropChannelPair.channel;
250-
string fbxChannel;
251-
if (!channels.TryGetValue(channel, out fbxChannel))
252-
{
253-
return null;
254-
}
255-
return new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxName, fbxChannel) };
256248
}
249+
return new FbxPropertyChannelPair[] { new FbxPropertyChannelPair(fbxProperty, fbxChannel) };
257250
}
258-
259251
return null;
260252
}
261253

@@ -278,52 +270,34 @@ public static bool TryGetValue(string uniPropertyName, out FbxPropertyChannelPai
278270
return true;
279271
}
280272

273+
var propertyMaps = new List<PropertyChannelMap>();
274+
281275
// Try get constraint specific channel pairs first as we know this is a constraint
282276
if (constraint != null)
283277
{
284278
// Aim constraint shares the RotationOffset property with RotationConstraint, so make sure that the correct FBX property is returned
285279
if (constraint.GetConstraintType() == FbxConstraint.EType.eAim)
286280
{
287-
prop = GetChannelPairs(uniPropertyName, AimConstraintPropertyMap);
288-
if (prop != null)
289-
{
290-
return true;
291-
}
281+
propertyMaps.Add(AimConstraintPropertyMap);
292282
}
293283

294-
var constraintPropertyMaps = new List<PropertyChannelMap>()
295-
{
296-
ConstraintSourcePropertyMap,
297-
ConstraintSourceTransformPropertyMap
298-
};
299-
300-
foreach(var propMap in constraintPropertyMaps)
301-
{
302-
prop = GetConstraintSourceChannelPairs(uniPropertyName, constraint, propMap);
303-
if(prop != null)
304-
{
305-
return true;
306-
}
307-
}
284+
propertyMaps.Add(ConstraintSourcePropertyMap);
285+
propertyMaps.Add(ConstraintSourceTransformPropertyMap);
308286
}
309287

310288
// Check if this is a transform, color, or other property and return the channel pairs if they match.
311-
var propertyMaps = new List<PropertyChannelMap>()
312-
{
313-
TransformPropertyMap,
314-
ColorPropertyMap,
315-
OtherPropertyMap
316-
};
289+
propertyMaps.Add(TransformPropertyMap);
290+
propertyMaps.Add(ColorPropertyMap);
291+
propertyMaps.Add(OtherPropertyMap);
317292

318293
foreach (var propMap in propertyMaps)
319294
{
320-
prop = GetChannelPairs(uniPropertyName, propMap);
295+
prop = GetChannelPairs(uniPropertyName, propMap, constraint);
321296
if (prop != null)
322297
{
323298
return true;
324299
}
325300
}
326-
327301
return false;
328302
}
329303
}

0 commit comments

Comments
 (0)