3
$\begingroup$

So I have this code that moves the vertices to the front on the affected objects:

import bpy, bmesh

#objects = bpy.context.selected_objects

objects = bpy.data.collections["Collection"].objects

for ob in objects:

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

    me = ob.data
    bm = bmesh.from_edit_mesh(me)

    verts = [v for v in bm.verts]

    bmesh.ops.translate(bm, verts=verts, vec=(0.0, -20.0, 0.0))

    bmesh.update_edit_mesh(me)
    
    
bpy.ops.object.mode_set(mode='OBJECT')

It works perfectly fine for 'selected objects' if it uses the second line instead of the third one. Similarly, the third line is directly copy-pasted from another code that works perfectly fine with the objects in "Collection".

But for whatever reason it just refuses to work here. It says that the objects must be in Edit mode

Oddly enough, if the objects are selected, even with the second line of code, it does work.

Any ideas why it's been stubborn?

$\endgroup$
3
  • 1
    $\begingroup$ Hello, I suggest you study the concept of operator override. bpy.ops.object.mode_set operates on selected objects unless you use an override $\endgroup$
    – Gorgious
    Commented 2 days ago
  • $\begingroup$ @Gorgious Ok, that makes sense. So I just have to select the objects in the collection first and use the second line instead? $\endgroup$
    – Cornivius
    Commented 2 days ago
  • $\begingroup$ @Gorgious yea it works perfectly now, thanks $\endgroup$
    – Cornivius
    Commented 2 days ago

1 Answer 1

2
$\begingroup$

I got it to work with @Gorgious comment. I just had to select the objects in the collection first and use the second line of code instead of the third:

import bpy, bmesh

bpy.ops.object.select_all(action='DESELECT')

for obj in bpy.data.collections['Collection'].all_objects:
    obj.select_set(True)

objects = bpy.context.selected_objects

#objects = bpy.data.collections["Collection"].objects

for ob in objects:

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

    me = ob.data
    bm = bmesh.from_edit_mesh(me)

    verts = [v for v in bm.verts]

    bmesh.ops.translate(bm, verts=verts, vec=(0.0, -20.0, 0.0))

    bmesh.update_edit_mesh(me)
    
    
bpy.ops.object.mode_set(mode='OBJECT')
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .