6

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 
Mike
  • 420
  • 2
  • 11
  • Consider using the bmesh operator alternative(s). Could you add an image of object and result desired. – batFINGER Dec 12 '19 at 05:29
  • 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. – Mike Dec 12 '19 at 06:26
  • 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. – batFINGER Dec 12 '19 at 12:07
  • "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"? – Mike Dec 12 '19 at 21:24
  • So, I created a new empty file, and ran this script: https://blender.stackexchange.com/questions/43060/loop-cut-and-slide-using-python 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. – Mike Dec 12 '19 at 21:32
  • To be clear: When I say "window", I'm not saying a cutout. I mean an actual window with glass. So, not "chopping through a cube". bpy.ops.mesh.subdivide(number_cuts=2) does get the very basics of subdivision I want. I'm guessing I can use Bmesh to slide the correct edges where I need them, with some practice.
    Thanks. I'll let you know if this solves the problem completely.
    – Mike Dec 12 '19 at 22:24
  • Doing the subdivide and trying to locate all the faces to change materials and shape is too tedious. This would be the case with loop cut as well. So, much better solution: Boolean. This would allow custom walls and custom windows picked from a list, with a scale factor applied to the window prior to the bool operator. Thanks a ton for your help, it put me on the right track. ^_^ – Mike Dec 13 '19 at 02:12
  • Bool Union works flawlessly. I just have to make sure the window is slightly thicker than the wall on both sides, and make sure the base wall has all the materials that the list of possible windows have. – Mike Dec 13 '19 at 04:53
  • Good one. Is the topo ok after boolean.. Edited this one https://blender.stackexchange.com/a/102239/15543 to 2.8 (would prob use grid extrude to make cuboid if remit wasn't emulating loop cut somewhat) this way tho can edit to have have non uniform "loop cuts". https://blender.stackexchange.com/questions/91108/using-python-to-create-cuboid-with-a-hole-in-the-centre popped up too searching for answer with cuboid. Another bonus, of avoiding operators that require UI elements, is code will also work in batch mode. – batFINGER Dec 13 '19 at 12:29
  • No, mesh is a disaster after boolean, but I remember seeing addons that fix that problem nicely. Haven't tested that part. – Mike Dec 14 '19 at 03:57
  • Cuboid thing won't work, as I want to pick randomly from a list of "Wall" and "Window" assets and insert the window into the wall smoothly. The length & height of both are determined by the script, and changed based on context. – Mike Dec 14 '19 at 04:03
  • Hmmm, I think it would. For example sake a 3 x 1 x 3 could punch out the middle face front and back join edge loops. to have a hole in the wall. A window of that size can be placed in the hole. – batFINGER Dec 14 '19 at 04:10
  • Problem is: Not all walls are cubes, or rectangles, but multi-faceted complex shapes. They also have specific faces with textures applied prior to adding windows, that I want to remain applied. There are also multiple windows of possibly different sizes and shapes on walls of different sizes and shapes. – Mike Dec 14 '19 at 04:42
  • Sorry Mike It's got to the whatever stage. And has diverged greatly from the question asked. Recommend asking a new more specific question. – batFINGER Dec 14 '19 at 04:44
  • I have a solution for what I want to accomplish. But the original question was never answered, and appears to be a bug. – Mike Dec 14 '19 at 04:53

1 Answers1

5

if you're still interested, 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