Skip to content

Commit 8e05fe7

Browse files
committed
make colors functional in openscad viewer
couldn't make it work in web though
1 parent cd825ed commit 8e05fe7

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

solid_node/node/base.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class AbstractBaseNode:
4040
# All children nodes, initialized as tuple for compliance
4141
children = tuple()
4242

43+
# Set to false to render scad directly instead of stls
44+
optimize = True
45+
4346
def __init__(self, *args, name=None, **kwargs):
4447
# self.uniq_id uniquely identifies a set of parameters of an instance.
4548
# self.name is used to refer to nodes in tests
@@ -127,9 +130,14 @@ def assemble(self, root=None):
127130

128131
self.validate(rendered)
129132
self.model = self.as_scad(rendered)
133+
if not self.optimize:
134+
self.model = self._colorize(self.model)
130135
self.generate_scad()
131136

132-
assembled = self.import_optimized()
137+
if self.optimize:
138+
assembled = self.import_optimized()
139+
else:
140+
assembled = self.model
133141

134142
for operation in self.operations:
135143
# Apply scad operation
@@ -143,8 +151,19 @@ def import_optimized(self):
143151
if self.rigid and self._up_to_date(self.stl_file):
144152
basedir = os.path.relpath(self.basedir, self.root)
145153
local_stl = os.path.join(basedir, self.local_stl)
146-
return import_stl(local_stl)
147-
return self.model
154+
imported_stl = import_stl(local_stl)
155+
return self._colorize(imported_stl)
156+
return self._colorize(self.model)
157+
158+
def _colorize(self, scad_code):
159+
if self.color is None:
160+
return scad_code
161+
hex_code = self.color.lstrip('#')
162+
if len(hex_code) != 6:
163+
raise ValueError(f"Invalid self.color at {self}. "
164+
"It should be in the format #RRGGBB")
165+
colors = [int(hex_code[i:i + 2], 16) / 255 for i in (0, 2, 4)]
166+
return color(colors, 1)(scad_code)
148167

149168
@property
150169
def stl(self):

solid_node/viewers/web/app/src/node.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface NodeData {
2121
children?: string[];
2222
mtime: number;
2323
operations: RawOperation[];
24+
color: string;
2425
}
2526

2627
const stlLoader = new STLLoader();
@@ -44,6 +45,8 @@ export abstract class Node {
4445

4546
mtime: number;
4647

48+
color: string;
49+
4750
// Operations matrix.
4851
// Each layer of the tree has a list of operations
4952
// rawOperations contains functions (can use $t or any variable)
@@ -60,6 +63,7 @@ export abstract class Node {
6063
this.children = [];
6164
this.rawOperations = [];
6265
this.operations = [];
66+
this.color = data.color;
6367
}
6468

6569
setOperations(op: RawOperation[], level: number = 0) {
@@ -102,7 +106,18 @@ export abstract class Node {
102106
const tstamp = new Date().getTime(); // avoid cache
103107

104108
stlLoader.load(`/node${this.path}${this.model}?t=${tstamp}`, (geometry) => {
105-
const material = new THREE.MeshNormalMaterial();
109+
let material;
110+
if (this.color) {
111+
const color = new THREE.Color(this.color);
112+
material = new THREE.MeshBasicMaterial({
113+
color: color,
114+
//metalness: 0.2,
115+
//roughness: 0.5,
116+
//emissive: color,
117+
});
118+
} else {
119+
material = new THREE.MeshNormalMaterial();
120+
}
106121
const mesh = new THREE.Mesh(geometry, material);
107122
if (this.context.scene) {
108123
if (this.mesh) {

solid_node/viewers/web/viewer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ async def state(self):
174174
'operations': self.operations,
175175
'type': self.node._type,
176176
'name': self.node.name,
177+
'color': self.node.color
177178
}
178179
if self.children:
179180
state['children'] = self.children

0 commit comments

Comments
 (0)