1
$\begingroup$

I'm trying (with help from ChatGPT) to write an addon that will simplify baking. What I'm essentially stuck on is adding a baking menu next to the other VIEW3D_MT menus. Did something change in 4.0 to create those dropdowns?

ChatGPT couldn't figure it out, it just kept giving the same kind of thing, with some things slightly renamed every time.

import bpy

class TextureBakingAddon(bpy.types.Operator):
    bl_idname = "object.texture_baking"
    bl_label = "Texture Baking"
    
    output_folder: bpy.props.StringProperty(
        name="Output Folder",
        default="//",  # Use the current blend file directory by default
        subtype='DIR_PATH'
    )
    
    def execute(self, context):
        # Get the selected objects
        selected_objects = bpy.context.selected_objects
        
        for obj in selected_objects:
            # Set the active object for baking
            bpy.context.view_layer.objects.active = obj
            
            # Create a folder for each object
            folder_name = bpy.path.clean_name(obj.name)
            output_folder = bpy.path.abspath(self.output_folder) + folder_name
            bpy.ops.script.python_file_run(filepath="path/to/create_folder_script.py",  # Replace with the actual path
                                          write_script=False, start_dir=output_folder)
            
            # Set up the bake settings
            bpy.ops.object.bake(type='DIFFUSE')  # You can customize this based on your needs
            
            # Save the baked textures to the specified folder
            bpy.ops.image.save_as(save_as_render=True, copy=True, filepath=output_folder + "/base_color.png")
            bpy.ops.image.save_as(save_as_render=True, copy=True, filepath=output_folder + "/metallic.png")
            bpy.ops.image.save_as(save_as_render=True, copy=True, filepath=output_folder + "/roughness.png")
            bpy.ops.image.save_as(save_as_render=True, copy=True, filepath=output_folder + "/normal.png")
            bpy.ops.image.save_as(save_as_render=True, copy=True, filepath=output_folder + "/specular.png")
        
        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self)

class VIEW3D_MT_BakingMenu(bpy.types.Menu):
    bl_idname = "VIEW3D_MT_BakingMenu"
    bl_label = "Baking"

    def draw(self, context):
        layout = self.layout
        layout.operator(TextureBakingAddon.bl_idname)

def menu_func(self, context):
    layout = self.layout
    layout.menu(VIEW3D_MT_BakingMenu.bl_idname)

def register():
    bpy.utils.register_class(TextureBakingAddon)
    bpy.utils.register_class(VIEW3D_MT_BakingMenu)
    bpy.types.VIEW3D_MT_BakingMenu.append(menu_func)  # Append to the custom VIEW3D_MT_BAKING_MENU

def unregister():
    bpy.utils.unregister_class(TextureBakingAddon)
    bpy.utils.unregister_class(VIEW3D_MT_BakingMenu)
    bpy.types.VIEW3D_MT_BakingMenu.remove(menu_func)

if __name__ == "__main__":
    register()
```
$\endgroup$
2
  • $\begingroup$ You need to append the menu function to an existing class in line 58 bpy.types.VIEW3D_MT_view.append(menu_func) $\endgroup$
    – relaxed
    Nov 26, 2023 at 8:29
  • $\begingroup$ Rather try VIEW3D_HT_header to add a menu to the header $\endgroup$
    – Gorgious
    Nov 26, 2023 at 18:45

0

You must log in to answer this question.

Browse other questions tagged .