【问题标题】:How to convert STL to rotating GIF using OpenSCAD?如何使用 OpenSCAD 将 STL 转换为旋转 GIF?
【发布时间】:2021-12-30 14:09:38
【问题描述】:

给定一个 STL 文件,如何使用命令行 (bash) 将其转换为动画 gif?

我发现一些文章含糊地描述了如何通过 GUI 执行此操作。我已经能够生成以下内容,但是动画非常粗糙并且阴影会四处跳动。

for ((angle=0; angle <=360; angle+=5)); do
    openscad /dev/null -o dump$angle.png  -D "cube([2,3,4]);" --imgsize=250,250 --camera=0,0,0,45,0,$angle,25
done

# https://unix.stackexchange.com/a/489210/39263
ffmpeg \
  -framerate 24 \
  -pattern_type glob \
  -i 'dump*.png' \
  -r 8 \
  -vf scale=512:-1 \
  out.gif \
;

OpenScad 有一个内置的--animation X 参数,但是在将摄像机角度作为参数传递时使用它可能不起作用。

资源

https://github.com/openscad/openscad/issues/1632#issuecomment-219203658
https://blog.prusaprinters.org/how-to-animate-models-in-openscad_29523/ https://github.com/openscad/openscad/issues/1573
https://github.com/openscad/openscad/pull/1808
https://forum.openscad.org/Product-Video-produced-with-OpenSCAD-td15783.html

【问题讨论】:

  • 我认为您需要手动调整每个对象。您可能会找到一种方法来计算对象的范围,将其分成两半,然后围绕该点旋转,但在视觉上大小不同或不平衡的对象中,它可能看起来不正确。如果您尝试为很多模型自动执行此操作,也许值得在 OpenSCAD 之外使用任何运行此操作的脚本进行居中计算?

标签: openscad


【解决方案1】:

Bash + Docker

将 STL 转换为 GIF 需要几个步骤

  1. 将 STL 置于原点的中心
  2. 从不同角度将 STL 转换为 .PNG 文件的集合
  3. 将这些 PNG 文件合并到一个 .gif 文件中

假设您安装了 docker,您可以运行以下命令将 STL 转换为动画 GIF

(注意:此脚本的更新版本可在 spuder/CAD-scripts/stl2gif 获得

这取决于 3 个 docker 容器

# 1. Use spuder/stl2origin:latest docker container to center the file at origin
# A file with the offsets will be saved to `${MYTMPDIR}/foo.sh`
file=/tmp/foo.stl
MYTMPDIR="$(mktemp -d)"
trap 'rm -rf -- "$MYTMPDIR"' EXIT
docker run \
    -e OUTPUT_BASH_FILE=/output/foo.sh \
    -v $(dirname "$file"):/input \
    -v $MYTMPDIR:/output \
    --rm spuder/stl2origin:latest \
    "/input/$(basename "$file")"
    cp "${file}" "$MYTMPDIR/foo.stl"
    
# 2. Read ${MYTMPDIR}/foo.sh and load the offset variables ($XTRANS, $XMID,$YTRANS,$YMID,$ZTRANS,$ZMID) 
# Save the new centered STL to `$MYTMPDIR/foo-centered.stl`
source $MYTMPDIR/foo.sh
docker run \
    -v "$MYTMPDIR:/input" \
    -v "$MYTMPDIR:/output" \
    openscad/openscad:2021.01 openscad /dev/null -D "translate([$XTRANS-$XMID,$YTRANS-$YMID,$ZTRANS-$ZMID])import(\"/input/foo.stl\");" -o "/output/foo-centered.stl"

# 3. Convert the STL into 60 .PNG images with the camera rotating around the object. Note `$t` is a built in openscad variable that is automatically set based on time when --animate option is used
# OSX users will need to replace `openscad` with `/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD`
# Save all images to $MYTMPDIR/foo{0..60}.png
# This is not yet running in a docker container due to a bug: https://github.com/openscad/openscad/issues/4028

openscad /dev/null \
    -D '$vpr = [60, 0, 360 * $t];' \
    -o "${MYTMPDIR}/foo.png"  \
    -D "import(\"${MYTMPDIR}/foo-centered.stl\");" \
    --imgsize=600,600 \
    --animate 60 \
    --colorscheme "Tomorrow Night" \
    --viewall --autocenter

# 4. Use ffmpeg to combine all images into a .GIF file
# Tune framerate (15) and -r (60) to produce a faster/slower/smoother image
    yes | ffmpeg \
        -framerate 15 \
        -pattern_type glob \
        -i "$MYTMPDIR/*.png" \
        -r 60 \
        -vf scale=512:-1 \
        "${file}.gif" \
        ;
    rm -rf -- "$MYTMPDIR"

STL 文件

Gif 没有居中

Gif 居中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 2021-09-19
    • 2017-07-20
    • 2021-10-07
    • 2013-06-05
    相关资源
    最近更新 更多