Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Scripted Importer Crashing Editor

Discussion in 'Editor Workflows' started by NumT, Apr 29, 2019.

  1. NumT

    NumT

    Joined:
    Feb 13, 2014
    Posts:
    22
    Hello community!

    I've attempted to write a very simple scripted importer to get splines out of 3ds Max and convert them into Linerenderers on Gameobjects on import. Everything was working fine but the resulting Linerenderers seemed to frequently lose the reference to the material I applied to them.

    So I thought I would script and editor that would allow me to pass in a material reference as an override. Unfortunately this is where the crashing has started. Weirdly, after the crash if I reopen the project the change has been made to the asset... its just that I still get the crash. Could anyone advise as to what I might be doing wrong here? The ScriptedImporter examples are a bit light on the ground.

    Here's the code:

    SplinesImporter.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor.Experimental.AssetImporters;
    3. using System.IO;
    4. using System.Collections.Generic;
    5.  
    6. [ScriptedImporter(1, "splines")]
    7. public class SplinesImporter : ScriptedImporter
    8. {
    9.     public Material m_Material;
    10.  
    11.     private class Spline
    12.     {
    13.         public string name;
    14.         public Vector3[] knots;
    15.     }
    16.  
    17.     public override void OnImportAsset(AssetImportContext ctx)
    18.     {
    19.         // setup material
    20.        if (m_Material == null) m_Material = new Material(Shader.Find("Standard"));
    21.  
    22.         GameObject mainObj = new GameObject();
    23.         mainObj.transform.position = Vector3.zero;
    24.         mainObj.transform.rotation = Quaternion.Euler(90, 0, 0);
    25.  
    26.         ctx.AddObjectToAsset("main", mainObj);
    27.         ctx.SetMainObject(mainObj);
    28.  
    29.         ctx.AddObjectToAsset("material", m_Material);
    30.  
    31.         Spline[] splines = ReadData(ctx.assetPath);
    32.  
    33.         for (int i = 0; i < splines.Length; i++)
    34.         {
    35.             GameObject currentSpline = new GameObject();
    36.             currentSpline.transform.position = Vector3.zero;
    37.             currentSpline.transform.rotation = Quaternion.Euler(90, 0, 0);
    38.             currentSpline.transform.parent = mainObj.transform;
    39.             currentSpline.name = splines[i].name;
    40.             // setup line renderer
    41.             LineRenderer line = currentSpline.AddComponent<LineRenderer>();
    42.             line.positionCount = splines[i].knots.Length;
    43.             line.SetPositions(splines[i].knots);
    44.             line.alignment = LineAlignment.TransformZ;
    45.             line.generateLightingData = true;
    46.             line.textureMode = LineTextureMode.Tile;
    47.             line.material = m_Material;
    48.             ctx.AddObjectToAsset(splines[i].name, currentSpline);
    49.         }
    50.     }
    51.  
    52.     // Parses data and returns an array of splines
    53.     private Spline[] ReadData(string file)
    54.     {
    55.         List<Spline> splines = new List<Spline>();
    56.  
    57.         try
    58.         {
    59.             using (StreamReader sr = new StreamReader(file))
    60.             {
    61.                 int counter = 0;
    62.                 string line;
    63.                 while (!sr.EndOfStream)
    64.                 {
    65.                     Spline spline = new Spline();
    66.                     spline.name = "Spline" + counter;
    67.                     line = sr.ReadLine();
    68.                     string[] knotStrings = line.Split(']');
    69.                     spline.knots = new Vector3[knotStrings.Length - 1];
    70.                     for (int i = 0; i < knotStrings.Length - 1; i++)
    71.                     {
    72.                         // remove '[' and split
    73.                         string[] posStrings = knotStrings[i].Substring(1, knotStrings[i].Length - 1).Split(',');
    74.                         // parse values and flip y and z coords (3ds Max)
    75.                         Vector3 pos = new Vector3(float.Parse(posStrings[0]), float.Parse(posStrings[2]), float.Parse(posStrings[1]));
    76.                         spline.knots[i] = pos;
    77.                     }
    78.                     // add spline to list
    79.                     splines.Add(spline);
    80.                     // increment spline counter
    81.                     counter++;
    82.                 }
    83.                 return splines.ToArray();
    84.             }
    85.         }
    86.         catch (System.Exception)
    87.         {
    88.  
    89.             throw;
    90.         }
    91.  
    92.         throw new System.NotImplementedException();
    93.     }
    94. }
    95.  
    SplinesImporterEditor.cs

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.Experimental.AssetImporters;
    3. using UnityEditor.SceneManagement;
    4. using UnityEngine;
    5.  
    6. [CustomEditor(typeof(SplinesImporter))]
    7. public class SplinesImporterEditor : ScriptedImporterEditor
    8. {
    9.     public override void OnInspectorGUI()
    10.     {
    11.         var content = new GUIContent("Material");
    12.         var mat = serializedObject.FindProperty("m_Material");
    13.         EditorGUILayout.PropertyField(mat, content);
    14.         base.ApplyRevertGUI();
    15.     }
    16. }
    17.  
    Example output.splines
    [-209.746,-175.853,227.941][-206.404,-170.597,227.907][-203.057,-165.312,227.874][-199.715,-160.033,227.84][-196.375,-154.765,227.807][-193.021,-149.504,227.773][-189.658,-144.27,227.74][-187.893,-141.551,227.722][-185.018,-137.207,227.694][-182.079,-132.881,227.665][-179.111,-128.624,227.637][-176.098,-124.398,227.609][-173.038,-120.196,227.581][-169.924,-116.003,227.553][-166.848,-111.939,227.526][-163.735,-107.89,227.499][-160.577,-103.84,227.472][-157.373,-99.7834,227.445][-154.113,-95.703,227.417][-150.816,-91.6156,227.39][-148.805,-89.1521,227.373][-144.789,-84.3564,227.34][-140.702,-79.6517,227.307][-136.602,-75.0753,227.273][-132.459,-70.5489,227.24][-128.269,-66.0395,227.206][-124.052,-61.5349,227.172]
    [-129.009,-56.8892,227.172][-133.231,-61.3999,227.206][-137.448,-65.9387,227.24][-141.641,-70.5193,227.273][-145.81,-75.1732,227.307][-149.939,-79.9257,227.34][-154.049,-84.8332,227.373][-156.096,-87.3409,227.39][-159.414,-91.4547,227.417][-162.686,-95.5504,227.445][-165.917,-99.6416,227.472][-169.107,-103.731,227.499][-172.255,-107.826,227.526][-175.359,-111.927,227.553][-178.504,-116.162,227.581][-181.608,-120.424,227.609][-184.668,-124.716,227.637][-187.686,-129.045,227.665][-190.651,-133.409,227.694][-193.578,-137.832,227.722][-195.366,-140.587,227.74][-198.746,-145.847,227.773][-202.106,-151.118,227.807][-205.455,-156.398,227.84][-208.797,-161.679,227.874][-212.139,-166.956,227.907][-215.477,-172.204,227.941]
    [-131.487,-54.5663,227.172][-135.712,-59.0801,227.206][-139.943,-63.6335,227.24][-144.161,-68.2413,227.273][-148.364,-72.934,227.307][-152.514,-77.7104,227.34][-156.671,-82.6738,227.373][-158.736,-85.2036,227.39][-162.065,-89.3306,227.417][-165.343,-93.4338,227.445][-168.588,-97.5422,227.472][-171.793,-101.652,227.499][-174.958,-105.769,227.526][-178.076,-109.889,227.553][-181.236,-114.144,227.581][-184.363,-118.437,227.609][-187.447,-122.762,227.637][-190.489,-127.127,227.665][-193.467,-131.51,227.694][-196.42,-135.973,227.722][-198.22,-138.745,227.74][-201.609,-144.019,227.773][-204.972,-149.294,227.807][-208.324,-154.581,227.84][-211.667,-159.863,227.874][-215.007,-165.136,227.907][-218.342,-170.38,227.941]
    [-133.531,-23.1702,228.252][-139.882,-27.6962,228.598][-144.82,-31.2413,228.873][-149.14,-34.3873,229.12][-154.425,-38.3025,229.433][-161.02,-43.2666,229.827][-168.074,-48.6456,230.249][-175.37,-54.2859,230.689][-182.577,-59.9837,231.128][-188.323,-64.6261,231.48][-189.76,-65.7867,231.568][-193.322,-68.7139,231.787][-196.884,-71.6411,232.007][-203.934,-77.5314,232.446][-210.947,-83.5239,232.886][-217.915,-89.5669,233.325][-224.767,-95.5945,233.76][-231.01,-101.183,234.161][-235.508,-105.265,234.45][-239.821,-109.247,234.729][-245.105,-114.186,235.075][-251.421,-120.239,235.492][-257.846,-126.492,235.919][-264.091,-132.656,236.337][-269.223,-137.818,236.685][-272.667,-141.344,236.919][-274.973,-143.738,237.076][-276.226,-145.069,237.163]
    [-137.768,-17.2253,228.252][-144.119,-21.7514,228.598][-149.098,-25.3268,228.873][-153.485,-28.5213,229.12][-158.77,-32.4365,229.433][-165.417,-37.4395,229.827][-172.535,-42.8672,230.249][-179.897,-48.5593,230.689][-187.104,-54.2573,231.128][-192.923,-58.9573,231.48][-194.377,-60.1323,231.568][-197.971,-63.0856,231.787][-201.564,-66.039,232.007][-208.614,-71.9291,232.446][-215.697,-77.9807,232.886][-222.731,-84.0808,233.325][-229.636,-90.1554,233.76][-235.879,-95.7436,234.161][-240.437,-99.8806,234.45][-244.806,-103.914,234.729][-250.089,-108.853,235.075][-256.475,-114.972,235.492][-262.966,-121.289,235.919][-269.268,-127.509,236.337][-274.4,-132.672,236.685][-277.91,-136.264,236.919][-280.287,-138.733,237.076][-281.541,-140.065,237.163]
    [-142.818,-9.6385,228.788][-149.377,-13.7379,229.11][-154.537,-16.9683,229.381][-159.215,-19.951,229.623][-164.745,-23.5087,229.894][-171.816,-28.1164,230.225][-179.384,-33.1335,230.583][-187.198,-38.4101,230.957][-194.777,-43.601,231.324][-200.965,-47.801,231.615][-202.513,-48.851,231.688][-206.411,-51.4123,231.869][-210.308,-53.9736,232.05][-218.497,-59.2945,232.422][-225.082,-63.5871,232.696][-231.68,-67.8698,232.947][-238.29,-72.1652,233.175][-244.895,-76.4656,233.373][-251.491,-80.755,233.546][-258.097,-85.0619,233.689][-264.694,-89.3702,233.802][-271.267,-93.6592,233.894][-277.868,-97.966,233.959][-284.469,-102.273,234.004][-290.916,-106.465,234.026][-296.847,-110.339,234.025][-300.149,-112.491,234.017]
    [-135.649,-20.1978,228.252][-142,-24.7238,228.598][-146.959,-28.2841,228.873][-151.312,-31.4543,229.12][-156.598,-35.3695,229.433][-163.219,-40.3531,229.827][-170.304,-45.7564,230.249][-177.633,-51.4226,230.689][-184.84,-57.1205,231.128][-190.623,-61.7917,231.48][-192.069,-62.9595,231.568][-195.646,-65.8998,231.787][-199.224,-68.8401,232.007][-206.274,-74.7302,232.446][-213.322,-80.7523,232.886][-220.323,-86.8239,233.325][-227.202,-92.875,233.76][-233.444,-98.4632,234.161][-237.973,-102.573,234.45][-242.314,-106.581,234.729][-247.597,-111.52,235.075][-253.948,-117.605,235.492][-260.406,-123.89,235.919][-266.679,-130.083,236.337][-271.811,-135.245,236.685][-275.289,-138.804,236.919][-277.63,-141.236,237.076][-278.884,-142.567,237.163]
    [-269.199,-151.683,237.163][-267.948,-150.354,237.076][-265.736,-148.058,236.919][-262.379,-144.622,236.685][-257.247,-139.459,236.337][-251.078,-133.371,235.919][-244.74,-127.202,235.492][-238.515,-121.236,235.075][-233.231,-116.297,234.729][-228.992,-112.383,234.45][-224.573,-108.373,234.161][-218.331,-102.785,233.76][-211.548,-96.819,233.325][-204.668,-90.8516,232.886][-197.747,-84.9371,232.446][-190.697,-79.0467,232.007][-187.177,-76.154,231.787][-183.656,-73.2612,231.568][-182.244,-72.1197,231.48][-176.592,-67.5536,231.128][-169.385,-61.8559,230.689][-162.177,-56.2843,230.249][-155.208,-50.9696,229.827][-148.681,-46.0569,229.433][-143.396,-42.1417,229.12][-139.163,-39.0598,228.873][-134.282,-35.5547,228.598][-127.931,-31.0288,228.252]
    [-297.177,-117.047,234.017][-293.878,-114.897,234.025][-287.936,-111.016,234.026][-281.496,-106.829,234.004][-274.896,-102.522,233.959][-268.295,-98.2158,233.894][-261.716,-93.9228,233.802][-255.125,-89.6178,233.689][-248.529,-85.3178,233.546][-241.922,-81.0219,233.371][-235.318,-76.7211,233.164][-228.715,-72.4297,232.921][-222.11,-68.1427,232.651][-215.524,-63.8482,232.353][-207.334,-58.5249,231.948][-203.411,-55.9467,231.749][-199.488,-53.3684,231.551][-197.928,-52.3092,231.471][-191.687,-48.0724,231.151][-184.14,-42.9026,230.749][-176.362,-37.6485,230.341][-168.841,-32.6612,229.954][-161.803,-28.074,229.6][-156.307,-24.5377,229.314][-151.667,-21.5783,229.062][-146.509,-18.3485,228.785][-139.844,-14.1821,228.459]
    [-266.542,-154.185,237.163][-265.29,-152.857,237.076][-263.115,-150.598,236.919][-259.791,-147.195,236.685][-254.658,-142.033,236.337][-248.518,-135.972,235.919][-242.213,-129.836,235.492][-236.023,-123.902,235.075][-230.739,-118.964,234.729][-226.527,-115.075,234.45][-222.139,-111.092,234.161][-215.896,-105.504,233.76][-209.14,-99.562,233.325][-202.293,-93.6232,232.886][-195.407,-87.7383,232.446][-188.357,-81.8478,232.007][-184.852,-78.9681,231.787][-181.348,-76.0884,231.568][-179.944,-74.9541,231.48][-174.328,-70.4169,231.128][-167.121,-64.7192,230.689][-159.946,-59.1735,230.249][-153.009,-53.8832,229.827][-146.509,-48.9899,229.433][-141.223,-45.0748,229.12][-137.024,-42.0171,228.873][-132.163,-38.5271,228.598][-125.812,-34.0012,228.252]
    [-271.857,-149.181,237.163][-270.605,-147.852,237.076][-268.358,-145.518,236.919][-264.968,-142.049,236.685][-259.835,-136.886,236.337][-253.638,-130.769,235.919][-247.267,-124.569,235.492][-241.007,-118.569,235.075][-235.724,-113.631,234.729][-231.457,-109.691,234.45][-227.008,-105.653,234.161][-220.765,-100.065,233.76][-213.956,-94.076,233.325][-207.043,-88.08,232.886][-200.088,-82.136,232.446][-193.037,-76.2457,232.007][-189.501,-73.3398,231.787][-185.965,-70.4341,231.568][-184.543,-69.2853,231.48][-178.856,-64.6904,231.128][-171.648,-58.9926,230.689][-164.407,-53.395,230.249][-157.406,-48.056,229.827][-150.854,-43.1239,229.433][-145.569,-39.2087,229.12][-141.303,-36.1026,228.873][-136.4,-32.5823,228.598][-130.049,-28.0564,228.252]
    [-298.663,-114.769,234.017][-295.362,-112.618,234.025][-289.426,-108.741,234.026][-282.983,-104.551,234.004][-276.382,-100.244,233.959][-269.781,-95.9375,233.894][-263.205,-91.6465,233.802][-256.611,-87.3399,233.689][-250.01,-83.0364,233.546][-243.409,-78.7438,233.372][-236.804,-74.4431,233.17][-230.198,-70.1498,232.934][-223.596,-65.8649,232.674][-217.01,-61.5713,232.388][-208.821,-56.2493,231.999][-204.911,-53.6795,231.809][-201.001,-51.1097,231.62][-199.447,-50.0551,231.543][-193.232,-45.8367,231.238][-185.669,-40.6564,230.854][-177.873,-35.391,230.462][-170.329,-30.3888,230.09][-163.274,-25.7913,229.747][-157.761,-22.2443,229.468][-153.102,-19.2733,229.222][-147.943,-16.0432,228.947][-141.331,-11.9103,228.623]
     
  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Why throw an exception at the end of ReadData()? I think this can causes the editor crash.
     
  3. NumT

    NumT

    Joined:
    Feb 13, 2014
    Posts:
    22
    Ah a very good point - it was left over from the initial method generation. Unfortunately it doesn't seem to be the source of the issue (the method was returning before hitting that anyway). I still get the crashes when changing material and hitting apply.

    Looking at the crash.dmp file generated the only clue I am getting is: "The thread tried to read from or write to a virtual address for which it does not have the appropriate access."

    If anyone wants the very simple Maxscript to export splines to the output format this is what I run by the way:

    Code (CSharp):
    1. macroScript OutputSplines
    2. Category:"_Custom Scripts"
    3. toolTip:"Outputs selected splines to a text file"
    4. buttonText:"Output Splines"
    5. (
    6. -- get output path
    7. out_name = getSavePath caption:"set output path" initialDir:"$scripts" + "/output.splines"
    8. out_file = createfile out_name
    9.  
    10. --collect Shapes in selection
    11. splines = for obj in selection where superclassOf obj == Shape collect obj
    12.  
    13. --iterate through sphapes splines and print knot positions
    14. --formatting is 3ds max = "[x,y,z]" = [right, forward, up]
    15. for l in splines do
    16. (
    17.     for s = 1 to (numsplines l) do
    18.     (
    19.         for k = 1 to (numknots l s) do
    20.         (
    21.        
    22.             format "%" (getKnotPoint l s k) to:out_file
    23.         )
    24.         format "\n" to:out_file
    25.     )
    26. )
    27.  
    28. -- all done close file stream
    29. close out_file
    30. -- open to review
    31. edit out_name
    32. )
     
  4. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Oh yes didn't see the return. Can you post the crash log please?
     
  5. NumT

    NumT

    Joined:
    Feb 13, 2014
    Posts:
    22