I'm attempting to write my first add on. It cycles through all the objects in the scene and applies all the modifiers each object has.
I've set up the panels correctly so when I press a button it calls the following method:
def applyAllModifiers():
context = bpy.context
scene = context.scene
settings = 'PREVIEW'
apply_modifiers = True
bpy.ops.object.mode_set(mode='OBJECT')
for ob in bpy.context.selected_objects:
print ("Applying modifiers on object " + ob.name)
bpy.context.scene.objects.active = bpy.data.objects[ob.name]
numModifiers = len(ob.modifiers)
if numModifiers != 0:
# Get all the info we need from the object
objName = ob.name
objLocation = ob.location
objRotation = ob.rotation_euler
objScale = ob.scale
mesh = ob.to_mesh(scene, apply_modifiers, settings)
# Delete the current object
scene.objects.unlink(ob)
bpy.data.objects.remove(ob)
# Create a new object with all modifers applied
new_object = bpy.data.objects.new(objName, mesh)
scene.objects.link(new_object)
new_object.location = objLocation
new_object.rotation_euler = objRotation
new_object.scale = objScale
new_object.select = False
print (" applied {} modifiers".format(numModifiers))
else:
ob.select = False
print (" object has no modifiers")
bpy.ops.object.select_all(action='SELECT')
It works if I have a single object selected, but if I have multiple objects selected it will randomly move some objects to (0,0,0). I can't seem to get any consistent behavior when I have more than 1 object selected. I'm not really sure this is even the best approach to what I'm trying to do.
Any help would be appreciated. Thanks!