Skip to content

Commit 1f4b45b

Browse files
committed
Add documentation for block children property
1 parent 458ecf1 commit 1f4b45b

File tree

11 files changed

+231
-6
lines changed

11 files changed

+231
-6
lines changed

docs/images/sliced-shape.png

39 KB
Loading

docs/images/stacked-shapes.png

39.2 KB
Loading

docs/ios/blocks.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ star
2929

3030
![Star](../images/star.png)
3131

32-
**Note:** there is a subtle distinction between the code above and the code below:
32+
**Note:** There is a subtle distinction between the code above and the code below:
3333

3434
```swift
3535
define star path {
@@ -80,5 +80,41 @@ star {
8080

8181
![Star](../images/six-pointed-star.png)
8282

83+
## Children
84+
85+
ShapeScript's [builder](builders.md) and [csg](csg.md) block commands accept child shapes which they use as inputs to construct a mesh:
86+
87+
```swift
88+
difference {
89+
cube { size 1 }
90+
sphere { size 1.1 }
91+
}
92+
```
93+
94+
You can do the same with your own custom blocks by using the `children` property. This property is available inside a block definition, and returns a [tuple](literals.md#vectors-and-tuples) of whatever child objects were passed in by the caller.
95+
96+
Here is a simple block that takes whatever child objects are passed in and stacks them vertically along the Y-axis:
97+
98+
```swift
99+
// define reusable stack command
100+
define stack {
101+
for mesh in children {
102+
mesh
103+
translate 0 mesh.bounds.size.height
104+
}
105+
}
106+
107+
// stack some shapes
108+
stack {
109+
cube { color red }
110+
sphere { color green }
111+
cone { color blue }
112+
}
113+
```
114+
115+
![Stacked shapes](../images/stacked-shapes.png)
116+
117+
**Note:** The children passed to a block can be any type, not just paths or meshes. ShapeScript uses [type inference](https://en.wikipedia.org/wiki/Type_inference) to raise an error if the caller passes children of a different type than the block expects.
118+
83119
---
84120
[Index](index.md) | Next: [Scope](scope.md)

docs/ios/functions.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,45 @@ define almostEqual(a b) {
297297
}
298298
```
299299

