9

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?

p2or
  • 15,860
  • 10
  • 83
  • 143
Gabriel
  • 621
  • 2
  • 9
  • 17

1 Answers1

9

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 "object_index" : 0, "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 } )

Markus von Broady
  • 36,563
  • 3
  • 30
  • 99
TLousky
  • 16,043
  • 1
  • 40
  • 72