Exemplo n.º 1
0
        public void SaveToData(IINode dataNode = null)
        {
            dataNode = dataNode ?? Loader.Core.RootNode;
            // ' ' and '=' are not allowed by max, ';' is our data separator
            if (name.Contains(' ') || name.Contains('=') || name.Contains(s_PropertySeparator))
            {
                throw new FormatException("Invalid character(s) in animation Name: " + name + ". Spaces, equal signs and the separator '" + s_PropertySeparator + "' are not allowed.");
            }

            SerializableAnimationGroup serializableAnimation = new SerializableAnimationGroup();

            serializableAnimation.GetDataToSerialize(this);

            string serializedInfo = JsonConvert.SerializeObject(serializableAnimation);

            dataNode.SetStringProperty(GetPropertyName(), serializedInfo);

            IsDirty = false;
        }
Exemplo n.º 2
0
        public void LoadFromData(string propertyName, IINode dataNode, Dictionary <string, string> rootNodePropDictionary = null)
        {
            if (!Guid.TryParse(propertyName, out serializedId))
            {
                throw new Exception("Invalid ID, can't deserialize.");
            }


            string propertiesString = string.Empty;

            if (rootNodePropDictionary == null)
            {
                if (!dataNode.GetUserPropString(propertyName, ref propertiesString))
                {
                    return;
                }
            }
            else
            {
                if (!rootNodePropDictionary.TryGetValue(propertyName, out propertiesString))
                {
                    return;
                }
            }


            int numFailed = 0;

            try // Try using the new way, if it's not working use the old way.
            {
                SerializableAnimationGroup serialAnimGroup = new SerializableAnimationGroup(propertiesString);
                serialAnimGroup.FillSerializedData(this);
            }
            catch
            {
                int indexOfguidPart = propertiesString
                                      .Select((c, i) => new { c, i })
                                      .Where(x => x.c == s_PropertySeparator)
                                      .Skip(2)
                                      .FirstOrDefault().i;
                string[] baseProperties = propertiesString.Substring(0, indexOfguidPart)?.Split(s_PropertySeparator);
                string   guidPart       = propertiesString.Substring(indexOfguidPart + 1);

                if (baseProperties.Length != 3)
                {
                    throw new Exception("Invalid number of properties, can't deserialize.");
                }

                // set dirty explicitly just before we start loading, set to false when loading is done
                // if any exception is thrown, it will have a correct value
                IsDirty = true;

                name = baseProperties[0];
                if (!int.TryParse(baseProperties[1], out ticksStart))
                {
                    throw new Exception("Failed to parse FrameStart property.");
                }
                if (!int.TryParse(baseProperties[2], out ticksEnd))
                {
                    throw new Exception("Failed to parse FrameEnd property.");
                }

                if (string.IsNullOrEmpty(guidPart) || guidPart == s_GUIDTypeSeparator.ToString())
                {
                    return;
                }


                if (!guidPart.Contains(s_GUIDTypeSeparator))
                {
                    // to grant retro-compatiblity
                    numFailed = ParseOldProperties(guidPart);
                }
                else
                {
                    //new format with nodes and node materials guid
                    numFailed = ParseNewProperties(guidPart);
                }
            }


            AnimationGroupNodes = new List <AnimationGroupNode>();
            foreach (Guid nodeGuid in nodeGuids)
            {
                IINode node = Tools.GetINodeByGuid(nodeGuid);

                if (node != null)
                {
                    string             name       = node.Name;
                    string             parentName = node.ParentNode.Name;
                    AnimationGroupNode nodeData   = new AnimationGroupNode(nodeGuid, name, parentName);
                    AnimationGroupNodes.Add(nodeData);
                }
            }

            AnimationGroupMaterials = new List <AnimationGroupMaterial>();
            foreach (Guid materialGUID in materialsGuids)
            {
                IMtl mat = Tools.GetIMtlByGuid(materialGUID);

                if (mat != null)
                {
                    string name = mat.Name;
                    AnimationGroupMaterial matData = new AnimationGroupMaterial(materialGUID, name);
                    AnimationGroupMaterials.Add(matData);
                }
            }

            if (numFailed > 0)
            {
                throw new Exception(string.Format("Failed to parse {0} node ids.", numFailed));
            }

            IsDirty = false;
        }
Exemplo n.º 3
0
        private static void ResolveMultipleInheritedContainer(IIContainerObject container)
        {
            //resolve container name
            int b = 0;

            if (container.ContainerNode.GetUserPropBool("flightsim_resolved", ref b) && b != 0)
            {
                return;
            }

            List <IINode> containerHierarchy = new List <IINode>()
            {
            };

            containerHierarchy.AddRange(container.ContainerNode.ContainerNodeTree(false));

            string helperPropBuffer = string.Empty;

            container.BabylonContainerHelper().GetUserPropBuffer(ref helperPropBuffer);
            int containerID = 1;

            container.ContainerNode.GetUserPropInt("babylonjs_ContainerID", ref containerID);
            container.ContainerNode.Name = containerID == -1 ? $"{container.ContainerNode.Name}" : $"{container.ContainerNode.Name}_{containerID}";
            // resolve nodes , adding an id at the end
            {
                //manage multiple containers inherited from the same source
                foreach (IINode n in containerHierarchy)
                {
                    if (n.IsBabylonContainerHelper())
                    {
                        continue;
                    }
                    //change the guid of the node
                    //replace the guid in the babylon helper
                    string oldGuid = n.GetStringProperty("babylonjs_GUID", Guid.NewGuid().ToString());
                    n.DeleteProperty("babylonjs_GUID");
                    Guid newGuid = n.GetGuid();
                    helperPropBuffer = helperPropBuffer.Replace(oldGuid, newGuid.ToString());

                    n.Name = containerID == -1 ? $"{n.Name}" : $"{n.Name}_{containerID}";
                    if (n.Mtl != null && FlightSimMaterialUtilities.HasFlightSimMaterials(n.Mtl) && FlightSimMaterialUtilities.HasRuntimeAccess(n.Mtl))
                    {
                        if (n.Mtl.IsMultiMtl)
                        {
                            throw new Exception($@"Material {n.Mtl.Name} has a property ""Unique In Container"" enabled, cannot be child of a multi material");
                        }
                        else
                        {
                            string cmd = $"mNode = maxOps.getNodeByHandle {n.Handle} \r\n" +
                                         $"newMat = copy mNode.material \r\n" +
                                         $"newMat.name = \"{n.Mtl.Name}_{containerID}\" \r\n" +
                                         $"mNode.material = newMat";

                            ScriptsUtilities.ExecuteMaxScriptCommand(cmd);
                        }
                    }
                }
            }



            //replace animationList guid to have distinct list of AnimationGroup for each container
            string animationListStr = string.Empty;
            IINode containerHelper  = container.BabylonContainerHelper();


            containerHelper.GetUserPropString(s_AnimationListPropertyName, ref animationListStr);
            if (!string.IsNullOrEmpty(animationListStr))
            {
                string[] animationGroupGuid = animationListStr.Split(AnimationGroup.s_PropertySeparator);
                foreach (string guidStr in animationGroupGuid)
                {
                    Guid newAnimGroupGuid = Guid.NewGuid();
                    helperPropBuffer = helperPropBuffer.Replace(guidStr, newAnimGroupGuid.ToString());
                }

                containerHelper.SetUserPropBuffer(helperPropBuffer);

                //add ID of container to animationGroup name to identify animation in viewer
                containerHelper.GetUserPropString(s_AnimationListPropertyName, ref animationListStr);
                string[] newAnimationGroupGuid = animationListStr.Split(AnimationGroup.s_PropertySeparator);

                foreach (string guidStr in newAnimationGroupGuid)
                {
                    string propertiesString = string.Empty;
                    if (!containerHelper.GetUserPropString(guidStr, ref propertiesString))
                    {
                        return;
                    }

                    try // new serialization method
                    {
                        SerializableAnimationGroup serialAnimGroup = new SerializableAnimationGroup(propertiesString);
                        serialAnimGroup.name = containerID == -1 ? serialAnimGroup.name : serialAnimGroup.name + $"_{containerID}";
                        string serializedInfo = JsonConvert.SerializeObject(serialAnimGroup);
                        container.BabylonContainerHelper().SetUserPropString(guidStr, serializedInfo);
                    }
                    catch (Exception)
                    {
                        string[] properties = propertiesString.Split(AnimationGroup.s_PropertySeparator);
                        if (properties.Length < 4)
                        {
                            throw new Exception($"Invalid number of properties, can't de-serialize property of {containerHelper.Name} of {container.ContainerNode.Name}.");
                        }

                        string name = properties[0];
                        if (!string.IsNullOrEmpty(name))
                        {
                            propertiesString = propertiesString.Replace(name, name + "_" + containerID);
                            container.BabylonContainerHelper().SetUserPropString(guidStr, propertiesString);
                        }
                    }
                }
            }
            container.ContainerNode.SetUserPropBool("flightsim_resolved", true);
        }