Search Unity

unitypackage to fbx

Discussion in 'Asset Importing & Exporting' started by toxxreport, Nov 21, 2017.

  1. toxxreport

    toxxreport

    Joined:
    Nov 9, 2017
    Posts:
    2
    Hi,
    I'm nor a unity guy... although I must say looks really tempting...just not at a place to learn yet another platform, and I don't make games.
    Anyway I have this model of a large dystopian city... and I've got all the individual models as fbx files, but there's a f_ton. there is also a demo in the unitypackage file. So I installed unity and got the demo to load, but seems if i want to export as fbx I need to buy an addon for that... if I have PC which I don't.

    Sooo, if there is a way I've overlooked be nice to know... otherwise... hoping to find someone who IS setup for this export I might perhaps work out a deal...(I have have a whole lot of stuff) to get this demo into an fbx.

    Hope I made sense
    Cheers!
    Tox
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
  3. toxxreport

    toxxreport

    Joined:
    Nov 9, 2017
    Posts:
    2
    yeah... tried that. ppu thescript in the editor folder, but under file menu there is not an option for export... so still at a loss
     
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Because the script is older than 3 years and one of the properties has been deprecated, causing a compile error.

    change line
    Code (CSharp):
    1. Material[] mats = mf.renderer.sharedMaterials;
    to
    Code (CSharp):
    1. Material[] mats = mf.GetComponent<Renderer>().sharedMaterials;
    This works for me (I get an OBJ file) but blender can't parse Exps (e.g. 9,536743E–07 ) and tries to convert string to float. :rolleyes:

    It may work in your software, though.

    Edit: maybe if I fix string formatting...
     
    Last edited: Nov 21, 2017
  5. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    I added
    Code (CSharp):
    1. .ToString("0.000000")
    every time a float is being appeded to output.
    ...
    And then I had to convert commas to dots becasue .net thinks I still have a culture. so instead of setting thread culture I just used replace becasue maybe some day invariant culture may also become culturally enriched and involve commas in float formatting :mad:
    ...
    And hypens/dashes/nminuses/mdashes to minuses. WTF, string.format... just WTF.

    It works in blender now. It looks like absolute... shoyte... But it works.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.IO;
    5. using System.Text;
    6.  
    7. public class ObjExporterScript
    8. {
    9.     private static int StartIndex = 0;
    10.  
    11.     public static void Start()
    12.     {
    13.         StartIndex = 0;
    14.     }
    15.     public static void End()
    16.     {
    17.         StartIndex = 0;
    18.     }
    19.  
    20.  
    21.     public static string MeshToString(MeshFilter mf, Transform t)
    22.     {
    23.         Vector3 s = t.localScale;
    24.         Vector3 p = t.localPosition;
    25.         Quaternion r = t.localRotation;
    26.  
    27.  
    28.         int numVertices = 0;
    29.         Mesh m = mf.sharedMesh;
    30.         if (!m)
    31.         {
    32.             return "####Error####";
    33.         }
    34.         //Material[] mats = mf.renderer.sharedMaterials;
    35.         Material[] mats = mf.GetComponent<Renderer>().sharedMaterials;
    36.  
    37.         StringBuilder sb = new StringBuilder();
    38.  
    39.         foreach (Vector3 vv in m.vertices)
    40.         {
    41.             Vector3 v = t.TransformPoint(vv);
    42.             numVertices++;
    43.             //sb.Append(string.Format("v {0} {1} {2}\n", v.x, v.y, -v.z));
    44.             sb.Append(
    45.                 string.Format(
    46.                     "v {0} {1} {2}\n",
    47.                     v.x.ToString("0.000000")
    48.                     .Replace(",", ".")
    49.                     .Replace("–", "-")
    50.                     .Replace("−", "-")
    51.                     .Replace("–", "-")
    52.                     .Replace("—", "-"),
    53.                     v.y.ToString("0.000000")
    54.                     .Replace(",", ".")
    55.                     .Replace("–", "-")
    56.                     .Replace("−", "-")
    57.                     .Replace("–", "-")
    58.                     .Replace("—", "-"),
    59.                     (-v.z).ToString("0.000000")
    60.                     .Replace(",", ".")
    61.                     .Replace("–", "-")
    62.                     .Replace("−", "-")
    63.                     .Replace("–", "-")
    64.                     .Replace("—", "-")
    65.                     ));
    66.         }
    67.         sb.Append("\n");
    68.         foreach (Vector3 nn in m.normals)
    69.         {
    70.             Vector3 v = r * nn;
    71.             //sb.Append(string.Format("vn {0} {1} {2}\n", -v.x, -v.y, v.z));
    72.             sb.Append(
    73.                 string.Format(
    74.                     "vn {0} {1} {2}\n",
    75.                     (-v.x).ToString("0.000000")
    76.                     .Replace(",", ".")
    77.                     .Replace("–", "-")
    78.                     .Replace("−", "-")
    79.                     .Replace("–", "-")
    80.                     .Replace("—", "-"),
    81.                     (-v.y).ToString("0.000000")
    82.                     .Replace(",", ".")
    83.                     .Replace("–", "-")
    84.                     .Replace("−", "-")
    85.                     .Replace("–", "-")
    86.                     .Replace("—", "-"),
    87.                     v.z.ToString("0.000000")
    88.                     .Replace(",", ".")
    89.                     .Replace("–", "-")
    90.                     .Replace("−", "-")
    91.                     .Replace("–", "-")
    92.                     .Replace("—", "-")
    93.                     ));
    94.         }
    95.         sb.Append("\n");
    96.         foreach (Vector3 v in m.uv)
    97.         {
    98.             //sb.Append(string.Format("vt {0} {1}\n", v.x, v.y));
    99.             sb.Append(string.Format(
    100.                 "vt {0} {1}\n",
    101.                 v.x.ToString("0.000000")
    102.                 .Replace(",", ".")
    103.                 .Replace("–", "-")
    104.                 .Replace("−", "-")
    105.                 .Replace("–", "-")
    106.                 .Replace("—", "-"),
    107.                 v.y.ToString("0.000000")
    108.                 .Replace(",", ".")
    109.                 .Replace("–", "-")
    110.                 .Replace("−", "-")
    111.                 .Replace("–", "-")
    112.                 .Replace("—", "-")
    113.                 ));
    114.         }
    115.         for (int material = 0; material < m.subMeshCount; material++)
    116.         {
    117.             sb.Append("\n");
    118.             sb.Append("usemtl ").Append(mats[material].name).Append("\n");
    119.             sb.Append("usemap ").Append(mats[material].name).Append("\n");
    120.  
    121.             int[] triangles = m.GetTriangles(material);
    122.             for (int i = 0; i < triangles.Length; i += 3)
    123.             {
    124.                 sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
    125.                     triangles[i] + 1 + StartIndex, triangles[i + 1] + 1 + StartIndex, triangles[i + 2] + 1 + StartIndex));
    126.             }
    127.         }
    128.  
    129.         StartIndex += numVertices;
    130.         return sb.ToString();
    131.     }
    132. }
    133.  
    134. public class ObjExporter : ScriptableObject
    135. {
    136.     [MenuItem("File/Export/Wavefront OBJ")]
    137.     static void DoExportWSubmeshes()
    138.     {
    139.         DoExport(true);
    140.     }
    141.  
    142.     [MenuItem("File/Export/Wavefront OBJ (No Submeshes)")]
    143.     static void DoExportWOSubmeshes()
    144.     {
    145.         DoExport(false);
    146.     }
    147.  
    148.  
    149.     static void DoExport(bool makeSubmeshes)
    150.     {
    151.         if (Selection.gameObjects.Length == 0)
    152.         {
    153.             Debug.Log("Didn't Export Any Meshes; Nothing was selected!");
    154.             return;
    155.         }
    156.  
    157.         string meshName = Selection.gameObjects[0].name;
    158.         string fileName = EditorUtility.SaveFilePanel("Export .obj file", "", meshName, "obj");
    159.  
    160.         ObjExporterScript.Start();
    161.  
    162.         StringBuilder meshString = new StringBuilder();
    163.  
    164.         meshString.Append("#" + meshName + ".obj"
    165.                             + "\n#" + System.DateTime.Now.ToLongDateString()
    166.                             + "\n#" + System.DateTime.Now.ToLongTimeString()
    167.                             + "\n#-------"
    168.                             + "\n\n");
    169.  
    170.         Transform t = Selection.gameObjects[0].transform;
    171.  
    172.         Vector3 originalPosition = t.position;
    173.         t.position = Vector3.zero;
    174.  
    175.         if (!makeSubmeshes)
    176.         {
    177.             meshString.Append("g ").Append(t.name).Append("\n");
    178.         }
    179.         meshString.Append(processTransform(t, makeSubmeshes));
    180.  
    181.         WriteToFile(meshString.ToString(), fileName);
    182.  
    183.         t.position = originalPosition;
    184.  
    185.         ObjExporterScript.End();
    186.         Debug.Log("Exported Mesh: " + fileName);
    187.     }
    188.  
    189.     static string processTransform(Transform t, bool makeSubmeshes)
    190.     {
    191.         StringBuilder meshString = new StringBuilder();
    192.  
    193.         meshString.Append("#" + t.name
    194.                         + "\n#-------"
    195.                         + "\n");
    196.  
    197.         if (makeSubmeshes)
    198.         {
    199.             meshString.Append("g ").Append(t.name).Append("\n");
    200.         }
    201.  
    202.         MeshFilter mf = t.GetComponent<MeshFilter>();
    203.         if (mf)
    204.         {
    205.             meshString.Append(ObjExporterScript.MeshToString(mf, t));
    206.         }
    207.  
    208.         for (int i = 0; i < t.childCount; i++)
    209.         {
    210.             meshString.Append(processTransform(t.GetChild(i), makeSubmeshes));
    211.         }
    212.  
    213.         return meshString.ToString();
    214.     }
    215.  
    216.     static void WriteToFile(string s, string filename)
    217.     {
    218.         using (StreamWriter sw = new StreamWriter(filename))
    219.         {
    220.             sw.Write(s);
    221.         }
    222.     }
    223. }
     
    Celtc likes this.
  6. rinehartx44

    rinehartx44

    Joined:
    Jun 8, 2023
    Posts:
    1
    i'm looking for an opposite way how to do it.. Fbx file to unitypackage..
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    479