2
$\begingroup$

I'm trying to use loop-cut and slide in Python.

I've tried the following: Loop cut and slide using python script in blender How do I override context for bpy.ops.mesh.loopcut? Loop Cut and Slide using Python

The best I can get is "no error and no loop cut". -_- (new_outsideWall is a procedurally created object)

The following code selects the correct object, enters 3D view, goes to edit mode, does NOT generate a loop cut, exits edit mode and returns to previous screen view. No error at all.

I know it has something to do with either bpy.ops.mesh not targeting the correct mesh, or override not being in the right context. But being a complete noob, I have no idea how to fix, nor can I locate the answer.

Thanks.

In case you're wondering, I'm trying to create a "random building generator" and this is the adjustable cutout for the window textures. ^_^

old_type = bpy.context.area.type
bpy.context.area.type = 'VIEW_3D'
bpy.context.view_layer.objects.active = new_outsideWall
new_outsideWall.select_set(True)
bpy.ops.object.editmode_toggle()
def view3d_find( return_area = False ):
    # 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':
                    if return_area: return region, rv3d, v3d, area
                    return region, rv3d, v3d
    return None, None

region, rv3d, v3d, area = view3d_find(True)

override = {
    'scene'  : bpy.context.scene,
    'region' : region,
    'area'   : area,
    'space'  : v3d
}
bpy.ops.mesh.loopcut_slide(
    override,
    MESH_OT_loopcut = {
        "number_cuts"           : 1,
        "smoothness"            : 0,     
        "falloff"               : 'INVERSE_SQUARE',  # Was 'INVERSE_SQUARE' that does not exist
        "edge_index"            : 1,
        "mesh_select_mode_init" : (True, False, False)
    },
    TRANSFORM_OT_edge_slide = {
        "value"           : 0,
        "mirror"          : False, 
        "snap"            : False,
        "snap_target"     : 'CLOSEST',
        "snap_point"      : (0, 0, 0),
        "snap_align"      : False,
        "snap_normal"     : (0, 0, 0),
        "correct_uv"      : False,
        "release_confirm" : False,
        "use_accurate"    : False
    }
)
bpy.ops.object.editmode_toggle()
bpy.context.area.type = old_type 
$\endgroup$
  • $\begingroup$ Consider using the bmesh operator alternative(s). Could you add an image of object and result desired. $\endgroup$ – batFINGER Dec 12 '19 at 5:29
  • $\begingroup$ Loop cut is exactly the method I want to use. I'm a noob, so unfamiliar with bmesh, but cursory research suggests it's most definitely not what I want, since I'll be using pre-built wall assets. Imagine a cube, and you want to create a rectangular window through it, using python. Height, pos, width and number of windows need to be configurable outside the script. $\endgroup$ – Mike Dec 12 '19 at 6:26
  • $\begingroup$ Definitely cursory. IMO Operators are a "bridge" between the UI and quite likely the case for mesh operators bmesh. Chopping random windows thru the cube based on whatever props would be a sinch with bmesh, anyhoo. Re question script. there is no need to both swap the area type and override context, do one or the other. My guess is the operator above is returning {'CANCELLED} ie doing nada. $\endgroup$ – batFINGER Dec 12 '19 at 12:07
  • $\begingroup$ "there is no need to both swap the area type and override context," This is like my 28th version. I've tried all kinds of crazy variations. I've tried just override, just area swap, and both. How do I get the operator to not return "cancelled"? $\endgroup$ – Mike Dec 12 '19 at 21:24
  • $\begingroup$ So, I created a new empty file, and ran this script: blender.stackexchange.com/questions/43060/… And it has the same problem, creates the plane, selects it, goes to edit mode, and does not do the loop cut. "Chopping random windows thru the cube based on whatever props would be a sinch with bmesh," Using python? I'll check it out. Thanks. $\endgroup$ – Mike Dec 12 '19 at 21:32
2
$\begingroup$

if you're still interessed, I've found the solution to your problem. I had more or less the same script for Blender 2.79 and run in the same issue in Blender 2.8x. All you have to do is adding the variable "object_index" : 0 in MESH_OT_loopcut

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.