Extract Assets from 3DS via C#
Build your own .NET apps to Extract Assets from 3DS files using server-side APIs.
How to Extract Assets from 3DS File Using C#
In order to extract assets from 3DS 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 3DS via C#
Aspose.3D makes it easy for the developers to extract assets from the 3DS file with just few lines of code.
- Load 3DS 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 3DS
//Source files that need to extract assets
string file = "template.3ds";
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 3DS
Check our live demos to Extractor 3DS with following benefits.
3DS What is 3DS File Format?
A file with 3DS extension represents 3D Studio (DOS) mesh file format used by Autodesk 3D Studio. Autodesk 3D Studio has been in 3D file format market since 1990s and has now evolved to 3D Studio MAX for working with 3D modeling, animation and rendering. A 3DS file contains data for 3D representation of scenes and images and is one of the popular file formats for 3D data import and export. It considers information like camera locations, Mesh data, lighting information, viewport configurations, smoothing group data, bitmap references and attributes to create vertices and polygons for rendering a scene.
Read MoreOther Supported App to Extract Assets from Formats
Using C#, One can also extract assets from many other file formats including.