Sponsored Links
オブジェクトの座標を揃えるためのパネルをつくってみました。コードを実行すると、3DビューのプロパティパネルにArrangementという名前のパネルが追加されます。適当なオブジェクトにobj1とobj2という名前をつけて、ボタンを押すと整列が実行されます。
仕様
- X、Y、Zごとに、整列を実行するボタンをパネルに配置する
- ボタンを押すと、シーンに配置されている”obj1″と”obj2″というオブジェクトに対して整列を実行する
- 整列はobj1を基準として行う。(obj1をキーオブジェクトとする)
コード
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 65 66 67 68 69 | import bpy # ------------------------- # 整列ボタンを配置するパネル # ------------------------- class UI(bpy.types.Panel): bl_label = "Arrangement" bl_space_type = "VIEW_3D" bl_region_type = "UI" def draw(self, context): self.layout.operator("my.arrangexbutton") self.layout.operator("my.arrangeybutton") self.layout.operator("my.arrangezbutton") # ------------------------- # X座標値の整列を実行するボタン # ------------------------- class ArrangeXButton(bpy.types.Operator): bl_idname = "my.arrangexbutton" bl_label = "X" def execute(self, context): obj1 = bpy.data.objects["obj1"] obj2 = bpy.data.objects["obj2"] obj2.location.x = obj1.location.x return{'FINISHED'} # ------------------------- # Y座標値の整列を実行するボタン # ------------------------- class ArrangeYButton(bpy.types.Operator): bl_idname = "my.arrangeybutton" bl_label = "Y" def execute(self, context): obj1 = bpy.data.objects["obj1"] obj2 = bpy.data.objects["obj2"] obj2.location.y = obj1.location.y return{'FINISHED'} # ------------------------- # Z座標値の整列を実行するボタン # ------------------------- class ArrangeZButton(bpy.types.Operator): bl_idname = "my.arrangezbutton" bl_label = "Z" def execute(self, context): obj1 = bpy.data.objects["obj1"] obj2 = bpy.data.objects["obj2"] obj2.location.z = obj1.location.z return{'FINISHED'} classes = ( UI, ArrangeXButton, ArrangeYButton, ArrangeZButton ) for cls in classes: bpy.utils.register_class(cls) |
パネルの用意
1 2 3 4 5 6 7 8 9 | class UI(bpy.types.Panel): bl_label = "Arrangement" # パネルの表示名 bl_space_type = "VIEW_3D" # 配置先のエディタタイプ bl_region_type = "UI" # エディタ上の配置位置 def draw(self, context): self.layout.operator("my.arrangexbutton") self.layout.operator("my.arrangeybutton") self.layout.operator("my.arrangezbutton") |
ボタンを配置するためのパネルを定義するクラスです。
bl_~の値にパネルの名前や配置先などを指定しています。今回は3Dビュー上のNキーを押すと表示されるプロパティシェルフに表示する設定にしてあります。
draw()内で、self.layout.operator()に追加するボタンのクラスを渡します。
ボタンの用意
1 2 3 4 5 6 7 8 9 10 11 | class ArrangeXButton(bpy.types.Operator): bl_idname = "my.arrangexbutton" bl_label = "X" def execute(self, context): obj1 = bpy.data.objects["obj1"] obj2 = bpy.data.objects["obj2"] obj2.location.x = obj1.location.x return{'FINISHED'} |
どの座標軸の場合もほぼ同じ内容です。
bl~でパネルのときと同様に、ボタンの名前や表示名を設定します。bl_idnameで大文字を指定するとエラーが起きてしまうので全て小文字で指定してあります。
execute()内にボタンを押したときの処理を記述しています。今回は単純にobj2の座標値をobj1の座標値に置き換えているだけです。
作成したクラスの登録
1 2 3 4 5 6 7 8 9 10 | classes = ( UI, ArrangeXButton, ArrangeYButton, ArrangeZButton ) for cls in classes: bpy.utils.register_class(cls) |
一通りクラスの用意が出来たら、Blenderから扱えるようにbpy.utils.register_class()を使って登録処理をします。
最初に作成したクラス一覧のタプルを用意して、ループを使ってから全てのクラスを登録するようにします。この処理は他のスクリプトをつくる場合でも共通の処理のようです。
Sponsored Links