@@ -53,14 +53,30 @@ def __init__(self, *args, **kwargs):
53
53
self .b = 0.0
54
54
self .a = 1.0
55
55
elif len (args ) == 1 and isinstance (args [0 ], str ):
56
- from .occ_impl .assembly import color_from_name
57
-
58
- # Handle color name case
59
- color = color_from_name (args [0 ])
60
- self .r = color .r
61
- self .g = color .g
62
- self .b = color .b
63
- self .a = color .a
56
+ from cadquery .occ_impl .assembly import color_from_name
57
+ from vtkmodules .vtkCommonColor import vtkNamedColors
58
+
59
+ # Try to get color from OCCT first, fall back to VTK if not found
60
+ try :
61
+ # Get color from OCCT
62
+ color = color_from_name (args [0 ])
63
+ self .r = color .r
64
+ self .g = color .g
65
+ self .b = color .b
66
+ self .a = color .a
67
+ except ValueError :
68
+ # Check if color exists in VTK
69
+ vtk_colors = vtkNamedColors ()
70
+ if not vtk_colors .ColorExists (args [0 ]):
71
+ raise ValueError (f"Unsupported color name: { args [0 ]} " )
72
+
73
+ # Get color from VTK
74
+ color = vtk_colors .GetColor4d (args [0 ])
75
+ self .r = color .GetRed ()
76
+ self .g = color .GetGreen ()
77
+ self .b = color .GetBlue ()
78
+ self .a = color .GetAlpha ()
79
+
64
80
elif len (args ) == 3 :
65
81
# Handle RGB case
66
82
r , g , b = args
@@ -118,15 +134,14 @@ def __eq__(self, other: object) -> bool:
118
134
119
135
120
136
@dataclass
121
- class CommonMaterial :
137
+ class SimpleMaterial :
122
138
"""
123
139
Traditional material model matching OpenCascade's XCAFDoc_VisMaterialCommon.
124
140
"""
125
141
126
142
ambient_color : Color
127
143
diffuse_color : Color
128
144
specular_color : Color
129
- emissive_color : Color
130
145
shininess : float
131
146
transparency : float
132
147
@@ -145,21 +160,19 @@ def __hash__(self) -> int:
145
160
self .ambient_color ,
146
161
self .diffuse_color ,
147
162
self .specular_color ,
148
- self .emissive_color ,
149
163
self .shininess ,
150
164
self .transparency ,
151
165
)
152
166
)
153
167
154
168
def __eq__ (self , other : object ) -> bool :
155
169
"""Compare two CommonMaterial objects."""
156
- if not isinstance (other , CommonMaterial ):
170
+ if not isinstance (other , SimpleMaterial ):
157
171
return False
158
172
return (
159
173
self .ambient_color == other .ambient_color
160
174
and self .diffuse_color == other .diffuse_color
161
175
and self .specular_color == other .specular_color
162
- and self .emissive_color == other .emissive_color
163
176
and self .shininess == other .shininess
164
177
and self .transparency == other .transparency
165
178
)
@@ -169,6 +182,8 @@ def __eq__(self, other: object) -> bool:
169
182
class PbrMaterial :
170
183
"""
171
184
PBR material definition matching OpenCascade's XCAFDoc_VisMaterialPBR.
185
+
186
+ Note: Emission support will be added in a future version with proper texture support.
172
187
"""
173
188
174
189
# Base color and texture
@@ -177,9 +192,6 @@ class PbrMaterial:
177
192
roughness : float
178
193
refraction_index : float
179
194
180
- # Optional properties
181
- emissive_factor : Color = Color (0.0 , 0.0 , 0.0 )
182
-
183
195
def __post_init__ (self ):
184
196
"""Validate the material properties."""
185
197
# Validate ranges
@@ -193,13 +205,7 @@ def __post_init__(self):
193
205
def __hash__ (self ) -> int :
194
206
"""Make PbrMaterial hashable."""
195
207
return hash (
196
- (
197
- self .base_color ,
198
- self .metallic ,
199
- self .roughness ,
200
- self .refraction_index ,
201
- self .emissive_factor ,
202
- )
208
+ (self .base_color , self .metallic , self .roughness , self .refraction_index ,)
203
209
)
204
210
205
211
def __eq__ (self , other : object ) -> bool :
@@ -211,7 +217,6 @@ def __eq__(self, other: object) -> bool:
211
217
and self .metallic == other .metallic
212
218
and self .roughness == other .roughness
213
219
and self .refraction_index == other .refraction_index
214
- and self .emissive_factor == other .emissive_factor
215
220
)
216
221
217
222
@@ -228,12 +233,12 @@ class Material:
228
233
229
234
# Material representations
230
235
color : Optional [Color ] = None
231
- common : Optional [CommonMaterial ] = None
236
+ simple : Optional [SimpleMaterial ] = None
232
237
pbr : Optional [PbrMaterial ] = None
233
238
234
239
def __post_init__ (self ):
235
240
"""Validate that at least one representation is provided."""
236
- if not any ([self .color , self .common , self .pbr ]):
241
+ if not any ([self .color , self .simple , self .pbr ]):
237
242
raise ValueError ("Material must have at least one representation defined" )
238
243
239
244
def __hash__ (self ) -> int :
@@ -244,7 +249,7 @@ def __hash__(self) -> int:
244
249
self .description ,
245
250
self .density ,
246
251
self .color ,
247
- self .common ,
252
+ self .simple ,
248
253
self .pbr ,
249
254
)
250
255
)
@@ -258,6 +263,6 @@ def __eq__(self, other: object) -> bool:
258
263
and self .description == other .description
259
264
and self .density == other .density
260
265
and self .color == other .color
261
- and self .common == other .common
266
+ and self .simple == other .simple
262
267
and self .pbr == other .pbr
263
268
)
0 commit comments