Issue Applying Edit Mode Changes to All Selected Objects

So I’m trying to create a script that will mirror all selected objects, freeze transforms, mirror their UVs, and flip their normals back. What I wrote works with one object, but with more than one it seems to work on only half of the selected objects. A little testing suggests what’s happening is that the edit mode flips are being repeated twice on some of the objects, canceling themselves out. Anyone know what I’m doing wrong to cause this? Thanks!


import bpy


bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY') #sets origin to center of geometry
bpy.ops.transform.mirror(constraint_axis=(True, False, False), constraint_orientation='GLOBAL', proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
    
for ob in bpy.context.selected_objects:
    bpy.ops.object.editmode_toggle()
    bpy.context.scene.objects.active = ob
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.flip_normals()
    #bpy.ops.mesh.normals_make_consistent(inside=False) #Test
    bpy.context.area.type = 'PROPERTIES'
    bpy.context.area.type = 'IMAGE_EDITOR'
    bpy.context.space_data.pivot_point = 'CURSOR'
    bpy.context.space_data.cursor_location[0] = 128
    bpy.ops.transform.resize(value=(-1, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
    bpy.ops.object.editmode_toggle() #puts scene back in object mode
    
bpy.context.area.type = 'TEXT_EDITOR' #CHANGE THIS TO 3D BEFORE PUBLISHING

Nevermind, I realized the problem; I was switching into edit mode before iterating through the active geometry.

Choose what mode you want with


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

pay to set the active object before setting the mode.


>>> bpy.ops.object.mode_set(mode='XXX')
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "/home/batfinger/src/BlenderGIT/qtcreator_build/bin/2.76/scripts/modules/bpy/ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
TypeError: Converting py args to operator properties:  enum "XXX" not found in ('OBJECT', 'EDIT', 'SCULPT', 'VERTEX_PAINT', 'WEIGHT_PAINT', 'TEXTURE_PAINT')

oops XXX isn’t a mode, oh well there’s a list to choose from.

Some operators operate on selected objects collection, if you want to only work on one at a time, copy the selected_objects list


cob = context.object
obs = [o for o in context.selected_objects]
bpy.ops.object.select_all(action='DESELECT') # ('TOGGLE', 'SELECT', 'DESELECT', 'INVERT')

for o in obs:
    # do your stuff

# return them selected
for ob in obs:
    ob.select = True
# return original context object
context.scene.objects.active = cob