public StaticObject spawnInstance(StaticModel model)
        {
            StaticObject obj = new StaticObject();
            obj.gameObject = GameDatabase.Instance.GetModel(model.path + "/" + model.getSetting("mesh"));
            obj.setSetting("RadiusOffset", (float)FlightGlobals.ActiveVessel.altitude);
            obj.setSetting("CelestialBody", KerbalKonstructs.instance.getCurrentBody());
            obj.setSetting("Group", "Ungrouped");
            obj.setSetting("RadialPosition", KerbalKonstructs.instance.getCurrentBody().transform.InverseTransformPoint(FlightGlobals.ActiveVessel.transform.position));
            obj.setSetting("RotationAngle", 0f);
            obj.setSetting("Orientation", Vector3.up);
            obj.setSetting("VisibilityRange", 25000f);

            string sPad = ((string)model.getSetting("DefaultLaunchPadTransform"));
            if (sPad != null) obj.setSetting("LaunchPadTransform", sPad);

            if (!KerbalKonstructs.instance.DevMode)
            {
                obj.setSetting("CustomInstance", "True");
            }

            obj.model = model;

            KerbalKonstructs.instance.getStaticDB().addStatic(obj);
            enableColliders = false;
            obj.spawnObject(true, false);
            return obj;
        }
        public void loadInstances(ConfigNode confconfig, StaticModel model, bool bSecondPass = false)
        {
            if (model == null)
            {
                Debug.Log("KK: Attempting to loadInstances for a null model. Check your model and config.");
                return;
            }

            if (confconfig == null)
            {
                Debug.Log("KK: Attempting to loadInstances for a null ConfigNode. Check your model and config.");
                return;
            }

            foreach (ConfigNode ins in confconfig.GetNodes("Instances"))
            {
                StaticObject obj = new StaticObject();
                obj.model = model;

                obj.gameObject = GameDatabase.Instance.GetModel(model.path + "/" + model.getSetting("mesh"));

                if (obj.gameObject == null)
                {
                    Debug.Log("KK: Could not find " + model.getSetting("mesh") + ".mu! Did the modder forget to include it or did you actually install it?");
                    continue;
                }

                // Fix colliders
                if (!String.IsNullOrEmpty(model.getSetting("concaveColliders").ToString().Trim()))
                {
                    string value = model.getSetting("concaveColliders").ToString();
                    string[] names = value.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    MeshCollider[] colliders = obj.gameObject.GetComponentsInChildren<MeshCollider>(true);
                    MeshCollider[] concave = value.ToLower() == "all" ? colliders : colliders.Where(c => names.Contains(c.name)).ToArray();
                    foreach (MeshCollider collider in concave)
                    {
                        if (DebugMode) Debug.Log("KK: Making collider " + collider.name + " concave.");
                        collider.convex = false;
                    }
                }

                obj.settings = KKAPI.loadConfig(ins, KKAPI.getInstanceSettings());

                if (obj.settings == null)
                {
                    Debug.Log("KK: Error loading instances for " + model.getSetting("mesh") + ".mu! Check your model and config.");
                    continue;
                }

                if (bSecondPass)
                {
                    Vector3 secondInstanceKey = (Vector3)obj.getSetting("RadialPosition");
                    bool bSpaceOccupied = false;

                    foreach (StaticObject soThis in KerbalKonstructs.instance.getStaticDB().getAllStatics())
                    {
                        Vector3 firstInstanceKey = (Vector3)soThis.getSetting("RadialPosition");

                        if (firstInstanceKey == secondInstanceKey)
                        {
                            string sThisMesh = (string)soThis.model.getSetting("mesh");
                            string sThatMesh = (string)obj.model.getSetting("mesh");

                            if (DebugMode)
                                Debug.Log("KK: Custom instance has a RadialPosition that already has an instance."
                                + sThisMesh + ":"
                                + (string)soThis.getSetting("Group") + ":" + firstInstanceKey.ToString() + "|"
                                + sThatMesh + ":"
                                + (string)obj.getSetting("Group") + ":" + secondInstanceKey.ToString());

                            if (sThisMesh == sThatMesh)
                            {
                                float fThisOffset = (float)soThis.getSetting("RadiusOffset");
                                float fThatOffset = (float)obj.getSetting("RadiusOffset");
                                float fThisRotation = (float)soThis.getSetting("RotationAngle");
                                float fThatRotation = (float)obj.getSetting("RotationAngle");

                                if ((fThisOffset == fThatOffset) && (fThisRotation == fThatRotation))
                                {
                                    bSpaceOccupied = true;
                                    break;
                                }
                                else
                                {
                                    if (DebugMode) Debug.Log("KK: Different rotation or offset. Allowing. Could be a feature of the same model such as a doorway being used. Will cause z tearing probably.");
                                }
                            }
                            else
                            {
                                if (DebugMode) Debug.Log("KK: Different models. Allowing. Could be a terrain foundation or integrator.");
                            }
                        }
                    }

                    if (bSpaceOccupied)
                    {
                        Debug.Log("KK: Attempted to import identical custom instance to same RadialPosition as existing instance. Skipped. Check for duplicate custom statics you have installed. Did you export the custom instances to make a pack? If not, ask the mod-makers if they are duplicating the same stuff as each other.");
                        continue;
                    }
                }

                if (!obj.settings.ContainsKey("LaunchPadTransform") && obj.settings.ContainsKey("LaunchSiteName"))
                {
                    if (model.settings.Keys.Contains("DefaultLaunchPadTransform"))
                    {
                        obj.settings.Add("LaunchPadTransform", model.getSetting("DefaultLaunchPadTransform"));
                    }
                    else
                    {
                        Debug.Log("KK: Launch site is missing a transform. Defaulting to " + obj.getSetting("LaunchSiteName") + "_spawn...");

                        if (obj.gameObject.transform.Find(obj.getSetting("LaunchSiteName") + "_spawn") != null)
                        {
                            obj.settings.Add("LaunchPadTransform", obj.getSetting("LaunchSiteName") + "_spawn");
                        }
                        else
                        {
                            Debug.Log("KK: FAILED: " + obj.getSetting("LaunchSiteName") + "_spawn does not exist! Attempting to use any transform with _spawn in the name.");
                            Transform lastResort = obj.gameObject.transform.Cast<Transform>().FirstOrDefault(trans => trans.name.EndsWith("_spawn"));

                            if (lastResort != null)
                            {
                                Debug.Log("KK: Using " + lastResort.name + " as launchpad transform");
                                obj.settings.Add("LaunchPadTransform", lastResort.name);
                            }
                            else
                            {
                                Debug.Log("KK: All attempts at finding a launchpad transform have failed (╯°□°)╯︵ ┻━┻ This static isn't configured for KK properly. Tell the modder.");
                            }
                        }
                    }
                }

                staticDB.addStatic(obj);
                obj.spawnObject(false, false);

                if (obj.settings.ContainsKey("LaunchPadTransform") && obj.settings.ContainsKey("LaunchSiteName"))
                    LaunchSiteManager.createLaunchSite(obj);
            }
        }
        public void CreatePreviewInstance(StaticModel model)
        {
            StaticObject obj = new StaticObject();
            obj.gameObject = GameDatabase.Instance.GetModel(model.path + "/" + model.getSetting("mesh"));
            obj.setSetting("RadiusOffset", (float)FlightGlobals.ActiveVessel.altitude);
            obj.setSetting("CelestialBody", KerbalKonstructs.instance.getCurrentBody());
            obj.setSetting("Group", "Ungrouped");
            obj.setSetting("RadialPosition", KerbalKonstructs.instance.getCurrentBody().transform.InverseTransformPoint(FlightGlobals.ActiveVessel.transform.position));
            obj.setSetting("RotationAngle", 0f);
            obj.setSetting("Orientation", Vector3.up);
            obj.setSetting("VisibilityRange", 25000f);

            obj.model = model;

            KerbalKonstructs.instance.getStaticDB().addStatic(obj);
            obj.spawnObject(true, true);
            // KerbalKonstructs.instance.selectObject(obj, false);
            currPreview = obj;
        }