How to edit all the objects in scene one by one automatically via script ?

Hi all,

I try to write a script to clean up all the objects in scens for two actions, remove double vertes and convert all tris to faces. But I face a issue which is: Blender cannot help me to edit all the objects in scene one by one coz the bpy.ops.object.mode_set.poll() could not be changed by script, so I can now only execute it by hand selected. Here below is my script, hope any one could suggest me to work this out! Thanks :slight_smile:


import bpy

def tris2Face():

if bpy.ops.mesh.tris_convert_to_quads.poll():
   bpy.ops.mesh.tris_convert_to_quads()
print ("tris convert done")

return

def removeDoubleVe():

if bpy.ops.mesh.remove_doubles.poll():
   bpy.ops.mesh.remove_doubles()
print ("remove done")
return

def oneButtonCleanUp( object ):

#bpy.ops.object.editmode_toggle()
if bpy.ops.object.mode_set.poll() == False:
   bpy.ops.object.mode_set.poll = True
   object
   ob.select = True
   ob = bpy.context.active_object
   bpy.ops.object.mode_set(mode='EDIT')
else:
   bpy.ops.object.mode_set(mode='EDIT')

if bpy.ops.mesh.select_all.poll():
   bpy.ops.mesh.select_all(action='SELECT')

tris2Face()
removeDoubleVe()

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

return object

class OneButtonCleanUp( bpy.types.Operator ):
bl_label = “Clean and Tris Convert”
bl_options = { ‘REGISTER’}
bl_idname = “object.onebuttonclean”

def execute( self, context ):
    
    scn = bpy.context.scene
    for ob in scn.objects:
        if ob.type == 'MESH':
           oneButtonCleanUp( ob )
        
    return {'FINISHED'}

class ObjectCleanUpPanel( bpy.types.Panel ):

bl_label = "Obj Clean Up Panel"
bl_region_type = "TOOLS"
bl_space_type = "VIEW_3D"

def draw( self, context ):
    scn = context.scene
    new_row = self.layout.row
  
    new_row().operator( "object.onebuttonclean" )

def register():
bpy.utils.register_class( OneButtonCleanUp )
bpy.utils.register_class( ObjectCleanUpPanel )

def unregister():
bpy.utils.register_class( OneButtonCleanUp )
bpy.utils.register_class( ObjectCleanUpPanel )

if name == ‘main’:
register()

‘poll’ is a function, not a property. The job of ‘poll’ is to test the data in the Context and determine if the operator can run successfully. For example, calling the operator to convert triangles into quads will only work if you are in Edit mode for a mesh. It would not make sense for that operator to run if had a camera selected in Object mode.

Rather than attempt to set the ‘poll’ function you need to control the ‘selected’ property on your objects and also control which object is the Active object.

You can change
obj.selected = False

To change the active object, you set this:
bpy.context.scene.objects.active = obj

I would recommend the following adjustments to your script.

  • At the start of the script, ensure that all items are deselected.
  • When you loop though the items, select the item you are iterating on and also set it active.
  • deselect the item when you are done with it.