How to Add Materials and Shading in Python
View as Markdown

How to Add Materials and Shading in Python

Aspose.3D for Python shades geometry with solid colour and factor properties on LambertMaterial, PhongMaterial, and PbrMaterial — all three are fully functional in this release. Texture image binding is not implemented: the Texture and TextureBase classes are stubs, and Material.get_texture() / set_texture() raise an error at call time. Build every look from colour and factor values rather than image textures.

Step-by-Step Guide

Step 1: Install the Package

pip install aspose-3d-foss
python -c "import aspose.threed; print(aspose.threed.__name__)"

Step 2: Import Required Classes

from aspose.threed import Scene
from aspose.threed.entities import Box
from aspose.threed.shading import LambertMaterial, PhongMaterial, PbrMaterial
from aspose.threed.utilities import Vector3

Step 3: Shade with LambertMaterial

LambertMaterial is a simple diffuse (matte) material — set its diffuse colour and, optionally, ambient colour, emissive colour, and transparency:

from aspose.threed import Scene
from aspose.threed.entities import Box
from aspose.threed.shading import LambertMaterial
from aspose.threed.utilities import Vector3

scene = Scene()
box = Box(2.0, 1.0, 1.0)
material = LambertMaterial()
material.diffuse_color = Vector3(0.2, 0.6, 0.9)

scene.root_node.create_child_node("Crate", entity=box.to_mesh(), material=material)
scene.save("crate.gltf")

Step 4: Shade with PhongMaterial

PhongMaterial extends LambertMaterial and adds specular highlighting — set its specular colour and shininess for a glossier look:

from aspose.threed.shading import PhongMaterial
from aspose.threed.utilities import Vector3

material = PhongMaterial()
material.diffuse_color = Vector3(0.6, 0.1, 0.1)
material.specular_color = Vector3(1.0, 1.0, 1.0)
material.shininess = 48.0

Step 5: Shade with PbrMaterial

PbrMaterial follows the modern metallic/roughness workflow used by glTF — pass the base colour as its albedo and tune its metallic and roughness factors:

from aspose.threed.shading import PbrMaterial
from aspose.threed.utilities import Vector3

material = PbrMaterial(albedo=Vector3(0.8, 0.1, 0.1))
material.metallic_factor = 0.9
material.roughness_factor = 0.2

A low roughness factor with a high metallic factor reads as a polished metal; a high roughness factor with a low metallic factor reads as a matte plastic or ceramic surface — both entirely from these two scalar values, no texture required.


Step 6: Assign the Material to a Node

create_child_node() accepts a material argument directly, or assign it to an existing node’s material property afterward — both set the same value:

node = scene.root_node.create_child_node("Ball")
node.entity = box.to_mesh()
node.material = material

A node that needs more than one material (for example, different polygon groups referencing different materials) uses its materials list property instead of its single material property.

Common Issues and Fixes

Calling get_texture() or set_texture() on any material raises an error. Texture image binding is not implemented in this release for any material class. Communicate surface appearance entirely through the colour and factor properties shown in Steps 3–5 — diffuse colour, albedo, specular colour, metallic factor, roughness factor, and similar — instead of binding an image.

Constructing PbrSpecularMaterial or ShaderMaterial raises an error immediately. Both classes are stubs in this release — their constructors raise an error before any property can even be set. Use PbrMaterial for a metallic/roughness look or LambertMaterial/PhongMaterial for a simpler diffuse or diffuse+specular look instead.

Material looks correct in the Python object but not in the exported file. Confirm the material was actually assigned — via create_child_node(..., material=material) or by setting the node’s material property afterward — before calling scene.save(). A mesh entity with no material attached exports with whatever default the target format applies.

Colours look washed out or overly dark after export. Vector3 colour components for these materials are plain floats in the 0.0–1.0 range (not 0–255); double-check values weren’t accidentally left in a 0–255 scale.

Frequently Asked Questions

Which material class should I use by default?

PbrMaterial if the target format is glTF/GLB (it maps directly onto glTF’s own metallic/roughness material model). LambertMaterial or PhongMaterial are a good fit for simpler formats or when you only need a flat diffuse look.

Can I combine PbrMaterial’s workflow with transparency?

Yes — PbrMaterial has its own transparency factor alongside albedo, metallic factor, and roughness factor; LambertMaterial/PhongMaterial expose an equivalent transparency and transparent-colour pair of properties.

Is any texture-related functionality usable at all in this release?

No — the entire Texture/TextureBase class hierarchy raises an error on every method, including construction. There is no partial or read-only texture support to fall back on; solid colour and factor properties are the only working shading mechanism.

Does PhongMaterial require setting its diffuse colour separately from its specular colour?

Yes — PhongMaterial inherits its diffuse colour (and ambient colour, emissive colour, transparent colour) from LambertMaterial and adds its own specular colour, specular factor, shininess, reflection colour, and reflection factor on top. Set both the inherited diffuse properties and the Phong-specific ones for a complete look.

See Also

 English