Skip to content

Commit 33ac382

Browse files
committed
Added pose_stl() module because pose() only works for assembly views.
1 parent 6a58d37 commit 33ac382

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7847,6 +7847,8 @@ The `pose()` module allows assembly views in the readme to be posed differently
78477847
* To get the parameter values make the GUI window square, pose the view with the mouse and then copy the viewport parameters from the Edit menu and paste them into the pose invocation.
78487848
* Two `pose()` modules can be chained to allow different poses for exploded and assembled views.
78497849

7850+
The `pose_stl()` module allows an STL child to be posed for its rendered image used in the readme for the project.
7851+
78507852
[utils/core/bom.scad](utils/core/bom.scad) Implementation.
78517853

78527854
[tests/BOM.scad](tests/BOM.scad) Code for this example.
@@ -7870,8 +7872,9 @@ The `pose()` module allows assembly views in the readme to be posed differently
78707872
| `no_explode()` | Prevent children being exploded |
78717873
| `no_pose()` | Force children not to be posed even if parent is |
78727874
| `not_on_bom(on = false)` | Specify the following child parts are not on the BOM, for example when they are on a PCB that comes assembled |
7873-
| `pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef, d = undef)` | Pose an STL or assembly for rendering to png by specifying rotation `a`, translation `t` and optionally `d`, `exploded = true for` just the exploded view or `false` for unexploded only. |
7875+
| `pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef, d = undef)` | Pose an assembly for rendering to png by specifying rotation `a`, translation `t` and optionally `d`, `exploded = true for` just the exploded view or `false` for unexploded only. |
78747876
| `pose_hflip(exploded = undef)` | Pose an STL or assembly for rendering to png by flipping around the Y axis, `exploded = true for` just the exploded view or `false` for unexploded only. |
7877+
| `pose_stl(a = [70, 0, 315], t = [0, 0, 0], d = 500)` | Pose an STL for its render, `a`, `t`, & `d` are camera parameters. |
78757878
| `pose_vflip(exploded = undef)` | Pose an STL or assembly for rendering to png by flipping around the X axis, `exploded = true for` just the exploded view or `false` for unexploded only. |
78767879
| `stl(name)` | Name an stl that will appear on the BOM, there needs to a module named `<name>_stl` to make it |
78777880
| `stl_colour(colour = pp1_colour, alpha = 1)` | Colour an stl where it is placed in an assembly. `alpha` can be used to make it appear transparent. |

scripts/bom.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ def __init__(self, args):
6969
self.count = 1
7070
for arg in args:
7171
arg = arg.replace('true', 'True').replace('false', 'False').replace('undef', 'None')
72-
exec('self.' + arg)
72+
try:
73+
exec('self.' + arg)
74+
except:
75+
print(arg)
7376

7477
def data(self):
7578
return self.__dict__
@@ -107,7 +110,7 @@ def add_part(self, s):
107110
match = re.match(r'^(.*?\.stl|.*?\.dxf|.*?\.svg)\((.*)\)$', s) #look for name.stl(...), name.dxf(...) or name.svg(...)
108111
if match:
109112
s = match.group(1)
110-
args = [match.group(2)]
113+
args = match.group(2).split('|')
111114
if s[-4:] == ".stl":
112115
parts = self.printed
113116
else:

scripts/render.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ def render(target, type):
6161

