3D in Python

3D modeling and visualization have become an essential part of various industries, such as gaming, architecture, and virtual reality. In this article, we’ll explore how to work with 3D models in Python, focusing on creating and reading 3D model scenes. We will explore the Aspose.3D library in Python, a powerful and easy-to-use tool for creating, manipulating, and reading 3D models. Let’s dive into 3D modeling with Python!

This article covers the following topics:

  1. Python 3D Library – 3D in Python
  2. Create a Simple 3D Scene in Python
  3. Create a Material in 3D Scene
  4. Add Light to a 3D Scene
  5. Create a Primitive 3D Model in Python
  6. Read 3D Scene in Python

Python 3D Library – 3D in Python

Aspose.3D for Python is a versatile, feature-rich, and highly efficient 3D modeling solution for Python developers. It offers a comprehensive set of features that enable users to create, read, and modify 3D models easily. Some key features of this Python 3D library include:

  • Support for popular 3D file formats such as FBX, OBJ, STL, and more
  • High-performance 3D rendering and visualization capabilities
  • Advanced scene management and manipulation tools
  • Extensive documentation and code samples to help developers get started quickly

Please either download the package or install the API from PyPI using the following pip command in the console:

pip install aspose-3d

Create a Basic 3D Scene in Python

We can create a simple 3D scene by following the steps below:

  1. Create an instance of the Scene class.
  2. After that, call the create_child_node() method to add some geometry, a shape to show on screen, e.g., a sphere.
  3. Finally, save the scene as FBX using the save() method.

The following code sample shows how to create a simple 3D scene in Python.

# This code example demonstrates how to create a basic 3D scene.
from aspose.threed import Scene
from aspose.threed.entities import Sphere
# Create an object of the Scene class
scene = Scene()
# Create a Sphere model
scene.root_node.create_child_node("Sphere", Sphere())
# Save 3D scene document
scene.save("C:\\Files\\my_scene.fbx")
3D Scene in Python

3D Scene in Python.

Create a Material in 3D Scene using Python

We can create the material with diffuse color in a 3D scene by following the steps below:

  1. Create an instance of the Scene class.
  2. Call the create_child_node() method to add a sphere.
  3. Initialize the LambertMaterial class object.
  4. Specify the diffuse_color and name properties.
  5. After that, assign the material object to the Sphere model.
  6. Finally, save the scene as FBX using the save() method.

The following code sample shows how to create material with diffuse color in a 3D scene using Python.

# This code example demonstrates how to create material in 3D scene.
from aspose.threed import Scene
from aspose.threed.entities import Sphere
from aspose.threed.utilities import MathUtils, Vector3
from aspose.threed.shading import LambertMaterial
from aspose.pydrawing import Color
# Create an object of the Scene class
scene = Scene()
# Create a Sphere model
node = scene.root_node.create_child_node("Sphere", Sphere())
# Set LambertMaterial
material = LambertMaterial()
material.diffuse_color = Vector3(Color.red)
material.name = "Lambert"
# Assign to Sphere Model
node.material = material
# Save 3D scene document
scene.save("C:\\Files\\material_scene.fbx")
Material in 3D Scene

Creating Material in 3D Scene.

Add Light to a 3D Scene using Python

We can also add light to a 3D scene by following the steps below:

  1. Create an instance of the Scene class.
  2. Call the create_child_node() method to add a sphere.
  3. Initialize the Light class object.
  4. Specify the light_type and name properties.
  5. After that, call the create_child_node() method to add the light entity.
  6. Finally, save the scene as FBX using the save() method.

The following code sample shows how to create light in a 3D scene using Python.

# This code example demonstrates how to create a light in 3D scene.
from aspose.threed import Scene
from aspose.threed.entities import Sphere, Light, LightType
from aspose.threed.utilities import MathUtils, Vector3
from aspose.threed.shading import LambertMaterial
from aspose.pydrawing import Color
# Create an object of the Scene class
scene = Scene()
# Create a Sphere model
scene.root_node.create_child_node("Sphere", Sphere())
# Add Light entity
light = Light()
# The point light
light.light_type = 0
# Name
light.name = "Light";
# Add to the scene
scene.root_node.create_child_node(light).transform.translation = Vector3(10, 0, 10);
# Save 3D scene document
scene.save("C:\\Files\\light_scene.fbx")

Primitive 3D Model Python

A primitive is a three-dimensional geometric shape that is the basis for creating complex geometric forms. We can create a primitive 3D model by following the steps mentioned earlier. However, we need to add multiple geometric shapes or objects, as shown in the code sample below:

# This code example demonstrates how to create a primitive 3D models
from aspose.threed import Scene
from aspose.threed.entities import Box, Cylinder
from aspose.threed.utilities import MathUtils, Vector3
# Initialize a Scene object
scene = Scene()
# Create a Box model
box = Box();
box.name = "Box";
scene.root_node.create_child_node(box)
# Create a Cylinder model
cylinder = Cylinder();
cylinder.name = "Cylinder";
scene.root_node.create_child_node(cylinder)
# Save 3D scene document
scene.save("C:\\Files\\primitive_scene.fbx")

Read 3D Scene in Python

We can read a 3D scene from the FBX document by following the steps below:

  1. Create an instance of the Scene class.
  2. Load an existing 3D document using the open() method.
  3. Loop through child_nodes.
  4. Finally, display property values.

The following code sample shows how to load and read a 3D scene in Python.

# This code example demonstrates how to read a 3D scene.
from aspose.threed import Scene
# Initialize a Scene class object
scene = Scene()
# Load an existing 3D document
scene.open("C:\\Files\\primitive_scene.fbx")
for node in scene.root_node.child_nodes:
entity = node.entity;
print("{0}", entity.name);
{0} Box
{0} Cylinder

Get a Free License

You can get a free temporary license to try Aspose.3D for Python without evaluation limitations.

3D in Python – Python 3D Library Learning Resources

You can learn more about creating and manipulating 3D models and scenes in Python and explore other features of the library using the resources given below:

Conclusion

In this article, we introduce the Aspose.3D library and demonstrate its capabilities in creating, reading, and manipulating 3D models in Python. With support for various 3D file formats, advanced scene management, and high-performance rendering, the Aspose.3D library is an invaluable tool for Python developers working with 3D content. By using the Aspose.3D library, you can streamline your 3D modeling projects and create stunning 3D scenes easily. In case of any ambiguity, please contact us on our free support forum.

See Also