0

I am trying to write a simple script which should loop through the selected objects (all simple square planes) and do a loop cut operation in order to split the plane in half.

EDIT: What I have runs, but now only cuts the active object:

EDITED IN A SIMPLER VERSION:

import bpy

bpy.ops.object.mode_set(mode = 'OBJECT')

If there ARE objects selected then act on all objects

if bpy.context.selected_objects != []: for obj in bpy.context.selected_objects: if obj.type == 'MESH': print(">>>>", obj.name) bpy.ops.object.mode_set(mode = 'EDIT')

        obj = bpy.context.active_object

        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"               : 'SMOOTH',  # Was 'INVERSE_SQUARE' that does not exist
                "edge_index"            : 1,
                "object_index"          : 0,
                "mesh_select_mode_init" : (True, False, False)
            }
        )

Seems the problem stems from the active object being the only thing taking the loop cuts. Help on where I've gone wrong would be great! Thanks!

  • 1
    All selected objects will be put into edit mode. Can set the active object with context.view_layer.objects.active = obj or add teh object to the overrirde, since all are in edit mode. Somewhat related https://blender.stackexchange.com/questions/102207/subdividing-cubes-at-different-intervals/102239?r=SearchResults&s=1|29.7080#102239 – batFINGER Apr 17 '21 at 06:06
  • I'm not sure I understand how to implement this code snippet, nothing changes when adding that in the loop body. – Austin Lelonek Apr 18 '21 at 00:12
  • bpy.context.view_layer.objects.active = obj is what I needed! Note the prefix bpy.* is the only thing missing. Can you add an answer batFINGER – Austin Lelonek Apr 18 '21 at 00:33

0 Answers0