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

Assimp Unity3d (C#)

Discussion in 'Scripting' started by timm123, May 20, 2013.

  1. timm123

    timm123

    Joined:
    May 20, 2013
    Posts:
    6
    Hello,

    i was trying to use the "assimp.net" library in unity (via C#-Script). I compiled the C#-Version of the library with ".Net v3.5" as target.
    However, i still have trouble calling library functions. For those who do not know assimp: it's a C++ library for loading 3D models (at runtime). There's also a C#-Wrapper library (called assimp.net).
    I'm getting following exception:

    Code (csharp):
    1.  
    2. NotImplementedException: The requested feature is not implemented.
    3. System.Runtime.InteropServices.Marshal.GetHRForLastWin32Error () (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs:412)
    4. Assimp.Unmanaged.AssimpLibraryImplementation.LoadLibrary (System.String path)
    5. Assimp.Unmanaged.AssimpLibrary.LoadLibrary (System.String libPath)
    6. Assimp.Unmanaged.AssimpLibrary.LoadLibrary (System.String lib32Path, System.String lib64Path)
    7. Assimp.Unmanaged.AssimpLibrary.LoadLibrary ()
    8. Assimp.Unmanaged.AssimpLibrary.LoadIfNotLoaded ()
    9. Assimp.Unmanaged.AssimpLibrary.EnableVerboseLogging (Boolean enable)
    10. Assimp.AssimpImporter.PrepareImport ()
    11. Assimp.AssimpImporter.ImportFile (System.String file, PostProcessSteps postProcessFlags)
    12. NewBehaviourScript.Start () (at Assets/Scripts/NewBehaviourScript.cs:20)
    13.  
    The unity3d documentation say, though, that this method should be compatible with the version of .Net unity is using (http://docs.unity3d.com/Documentation/ScriptReference/MonoCompatibility.html). Maybe anyone had similar problems using external libraries?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Marshal.GetHRForLastWin32Error isn't implemented in Mono at all (my guess being that it's specific to Windows so they decided to simply throw a not implemented exception). The real reason you're getting that is because the assembly can't be loaded and it's attempting to get more information as to why. Are you using Pro?
     
  3. timm123

    timm123

    Joined:
    May 20, 2013
    Posts:
    6
    Hey, thanks for the answer. I'm using the 30-days trial, so yeah that should be pro.

    Actually the only thing i've done is checking out http://assimp-net.googlecode.com/svn/trunk and compiling the project (after changing target framework to ".net 3.5"). Then i imported the resulting "dll" into the "Plugins"-subfolder of the unity3d-project's assets-folder. Then i created a new C#-behaviour script with very simple code to test if the library is accessible from there.

    Well, assimp-net is just a C#-wrapper for the C++-project, so do i need to take care of something else i missed? Any ideas would be very appriciated. I'm quite sure some guys already managed to use assimp in unity (because that's how i got the idea from google) but somehow i got stuck at this point.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I got to the point you did before remembering that you need Pro for native plugins. I bypassed the issue you have by just commenting out the calls to that method which just result in less-useful error messages being displayed when the assembly fails to load. So if you get anywhere, I'd be curious to know what you come up with.
     
  5. timm123

    timm123

    Joined:
    May 20, 2013
    Posts:
    6
    btw. i'm using another library (for zipping/unzipping files) so that should not be the problem.

    Best thing i can offer is another part of the code where i call library functions, i guess:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using Assimp;
    5. using Assimp.Configs;
    6.  
    7. public class NewBehaviourScript : MonoBehaviour
    8. {
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.             string fileName = "C:/Users/Timm/Downloads/Monster_1/em1000.obj";
    13.        
    14.             //Create a new importer
    15.             AssimpImporter importer = new AssimpImporter(); // THIS SEEMS TO WORK
    16.  
    17.             Scene model = importer.ImportFile(fileName); // THIS THROWS THE EXCEPTION
    18.  
    19.             importer.Dispose();
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.    
    25.     }
    26. }
    27.  
    28.  
    P.s: okey, had a look into the C# code which is throwing an exception. So you're right, it fails to load the "Assimp32.dll" (the actual C++ lib). Now i installed the complete assimp-sdk, but still doesn't find the dll. That's probably not too amazing because i really have no clue where to put it ... :-(
     
    Last edited: May 20, 2013
  6. timm123

    timm123

    Joined:
    May 20, 2013
    Posts:
    6
    Sorry for the double-post. The solution is to put the "Assimp32/64.dll" into the project folder. From there the wrapper class finds it ... :)
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Interesting - this goes along with what someone posted a week or so ago with native code DLLs loading ok from the root directory - which isn't mentioned in the documentation.
     
  8. timm123

    timm123

    Joined:
    May 20, 2013
    Posts:
    6
    Hey, another question concering a similar problem.
    I want to run the same C# script on Android now, and the adb-logcat output says "unable to find Assimp32.dll". Any idea, where to put the dll to deploy it on the android device?
     
  9. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    194
  10. Torigas

    Torigas

    Joined:
    Jan 1, 2014
    Posts:
    63
    One problem is that the assimpnet.dll looks for the assimpPLATFORM.dll (i.e. assimp64.dll) in your PATH/TO/PROJECT/ProjectName folder. The assimpnet.dll is not written to look for the dlls dynamically and expects them to be in the executable directory (https://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.110).aspx)
    If you copy them in there it should work.
    Sorry for necroing this thread but mayhaps someone still needs this little piece of advice.
     
  11. sameer-mirza

    sameer-mirza

    Joined:
    Nov 14, 2011
    Posts:
    36
    Hey guys, I'm also attempting to get assimp up and running using the assimp-net library. However, after fixing the problem addressed in this thread, I'm receiving the following error:

    NullReferenceException: Object reference not set to an instance of an object
    (wrapper stelemref) object:stelemref (object,intptr,object)
    System.String.Format (System.String format, System.Object arg0, System.Object arg1, System.Object arg2) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/String.cs:1863)
    Assimp.Material.CreateFullyQualifiedName (System.String baseName, TextureType texType, Int32 texIndex)
    Assimp.MaterialProperty.get_FullyQualifiedName ()
    Assimp.Material.AddProperty (Assimp.MaterialProperty matProp)
    Assimp.Material.Assimp.IMarshalable<Assimp.Material,Assimp.Unmanaged.AiMaterial>.FromNative (Assimp.Unmanaged.AiMaterial& nativeValue)
    Assimp.MemoryHelper.FromNativeArray[Material,AiMaterial] (IntPtr nativeArray, Int32 length, Boolean arrayOfPointers)
    Assimp.Scene.Assimp.IMarshalable<Assimp.Scene,Assimp.Unmanaged.AiScene>.FromNative (Assimp.Unmanaged.AiScene& nativeValue)
    Assimp.MemoryHelper.FromNativePointer[Scene,AiScene] (IntPtr ptr)
    Assimp.Scene.FromUnmanagedScene (IntPtr scenePtr)
    Assimp.AssimpContext.ImportFile (System.String file, PostProcessSteps postProcessFlags)
    Assimp.AssimpContext.ImportFile (System.String file)
    assImporter.Start () (at Assets/assImporter.cs:13)
     
  12. SioHio

    SioHio

    Joined:
    Oct 3, 2015
    Posts:
    6
  13. rkamalya

    rkamalya

    Joined:
    Jan 21, 2015
    Posts:
    8
    Hi sammer.mirza, I'm getting the same exception. Did you have a success on fixing that ? If yes - sharing your solution would be a great help. Thanks.
     
  14. sameer-mirza

    sameer-mirza

    Joined:
    Nov 14, 2011
    Posts:
    36
    I ended up buying this asset from the Unity Store:
    https://www.assetstore.unity3d.com/en/#!/content/28909

    This plugin uses assimp as a middleware and exports the mesh in a custom binary readable format for unity which is read at runtime.
     
  15. rkamalya

    rkamalya

    Joined:
    Jan 21, 2015
    Posts:
    8

    Thanks for the answer. I'll check that.
     
  16. m.marc

    m.marc

    Joined:
    Mar 23, 2016
    Posts:
    5
    Hi there
    I'm trying to use assimp.NET in unity but i cannot get a good meshes from ifc file...I think it's a probleme from the tree structure...
    someone succeed that before?
     
  17. 4ipideil

    4ipideil

    Joined:
    Jan 22, 2015
    Posts:
    13
    for me, the problem was related to String.IsNullOrEmpty - im replacing it with my function

    Code (CSharp):
    1. public static bool Check(String target)
    2.         {
    3.             try
    4.             {
    5.                 String.Format("{0}", target);
    6.                 return String.IsNullOrEmpty(target);
    7.             }
    8.             catch
    9.             {
    10.                 return true;
    11.             }
    12.         }
    in that c# assimp wrapper and its start to work, but it crashed unity constantly when i stop unity application
     
  18. Whitebread

    Whitebread

    Joined:
    Oct 31, 2011
    Posts:
    9
    For anyone else getting this:

    NullReferenceException: Object reference not set to an instance of an object
    (wrapper stelemref) object:stelemref (object,intptr,object)

    As far as I can tell it is because a pointer reference is being garbage collected while it is in an unmanaged DLL (assimp64.dll).

    I hooked the UnityEngine.dll into AssimpNet so that I could spit out a gratuitous amount of debug messages and found that by doing so the references that were going null were somehow magically kept alive.

    You will have to call GC.KeepAlive(<object reference>) to keep your IntPtrs alive long enough for them to be processed through the variety of unmanaged memory calls (looks to be largely in MemoryHelper.cs) while the file is being imported.

    If you look at the stack trace you can see where the pointer is being lost in the midst of importing materials.

    I haven't got it exactly where I want it yet, but I am able to use AssimpNet 3.3 in Unity 5.5.4f to import models at runtime.
     
  19. neosca

    neosca

    Joined:
    Jul 1, 2014
    Posts:
    11
    @Whitebread

    I am stuck with the same NullReferenceException: Object reference not set to an instance of an object
    (wrapper stelemref) object:stelemref (object,intptr,object).

    Can you please explain in detail what exactly I need to do resolve this issue?
    In which function I have to call GC.KeepAlive in MemoryHelper.cs?

    It could be great if you can post the modified MemoryHelper.cs.
     
  20. Whitebread

    Whitebread

    Joined:
    Oct 31, 2011
    Posts:
    9
    I'm actually back to the drawing board on this one. GC.KeepAlive did not in fact do what I wanted it to do. The Debug messages I put in previously were somehow allowing it to work (which I assumed just meant it was keeping the references alive) but it is not working properly. I will be investigating it further in the next few weeks though. I will post again to let you know what I find.
     
  21. sameer-mirza

    sameer-mirza

    Joined:
    Nov 14, 2011
    Posts:
    36
    I've just recently tested Assimp.NET 3.3.2 and the latest Assimp (Native) Dlls in Unity 2017 directly and they seem to work just fine! I used the LoadLibrary call to load the Native DLL manually
    Code (CSharp):
    1. Assimp.Unmanaged.AssimpLibrary.Instance.LoadLibrary (libPath);
     
    XiaGu and Whitebread like this.
  22. Whitebread

    Whitebread

    Joined:
    Oct 31, 2011
    Posts:
    9
    Wow... ok. Well that gets me a little bit further. I just tried it in 5.5 and I am no longer getting crashes and null reference errors. My model did not import entirely correctly but I believe it is now an issues with how i'm handling imported data.

    Thanks Sameer!
     
  23. sameer-mirza

    sameer-mirza

    Joined:
    Nov 14, 2011
    Posts:
    36
    No Problem. Here's what I use in the Importer Configs:

    Code (CSharp):
    1. Assimp.AssimpContext importer = new Assimp.AssimpContext();
    2. importer.SetConfig(new Assimp.Configs.MeshVertexLimitConfig(60000));
    3. importer.SetConfig(new Assimp.Configs.MeshTriangleLimitConfig(60000));
    4. importer.SetConfig(new Assimp.Configs.RemoveDegeneratePrimitivesConfig(true));
    5. importer.SetConfig(new Assimp.Configs.SortByPrimitiveTypeConfig(Assimp.PrimitiveType.Line | Assimp.PrimitiveType.Point));
    And the PostProcessSteps:
    Code (CSharp):
    1. Assimp.PostProcessSteps postProcessSteps = Assimp.PostProcessPreset.TargetRealTimeMaximumQuality | Assimp.PostProcessSteps.MakeLeftHanded | Assimp.PostProcessSteps.FlipWindingOrder;
    2. Assimp.Scene assimpScene = importer.ImportFile(meshPath, postProcessSteps);
    Let me know if you need more help
     
  24. dax-thomas

    dax-thomas

    Joined:
    Jun 22, 2013
    Posts:
    3
    Hi,

    I started a project using TriLib to load ifc/fbx files.

    But Im getting this error after upgrading Trilib to the new version in My Main Project
    error CS0246: The type or namespace name `AssetLoader' could not be found. Are you missing an assembly reference?

    However if i dont use my Main project and just make a smaller new project The error goes away as it may be in conflict with my Main project Files.

    Im currently using Unity 2017.4.18f1.
    I dont know if its a Unity settings issue or Trilib issue or an assimp.dll location issue.

    I tried setting the .NET Version to 4.6 experimental. No result
    Also tried copying assimp.dll to the Assets folder or the plugins folder . No result

    At this point im stuck , Please help.

    Dax Thomas
     
  25. StarCoop

    StarCoop

    Joined:
    Nov 26, 2016
    Posts:
    44
    Hi there,

    it seems that you should remove TriLib library in your project before updating. Otherwise there are always old files or references that compete with each other. Also don't forget to look in Plugins folder for some TriLib folders maybe.