I have an issue with exporting as an OBJ where no matter what I try I can't get the "g" tag in the OBJ file to be assigned to the material name, is there a script that could rename them or a setting I'm missing for this.

Currently I have a test scene of a cube with the object name of "Mesh Name" the mesh name left as "Cube" and a material assigned called "swatch" however when I export as an OBJ and have a look at the file in notepad the g tag is as follows;

g Mesh_Name_Cube_Swatch

but I need it to be just

g Swatch

If anyone knows how I can set that up in blender that would be great.

All the best, Brad


1 Answer 1


I don´t think there is a setting in io_scene_obj to change the material group output (g tag). It´s fixed to 'g ob.name_ob.data.name_mat.name'. You can change it in a text editor and save file with extension .obj again because it´s a text file format. OK, that´s no fun with many entries. The little script should change all lines in an exported obj file to 'g mat.name'. Please note that mat.name starts from the last underscore. In other words, for this you shouldn´t use underscores in the material name. The original file is saved as file_old.obj

import bpy
from bpy_extras.io_utils import(ImportHelper)

class Readfile_OT_Obj(bpy.types.Operator, ImportHelper):
    bl_idname = "readfile.obj" 
    bl_label = "Open"  
    filter_glob : bpy.props.StringProperty(default = "*.obj",
                                options = {'HIDDEN'},
                                maxlen = 255, ) 
    def invoke(self, context, event): 
        return {'RUNNING_MODAL'}
    def execute(self, context): 
        data_old = import_data(context, self.filepath, import_data) 
        return export_data(context, self.filepath, data_old) 

def import_data(context, filepath, import_data):
    f = open(filepath, 'r', encoding = 'utf-8')
    import_data = f.read()
    return import_data

def export_data(context, filepath , data_old):
    # make copy 
    filepath_copy = filepath
    filepath = filepath_copy[0:-4] + "_old.obj"
    f = open(filepath, 'w', encoding = 'utf-8')
    # change file
    filepath = filepath_copy    
    data_new = ""
    lines = data_old.splitlines()
    for line in lines:
        if line.startswith("g ") and "_" in line:
            line = "g %s" % line[line.rfind("_")+1:]
        data_new += "%s%s" % (line, "\n")
    f = open(filepath, 'w', encoding = 'utf-8')
    return {'FINISHED'}        

classes = (Readfile_OT_Obj,)
def register():
    from bpy.utils import register_class
    for cls in classes:
def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
if __name__ == "__main__":
  • $\begingroup$ This is dead on what was needed. Thanks so much! works perfectly $\endgroup$ Nov 19, 2021 at 10:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .