/// <summary>
        /// Writes SerializedProperty to USD, traversing all nested properties.
        /// </summary>
        static void PropertyToUsd(string path,
                                  string propPrefix,
                                  Scene scene,
                                  SerializedProperty prop,
                                  System.Text.StringBuilder sb)
        {
            string prefix = "";

            try
            {
                var nameStack = new List <string>();
                nameStack.Add("unity");
                if (!string.IsNullOrEmpty(propPrefix))
                {
                    nameStack.Add(propPrefix);
                }

                string lastName  = "";
                int    lastDepth = 0;

                while (prop.Next(prop.propertyType == SerializedPropertyType.Generic && !prop.isArray))
                {
                    string tabIn = "";
                    for (int i = 0; i < prop.depth; i++)
                    {
                        tabIn += "  ";
                    }

                    if (prop.depth > lastDepth)
                    {
                        Debug.Assert(lastName != "");
                        nameStack.Add(lastName);
                    }
                    else if (prop.depth < lastDepth)
                    {
                        nameStack.RemoveRange(nameStack.Count - (lastDepth - prop.depth), lastDepth - prop.depth);
                    }

                    lastDepth = prop.depth;
                    lastName  = prop.name;

                    if (nameStack.Count > 0)
                    {
                        prefix  = string.Join(":", nameStack.ToArray());
                        prefix += ":";
                    }
                    else
                    {
                        prefix = "";
                    }

                    sb.Append(tabIn + prefix + prop.name + "[" + prop.propertyType.ToString() + "] = ");
                    if (prop.isArray && prop.propertyType != SerializedPropertyType.String)
                    {
                        // TODO.
                        sb.AppendLine("ARRAY");
                    }
                    else if (prop.propertyType == SerializedPropertyType.Generic)
                    {
                        sb.AppendLine("Generic");
                    }
                    else if (prop.propertyType == SerializedPropertyType.AnimationCurve ||
                             prop.propertyType == SerializedPropertyType.Gradient)
                    {
                        // TODO.
                        sb.AppendLine(NativeSerialization.ValueToString(prop));
                    }
                    else
                    {
                        sb.AppendLine(NativeSerialization.ValueToString(prop));
                        var vtValue  = NativeSerialization.PropToVtValue(prop);
                        var primPath = new pxr.SdfPath(path);
                        var attrName = new pxr.TfToken(prefix + prop.name);

                        /*
                         * var oldPrim = context.prevScene.Stage.GetPrimAtPath(primPath);
                         * pxr.VtValue oldVtValue = null;
                         * if (oldPrim.IsValid()) {
                         * var oldAttr = oldPrim.GetAttribute(attrName);
                         * if (oldAttr.IsValid()) {
                         *  oldVtValue = oldAttr.Get(0);
                         * }
                         * }
                         *
                         * if (oldVtValue != null && vtValue == oldVtValue) {
                         * Debug.Log("skipping: " + prop.name);
                         * continue;
                         * }
                         */

                        var sdfType = NativeSerialization.GetSdfType(prop);
                        var prim    = scene.GetPrimAtPath(primPath);
                        var attr    = prim.CreateAttribute(attrName, sdfType);
                        attr.Set(vtValue);
                    }
                }
            }
            catch
            {
                Debug.LogWarning("Failed on: " + path + "." + prefix + prop.name);
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Constructs Unity SerializedProperties from USD.
        /// </summary>
        static void PropertyFromUsd(pxr.UsdPrim prim,
                                    SerializedProperty prop,
                                    System.Text.StringBuilder sb,
                                    string propPrefix)
        {
            if (prim == null)
            {
                Debug.LogError("Null prim - " + propPrefix);
            }

            if (!prim.IsValid())
            {
                Debug.LogError("Invalid prim: " + prim.GetPath().ToString());
            }

            string prefix = "";

            try
            {
                var nameStack = new List <string>();
                nameStack.Add("unity");
                if (!string.IsNullOrEmpty(propPrefix))
                {
                    nameStack.Add(propPrefix);
                }

                string lastName  = "";
                int    lastDepth = 0;

                while (prop.Next(prop.propertyType == SerializedPropertyType.Generic && !prop.isArray))
                {
                    string tabIn = "";
                    for (int i = 0; i < prop.depth; i++)
                    {
                        tabIn += "  ";
                    }

                    if (prop.depth > lastDepth)
                    {
                        Debug.Assert(lastName != "");
                        nameStack.Add(lastName);
                    }
                    else if (prop.depth < lastDepth)
                    {
                        nameStack.RemoveRange(nameStack.Count - (lastDepth - prop.depth), lastDepth - prop.depth);
                    }

                    lastDepth = prop.depth;
                    lastName  = prop.name;

                    if (nameStack.Count > 0)
                    {
                        prefix  = string.Join(":", nameStack.ToArray());
                        prefix += ":";
                    }
                    else
                    {
                        prefix = "";
                    }

                    sb.Append(tabIn + prefix + prop.name + "[" + prop.propertyType.ToString() + "] = ");
                    if (prop.isArray && prop.propertyType != SerializedPropertyType.String)
                    {
                        // TODO.
                        sb.AppendLine("ARRAY");
                    }
                    else if (prop.propertyType == SerializedPropertyType.Generic)
                    {
                        sb.AppendLine("Generic");
                    }
                    else if (prop.propertyType == SerializedPropertyType.AnimationCurve ||
                             prop.propertyType == SerializedPropertyType.Gradient)
                    {
                        // TODO.
                        sb.AppendLine(NativeSerialization.ValueToString(prop));
                    }
                    else
                    {
                        sb.AppendLine(NativeSerialization.ValueToString(prop));
                        var attrName = new pxr.TfToken(prefix + prop.name);
                        var attr     = prim.GetAttribute(attrName);

                        if (attr == null)
                        {
                            Debug.LogError("Null attr: " + prim.GetPath().ToString() + "." + attrName.ToString());
                        }

                        if (!attr.IsValid())
                        {
                            Debug.LogError("Attribute not found:" + attr.GetPath().ToString());
                        }

                        NativeSerialization.VtValueToProp(prop, attr.Get(0));
                    }
                }
            }
            catch
            {
                Debug.LogWarning("Failed on: " + prim.GetPath() + "." + prefix + prop.name);
                throw;
            }
        }