/// <summary> /// Serializes an arbitrary object descending from SampleBase from C# to USD. /// </summary> /// <typeparam name="T">Any type which inherits from SampleBase</typeparam> /// <param name="t">The object/data to be serialized.</param> /// <param name="prim">The UsdPrim to which the object should be written.</param> /// <param name="usdTime">The tiem at which key frames should be created.</param> /// <param name="usdNamespace">The USD namespace (if any) of the object.</param> public void Serialize <T>(T t, pxr.UsdPrim prim, pxr.UsdTimeCode usdTime, string usdNamespace = null) { PropertyInfo[] properties = Reflect.GetCachedProperties(t.GetType()); FieldInfo[] fields = Reflect.GetCachedFields(t.GetType()); var imgble = new pxr.UsdGeomImageable(prim); for (int i = 0; i < properties.Length; i++) { PropertyInfo csProp = properties[i]; Type csType = csProp.PropertyType; if (csType == typeof(object)) { if (Reflect.IsCustomData(csProp) || Reflect.IsMetadata(csProp)) { throw new ArgumentException("Writing metadata/customdata with type of object is not currently allowed"); } object o = csProp.GetValue(t, index: null); if (o != null) { csType = o.GetType(); } } if (!WriteAttr(csProp.Name, csType, csProp.GetValue(t, index:null), usdTime, prim, imgble, csProp, usdNamespace)) { // TODO: add options to dictate behavior here } } for (int i = 0; i < fields.Length; i++) { FieldInfo csField = fields[i]; Type csType = csField.FieldType; if (csType == typeof(object)) { if (Reflect.IsCustomData(csField) || Reflect.IsMetadata(csField)) { throw new ArgumentException("Writing metadata/customdata with type of object is not currently allowed"); } object o = csField.GetValue(t); if (o != null) { csType = o.GetType(); } } if (!WriteAttr(csField.Name, csType, csField.GetValue(t), usdTime, prim, imgble, csField, usdNamespace)) { // TODO: add options to dictate behavior here } } }
/// <summary> /// Deserializes an arbitrary object descending from SampleBase from USD to C#. /// </summary> /// <typeparam name="T">The type to serialize, descending from SampleBase</typeparam> /// <param name="t">The object to to populate.</param> /// <param name="prim">The USD prim from which to read data.</param> /// <param name="usdTime">The time at which to read key frames.</param> /// <param name="usdNamespace">The object namespace, if any.</param> public void Deserialize <T>(T t, pxr.UsdPrim prim, pxr.UsdTimeCode usdTime, string usdNamespace = null) where T : SampleBase { if (t == null) { return; } PropertyInfo[] properties = Reflect.GetCachedProperties(t.GetType()); FieldInfo[] fields = Reflect.GetCachedFields(t.GetType()); object value = t; for (int i = 0; i < properties.Length; i++) { PropertyInfo csProp = properties[i]; if (Reflect.IsNonSerialized(csProp)) { continue; } object propValue = csProp.GetValue(t, null); Deserialize(ref propValue, prim, usdTime, csProp, usdNamespace); csProp.SetValue(t, propValue, index: null); } for (int i = 0; i < fields.Length; i++) { FieldInfo csField = fields[i]; if (Reflect.IsNonSerialized(csField)) { continue; } object fieldValue = csField.GetValue(t); Deserialize(ref fieldValue, prim, usdTime, csField, usdNamespace); csField.SetValue(t, fieldValue); } t = (T)value; }
/// <summary> /// Deserializes an arbitrary object descending from SampleBase from USD to C#. /// </summary> /// <typeparam name="T">The type to serialize, descending from SampleBase</typeparam> /// <param name="t">The object to to populate.</param> /// <param name="prim">The USD prim from which to read data.</param> /// <param name="usdTime">The time at which to read key frames.</param> /// <param name="accessMap">A list of memebers to include when reading.</param> /// <param name="mayVary">Indicates if this prim had any time-varying members.</param> /// <param name="usdNamespace">The object namespace, if any.</param> public void Deserialize <T>(T t, pxr.UsdPrim prim, pxr.UsdTimeCode usdTime, HashSet <MemberInfo> accessMap, ref bool?mayVary, string usdNamespace = null) where T : SampleBase { if (t == null) { return; } PropertyInfo[] properties = Reflect.GetCachedProperties(t.GetType()); FieldInfo[] fields = Reflect.GetCachedFields(t.GetType()); var localVarMap = accessMap; bool mayVaryWasNull = mayVary == null; if (mayVary == null) { localVarMap = null; } for (int i = 0; i < properties.Length; i++) { PropertyInfo csProp = properties[i]; if (Reflect.IsNonSerialized(csProp)) { continue; } if (accessMap != null && mayVary == null && !accessMap.Contains(csProp)) { continue; } object propValue = csProp.GetValue(t, null); Deserialize(ref propValue, prim, usdTime, csProp, localVarMap, ref mayVary, usdNamespace); csProp.SetValue(t, propValue, index: null); if ((mayVary == null) != mayVaryWasNull) { throw new ApplicationException("Deserialize modified mayVary to be non-null"); } } for (int i = 0; i < fields.Length; i++) { FieldInfo csField = fields[i]; if (Reflect.IsNonSerialized(csField)) { continue; } if (accessMap != null && mayVary == null && !accessMap.Contains(csField)) { continue; } object fieldValue = csField.GetValue(t); Deserialize(ref fieldValue, prim, usdTime, csField, localVarMap, ref mayVary, usdNamespace); csField.SetValue(t, fieldValue); if ((mayVary == null) != mayVaryWasNull) { throw new ApplicationException("Deserialize modified mayVary to be non-null"); } } }