HTML JPG PDF XML U3D
Aspose.3D  for .NET
U3D

Extract Assets from U3D via C#

Build your own .NET apps to Extract Assets from U3D files using server-side APIs.

How to Extract Assets from U3D File Using C#

In order to extract assets from U3D file, we’ll use

Aspose.3D for .NET

API which is a feature-rich, powerful and easy to use API for C# platform to be used with extract assets. Open

NuGet

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 U3D via C#

Aspose.3D makes it easy for the developers to extract assets from the U3D file with just few lines of code.

  • Load U3D 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 U3D


//Source files that need to extract assets
string file = "template.u3d";
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 U3D

    Check our live demos to Extractor U3D with following benefits.

      No need to download or setup anything
      No need to write or compile code
      Just upload U3D file and hit the "Extract" button
      Download U3D file from the link, if required

    U3D What is U3D File Format?

    U3D (Universal 3D) is a compressed file format and data structure for 3D computer graphics. It contains 3D model information such as triangle meshes, lighting, shading, motion data, lines and points with color and structure. The format was accepted as ECMA-363 standard in August 2005. 3D PDF documents support U3D objects embedding and can be viewed in Adobe Reader (version 7 and onwards).

    Read More

    Other Supported App to Extract Assets from Formats

    Using C#, One can also extract assets from many other file formats including.

    3MF (3D Manufacturing Format)
    AMF (Additive Manufacturing Format)
    ASE (2D Animation File)
    DAE (Digital Asset Exchange)
    DXF (Drawing Interchange Format)
    DRC (Google Draco)
    FBX (3D Format)
    GLB (3D File Binary Representation)
    GLTF (GL Transmission Format)
    JT (Jupiter Tessellation File)
    OBJ (3D File Format)
    PLY (Polygon File Format)
    PDF (3D PDF)
    RVM (AVEVA Plant Design Model)
    STL (Interchangeable 3D Surface Geometry)
    3DS (3D Studio Mesh File Format)
    VRML (Virtual Reality Modeling Language)
    X (DirectX Model Image)
    USD (Universal Scene Description)
    USDZ (Universal Scene Description Zip Archive)