Can I directly export a specific object using Python?

I’m working on an addon where I’d like to add the ability to export a mesh to OBJ. Until now, I’ve been relying on the operator bpy.ops.export_scene.obj(). I’ve been trying to ensure that only the object I want to export is selected before calling the operator. However, this gets tricky when there are multiple objects that need to be exported.

Is there a way to use Python to export one specific scene object to OBJ by passing the object ID/Name?

Remember you can have multiple objects selected at once. It is only the active object that has to be a single object.

Alternatively, the exporter only exports the current scene. So another way is to create a new, temporary scene, and link only the things into that you want exported. Then delete that Scene.