discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Makefile for building complex models from a single file

WC
W. Craig Trader
Wed, Sep 27, 2017 11:47 PM

I've been using OpenSCAD for ~6 years, and it's my go-to tool for both 2D
and 3D modeling. Recently I've been using OpenSCAD to model organizers for
some of my board games, and beyond a certain point it became irritating to
manually export the STL files. So I started writing makefiles. The manual
gives some suggestions for using makefiles, but I found them to be somewhat
clunky. So I did something that worked for me. I'm sharing it here; maybe
it will work for someone else.

Goals:

  • Adhere to DRY (Don't Repeat Yourself) -- there should be no
    duplication of information between the Makefile and the OpenSCAD file.
  • Generate STL models and PNG images with OpenSCAD, and then slice the
    models with Slic3r.
  • All generated files should go into segregated subdirectories.
  • The OpenSCAD file should still be useful without the Makefile.
  • If I change the OpenSCAD file, I accept that everything is going to be
    rebuilt. (Computers are fast.)
  • Work on Linux.

Here's what I ended up with:

Splendor organizers to fit in Splendor box

by W. Craig Trader is dual-licensed under

Creative Commons Attribution-ShareAlike 3.0 Unported License and

GNU Lesser GPL 3.0 or later.

This makefile expects an OpenSCAD file named $(PREFIX).scad that defines

the following variables:

PART    -- the name of a part to generate

VERBOSE -- logically true if you want verbose output (optional)

The OpenSCAD program should have logic that renders a single part by name:

if (PART == "foo") {

foo();

} else if (PART == "bar") {

bar();

} else if (PART == "foo-bar") {

foo_bar();

}

This makefile will use OpenSCAD to create individual model (STL) files

and

image (PNG) files for each declared part, and will use Slic3r to generate

G-Code for each declared part.

So if PREFIX=widget, then the created files will be:

stl/widget-foo.stl stl/widget-bar.stl stl/widget-foo-bar.stl

png/widget-foo.png png/widget-bar.png png/widget-foo-bar.png

gcode/widget-foo.gcode gcode/widget-bar.gcode gcode/widget-foo-bar.gcode

OpenSCAD binary and options

OPENSCAD=/usr/bin/openscad
OPENSCAD_OPTIONS=-DVERBOSE=false
IMAGE_OPTIONS=--imgsize=1024,768 --colorscheme=DeepOcean

Slic3r binary and options

SLIC3R=/opt/Slic3r/latest/slic3r
SLIC3R_INI=slic3r.ini
SLIC3R_OPTIONS=--load $(SLIC3R_INI) --print-center 125,105

Name of OpenSCAD model file

PREFIX=splendor

----- Everything after this should not need modification

Names of parts to build

PARTS=$(shell grep 'PART == ' $(PREFIX).scad | cut -d'"' -f2)

STL=stl
IMAGE=png
GCODE=gcode

MODELS=$(patsubst %,$(STL)/$(PREFIX)-%.$(STL),$(PARTS))
IMAGES=$(patsubst %,$(IMAGE)/$(PREFIX)-%.$(IMAGE),$(PARTS))
GCODES=$(patsubst %,$(GCODE)/$(PREFIX)-%.$(GCODE),$(PARTS))

all: models gcodes images

directories:
@mkdir -p $(STL) $(IMAGE) $(GCODE)

models: directories $(MODELS)

gcodes: directories $(GCODES)

images: directories $(IMAGES)

clean:
rm -rf $(STL) $(GCODE) $(IMAGE)

Dependencies for models

$(MODELS) : $(STL)/$(PREFIX)-%.$(STL) : $(PREFIX).scad
$(OPENSCAD) $(OPENSCAD_OPTIONS) -o $@ -DPART="$(subst $(PREFIX)-,,$(subst
.$(STL),,$(@F)))" $<

Dependencies for images

$(IMAGES) : $(IMAGE)/$(PREFIX)-%.$(IMAGE) : $(PREFIX).scad
$(OPENSCAD) $(OPENSCAD_OPTIONS) -o $@ -DPART="$(subst $(PREFIX)-,,$(subst
.$(IMAGE),,$(@F)))" $(IMAGE_OPTIONS) $<

