1

I am trying to Loop Cut a Plane, made in the Code beforehand.

I cant find a short and precise online how to JUST do that. All answers include some kind of fixing other issues. Can someone post the code of simply:

create plane

loopcut plane in X-Direction a number of times

loopcut plane in Y-Direction a number of times

When making everything manually, the console posts the following lines to create the loopcut:

    bpy.ops.mesh.loopcut_slide(
    MESH_OT_loopcut={"number_cuts":1, "smoothness":0, "falloff":'INVERSE_SQUARE', 
    "object_index":0, "edge_index":3, "mesh_select_mode_init":(True, False, False)}, 
    TRANSFORM_OT_edge_slide={"value":0, "single_side":False, "use_even":False,
    "flipped":False, "use_clamp":True, "mirror":False, "snap":False, "snap_target":'CLOSEST',
    "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), 
    "correct_uv":True, "release_confirm":False, "use_accurate":False})

When I use that in my console it throws following Error:

    RuntimeError: Operator bpy.ops.mesh.loopcut_slide.poll() expected a view3d region & editmesh

Thanks

quest-12
  • 143
  • 4

1 Answers1

0

I'm marking your question as a duplicate of: Loop Cut and Slide using Python

However since you mention you had a problem with existing answers, here's a minified version of the linked answer:

import bpy
from bpy import context as C

def main(): bpy.ops.mesh.primitive_plane_add() bpy.ops.object.mode_set(mode='EDIT') x_edge, y_edge = find_xy_edges(C.object.data) area = next(a for a in C.window.screen.areas if a.type == 'VIEW_3D') region = next(r for r in area.regions if r.type == 'WINDOW') c_override = {'area': area, 'region': region} loopcut(c_override, edge_index=x_edge, number_cuts=3) loopcut(c_override, edge_index=y_edge, number_cuts=10) bpy.ops.object.mode_set(mode='OBJECT')

def find_xy_edges(mesh): x_edge = None y_edge = None verts = mesh.vertices for edge in mesh.edges: v1_i, v2_i = edge.vertices vec = verts[v1_i].co - verts[v2_i].co if vec.x > vec.y: x_edge = edge.index else: y_edge = edge.index if not (x_edge is None or y_edge is None): return x_edge, y_edge

def loopcut(override, *, edge_index, number_cuts): cutting_data = { "number_cuts" : number_cuts, "object_index" : 0, "edge_index" : edge_index, } bpy.ops.mesh.loopcut_slide(override, MESH_OT_loopcut=cutting_data)

main()

I wasn't sure if the x_edge and y_edge names perhaps should be swapped...

Markus von Broady
  • 36,563
  • 3
  • 30
  • 99