I have a script that allows to import and export .obj
files which is working fine. However, I'd like to perform some operations in between these calls, for example select the mesh and recalculate the normals (outside). If I try that, I get a context error and I'm not sure why...?
RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect
bpy.ops.import_scene.obj(filepath=source_to_file)
print("Starting--------------------------------------------")
for obj in bpy.data.objects:
bpy.context.scene.objects.active = obj
# go edit mode
bpy.ops.object.mode_set(mode='EDIT')
# select al faces
bpy.ops.mesh.select_all(action='SELECT')
# recalculate outside normals
bpy.ops.mesh.normals_make_consistent(inside=False)
# go object mode again
bpy.ops.object.editmode_toggle()
print("Ending--------------------------------------------")
bpy.ops.export_scene.obj(filepath=target_to_file,use_materials=True)
How is it possible to know the object imported by any operator (there is no return value) and how can I recalculate the normals per object that is triggered by a button?