private static void TryConnectToProject(JSONNode jsonData) { JSONClass projectInfo = jsonData as JSONClass; string projectUrl = new System.Uri(projectInfo["projectUrl"].Value).AbsoluteUri; int instanceId = projectInfo["linkIdentifier"].AsInt; GameObject gameObject = EditorUtility.InstanceIDToObject(instanceId) as GameObject; // Check if the game object exists if (!gameObject) { return; } MeshInfo[] meshesInfo = MaterialsManipulation.MeshesInfoFromGameObject(gameObject); // Check the game object only matches one mesh info if (meshesInfo.Length != 1) { return; } MeshInfo meshInfo = meshesInfo[0]; // Check that the project url matches string substancePainterProjectPath = GetSpProjectPath(meshInfo); string gameObjectProjectUrl = new System.Uri(substancePainterProjectPath).AbsoluteUri; if (projectUrl != gameObjectProjectUrl) { return; } // If it matches; link both applications SendMeshToSP(meshInfo); }
private static string GetSpProjectPath(MeshInfo meshInfo) { string meshName = Path.GetFileNameWithoutExtension(meshInfo.AssetPath); string meshPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Application.dataPath), meshInfo.AssetPath).ToString()); string folderPath = Path.GetDirectoryName(meshPath); // Put Substance Painter projects in a folder name started with '.' to hide it from the Unity asset browser return(Path.Combine(Path.Combine(folderPath, ".sp"), meshName + ".spp")); }
private static JSONNode GetSpDataLinkToSend(MeshInfo meshInfo, string substancePainterProjectPath) { // Prepare data to send to SP string workspacePath = Path.GetDirectoryName(Application.dataPath); string meshPath = Path.GetFullPath(Path.Combine(workspacePath, meshInfo.AssetPath).ToString()); string exportPath = Path.Combine(Path.Combine("Assets", "SP_Textures"), Path.GetFileNameWithoutExtension(meshInfo.AssetPath)).ToString(); string meshUri = new System.Uri(meshPath).AbsoluteUri; JSONClass materialsLink = new JSONClass(); foreach (Material material in meshInfo.Materials) { ShaderInfos shaderInfos = ShadersInfos.GetShaderInfos(material.shader); JSONClass propertiesAssociation = new JSONClass(); foreach (string propertyName in shaderInfos.PropertiesAssociation.Keys) { propertiesAssociation.Add(propertyName, new JSONData(shaderInfos.PropertiesAssociation[propertyName])); } JSONClass materialLink = new JSONClass(); materialLink.Add("assetPath", AssetDatabase.GetAssetPath(material)); materialLink.Add("exportPreset", shaderInfos.ExportPreset); materialLink.Add("resourceShader", shaderInfos.ResourceShader); materialLink.Add("spToLiveLinkProperties", propertiesAssociation); // HACK: Sanitize the name the same way SP internally do it string sanitizedName = MaterialsManipulation.SanitizeMaterialName(material.name); materialsLink.Add(sanitizedName, materialLink); } JSONClass project = new JSONClass(); project.Add("meshUrl", new JSONData(meshUri)); project.Add("normal", new JSONData("OpenGL")); project.Add("template", new JSONData("")); project.Add("url", new JSONData(new System.Uri(substancePainterProjectPath).AbsoluteUri)); JSONClass jsonData = new JSONClass(); jsonData.Add("applicationName", new JSONData("Unity")); jsonData.Add("exportPath", new JSONData(exportPath)); jsonData.Add("workspacePath", new JSONData(workspacePath)); jsonData.Add("materials", materialsLink); jsonData.Add("project", project); jsonData.Add("linkIdentifier", new JSONData(meshInfo.Identifier)); return(jsonData); }
private static void SendMeshToSP(MeshInfo meshInfo) { if (!webSocket.IsAlive) { ConnectSocket(); } if (!webSocket.IsAlive || !webSocket.Ping()) { EditorUtility.DisplayDialog( "Send to Substance Painter", "Substance Painter is not detected.\n" + "Please check if Substance Painter is correctly started and if the \"unity-link\" plugin is enabled.", "OK"); return; } // Ensure compatibility foreach (Material material in meshInfo.Materials) { if (ShadersInfos.ContainsShader(material.shader)) { ShadersInfos.GetShaderInfos(material.shader).EnsureMaterialCompatibility(material); } } // Check if a project already exist string substancePainterProjectPath = GetSpProjectPath(meshInfo); if (File.Exists(substancePainterProjectPath)) { // If the project exists, open it then reimport the mesh Debug.Log(string.Format("Reopening Substance Painter project located at {0}", substancePainterProjectPath)); OpenSpProject(meshInfo, substancePainterProjectPath); } else { // If the project doesn't exist, create a new one and save it here Debug.Log(string.Format("Creating a new Substance Painter project located at {0}", substancePainterProjectPath)); CreateSpProject(meshInfo, substancePainterProjectPath); } }
private static void CreateSpProject(MeshInfo meshInfo, string substancePainterProjectPath) { JSONNode jsonData = GetSpDataLinkToSend(meshInfo, substancePainterProjectPath); webSocket.Send(string.Format("{0} {1}", "CREATE_PROJECT", jsonData.ToString())); }