Skip to content

Commit 640d456

Browse files
committed
Track named objects
1 parent c1f4ff6 commit 640d456

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

ShapeScript/EvaluationContext.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ final class EvaluationContext {
4949
var transform = Transform.identity
5050
var childTransform = Transform.identity
5151
var childTypes: Set<ValueType> = [.mesh]
52-
var children = [Value]()
5352
var name: String = ""
53+
var namedObjects: [String: Geometry] = [:]
54+
var children = [Value]() {
55+
didSet {
56+
for case let .mesh(geometry) in children {
57+
geometry.gatherNamedObjects(&namedObjects)
58+
}
59+
}
60+
}
61+
5462
var random: RandomSequence
5563
var detail = 16
5664
var font: String = ""
@@ -92,6 +100,7 @@ final class EvaluationContext {
92100
background = parent.background
93101
material = parent.material
94102
childTypes = parent.childTypes
103+
namedObjects = parent.namedObjects
95104
random = parent.random
96105
detail = parent.detail
97106
font = parent.font
@@ -132,6 +141,7 @@ final class EvaluationContext {
132141
func pushDefinition() -> EvaluationContext {
133142
let new = EvaluationContext(parent: self)
134143
new.name = name
144+
new.namedObjects = namedObjects
135145
new.transform = transform
136146
new.opacity = opacity
137147
new.childTypes = ValueType.any

ShapeScript/Geometry.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ public extension Geometry {
265265
}
266266
}
267267

268+
internal func gatherNamedObjects(_ dictionary: inout [String: Geometry]) {
269+
if let name = self.name {
270+
dictionary[name] = self
271+
}
272+
children.forEach { $0.gatherNamedObjects(&dictionary) }
273+
}
274+
268275
var childDebug: Bool {
269276
debug || children.contains(where: { $0.childDebug })
270277
}

0 commit comments

Comments
 (0)