BlenderでコレクションごとにSTLファイルを出力するスクリプト

フルアーマーガンダムMk-IIのアーマーを自作するために3DプリンタELEGOO Mars 2を購入した。 Blenderを使って3Dモデルを作成し、CHITUBOXでスライス、印刷をしている。

f:id:couger:20210506212539p:plain

CHITUBOXで読み込むために、肩、腰、胸などのパーツごとにSTLをエクスポートしているんだけども、オブジェクトを選択、エクスポート処理を呼び出し、ファイル名を入力するという操作を何回も何回も何回も繰り返すのが嫌になってきたので、簡単なスクリプトを書いた。

  • targetsに指定したコレクションをSTLファイルに出力
  • 出力先は、"プロジェクトファイルのあるフォルダ"/stl。なければ勝手に作成
  • ファイル名は "full-armor-gundam-mk2_" + targetsで指定したsuffix
import bpy
import os
import shutil

bpy.ops.object.mode_set(mode='OBJECT')

targets = []
targets.append({"collections":["Shoulder"],"suffix":"shoulder"})
targets.append({"collections":["Chest-Side", "Chest-Center"],"suffix":"chest"})

out_dir_path = bpy.path.abspath("//stl")

if os.path.exists(out_dir_path):
  shutil.rmtree(out_dir_path)
os.mkdir(out_dir_path)

out_file_path_base = bpy.path.abspath("//stl/full-armor-gundam-mk2")

for target in targets:
  #print(target["suffix"])
  bpy.ops.object.select_all(action="DESELECT")
  for collection in target["collections"]:
      #print(collection)
      for obj in bpy.data.collections[collection].all_objects: 
          obj.select_set(True)
      
  bpy.ops.export_mesh.stl(filepath=out_file_path_base + "-" + target["suffix"] + ".stl", use_selection=True)