スポンサーリンク

オブジェクトの頂点に行列を適用するプログラムにUIを付けてアドオン風にする

import bpy
import mathutils

# 行列をモデルに適用するボタン
class execButton(bpy.types.Operator):
  bl_idname = "szl.multmatrixbutton"
  bl_label = "multi matrix"
  
  def execute(self, context):
      
      M11 = context.scene.myinputs.m11
      M12 = context.scene.myinputs.m12
      M13 = context.scene.myinputs.m13
      M14 = context.scene.myinputs.m14
      #######################################
      M21 = context.scene.myinputs.m21
      M22 = context.scene.myinputs.m22
      M23 = context.scene.myinputs.m23
      M24 = context.scene.myinputs.m24
      #######################################
      M31 = context.scene.myinputs.m31
      M32 = context.scene.myinputs.m32
      M33 = context.scene.myinputs.m33
      M34 = context.scene.myinputs.m34
      #######################################
      M41 = context.scene.myinputs.m41
      M42 = context.scene.myinputs.m42
      M43 = context.scene.myinputs.m43
      M44 = context.scene.myinputs.m44
      
      #######################################
      #apply matrix
      matrix = mathutils.Matrix((
      (M11  ,M12  ,M13  ,M14),
      (M21  ,M22  ,M23  ,M24),
      (M31  ,M32  ,M33  ,M34),
      (M41  ,M42  ,M43  ,M44)))
      
      msh = bpy.context.active_object
      for vt in msh.data.vertices:
        
          src = mathutils.Vector((vt.co[0],vt.co[1],vt.co[2],1))
        
          dst = matrix @ src
        
          vt.co[0] = dst[0]
          vt.co[1] = dst[1]
          vt.co[2] = dst[2]      
          
      return{'FINISHED'}
# 行列を単位行列にする
class execButtonLoadIdentity(bpy.types.Operator):
  bl_idname = "szl.loadidentitybutton"
  bl_label = "load identity"
  
  def execute(self, context):
    context.scene.myinputs.m11 =1.0
    context.scene.myinputs.m12 =0.0
    context.scene.myinputs.m13 =0.0
    context.scene.myinputs.m14 =0.0
    #######################################
    context.scene.myinputs.m21 =0.0
    context.scene.myinputs.m22 =1.0
    context.scene.myinputs.m23 =0.0
    context.scene.myinputs.m24 =0.0
    #######################################
    context.scene.myinputs.m31 =0.0
    context.scene.myinputs.m32 =0.0
    context.scene.myinputs.m33 =1.0
    context.scene.myinputs.m34 =0.0
    #######################################
    context.scene.myinputs.m41 =0.0
    context.scene.myinputs.m42 =0.0
    context.scene.myinputs.m43 =0.0
    context.scene.myinputs.m44 =1.0

    return{'FINISHED'}
#テキストボックスを定義
class myCInputs(bpy.types.PropertyGroup):

    m11: bpy.props.FloatProperty(name="m11")
    m12: bpy.props.FloatProperty(name="m12")
    m13: bpy.props.FloatProperty(name="m13")
    m14: bpy.props.FloatProperty(name="m14")
    #######################################
    m21: bpy.props.FloatProperty(name="m21")
    m22: bpy.props.FloatProperty(name="m22")
    m23: bpy.props.FloatProperty(name="m23")
    m24: bpy.props.FloatProperty(name="m24")
    #######################################
    m31: bpy.props.FloatProperty(name="m31")
    m32: bpy.props.FloatProperty(name="m32")
    m33: bpy.props.FloatProperty(name="m33")
    m34: bpy.props.FloatProperty(name="m34")
    #######################################
    m41: bpy.props.FloatProperty(name="m41")
    m42: bpy.props.FloatProperty(name="m42")
    m43: bpy.props.FloatProperty(name="m43")
    m44: bpy.props.FloatProperty(name="m44")
class myCTool_PT_panel(bpy.types.Panel):
    bl_label = "行列適用"
    bl_category = "行列適用"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout

        column = layout.column(align=True)

        # テキストボックスを並べる
        row = column.row(align=True)
        row.prop(context.scene.myinputs, "m11", text="")
        row.prop(context.scene.myinputs, "m12", text="")
        row.prop(context.scene.myinputs, "m13", text="")
        row.prop(context.scene.myinputs, "m14", text="")
        row = column.row(align=True)
        row.prop(context.scene.myinputs, "m21", text="")
        row.prop(context.scene.myinputs, "m22", text="")
        row.prop(context.scene.myinputs, "m23", text="")
        row.prop(context.scene.myinputs, "m24", text="")
        row = column.row(align=True)
        row.prop(context.scene.myinputs, "m31", text="")
        row.prop(context.scene.myinputs, "m32", text="")
        row.prop(context.scene.myinputs, "m33", text="")
        row.prop(context.scene.myinputs, "m34", text="")
        row = column.row(align=True)
        row.prop(context.scene.myinputs, "m41", text="")
        row.prop(context.scene.myinputs, "m42", text="")
        row.prop(context.scene.myinputs, "m43", text="")
        row.prop(context.scene.myinputs, "m44", text="")
        
        # ボタンを配置
        layout.operator("szl.loadidentitybutton")
        layout.operator("szl.multmatrixbutton")
classes = (
    myCInputs,
    execButton,
    execButtonLoadIdentity,
    myCTool_PT_panel
)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.myinputs = bpy.props.PointerProperty(type=myCInputs)

def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.myinputs

if __name__ == "__main__":
    register()

コメントを残す

メールアドレスが公開されることはありません。

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


この記事のトラックバックURL: