-
Notifications
You must be signed in to change notification settings - Fork 22
/
common.mk
64 lines (51 loc) · 2.3 KB
/
common.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright (c) 2017 Clément Bœsch <u pkh.me>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
OPENSCAD ?= openscad --colorscheme="Tomorrow Night" --imgsize=1024,1024 $(OPENSCAD_EXTRA_SETTINGS)
DSCALE ?= scale=iw/4:ih/4:flags=lanczos
FFMPEG ?= ffmpeg -v warning
STEPS ?= 200
FPS ?= 30
GIF_FINAL_DELAY ?= -1
define anim_pattern
%$(shell printf $(1) | wc -c)d
endef
define anim_pictures
$(addsuffix .png,$(addprefix anim-$(1)-,$(shell seq -w $(2))))
endef
define dashsplit
$(word $(1),$(subst -, ,$(2)))
endef
define get_openscad_var
$(if $(shell grep ^\$$$(1) $(2)),$(shell sed -nE 's#^\$$$(1)\s*=\s*([0-9]+)\s*;#\1#p' $(2)),$(3))
endef
ANIM_PATTERN := $(call anim_pattern,$(STEPS))
TARGETS = $(sort $(filter-out _internal,$(subst .scad,,$(wildcard *.scad))))
GIFS = $(addsuffix .gif,$(addprefix gif/,$(TARGETS)))
STLS = $(addsuffix -bottom.stl,$(TARGETS)) $(addsuffix -top.stl,$(TARGETS))
gif: $(GIFS)
stl: $(STLS) $(STLS_EXTRA)
.PHONY: gif stl
.SECONDEXPANSION: # XXX: wtf make...
palette-%.png: $$(call anim_pictures,%,$(STEPS))
$(FFMPEG) -i anim-$*-$(ANIM_PATTERN).png -vf $(DSCALE),palettegen -y $@
gif/%.gif: palette-%.png $$(call anim_pictures,%,$(STEPS))
$(FFMPEG) -framerate $(FPS) -i anim-$*-$(ANIM_PATTERN).png \
-i $< -lavfi "$(DSCALE)[x];[x][1:v]paletteuse" \
-y -frames:v $(STEPS) -loop 0 -final_delay $(GIF_FINAL_DELAY) $@
anim-%.png: NUM = $(shell echo $(call dashsplit,2,$*) | sed 's/^0*//')
anim-%.png: VPD = $(call get_openscad_var,vpd,$<,140)
anim-%.png: $$(call dashsplit,1,%).scad
$(OPENSCAD) $< -D'$$t=$(NUM)/$(STEPS)' -o $@
%.stl: $$(call dashsplit,1,%).scad
openscad $< -D'mode="$(call dashsplit,2,$*)"' -o $@