Foundation (Python)How-to guidesApply materials

Apply materials

Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
from cadbuildr.foundation import *


def get_cube():
    # initialize the component
    cube = Part()
    # Create a Sketch from XY Plane
    s = Sketch(cube.xy())
    # Use the Square class instead of points !
    square = Square.from_center_and_side(s.origin, 20)
    e = Extrusion(square, 20)
    cube.add_operation(e)

    # Setting the material
    mat = Material()
    mat.set_diffuse_color("purple")
    cube.set_material(mat)
    return cube


show(get_cube())

This example simplifies the Getting Started cube by using the Square class.

# Use the Square class instead of points !
    square = Square.from_center_and_side(s.origin, 20)

We can also use the Material class to set different materials for the part.

# Setting the material
    mat = Material()
    mat.set_diffuse_color("purple")
    cube.set_material(mat)

The Material class allows us to set the color and properties of the part. We can set the color using the mat.set_diffuse_color function, which accepts a color name or using RGB values (e.g. mat.set_diffuse_colorRGB(255, 255, 255)).

Actually, if you just want to change the color of a part you can also use cube.paint('blue').