Skip to content

Commit c0691cb

Browse files
Added docs for materials
1 parent f2e22bc commit c0691cb

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

doc/apireference.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,16 @@ Workplane and Shape objects can be connected together into assemblies
291291
Assembly.constrain
292292
Assembly.solve
293293
Constraint
294+
295+
Materials
296+
----------
297+
298+
For material properties (physical and visual) of assembly items.
299+
300+
.. currentmodule:: cadquery
301+
.. autosummary::
302+
294303
Color
304+
Material
305+
PbrMaterial
306+
CommonMaterial

doc/assy.rst

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,131 @@ Where:
879879
show_object(assy)
880880

881881

882-
Assembly colors
882+
Materials
883+
----------------
884+
885+
Materials can be assigned to objects in an assembly to define their visual
886+
and physical properties. CadQuery supports three types of material representations:
887+
888+
1. Simple Color Material
889+
2. PBR (Physically Based Rendering) Material - the modern standard for material definition
890+
3. Common (Legacy) Material - legacy material representation in OCCT
891+
892+
A material can have multiple representations defined simultaneously - color,
893+
common, and PBR properties can all be specified for the same material.
894+
The appropriate representation will be used based on the export format.
895+
This allows you to define a material once and have it work well across
896+
different export formats. For example:
897+
898+
.. code-block:: python
899+
900+
# Material with all three representations
901+
gold_material = cq.Material(
902+
name="Gold",
903+
description="A golden material with multiple representations",
904+
density=19300, # kg/m³
905+
# Simple color for basic visualization
906+
color=cq.Color(1.0, 0.8, 0.0, 1.0),
907+
# PBR material for modern physically-based rendering
908+
pbr=cq.PbrMaterial(
909+
base_color=cq.Color(1.0, 0.8, 0.0, 1.0),
910+
metallic=1.0,
911+
roughness=0.2,
912+
refraction_index=1.0,
913+
),
914+
# Legacy common material for backward compatibility
915+
common=cq.CommonMaterial(
916+
ambient_color=cq.Color(0.2, 0.2, 0.0, 1.0),
917+
diffuse_color=cq.Color(0.8, 0.8, 0.0, 1.0),
918+
specular_color=cq.Color(1.0, 1.0, 0.0, 1.0),
919+
emissive_color=cq.Color(0.0, 0.0, 0.0, 1.0),
920+
shininess=0.9,
921+
transparency=0.0,
922+
),
923+
)
924+
925+
When this material is exported:
926+
927+
- STEP files will use the simple color representation
928+
- GLTF/GLB files will use the PBR properties
929+
930+
Material Types
931+
=============
932+
933+
Simple Color Material
934+
~~~~~~~~~~~~~~~~~~~
935+
936+
The simplest form of material definition includes just a name,
937+
description, density, and color:
938+
939+
.. code-block:: python
940+
941+
material = cq.Material(
942+
name="Red Plastic",
943+
description="A simple red plastic material",
944+
density=1200, # kg/m³
945+
color=cq.Color(1.0, 0.0, 0.0, 1.0), # Red with full opacity
946+
)
947+
948+
PBR Material
949+
~~~~~~~~~~~
950+
951+
PBR (Physically Based Rendering) materials provide physically accurate material representation and are the recommended way to define materials in CadQuery:
952+
953+
.. code-block:: python
954+
955+
material = cq.Material(
956+
name="Clear Glass",
957+
description="A transparent glass material",
958+
density=2500, # kg/m³
959+
pbr=cq.PbrMaterial(
960+
base_color=cq.Color(0.9, 0.9, 0.9, 0.3), # Base color with transparency
961+
metallic=0.0, # 0.0 for non-metals, 1.0 for metals
962+
roughness=0.1, # 0.0 for smooth, 1.0 for rough
963+
refraction_index=1.5, # Must be between 1.0 and 3.0
964+
emissive_factor=cq.Color(0.0, 0.0, 0.0), # Optional self-illumination
965+
),
966+
)
967+
968+
Common Material (Legacy)
969+
~~~~~~~~~~~~~~~~~~~~~~~
970+
971+
Common materials use a traditional lighting model with ambient, diffuse, specular, and emissive colors.
972+
This representation is the legacy representation in OCCT but can be useful for some formats.
973+
974+
.. code-block:: python
975+
976+
material = cq.Material(
977+
name="Polished Steel",
978+
description="A shiny metallic material",
979+
density=7850, # kg/m³
980+
common=cq.CommonMaterial(
981+
ambient_color=cq.Color(0.2, 0.2, 0.2, 1.0), # Base color in shadow
982+
diffuse_color=cq.Color(0.5, 0.5, 0.5, 1.0), # Main surface color
983+
specular_color=cq.Color(0.8, 0.8, 0.8, 1.0), # Highlight color
984+
emissive_color=cq.Color(0.0, 0.0, 0.0, 1.0), # Self-illumination
985+
shininess=0.8, # Controls highlight size (0.0-1.0)
986+
transparency=0.0, # 0.0 is opaque, 1.0 is transparent
987+
),
988+
)
989+
990+
Export Support
991+
=============
992+
993+
Different export formats support different material properties:
994+
995+
- STEP: Currently only supports colors and basic material properties like density
996+
- GLTF/GLB: Full support for PBR materials
997+
998+
For the best visual representation, export to GLTF/GLB format:
999+
1000+
.. code-block:: python
1001+
1002+
assy.export("assembly.glb")
1003+
1004+
1005+
1006+
Predefined Colors
8831007
---------------
8841008

8851009
Aside from RGBA values, the :class:`~cadquery.Color` class can be instantiated from a text name. Valid names are
@@ -1410,3 +1534,4 @@ listed along with a color sample below:
14101534
<div style="background-color:rgba(65,65,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">yellow4</div>
14111535
<div style="background-color:rgba(82,155,8,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">yellowgreen</div>
14121536
</div>
1537+

0 commit comments

Comments
 (0)