Skip to content

Commit baed31f

Browse files
committed
Add material scheme validation
1 parent 59dc361 commit baed31f

4 files changed

Lines changed: 91 additions & 62 deletions

File tree

examples/cells.ipynb

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"id": "59e8a2c3",
1717
"metadata": {
1818
"execution": {
19-
"iopub.execute_input": "2025-05-06T10:44:43.184303Z",
20-
"iopub.status.busy": "2025-05-06T10:44:43.184199Z",
21-
"iopub.status.idle": "2025-05-06T10:44:43.590216Z",
22-
"shell.execute_reply": "2025-05-06T10:44:43.589677Z"
19+
"iopub.execute_input": "2025-05-06T11:00:43.020707Z",
20+
"iopub.status.busy": "2025-05-06T11:00:43.020500Z",
21+
"iopub.status.idle": "2025-05-06T11:00:43.437334Z",
22+
"shell.execute_reply": "2025-05-06T11:00:43.435925Z"
2323
},
2424
"vscode": {
2525
"languageId": "plaintext"
@@ -45,10 +45,10 @@
4545
"id": "97c2c4ed",
4646
"metadata": {
4747
"execution": {
48-
"iopub.execute_input": "2025-05-06T10:44:43.592250Z",
49-
"iopub.status.busy": "2025-05-06T10:44:43.591985Z",
50-
"iopub.status.idle": "2025-05-06T10:44:43.661959Z",
51-
"shell.execute_reply": "2025-05-06T10:44:43.661554Z"
48+
"iopub.execute_input": "2025-05-06T11:00:43.438963Z",
49+
"iopub.status.busy": "2025-05-06T11:00:43.438758Z",
50+
"iopub.status.idle": "2025-05-06T11:00:43.508350Z",
51+
"shell.execute_reply": "2025-05-06T11:00:43.507905Z"
5252
},
5353
"vscode": {
5454
"languageId": "plaintext"
@@ -90,10 +90,10 @@
9090
"id": "ed16156a",
9191
"metadata": {
9292
"execution": {
93-
"iopub.execute_input": "2025-05-06T10:44:43.663179Z",
94-
"iopub.status.busy": "2025-05-06T10:44:43.662930Z",
95-
"iopub.status.idle": "2025-05-06T10:44:44.590815Z",
96-
"shell.execute_reply": "2025-05-06T10:44:44.590287Z"
93+
"iopub.execute_input": "2025-05-06T11:00:43.509672Z",
94+
"iopub.status.busy": "2025-05-06T11:00:43.509336Z",
95+
"iopub.status.idle": "2025-05-06T11:00:44.396032Z",
96+
"shell.execute_reply": "2025-05-06T11:00:44.395559Z"
9797
},
9898
"vscode": {
9999
"languageId": "plaintext"
@@ -158,10 +158,10 @@
158158
"id": "d35452de",
159159
"metadata": {
160160
"execution": {
161-
"iopub.execute_input": "2025-05-06T10:44:44.592456Z",
162-
"iopub.status.busy": "2025-05-06T10:44:44.592183Z",
163-
"iopub.status.idle": "2025-05-06T10:44:45.001606Z",
164-
"shell.execute_reply": "2025-05-06T10:44:45.001120Z"
161+
"iopub.execute_input": "2025-05-06T11:00:44.397378Z",
162+
"iopub.status.busy": "2025-05-06T11:00:44.397273Z",
163+
"iopub.status.idle": "2025-05-06T11:00:44.797195Z",
164+
"shell.execute_reply": "2025-05-06T11:00:44.796654Z"
165165
},
166166
"vscode": {
167167
"languageId": "plaintext"
@@ -210,10 +210,10 @@
210210
"id": "c33d38bc",
211211
"metadata": {
212212
"execution": {
213-
"iopub.execute_input": "2025-05-06T10:44:45.006012Z",
214-
"iopub.status.busy": "2025-05-06T10:44:45.005522Z",
215-
"iopub.status.idle": "2025-05-06T10:44:45.014440Z",
216-
"shell.execute_reply": "2025-05-06T10:44:45.013916Z"
213+
"iopub.execute_input": "2025-05-06T11:00:44.800864Z",
214+
"iopub.status.busy": "2025-05-06T11:00:44.800735Z",
215+
"iopub.status.idle": "2025-05-06T11:00:44.806478Z",
216+
"shell.execute_reply": "2025-05-06T11:00:44.806087Z"
217217
},
218218
"vscode": {
219219
"languageId": "plaintext"
@@ -240,10 +240,10 @@
240240
"id": "ea6ed2dd",
241241
"metadata": {
242242
"execution": {
243-
"iopub.execute_input": "2025-05-06T10:44:45.016422Z",
244-
"iopub.status.busy": "2025-05-06T10:44:45.016084Z",
245-
"iopub.status.idle": "2025-05-06T10:44:45.467870Z",
246-
"shell.execute_reply": "2025-05-06T10:44:45.467379Z"
243+
"iopub.execute_input": "2025-05-06T11:00:44.807772Z",
244+
"iopub.status.busy": "2025-05-06T11:00:44.807667Z",
245+
"iopub.status.idle": "2025-05-06T11:00:45.263832Z",
246+
"shell.execute_reply": "2025-05-06T11:00:45.263352Z"
247247
},
248248
"vscode": {
249249
"languageId": "plaintext"
@@ -304,10 +304,10 @@
304304
"id": "b00a9d85",
305305
"metadata": {
306306
"execution": {
307-
"iopub.execute_input": "2025-05-06T10:44:45.470042Z",
308-
"iopub.status.busy": "2025-05-06T10:44:45.469930Z",
309-
"iopub.status.idle": "2025-05-06T10:44:46.041516Z",
310-
"shell.execute_reply": "2025-05-06T10:44:46.041072Z"
307+
"iopub.execute_input": "2025-05-06T11:00:45.266264Z",
308+
"iopub.status.busy": "2025-05-06T11:00:45.266135Z",
309+
"iopub.status.idle": "2025-05-06T11:00:45.821183Z",
310+
"shell.execute_reply": "2025-05-06T11:00:45.820739Z"
311311
},
312312
"vscode": {
313313
"languageId": "plaintext"

examples/cthead.ipynb

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"id": "94b5ef80",
1717
"metadata": {
1818
"execution": {
19-
"iopub.execute_input": "2025-05-06T10:44:47.601141Z",
20-
"iopub.status.busy": "2025-05-06T10:44:47.600958Z",
21-
"iopub.status.idle": "2025-05-06T10:44:48.017883Z",
22-
"shell.execute_reply": "2025-05-06T10:44:48.017342Z"
19+
"iopub.execute_input": "2025-05-06T11:00:47.377758Z",
20+
"iopub.status.busy": "2025-05-06T11:00:47.377654Z",
21+
"iopub.status.idle": "2025-05-06T11:00:47.801179Z",
22+
"shell.execute_reply": "2025-05-06T11:00:47.800864Z"
2323
},
2424
"vscode": {
2525
"languageId": "plaintext"
@@ -51,10 +51,10 @@
5151
"id": "1ee080d3",
5252
"metadata": {
5353
"execution": {
54-
"iopub.execute_input": "2025-05-06T10:44:48.019605Z",
55-
"iopub.status.busy": "2025-05-06T10:44:48.019304Z",
56-
"iopub.status.idle": "2025-05-06T10:44:48.145665Z",
57-
"shell.execute_reply": "2025-05-06T10:44:48.145138Z"
54+
"iopub.execute_input": "2025-05-06T11:00:47.802426Z",
55+
"iopub.status.busy": "2025-05-06T11:00:47.802271Z",
56+
"iopub.status.idle": "2025-05-06T11:00:47.923854Z",
57+
"shell.execute_reply": "2025-05-06T11:00:47.923587Z"
5858
},
5959
"vscode": {
6060
"languageId": "plaintext"
@@ -112,10 +112,10 @@
112112
"id": "7af5a922",
113113
"metadata": {
114114
"execution": {
115-
"iopub.execute_input": "2025-05-06T10:44:48.147262Z",
116-
"iopub.status.busy": "2025-05-06T10:44:48.147155Z",
117-
"iopub.status.idle": "2025-05-06T10:44:49.107240Z",
118-
"shell.execute_reply": "2025-05-06T10:44:49.106911Z"
115+
"iopub.execute_input": "2025-05-06T11:00:47.924813Z",
116+
"iopub.status.busy": "2025-05-06T11:00:47.924710Z",
117+
"iopub.status.idle": "2025-05-06T11:00:48.851246Z",
118+
"shell.execute_reply": "2025-05-06T11:00:48.850703Z"
119119
},
120120
"vscode": {
121121
"languageId": "plaintext"
@@ -180,10 +180,10 @@
180180
"id": "493f1153",
181181
"metadata": {
182182
"execution": {
183-
"iopub.execute_input": "2025-05-06T10:44:49.108793Z",
184-
"iopub.status.busy": "2025-05-06T10:44:49.108686Z",
185-
"iopub.status.idle": "2025-05-06T10:44:49.606622Z",
186-
"shell.execute_reply": "2025-05-06T10:44:49.606177Z"
183+
"iopub.execute_input": "2025-05-06T11:00:48.853248Z",
184+
"iopub.status.busy": "2025-05-06T11:00:48.852828Z",
185+
"iopub.status.idle": "2025-05-06T11:00:49.349134Z",
186+
"shell.execute_reply": "2025-05-06T11:00:49.348770Z"
187187
},
188188
"vscode": {
189189
"languageId": "plaintext"
@@ -234,10 +234,10 @@
234234
"id": "6e7f236c",
235235
"metadata": {
236236
"execution": {
237-
"iopub.execute_input": "2025-05-06T10:44:49.609085Z",
238-
"iopub.status.busy": "2025-05-06T10:44:49.608975Z",
239-
"iopub.status.idle": "2025-05-06T10:44:50.447964Z",
240-
"shell.execute_reply": "2025-05-06T10:44:50.447516Z"
237+
"iopub.execute_input": "2025-05-06T11:00:49.352283Z",
238+
"iopub.status.busy": "2025-05-06T11:00:49.352129Z",
239+
"iopub.status.idle": "2025-05-06T11:00:50.193974Z",
240+
"shell.execute_reply": "2025-05-06T11:00:50.193393Z"
241241
},
242242
"vscode": {
243243
"languageId": "plaintext"
@@ -298,10 +298,10 @@
298298
"id": "5d71b6c8",
299299
"metadata": {
300300
"execution": {
301-
"iopub.execute_input": "2025-05-06T10:44:50.450105Z",
302-
"iopub.status.busy": "2025-05-06T10:44:50.449992Z",
303-
"iopub.status.idle": "2025-05-06T10:44:50.503136Z",
304-
"shell.execute_reply": "2025-05-06T10:44:50.502791Z"
301+
"iopub.execute_input": "2025-05-06T11:00:50.195575Z",
302+
"iopub.status.busy": "2025-05-06T11:00:50.195468Z",
303+
"iopub.status.idle": "2025-05-06T11:00:50.246105Z",
304+
"shell.execute_reply": "2025-05-06T11:00:50.245722Z"
305305
},
306306
"vscode": {
307307
"languageId": "plaintext"
@@ -355,10 +355,10 @@
355355
"id": "f2495cbb",
356356
"metadata": {
357357
"execution": {
358-
"iopub.execute_input": "2025-05-06T10:44:50.504158Z",
359-
"iopub.status.busy": "2025-05-06T10:44:50.504044Z",
360-
"iopub.status.idle": "2025-05-06T10:44:50.557255Z",
361-
"shell.execute_reply": "2025-05-06T10:44:50.556916Z"
358+
"iopub.execute_input": "2025-05-06T11:00:50.247491Z",
359+
"iopub.status.busy": "2025-05-06T11:00:50.247380Z",
360+
"iopub.status.idle": "2025-05-06T11:00:50.307308Z",
361+
"shell.execute_reply": "2025-05-06T11:00:50.306770Z"
362362
},
363363
"vscode": {
364364
"languageId": "plaintext"
@@ -393,7 +393,7 @@
393393
" features={\n",
394394
" libcarna.opaque.ROLE_DEFAULT_MESH: ball,\n",
395395
" libcarna.opaque.ROLE_DEFAULT_MATERIAL:\n",
396-
" libcarna.material('solid', color=(1, 0, 0, 1)),\n",
396+
" libcarna.material(color=libcarna.color.RED),\n",
397397
" },\n",
398398
")\n",
399399
"\n",
@@ -405,7 +405,7 @@
405405
" features={\n",
406406
" libcarna.opaque.ROLE_DEFAULT_MESH: ball,\n",
407407
" libcarna.opaque.ROLE_DEFAULT_MATERIAL:\n",
408-
" libcarna.material('solid', color=(0, 1, 0, 1)),\n",
408+
" libcarna.material(color=libcarna.color.GREEN),\n",
409409
" },\n",
410410
")\n",
411411
"\n",

misc/libcarna/_material.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
import libcarna
22

33

4-
def material(shader_name: str, **kwargs) -> libcarna.base.Material:
4+
def scheme_color(value):
5+
if isinstance(value, libcarna.base.Color):
6+
return value.toarray()
7+
elif hasattr(value, '__len__') and len(value) == 4:
8+
return value
9+
else:
10+
raise ValueError(f'Found "{value}", expected color with 4 components (RGBA).')
11+
12+
13+
shader_schemes = {
14+
'unshaded': {
15+
'color': scheme_color,
16+
},
17+
'solid': {
18+
'color': scheme_color,
19+
},
20+
}
21+
22+
23+
def material(shader_name: str = 'solid', **kwargs) -> libcarna.base.Material:
524
"""
625
Create a :class:`libcarna.base.Material` object.
726
827
Arguments:
928
shader_name: The shader to be used for rendering.
1029
**kwargs: Uniform shader parameters.
1130
"""
31+
assert shader_name in shader_schemes, (
32+
f'Unknown shader name: "{shader_name}" (supported: {", ".join(shader_schemes.keys())})'
33+
)
34+
shader_scheme = shader_schemes[shader_name]
35+
1236
class Material(libcarna.base.Material):
1337

1438
def __init__(self, *args, **kwargs):
1539
super().__init__(*args, **kwargs)
1640

1741
def __setitem__(self, key, value):
18-
if shader_name in ('unshaded', 'solid') and (not hasattr(value, '__len__') or len(value) != 4):
19-
raise ValueError(f'Material "{shader_name}" requires a color value with 4 components (RGBA).')
42+
assert key in shader_scheme, (
43+
f'Unknown shader parameter: "{key}" (supported: {", ".join(shader_scheme.keys())})'
44+
)
45+
value = shader_scheme[key](value)
2046
super().__setitem__(key, value)
2147

2248
material = Material(shader_name)

misc/libcarna/_py.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def _expand_module(module):
2626

2727
# Resolve target name
2828
target_name = _camel_to_snake(member_name)
29-
target_name = _strip_suffix(target_name, '_rendering_stage')
29+
if target_name == 'mask_rendering_stage':
30+
target_name = 'mask_renderer'
31+
else:
32+
target_name = _strip_suffix(target_name, '_rendering_stage')
3033
target_name = _strip_suffix(target_name, '_stage')
3134

3235
# Skip if the target already exists

0 commit comments

Comments
 (0)