300-
Unlike [block options](blocks.md#options), function inputs do not have default values. Calling a function without passing a value for every input will result in an error.
300+
**Note:** Unlike [block options](blocks.md#options), function parameters do not have default values. Calling a function without passing a value for every parameter will result in an error.
301+
302+
ShapeScript uses [type inference](https://en.wikipedia.org/wiki/Type_inference) to raise an error if the caller passes values of a different type than the function expects.
303+
304+
Function parameters can be any type, including paths and meshes. Here is a function that splits a shape into horizontal slices:
305+
306+
```swift
307+
// function to split a mesh into slices
308+
define split(shape slices) {
309+
define axis 0 1 0 // y-axis
310+
define offset shape.bounds.center
311+
define shapeSize shape.bounds.size
312+
define step shapeSize / slices
313+
for i in 1 to slices {
314+
difference {
315+
shape
316+
cube {
317+
size shapeSize
318+
position offset + axis * i * step
319+
}
320+
cube {
321+
size shapeSize
322+
position offset + axis * ((i - 1) * step - shapeSize)
323+
}
324+
}
325+
}
326+
}
327+
328+
// split a sphere into 5 slices
329+
define result split(sphere 5)
330+
331+
// display the result
332+
for slice in result {
333+
slice
334+
translate 0.2
335+
}
336+
```
337+
338+
![Sliced shape](../images/sliced-shape.png)
301339

302340
---
303341
[Index](index.md) | Next: [Commands](commands.md)

docs/ios/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ ShapeScript Help
147147
- [Conditional Defines](control-flow.md#conditional-defines)
148148
- [Blocks](blocks.md)
149149
- [Options](blocks.md#options)
150+
- [Children](blocks.md#children)
150151
- [Scope](scope.md)
151152
- [Block Scope](scope.md#block-scope)
152153
- [Function Scope](scope.md#function-scope)

docs/mac/blocks.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ star
2929

3030
![Star](../images/star.png)
3131

32-
**Note:** there is a subtle distinction between the code above and the code below:
32+
**Note:** There is a subtle distinction between the code above and the code below:
3333

3434
```swift
3535
define star path {
@@ -80,5 +80,41 @@ star {
8080

8181
![Star](../images/six-pointed-star.png)
8282

83+
## Children
84+
85+
ShapeScript's [builder](builders.md) and [csg](csg.md) block commands accept child shapes which they use as inputs to construct a mesh:
86+
87+
```swift
88+
difference {
89+
cube { size 1 }
90+
sphere { size 1.1 }
91+
}
92+
```
93+
94+
You can do the same with your own custom blocks by using the `children` property. This property is available inside a block definition, and returns a [tuple](literals.md#vectors-and-tuples) of whatever child objects were passed in by the caller.
95+
96+
Here is a simple block that takes whatever child objects are passed in and stacks them vertically along the Y-axis:
97+
98+
```swift
99+
// define reusable stack command
100+
define stack {
101+
for mesh in children {
102+
mesh
103+
translate 0 mesh.bounds.size.height
104+
}
105+
}
106+
107+
// stack some shapes
108+
stack {
109+
cube { color red }
110+
sphere { color green }
111+
cone { color blue }
112+
}
113+
```
114+
115+
![Stacked shapes](../images/stacked-shapes.png)
116+
117+
**Note:** The children passed to a block can be any type, not just paths or meshes. ShapeScript uses [type inference](https://en.wikipedia.org/wiki/Type_inference) to raise an error if the caller passes children of a different type than the block expects.
118+
83119
---
84120
[Index](index.md) | Next: [Scope](scope.md)

docs/mac/functions.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,45 @@ define almostEqual(a b) {
297297
}
298298
```
299299

300-
Unlike [block options](blocks.md#options), function inputs do not have default values. Calling a function without passing a value for every input will result in an error.
300+
**Note:** Unlike [block options](blocks.md#options), function parameters do not have default values. Calling a function without passing a value for every parameter will result in an error.
301+
302+
ShapeScript uses [type inference](https://en.wikipedia.org/wiki/Type_inference) to raise an error if the caller passes values of a different type than the function expects.
303+
304+
Function parameters can be any type, including paths and meshes. Here is a function that splits a shape into horizontal slices:
305+
306+
```swift
307+
// function to split a mesh into slices
308+
define split(shape slices) {
309+
define axis 0 1 0 // y-axis
310+
define offset shape.bounds.center
311+
define shapeSize shape.bounds.size
312+
define step shapeSize / slices
313+
for i in 1 to slices {
314+
difference {
315+
shape
316+
cube {
317+
size shapeSize
318+
position offset + axis * i * step
319+
}
320+
cube {
321+
size shapeSize
322+
position offset + axis * ((i - 1) * step - shapeSize)
323+
}
324+
}
325+
}
326+
}
327+
328+
// split a sphere into 5 slices
329+
define result split(sphere 5)
330+
331+
// display the result
332+
for slice in result {
333+
slice
334+
translate 0.2
335+
}
336+
```
337+
338+
![Sliced shape](../images/sliced-shape.png)
301339

302340
---
303341
[Index](index.md) | Next: [Commands](commands.md)

docs/mac/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ ShapeScript Help
147147
- [Conditional Defines](control-flow.md#conditional-defines)
148148
- [Blocks](blocks.md)
149149
- [Options](blocks.md#options)
150+
- [Children](blocks.md#children)
150151
- [Scope](scope.md)
151152
- [Block Scope](scope.md#block-scope)
152153
- [Function Scope](scope.md#function-scope)

docs/src/blocks.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ star
2929

3030
![Star](../images/star.png)
3131

32-
**Note:** there is a subtle distinction between the code above and the code below:
32+
**Note:** There is a subtle distinction between the code above and the code below:
3333

3434
```swift
3535
define star path {
@@ -80,5 +80,41 @@ star {
8080

8181
![Star](../images/six-pointed-star.png)
8282

83+
## Children
84+
85+
ShapeScript's [builder](builders.md) and [csg](csg.md) block commands accept child shapes which they use as inputs to construct a mesh:
86+
87+
```swift
88+
difference {
89+
cube { size 1 }
90+
sphere { size 1.1 }
91+
}
92+
```
93+
94+
You can do the same with your own custom blocks by using the `children` property. This property is available inside a block definition, and returns a [tuple](literals.md#vectors-and-tuples) of whatever child objects were passed in by the caller.
95+
96+
Here is a simple block that takes whatever child objects are passed in and stacks them vertically along the Y-axis:
97+
98+
```swift
99+
// define reusable stack command
100+
define stack {
101+
for mesh in children {
102+
mesh
103+
translate 0 mesh.bounds.size.height
104+
}
105+
}
106+
107+
// stack some shapes
108+
stack {
109+
cube { color red }
110+
sphere { color green }
111+
cone { color blue }
112+
}
113+
```
114+
115+
![Stacked shapes](../images/stacked-shapes.png)
116+
117+
**Note:** The children passed to a block can be any type, not just paths or meshes. ShapeScript uses [type inference](https://en.wikipedia.org/wiki/Type_inference) to raise an error if the caller passes children of a different type than the block expects.
118+
83119
---
84120
[Index](index.md) | Next: [Scope](scope.md)

docs/src/functions.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,45 @@ define almostEqual(a b) {
297297
}
298298
```
299299

300-
Unlike [block options](blocks.md#options), function inputs do not have default values. Calling a function without passing a value for every input will result in an error.
300+
**Note:** Unlike [block options](blocks.md#options), function parameters do not have default values. Calling a function without passing a value for every parameter will result in an error.
301+
302+
ShapeScript uses [type inference](https://en.wikipedia.org/wiki/Type_inference) to raise an error if the caller passes values of a different type than the function expects.
303+
304+
Function parameters can be any type, including paths and meshes. Here is a function that splits a shape into horizontal slices:
305+
306+
```swift
307+
// function to split a mesh into slices
308+
define split(shape slices) {
309+
define axis 0 1 0 // y-axis
310+
define offset shape.bounds.center
311+
define shapeSize shape.bounds.size
312+
define step shapeSize / slices
313+
for i in 1 to slices {
314+
difference {
315+
shape
316+
cube {
317+
size shapeSize
318+
position offset + axis * i * step
319+
}
320+
cube {
321+
size shapeSize
322+
position offset + axis * ((i - 1) * step - shapeSize)
323+
}
324+
}
325+
}
326+
}
327+
328+
// split a sphere into 5 slices
329+
define result split(sphere 5)
330+
331+
// display the result
332+
for slice in result {
333+
slice
334+
translate 0.2
335+
}
336+
```
337+
338+
![Sliced shape](../images/sliced-shape.png)
301339

302340
---
303341
[Index](index.md) | Next: [Commands](commands.md)

0 commit comments

Comments
 (0)