Create and Read an Existing 3D Scene
Overview
The article explains the following topics using C# 3D file formats manipulation library.
- Create an Empty 3D Scene in C# from scratch
- Read or Load Existing 3D Scene in C#
- Save the 3D Scene in Supported 3D Formats using C#
- Work with 3D Scene Properties in C#
Create an Empty 3D Scene and Save in Supported 3D File Formats
Aspose.3D API supports creating the new 3D scenes from the scratch and then save in any supported file format. Developers can also load an existing 3D Scene for the modification, addition or processing purposes.
Creating a 3D Scene Document
Please follow these steps in C# to create a 3D Scene document using the Aspose.3D APIs:
- Create an instance of the
Scene
class that represents a 3D scene document. - Generate a 3D Scene document by calling the
Save
method of the Scene class object.
// For complete examples and data files, please go to https://github.com/aspose-3d/Aspose.3D-for-.NET | |
// Create an object of the Scene class | |
Scene scene = new Scene(); | |
// Save 3D scene document | |
scene.Save("document.fbx"); |
Reading a 3D Scene
Using Aspose.3D API, developers can load all the supported 3D documents. The available constructors of the Scene
class allow to do so and they accept a valid file path string. The supported readable file formats are as follows:
- FBX 7.5 (ASCII, Binary)
- FBX 7.4 (ASCII, Binary)
- FBX 7.3 (ASCII, Binary)
- FBX 7.2 (ASCII, Binary)
- FBX 6.1 (ASCII, Binary)
- STL (ASCII, Binary)
- WavefrontOBJ
- Discreet3DS
- Universal3D
- Collada
- glTF (ASCII, Binary)
- Maya (ASCII, Binary)
- OpenUSD (USD, USDZ)
- Blender
- DXF
- PLY (ASCII, Binary)
- X (ASCII, Binary)
- Draco
- 3MF
- RVM (Text, Binary)
- ASE
Constructors of the Scene
class detect 3D document format internally.
// For complete examples and data files, please go to https://github.com/aspose-3d/Aspose.3D-for-.NET | |
// The path to the documents directory. | |
// Initialize a Scene class object | |
Scene scene = new Scene(); | |
// Load an existing 3D document | |
scene.Open("document.fbx"); | |
Working with 3D Scene Properties
Aspose.3D API lets you read 3D Scene properties using the scene’s child nodes. The following C# code sample demonstrates the usage of this feature.
// For complete examples and data files, please go to https://github.com/aspose-3d/Aspose.3D-for-.NET | |
Scene scene = Scene.FromFile("EmbeddedTexture.fbx"); | |
Material material = scene.RootNode.ChildNodes[0].Material; | |
PropertyCollection props = material.Properties; | |
//List all properties using foreach | |
foreach (var prop in props) | |
{ | |
Console.WriteLine("{0} = {1}", prop.Name, prop.Value); | |
} | |
//or using ordinal for loop | |
for (int i = 0; i < props.Count; i++) | |
{ | |
var prop = props[i]; | |
Console.WriteLine("{0} = {1}", prop.Name, prop.Value); | |
} | |
//Get property value by name | |
var diffuse = props["Diffuse"]; | |
Console.WriteLine(diffuse); | |
//modify property value by name | |
props["Diffuse"] = new Vector3(1, 0, 1); | |
//Get property instance by name | |
Property pdiffuse = props.FindProperty("Diffuse"); | |
Console.WriteLine(pdiffuse); | |
//Since Property is also inherited from A3DObject | |
//It's possible to get the property of the property | |
Console.WriteLine("Property flags = {0}", pdiffuse.GetProperty("flags")); | |
//and some properties that only defined in FBX file: | |
Console.WriteLine("Label = {0}", pdiffuse.GetProperty("label")); | |
Console.WriteLine("Type Name = {0}", pdiffuse.GetProperty("typeName")); | |
//so traversal on property's property is possible | |
foreach (var pp in pdiffuse.Properties) | |
{ | |
Console.WriteLine("Diffuse.{0} = {1}", pp.Name, pp.Value); | |
} |