Extract Assets from ASE via C#
Build your own .NET apps to Extract Assets from ASE files using server-side APIs.
How to Extract Assets from ASE File Using C#
In order to extract assets from ASE file, we’ll use
API which is a feature-rich, powerful and easy to use API for C# platform to be used with extract assets. Open
package manager, search for Aspose.3D and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.3D
Steps to Extract Assets from ASE via C#
Aspose.3D makes it easy for the developers to extract assets from the ASE file with just few lines of code.
- Load ASE file via the constructor of Scene class
- Create zip file format object as output file format
- Create archive class and handle extract asset class
- Call the Extract method and save the file
System Requirements
Aspose.3D for .NET is supported on all major operating systems. Just make sure that you have the following prerequisites.
- Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Mono
- Development environment like Microsoft Visual Studio
- Aspose.3D for .NET referenced in your project
C# code to Extract Assets from ASE
//Source files that need to extract assets
string file = "template.ase";
Scene scene = new Scene(file);
//The output is in a compressed file format, and Directory represents the name of an existing folder
var zipOutput = Path.Combine("Directory", "OutputFile.zip");
using var output = new FileStream(zipOutput, FileMode.Create);
using var za = new Zip(output);
//Call the Extract method to perform asset extraction operations
Extract(scene,za,true);
//Callable Extract method,The parameter texture indicates: whether to extract the texture
private void Extract(Scene scene, Zip za,bool texture)
{
var extractor = new Extractor(za,texture);
extractor.Extract(scene);
}
//Create a compressed file processing class
class Zip : IDisposable
{
private ZipArchive archive;
private HashSet<string> entries = new HashSet<string>();
public Zip(Stream stream)
{
archive = new ZipArchive(stream, ZipArchiveMode.Create);
}
public void Dispose()
{
archive.Dispose();
}
public void Add(string fileName, byte[] content, bool enableCompression)
{
var entryName = PickName(fileName);
var compressionLevel = enableCompression ? CompressionLevel.Fastest : CompressionLevel.NoCompression;
var entry = archive.CreateEntry(entryName, compressionLevel);
using var stream = entry.Open();
stream.Write(content, 0, content.Length);
}
private string PickName(string fileName)
{
if (!entries.Contains(fileName))
{
entries.Add(fileName);
return fileName;
}
var baseName = Path.GetFileNameWithoutExtension(fileName);
var ext = Path.GetExtension(fileName);
for (var idx = 2; ; idx++)
{
var newName = baseName + "_" + idx;
if (!string.IsNullOrEmpty(ext))
newName += ext;
if (entries.Contains(newName))
continue;
entries.Add(newName);
return newName;
}
}
}
//Create an asset extraction processing class
class Extractor
{
private Zip zip;
private bool texture;
HashSet<A3DObject> visited = new HashSet<A3DObject>();
public Extractor(Zip zip,bool texture)
{
this.zip = zip;
this.texture = texture;
}
private bool CanVisit(A3DObject obj)
{
if (visited.Contains(obj))
return false;
visited.Add(obj);
return true;
}
public void Extract(Scene scene)
{
if (scene.Library != null && scene.Library.Count > 0)
{
foreach (var obj in scene.Library)
{
Visit(obj);
}
}
VisitNode(scene.RootNode);
}
private void VisitNode(Node node)
{
if (!CanVisit(node))
return;
if (texture)
{
foreach (var mat in node.Materials)
{
VisitMaterial(mat);
}
}
foreach (var entity in node.Entities)
{
if (entity is Mesh)
Save((Mesh)entity, node.Name);
}
foreach (var child in node.ChildNodes)
{
VisitNode(child);
}
}
private void VisitMaterial(Material mat)
{
if (!CanVisit(mat))
return;
if (!texture)
return;
foreach (var tslot in mat)
{
if (tslot.Texture is Texture)
{
Save((Texture)tslot.Texture);
}
}
}
private void Visit(A3DObject obj)
{
if (texture && obj is Texture)
{
Save((Texture)obj);
}
else if (obj is Mesh)
{
Save((Mesh)obj, null);
}
else if (obj is Node)
{
VisitNode((Node)obj);
}
}
private void Save(Mesh mesh, string? nodeName)
{
if (!CanVisit(mesh))
return;
Scene scene = new Scene(mesh);
using (var ms = new MemoryStream())
{
scene.Save(ms, FileFormat.FBX7400ASCII);
var name = nodeName;
if (string.IsNullOrEmpty(name))
name = mesh.Name;
if (string.IsNullOrEmpty(name))
name = "mesh";
var ext = ".fbx";
zip.Add(name + ext, ms.ToArray(), true);
}
}
private void Save(Texture tex)
{
if (tex.Content == null || !CanVisit(tex))
return;
var fileName = tex.FileName != null ? Path.GetFileName(tex.FileName) : null;
zip.Add(fileName, tex.Content, false);
}
}
About Aspose.3D for .NET API
Aspose.3D is a CAD and Gameware API to load, modify and convert 3D files. API is a standalone and does not require any any 3D modeling or rendering software. One can easily use API for Discreet3DS, WavefrontOBJ, STL (ASCII, Binary), Universal3D, FBX (ASCII, Binary), Collada, glTF, PLY, GLB, DirectX and more formats.Free App to Extract Assets from ASE
Check our live demos to Extractor ASE with following benefits.
ASE What is ASE File Format?
An ASE file is a 2D animation or graphics that contain layers, frames, palettes, tags, and settings.
Read MoreOther Supported App to Extract Assets from Formats
Using C#, One can also extract assets from many other file formats including.