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):
context.window_manager.fileselect_add(self)
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()
f.close()
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')
f.write(data_old)
f.close()
# 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')
f.write(data_new)
f.close()
return {'FINISHED'}
classes = (Readfile_OT_Obj,)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
if __name__ == "__main__":
register()
bpy.ops.readfile.obj('INVOKE_DEFAULT')