Dependencies for slicing

$(GCODES) : $(GCODE)/%.$(GCODE) : $(STL)/%.$(STL) $(SLIC3R_INI)
$(SLIC3R) -o $@ $(SLIC3R_OPTIONS) $<

When I want to add this Makefile to an existing project, all I usually have
to do is set the PREFIX variable in the Makefile, and then add the switch
block at the bottom of the OpenSCAD source file.

You can find source code examples on GitHub:

https://github.com/wcraigtrader/game-parts/blob/master/splendor/Makefile
https://github.com/wcraigtrader/game-parts/blob/master/splendor/splendor.scad

Feedback is always appreciated.

  • Craig -
I've been using OpenSCAD for ~6 years, and it's my go-to tool for both 2D and 3D modeling. Recently I've been using OpenSCAD to model organizers for some of my board games, and beyond a certain point it became irritating to manually export the STL files. So I started writing makefiles. The manual gives some suggestions for using makefiles, but I found them to be somewhat clunky. So I did something that worked for me. I'm sharing it here; maybe it will work for someone else. *Goals:* - Adhere to DRY (Don't Repeat Yourself) -- there should be no duplication of information between the Makefile and the OpenSCAD file. - Generate STL models and PNG images with OpenSCAD, and then slice the models with Slic3r. - All generated files should go into segregated subdirectories. - The OpenSCAD file should still be useful without the Makefile. - If I change the OpenSCAD file, I accept that everything is going to be rebuilt. (Computers are fast.) - Work on Linux. Here's what I ended up with: # Splendor organizers to fit in Splendor box # # by W. Craig Trader is dual-licensed under # Creative Commons Attribution-ShareAlike 3.0 Unported License and # GNU Lesser GPL 3.0 or later. # This makefile expects an OpenSCAD file named $(PREFIX).scad that defines # the following variables: # # PART -- the name of a part to generate # VERBOSE -- logically true if you want verbose output (optional) # # The OpenSCAD program should have logic that renders a single part by name: # # if (PART == "foo") { # foo(); # } else if (PART == "bar") { # bar(); # } else if (PART == "foo-bar") { # foo_bar(); # } # # This makefile will use OpenSCAD to create individual model (STL) files and # image (PNG) files for each declared part, and will use Slic3r to generate # G-Code for each declared part. # # So if PREFIX=widget, then the created files will be: # # stl/widget-foo.stl stl/widget-bar.stl stl/widget-foo-bar.stl # png/widget-foo.png png/widget-bar.png png/widget-foo-bar.png # gcode/widget-foo.gcode gcode/widget-bar.gcode gcode/widget-foo-bar.gcode # OpenSCAD binary and options OPENSCAD=/usr/bin/openscad OPENSCAD_OPTIONS=-DVERBOSE=false IMAGE_OPTIONS=--imgsize=1024,768 --colorscheme=DeepOcean # Slic3r binary and options SLIC3R=/opt/Slic3r/latest/slic3r SLIC3R_INI=slic3r.ini SLIC3R_OPTIONS=--load $(SLIC3R_INI) --print-center 125,105 # Name of OpenSCAD model file PREFIX=splendor # ----- Everything after this should not need modification # Names of parts to build PARTS=$(shell grep 'PART == ' $(PREFIX).scad | cut -d'"' -f2) STL=stl IMAGE=png GCODE=gcode MODELS=$(patsubst %,$(STL)/$(PREFIX)-%.$(STL),$(PARTS)) IMAGES=$(patsubst %,$(IMAGE)/$(PREFIX)-%.$(IMAGE),$(PARTS)) GCODES=$(patsubst %,$(GCODE)/$(PREFIX)-%.$(GCODE),$(PARTS)) all: models gcodes images directories: @mkdir -p $(STL) $(IMAGE) $(GCODE) models: directories $(MODELS) gcodes: directories $(GCODES) images: directories $(IMAGES) clean: rm -rf $(STL) $(GCODE) $(IMAGE) # Dependencies for models $(MODELS) : $(STL)/$(PREFIX)-%.$(STL) : $(PREFIX).scad $(OPENSCAD) $(OPENSCAD_OPTIONS) -o $@ -DPART=\"$(subst $(PREFIX)-,,$(subst .$(STL),,$(@F)))\" $< # Dependencies for images $(IMAGES) : $(IMAGE)/$(PREFIX)-%.$(IMAGE) : $(PREFIX).scad $(OPENSCAD) $(OPENSCAD_OPTIONS) -o $@ -DPART=\"$(subst $(PREFIX)-,,$(subst .$(IMAGE),,$(@F)))\" $(IMAGE_OPTIONS) $< # Dependencies for slicing $(GCODES) : $(GCODE)/%.$(GCODE) : $(STL)/%.$(STL) $(SLIC3R_INI) $(SLIC3R) -o $@ $(SLIC3R_OPTIONS) $< When I want to add this Makefile to an existing project, all I usually have to do is set the PREFIX variable in the Makefile, and then add the switch block at the bottom of the OpenSCAD source file. You can find source code examples on GitHub: https://github.com/wcraigtrader/game-parts/blob/master/splendor/Makefile https://github.com/wcraigtrader/game-parts/blob/master/splendor/splendor.scad Feedback is always appreciated. - Craig -
ST
Shaporev, Timur
Thu, Sep 28, 2017 8:19 AM

Please be warned that left tabs are removed (at least from my E-mail client), so the quoted code needs small manual repair.

It looks like a good idea to get the thing from github.


From: Discuss discuss-bounces@lists.openscad.org on behalf of W. Craig Trader craig.trader@gmail.com
Sent: 28 September 2017 02:47:14
To: discuss@lists.openscad.org
Subject: [OpenSCAD] Makefile for building complex models from a single file

I've been using OpenSCAD for ~6 years, and it's my go-to tool for both 2D and 3D modeling. Recently I've been using OpenSCAD to model organizers for some of my board games, and beyond a certain point it became irritating to manually export the STL files. So I started writing makefiles. The manual gives some suggestions for using makefiles, but I found them to be somewhat clunky. So I did something that worked for me. I'm sharing it here; maybe it will work for someone else.

Goals:

  • Adhere to DRY (Don't Repeat Yourself) -- there should be no duplication of information between the Makefile and the OpenSCAD file.
  • Generate STL models and PNG images with OpenSCAD, and then slice the models with Slic3r.
  • All generated files should go into segregated subdirectories.
  • The OpenSCAD file should still be useful without the Makefile.
  • If I change the OpenSCAD file, I accept that everything is going to be rebuilt. (Computers are fast.)
  • Work on Linux.

Here's what I ended up with:

Splendor organizers to fit in Splendor box

by W. Craig Trader is dual-licensed under

Creative Commons Attribution-ShareAlike 3.0 Unported License and

GNU Lesser GPL 3.0 or later.

This makefile expects an OpenSCAD file named $(PREFIX).scad that defines

the following variables:

PART    -- the name of a part to generate

VERBOSE -- logically true if you want verbose output (optional)

The OpenSCAD program should have logic that renders a single part by name:

if (PART == "foo") {

foo();

} else if (PART == "bar") {

bar();

} else if (PART == "foo-bar") {

foo_bar();

}

This makefile will use OpenSCAD to create individual model (STL) files and

image (PNG) files for each declared part, and will use Slic3r to generate

G-Code for each declared part.

So if PREFIX=widget, then the created files will be:

stl/widget-foo.stl stl/widget-bar.stl stl/widget-foo-bar.stl

png/widget-foo.png png/widget-bar.png png/widget-foo-bar.png

gcode/widget-foo.gcode gcode/widget-bar.gcode gcode/widget-foo-bar.gcode

OpenSCAD binary and options

OPENSCAD=/usr/bin/openscad
OPENSCAD_OPTIONS=-DVERBOSE=false
IMAGE_OPTIONS=--imgsize=1024,768 --colorscheme=DeepOcean

Slic3r binary and options

SLIC3R=/opt/Slic3r/latest/slic3r
SLIC3R_INI=slic3r.ini
SLIC3R_OPTIONS=--load $(SLIC3R_INI) --print-center 125,105

Name of OpenSCAD model file

PREFIX=splendor

----- Everything after this should not need modification

Names of parts to build

PARTS=$(shell grep 'PART == ' $(PREFIX).scad | cut -d'"' -f2)

STL=stl
IMAGE=png
GCODE=gcode

MODELS=$(patsubst %,$(STL)/$(PREFIX)-%.$(STL),$(PARTS))
IMAGES=$(patsubst %,$(IMAGE)/$(PREFIX)-%.$(IMAGE),$(PARTS))
GCODES=$(patsubst %,$(GCODE)/$(PREFIX)-%.$(GCODE),$(PARTS))

all: models gcodes images

directories:
@mkdir -p $(STL) $(IMAGE) $(GCODE)

models: directories $(MODELS)

gcodes: directories $(GCODES)

images: directories $(IMAGES)

clean:
rm -rf $(STL) $(GCODE) $(IMAGE)

Dependencies for models

$(MODELS) : $(STL)/$(PREFIX)-%.$(STL) : $(PREFIX).scad
$(OPENSCAD) $(OPENSCAD_OPTIONS) -o $@ -DPART="$(subst $(PREFIX)-,,$(subst .$(STL),,$(@F)))" $<

Dependencies for images

$(IMAGES) : $(IMAGE)/$(PREFIX)-%.$(IMAGE) : $(PREFIX).scad
$(OPENSCAD) $(OPENSCAD_OPTIONS) -o $@ -DPART="$(subst $(PREFIX)-,,$(subst .$(IMAGE),,$(@F)))" $(IMAGE_OPTIONS) $<

Dependencies for slicing

$(GCODES) : $(GCODE)/%.$(GCODE) : $(STL)/%.$(STL) $(SLIC3R_INI)
$(SLIC3R) -o $@ $(SLIC3R_OPTIONS) $<

When I want to add this Makefile to an existing project, all I usually have to do is set the PREFIX variable in the Makefile, and then add the switch block at the bottom of the OpenSCAD source file.

You can find source code examples on GitHub:

https://github.com/wcraigtrader/game-parts/blob/master/splendor/Makefile
https://github.com/wcraigtrader/game-parts/blob/master/splendor/splendor.scad

Feedback is always appreciated.

  • Craig -
Please be warned that left tabs are removed (at least from my E-mail client), so the quoted code needs small manual repair. It looks like a good idea to get the thing from github. ________________________________ From: Discuss <discuss-bounces@lists.openscad.org> on behalf of W. Craig Trader <craig.trader@gmail.com> Sent: 28 September 2017 02:47:14 To: discuss@lists.openscad.org Subject: [OpenSCAD] Makefile for building complex models from a single file I've been using OpenSCAD for ~6 years, and it's my go-to tool for both 2D and 3D modeling. Recently I've been using OpenSCAD to model organizers for some of my board games, and beyond a certain point it became irritating to manually export the STL files. So I started writing makefiles. The manual gives some suggestions for using makefiles, but I found them to be somewhat clunky. So I did something that worked for me. I'm sharing it here; maybe it will work for someone else. Goals: * Adhere to DRY (Don't Repeat Yourself) -- there should be no duplication of information between the Makefile and the OpenSCAD file. * Generate STL models and PNG images with OpenSCAD, and then slice the models with Slic3r. * All generated files should go into segregated subdirectories. * The OpenSCAD file should still be useful without the Makefile. * If I change the OpenSCAD file, I accept that everything is going to be rebuilt. (Computers are fast.) * Work on Linux. Here's what I ended up with: # Splendor organizers to fit in Splendor box # # by W. Craig Trader is dual-licensed under # Creative Commons Attribution-ShareAlike 3.0 Unported License and # GNU Lesser GPL 3.0 or later. # This makefile expects an OpenSCAD file named $(PREFIX).scad that defines # the following variables: # # PART -- the name of a part to generate # VERBOSE -- logically true if you want verbose output (optional) # # The OpenSCAD program should have logic that renders a single part by name: # # if (PART == "foo") { # foo(); # } else if (PART == "bar") { # bar(); # } else if (PART == "foo-bar") { # foo_bar(); # } # # This makefile will use OpenSCAD to create individual model (STL) files and # image (PNG) files for each declared part, and will use Slic3r to generate # G-Code for each declared part. # # So if PREFIX=widget, then the created files will be: # # stl/widget-foo.stl stl/widget-bar.stl stl/widget-foo-bar.stl # png/widget-foo.png png/widget-bar.png png/widget-foo-bar.png # gcode/widget-foo.gcode gcode/widget-bar.gcode gcode/widget-foo-bar.gcode # OpenSCAD binary and options OPENSCAD=/usr/bin/openscad OPENSCAD_OPTIONS=-DVERBOSE=false IMAGE_OPTIONS=--imgsize=1024,768 --colorscheme=DeepOcean # Slic3r binary and options SLIC3R=/opt/Slic3r/latest/slic3r SLIC3R_INI=slic3r.ini SLIC3R_OPTIONS=--load $(SLIC3R_INI) --print-center 125,105 # Name of OpenSCAD model file PREFIX=splendor # ----- Everything after this should not need modification # Names of parts to build PARTS=$(shell grep 'PART == ' $(PREFIX).scad | cut -d'"' -f2) STL=stl IMAGE=png GCODE=gcode MODELS=$(patsubst %,$(STL)/$(PREFIX)-%.$(STL),$(PARTS)) IMAGES=$(patsubst %,$(IMAGE)/$(PREFIX)-%.$(IMAGE),$(PARTS)) GCODES=$(patsubst %,$(GCODE)/$(PREFIX)-%.$(GCODE),$(PARTS)) all: models gcodes images directories: @mkdir -p $(STL) $(IMAGE) $(GCODE) models: directories $(MODELS) gcodes: directories $(GCODES) images: directories $(IMAGES) clean: rm -rf $(STL) $(GCODE) $(IMAGE) # Dependencies for models $(MODELS) : $(STL)/$(PREFIX)-%.$(STL) : $(PREFIX).scad $(OPENSCAD) $(OPENSCAD_OPTIONS) -o $@ -DPART=\"$(subst $(PREFIX)-,,$(subst .$(STL),,$(@F)))\" $< # Dependencies for images $(IMAGES) : $(IMAGE)/$(PREFIX)-%.$(IMAGE) : $(PREFIX).scad $(OPENSCAD) $(OPENSCAD_OPTIONS) -o $@ -DPART=\"$(subst $(PREFIX)-,,$(subst .$(IMAGE),,$(@F)))\" $(IMAGE_OPTIONS) $< # Dependencies for slicing $(GCODES) : $(GCODE)/%.$(GCODE) : $(STL)/%.$(STL) $(SLIC3R_INI) $(SLIC3R) -o $@ $(SLIC3R_OPTIONS) $< When I want to add this Makefile to an existing project, all I usually have to do is set the PREFIX variable in the Makefile, and then add the switch block at the bottom of the OpenSCAD source file. You can find source code examples on GitHub: https://github.com/wcraigtrader/game-parts/blob/master/splendor/Makefile https://github.com/wcraigtrader/game-parts/blob/master/splendor/splendor.scad Feedback is always appreciated. - Craig -
O
oystein.krog
Sat, Sep 30, 2017 8:34 AM

This is my version of an automatic makefile:
https://github.com/oysteinkrog/thing_spool_stand/blob/master/Makefile

It lets you define your parts as modules in your scad files then
automatically detects them (searches all .scad files) and generates stl
files.
It is also clever in that it will only rebuild parts if any of the dependent
files are modified.
Here is an example:
https://github.com/oysteinkrog/thing_spool_stand/blob/master/spool-stand.scad

The makefile is fully automatic and can just be dropped into any project
without changes.

--
Sent from: http://forum.openscad.org/

This is my version of an automatic makefile: https://github.com/oysteinkrog/thing_spool_stand/blob/master/Makefile It lets you define your parts as modules in your scad files then automatically detects them (searches all .scad files) and generates stl files. It is also clever in that it will only rebuild parts if any of the dependent files are modified. Here is an example: https://github.com/oysteinkrog/thing_spool_stand/blob/master/spool-stand.scad The makefile is fully automatic and can just be dropped into any project without changes. -- Sent from: http://forum.openscad.org/