2025年4月27日日曜日

3D座標付き遺物リストのプロット用Blenderアドオン自作

 I made a Blender addon for plotting a list of relics with 3D coordinates


I made a Blender addon that plots a list of relics with 3D coordinates in Blender3D space. Until now I've been doing this with BlenderPython, but it's much more convenient to make it an addon. I was able to create the addon quickly with ChatGPT support.


3D座標付き遺物リストをBlender3D空間にプロットするBlenderアドオンを自作しました。これまではBlenderPythonで行ってきましたが、アドオンにした方がはるかに便利です。アドオン作成はChatGPT支援で直ぐにできました。

1 3D座標付き遺物リストのプロット用Blenderアドオン CSV Tools


3D座標付き遺物リストのプロット用Blenderアドオンの機能

csvファイルは単純に次の形式としました。

ID,X,Y,Z,TYPE

1,0,0,0,a

2,2,3,1,b

3,5,-2,4,c

csvファイルを選択できるようにしたので、便利です。

ID名(遺物名)のオブジェクトを作成し、TYPE名のコレクションに格納するので、多数遺物の扱いがしやすくなっています。

オブジェクトは0.05m×0.05m×0.05mのCUBEとして設定してあります。

アドオンづくりの楽しみという観点から言えば、色塗りを付加したり、オブジェクトの大きさや形状を変えたりといろいろ工夫はできるのですが、それらの機能を付加すると、自分の作業ではかえって煩雑になるので、それらからは試したうえで、撤退しました。

プロットしたものをgeometry nodesで凝った装飾を加える方が手軽で、自由で、効率的です。最初にプロットしたものに対して非破壊的にできます。

このアドオン作成はChatGPTの支援を受けました。

2 参考 自作アドオンのPythonスクリプト

以下のスクリプトをテキストファイルにして、拡張子をtxtからpyに変更するとPythonスクリプトになります。そのPythonスクリプトをBlenderアドオンとして読み込むと、アドオンCSV Toolsが使えるようになります。

  1. bl_info = {
  2. "name": "CSVからCUBE配置ツール",
  3. "author": "ChatGPTアシスト",
  4. "version": (1, 1),
  5. "blender": (3, 0, 0),
  6. "location": "3Dビュー > サイドバー > CSV Tools",
  7. "description": "CSVファイルの座標にCUBEを配置し、TYPEごとにコレクションに整理するツール (ファイル選択対応)",
  8. "category": "Object",
  9. }
  10.  
  11. import bpy
  12. import csv
  13. from bpy.props import StringProperty
  14. from bpy_extras.io_utils import ImportHelper
  15. from bpy.types import Operator, Panel
  16.  
  17. # --- CSVからCUBEを配置するメイン処理 ---
  18. def get_or_create_collection(name):
  19. if name in bpy.data.collections:
  20. return bpy.data.collections[name]
  21. else:
  22. new_collection = bpy.data.collections.new(name)
  23. bpy.context.scene.collection.children.link(new_collection)
  24. return new_collection
  25.  
  26. def create_cubes_from_csv(csv_file):
  27. with open(csv_file, newline='', encoding='utf-8') as f:
  28. reader = csv.DictReader(f)
  29. for row in reader:
  30. x = float(row['X'])
  31. y = float(row['Y'])
  32. z = float(row['Z'])
  33. type_name = row['TYPE']
  34. # Cube作成
  35. bpy.ops.mesh.primitive_cube_add(size=1, location=(x, y, z))
  36. obj = bpy.context.active_object
  37. obj.scale = (0.05, 0.05, 0.05)
  38. obj.name = f"Artifact_{row['ID']}"
  39.  
  40. # 適切なコレクションに移動
  41. collection = get_or_create_collection(type_name)
  42. collection.objects.link(obj)
  43. bpy.context.scene.collection.objects.unlink(obj)
  44.  
  45. # --- オペレータークラス ---
  46. class OBJECT_OT_import_csv_create_cubes(Operator, ImportHelper):
  47. bl_idname = "object.import_csv_create_cubes"
  48. bl_label = "CSVを読み込んでCUBEを配置"
  49. bl_options = {'REGISTER', 'UNDO'}
  50.  
  51. filename_ext = ".csv"
  52. filter_glob: StringProperty(
  53. default="*.csv",
  54. options={'HIDDEN'},
  55. )
  56.  
  57. def execute(self, context):
  58. create_cubes_from_csv(self.filepath)
  59. return {'FINISHED'}
  60.  
  61. # --- パネルクラス ---
  62. class VIEW3D_PT_csv_tools(Panel):
  63. bl_label = "CSV Tools"
  64. bl_idname = "VIEW3D_PT_csv_tools"
  65. bl_space_type = 'VIEW_3D'
  66. bl_region_type = 'UI'
  67. bl_category = 'CSV Tools'
  68.  
  69. def draw(self, context):
  70. layout = self.layout
  71. layout.operator(OBJECT_OT_import_csv_create_cubes.bl_idname, text="CSVファイルを選択して配置")
  72.  
  73. # --- 登録処理 ---
  74. classes = (
  75. OBJECT_OT_import_csv_create_cubes,
  76. VIEW3D_PT_csv_tools,
  77. )
  78.  
  79. def register():
  80. for cls in classes:
  81. bpy.utils.register_class(cls)
  82.  
  83. def unregister():
  84. for cls in reversed(classes):
  85. bpy.utils.unregister_class(cls)
  86.  
  87. if __name__ == "__main__":
  88. register()

0 件のコメント:

コメントを投稿