1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonSCADコミュニティ・開発者に感謝申し上げます。ありがとうございます。

cube(10)

from pythonscad import *

cube(10).show()

スクリーンショット 2026-09-08 103236.png

cube(10).color("tomato")

from pythonscad import *

cube(10).color("tomato").show()

スクリーンショット 2026-09-08 103433.png

5つ並べる.エラー!

from pythonscad import *
for i in range(5):
    result = result + (cube(10).color("tomato") + [i*10,0,0])
    
show(result)

エラー!。
ERROR: Traceback (most recent call last):
File "", line 3, in
NameError: name 'result' is not defined

5つ並べる. 最初の1個を作って

from pythonscad import *
result=cube(10)
for i in range(5):
    result = result + (cube(10).color("tomato") + [i*10,0,0])
    
show(result)

スクリーンショット 2026-09-08 105422.png

大きさを変えてみる

cube(8)

from pythonscad import *
result=cube(10)
for i in range(5):
    result = result + (cube(8).color("tomato") + [i*10,0,0])
    
show(result)

スクリーンショット 2026-09-08 105632.png
重なっているのがわかる。

くりかえしの初期値を変更する

from pythonscad import *
result=cube(10)
for i in range(1,5):
    result = result + (cube(8).color("tomato") + [i*10,0,0])
    
show(result)

スクリーンショット 2026-09-08 110051.png

色と大きさをそろえる

from pythonscad import *
result=cube(8).color("tomato")

for i in range(1,5):
    result = result + (cube(8).color("tomato") + [i*10,0,0])
    
show(result)

image.png

OpenSCAD風に書いてみる

エラー!。
ERROR: Traceback (most recent call last):
File "", line 3, in
NameError: name 'result' is not defined
原因は、ここです。

result = result + ...

右側の result を使おうとしていますが、その前に result がまだ作られていません。
つまり Python から見ると「result って何?」となり、NameError になります。


修正版コード(PythonSCAD)

# ==========================================
# 5個の立方体を横に並べる
# ==========================================

from pythonscad import *

# --- パラメータ設定 (単位: mm) ---
cube_size = 10.0
count     = 5
pitch     = 10.0

# --- 最初の1個を作って result に入れる ---
result = color(
    cube([cube_size, cube_size, cube_size], center=True),
    "tomato"
)

# --- 2個目以降を追加する ---
for i in range(1, count):
    part = color(
        translate(
            cube([cube_size, cube_size, cube_size], center=True),
            [i * pitch, 0, 0]
        ),
        "tomato"
    )
    result = result + part

show(result)

スクリーンショット 2026-09-08 110553.png


ポイント

最初に

result = ...

として、result の中身を作っておく必要があります。

そのあとで、

result = result + part

のように追加していきます。


もう少し短く書く. pitchを11.

from pythonscad import *

cube_size = 10.0
count     = 5
pitch     = 11.0

result = color(cube([cube_size, cube_size, cube_size], center=True), "tomato")

for i in range(1, count):
    result = result + color(
        translate(cube([cube_size, cube_size, cube_size], center=True), [i * pitch, 0, 0]),
        "tomato"
    )

show(result)

スクリーンショット 2026-09-08 110846.png


今回の考え方は、

  1. 最初の立方体を作る
  2. result に入れる
  3. 2個目以降を + で足していく

です。
プログラムでは「入れ物を先に作ってから使う」と覚えるとよいです。
一歩一歩です。ありがとうございます。

参考資料

📄 ライセンスと「オープンソース」の文化について

本記事のソースコードは、すべて MIT ライセンス で公開しています。

  • 高校生のみなさんへ 🚀
    プログラミングの世界には、「自分が作った便利な仕組みをみんなに共有し、お互いに助け合って技術を発展させる(オープンソース)」という素晴らしい文化があります。このコードも、作者の名前(クレジット)さえ残してもらえれば、改造して学校の課題に使ったり、自分のアプリに組み込んだりして自由に無料で使ってOKです!
    ぜひこのコードをベースに、自分だけの新しいプログラムを作って挑戦してみてください。
  • 免責事項 ⚠️
    本記事およびコードは個人の研究・検証に基づくものであり、所属する組織の公式見解ではありません。自由に使っていただけますが、利用に伴ういかなる損害についても執筆者は責任を負いかねますので、すべて「自己責任(無保証)」の範囲内で楽しく学んでくださいね。
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?