6262
things = { 'stl' : 'printed', 'dxf' : 'routed', 'svg' : 'routed' }[type]
6363
colours = {}
64+
cameras = {}
6465
for ass in flat_bom:
6566
for part in ass[things]:
6667
obj = ass[things][part]
6768
if "colour" in obj:
6869
colours[part] = obj["colour"]
70+
if "camera" in obj:
71+
cameras[part] = obj["camera"]
6972
#
7073
# Remove unused png files
7174
#
@@ -87,17 +90,20 @@ def render(target, type):
8790
png_maker_name = tmp_dir + "/png.scad"
8891
pp1 = [0, 146/255, 0]
8992
colour = pp1
93+
camera = "0,0,0,70,0,315,500"
94+
if part in cameras:
95+
camera = cameras[part]
9096
if part in colours:
9197
colour = colours[part]
9298
if not '[' in colour:
9399
colour = '"' + colour + '"'
94100
with open(png_maker_name, "w") as f:
95101
f.write('color(%s) import("%s");\n' % (colour, reltmp(part_file, target)))
96-
cam = "--camera=0,0,0,70,0,315,500" if type == 'stl' else "--camera=0,0,0,0,0,0,500"
102+
cam = "--camera=" + camera if type == 'stl' else "--camera=0,0,0,0,0,0,500"
97103
render = "--preview" if type == 'stl' or colour != pp1 else "--render"
98104
tmp_name = tmp_dir + '/' + part[:-4] + '.png'
99105
dummy_deps_name = tmp_dir + '/tmp.deps' # work around for OpenSCAD issue #3879
100-
openscad.run("-o", tmp_name, png_maker_name, colour_scheme, "--projection=p", image_size, cam, render, "-D$pose=1", "-D$explode=0", "--autocenter", "--viewall", "-d", dummy_deps_name)
106+
openscad.run("-o", tmp_name, png_maker_name, colour_scheme, "--projection=p", image_size, cam, render, "--autocenter", "--viewall", "-d", dummy_deps_name)
101107
do_cmd(("magick "+ tmp_name + " -trim -resize 280x280 -background %s -gravity Center -extent 280x280 -bordercolor %s -border 10 %s"
102108
% (background, background, tmp_name)).split())
103109
update_image(tmp_name, png_name)

utils/core/bom.scad

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
//! * If the `d` parameter is set to specify the camera distance then the normal `viewall` and `autocenter` options are suppressed allowing a small section to be zoomed in to fill the view.
4242
//! * To get the parameter values make the GUI window square, pose the view with the mouse and then copy the viewport parameters from the Edit menu and paste them into the pose invocation.
4343
//! * Two `pose()` modules can be chained to allow different poses for exploded and assembled views.
44+
//!
45+
//! The `pose_stl()` module allows an STL child to be posed for its rendered image used in the readme for the project.
4446
//
4547
function bom_mode(n = 1) = (is_undef($bom) ? 0 : $bom) >= n && (is_undef($on_bom) || $on_bom); //! Current BOM mode, 0 = none, 1 = printed and routed parts and assemblies, 2 includes vitamins as well
4648
function exploded() = is_undef($exploded_parent) ? is_undef($explode) ? 0 : $explode : 0; //! Returns the value of `$explode` if it is defined, else `0`
@@ -73,7 +75,7 @@ module explode(d, explode_children = false, offset = [0,0,0], show_line = true)
7375
module no_pose() //! Force children not to be posed even if parent is
7476
let($posed = true, $zoomed = undef) children();
7577

76-
module pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef, d = undef) //! Pose an STL or assembly for rendering to png by specifying rotation `a`, translation `t` and optionally `d`, `exploded = true for` just the exploded view or `false` for unexploded only.
78+
module pose(a = [55, 0, 25], t = [0, 0, 0], exploded = undef, d = undef) //! Pose an assembly for rendering to png by specifying rotation `a`, translation `t` and optionally `d`, `exploded = true for` just the exploded view or `false` for unexploded only.
7779
let($zoomed = is_undef(d)
7880
? is_undef($zoomed)
7981
? undef
@@ -136,10 +138,15 @@ module stl_colour(colour = pp1_colour, alpha = 1) { //! Colour an stl where it i
136138
children();
137139
}
138140

141+
module pose_stl(a = [70, 0, 315], t = [0, 0, 0], d = 500) //! Pose an STL for its render, `a`, `t`, & `d` are camera parameters.
142+
let($stl_camera = str(t.x, ",", t.y, ",", t.z, ",", a.x, ",", a.y, ",", a.z, ",", d))
143+
children();
144+
139145
module stl(name) { //! Name an stl that will appear on the BOM, there needs to a module named `<name>_stl` to make it
140146
if(bom_mode() && is_undef($in_stl)) {
141147
colour = is_undef($stl_colour) ? pp1_colour : $stl_colour;
142-
echo(str("~", name, ".stl(colour='", colour, "')"));
148+
camera = is_undef($stl_camera) ? "0,0,0,70,0,315,500" : $stl_camera;
149+
echo(str("~", name, ".stl(colour='", colour, "'| camera='", camera, "')" ));
143150
}
144151
if($children)
145152
if(is_undef($pose))

0 commit comments

Comments
 (0)