2

How do I load an XNA model from a file path instead of from the content (eg. Model.FromStream but it doesnt exist)?? Texture2D has .FromStream, how can I do the equivalent for Model?

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
  • 1
    Are you talking about a model that is already processed to a .xnb format, or do you mean a .x or .fbx (or other) model? Because unless it's in the xnb format, content.Load<> won't work and it can't become a "Model". If, on the other hand, it is in the xnb format, it would be simplest to direct the content manager to its location and load it normally. – Steve H Jan 02 '12 at 01:12
  • Is there a specific reason why you don't want to use content.Load? That is simply how it works. – James Mar 02 '12 at 01:35
  • @James You disappoint me :P – Reece Aaron Lecrivain Aug 23 '13 at 10:05

1 Answers1

2

You can use Content.Load<Model>("Path here, make sure you use drive letter");

If you really need to have a FromStream method on Model you can use extension methods (Not exactly a perfect duplicate of FromStream but it should work):

public static Model FromPath(this Model model, ContentManager content, string path)
{
    return content.Load<Model>(path);
}

EDIT:

I have just tested the above code and apparently, rather than using the drive letter, you need to match the number of "..\\" in areversePathstring with the number of levels of the root directory of theContentManager. The problem is accessing the full directory of theContentManager` which is private (or protected, I'm not sure). Short of using reflection I don't think that that variable can be accessed. If we do know the full Root Directory Path then this should work:

string reversePath = "";
foreach (string level in Content.FullRootDirectory.Split('\\'))// I know there isn't actually a property 'FullRootDirectory' but for the sake of argument,
{
    reversePath += "..\\";
}
reversePath = reversePath.Substring(4);

I've googled a few times and haven't been able to find a way to get the root directory of the ContentManager. I might even ask this as a question here on SO in a bit here.

OK here is the final thing (using the answer from the question linked to above):

  1. Add a reference to System.Windows.Forms to your project

  2. Add the following code to the top of your Game1.cs file:

    using System.IO;
    using TApplication = System.Windows.Forms.Application;
    
  3. Add this code wherever you need it, like in LoadContent or an extension method:

    string ContentFullPath = Path.Combine(Path.GetDirectoryName(TApplication.ExecutablePath),
    Content.RootDirectory);
    
    string reversePath = "";
    foreach (string level in ContentFullPath.Split('\\'))
    {
        reversePath += "..\\";
    }
    
    reversePath = reversePath.Substring(3);
    
    
    Model test = Content.Load<Model>(Path.Combine(ContentFullPath, reversePath) + "*Your file name here*");
    
Community
  • 1
  • 1
annonymously
  • 4,708
  • 6
  • 33
  • 47
  • Why cant i just do eg. Content.RootDirectory = "C:\\Content" Also, why is there no Model.FromStream but there is Texture2D.FromStream??????? – Reece Aaron Lecrivain Jan 02 '12 at 09:58
  • Well, I think you might actually be able to do that, if that's what you need then I misunderstood your question. As for why, well you can take that up with Microsoft, but I think it might be because a model file is much more complex than a texture. Also, it has no animation support by default so you need to make a custom content processor or use the skinned model sample for animation. – annonymously Jan 02 '12 at 10:58
  • Loading a model also loads its materials, which contain effects and textures. Both of these are IDisposable but Model is not, so how do they get cleaned up? Well its because they are ALSO loaded through the content manager internally when you load a model. – James Jul 31 '14 at 23:11