end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

python for blender 3.3 による 自動 loop cut

import bpy
import math
import bmesh

def main():
    # 一旦、全object削除
    remove_all_obj()
    
    # キューブオブジェクト 追加
    bpy.ops.mesh.primitive_cube_add(
        size=1.0,
        align='WORLD',
        location=(1.0, 2.0, 3.0), # meter
        rotation=(math.radians(0),
                  math.radians(0),
                  math.radians(0)),
        scale=(3.6+0.25,        #桁
               2.475,           #妻
               2.7+0.25)        #高
    )
    
    bpy.ops.object.editmode_toggle()

    # ループカット
    #        obj_id  edge_id  cuts
    loop_cut(0,      0,       5)
    loop_cut(0,      1,       5)
    loop_cut(0,      4,       5)

    # 最短経路探索 TODO
    # bpy.ops.mesh.select_linked()
    
    
# cf. https://blender.stackexchange.com/questions/43060/loop-cut-and-slide-using-python
def loop_cut(obj_index,edge_index,cuts):
    
    region, rv3d, v3d, area = find_view3d()

    override = {'scene'  : bpy.context.scene,
                'region' : region,
                'area'   : area,
                'space'  : v3d }

    bpy.ops.mesh.loopcut_slide(
        override,
        MESH_OT_loopcut = {
            "object_index"      : obj_index,
            "edge_index"        : edge_index,
            "number_cuts"       : cuts }
    )

    
def find_view3d():
    # returns first 3d view, normally we get from context
    for area in bpy.context.window.screen.areas:
        if area.type == 'VIEW_3D':
            v3d = area.spaces[0]
            rv3d = v3d.region_3d
            for region in area.regions:
                if region.type == 'WINDOW':
                    return region, rv3d, v3d, area
    
def remove_all_obj():
    for item in bpy.data.meshes:
        bpy.data.meshes.remove(item)
    for item in bpy.data.materials:
        bpy.data.materials.remove(item)

        
if __name__ == '__main__':
    main()

↑こう書くと、↓こう表示されます