2
$\begingroup$

I get this error:

Python script failed

when I run the following code involving a loop cut and slide:

import bpy

bpy.ops.mesh.primitive_plane_add()
bpy.context.object.dimensions[0] = 1
bpy.ops.object.editmode_toggle()
bpy.context.area.type = 'VIEW_3D'

bpy.ops.mesh.loopcut_slide(MESH_OT_loopcut={"number_cuts":2, "smoothness":0,     
"falloff":'INVERSE_SQUARE', "edge_index":2, "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}) // ERROR IN THIS LINE

I get the error in the loop cut statement. How do I rectify this?

$\endgroup$
3
$\begingroup$

There were several issues with your code:

  1. The loop cut and slide operator is supposed to be executed from the 3D viewport. To execute it from other windows (Console, text editor, etc), you need to override the context.

  2. You used an illegal value for the "falloff" parameter (used to be "Inverse_Square", I changed it to "Smooth". You can also choose: "SHPERE", "ROOT", "SHARP" or "LINEAR" if you prefer).

Here's the code:

import bpy

bpy.ops.mesh.primitive_plane_add()
bpy.context.object.dimensions[0] = 1
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"           : 2,
        "smoothness"            : 0,     
        "falloff"               : 'SMOOTH',  # Was 'INVERSE_SQUARE' that does not exist
        "edge_index"            : 2,
        "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
    }
)
$\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.