Add-ons and more

Export .obj file with the name of the active object preselected

When you select export .obj from the File menu the name of the file will typically be preselected as the name of your Blender file but with the .obj extension or just object.obj if you haven't named your Blender file yet.

That's fine, you can change the name of course, but when I am working on a collection of object variants, I typically want to export them one a time and to be able to recognize them easily, I typically name the file after the name of the object.

Can this be simplified?

The Export Active Obj add-on

Yes, this can be simplified because we can add the export obj operator to the File menu again, but with the filename prefilled with the name of the active object.

The code is shown below, and it sets some common options I use all the time as well, but the essence is in line 7 (there is some more code in the add-on to set things up). 


def menu_func(self, context):
    self.layout.separator()
    op = self.layout.operator(
        "wm.obj_export",
        text="Export Active Obj",
    )
    op.filepath = context.active_object.name + ".obj"
    op.forward_axis = "Y"
    op.up_axis = "Z"
    op.export_selected_objects = True


def register():
    bpy.types.TOPBAR_MT_file_export.append(menu_func)

The code can be downloaded from my GitHub repo [file: export_active_obj.py] or directly from this link.


No comments:

Post a Comment