Sunday, January 01, 2023

Creating a Blender Add-On with ChatGPT

ChatGPT

A video about my first attempt to get ChatGPT to write a Python script for Blender to render multiple cameras:


 

A video about my successful second attempt to get ChatGPT to modify this script into a Blender add-on:




Here is my Github repository with the completed add-on.

It looks like the code you provided is for creating a Blender add-on that allows the user to render the animation for each camera in the scene. Here is a version of the code with some minor corrections and improvements:

Copy code
bl_info = {
    "name": "Render Cameras",
    "blender": (3, 4, 1),
    "category": "Render",
}

import bpy

class RenderCamerasOperator(bpy.types.Operator):
    """Renders the animation for each camera in the scene"""
    bl_idname = "render.render_cameras"
    bl_label = "Render Cameras"

    def execute(self, context):
        # Get the current settings
        frame_start = bpy.context.scene.frame_start
        frame_end = bpy.context.scene.frame_end
        file_format = bpy.context.scene.render.image_settings.file_format
        encoding_container = bpy.context.scene.render.ffmpeg.format
        video_codec = bpy.context.scene.render.ffmpeg.codec
        color_depth = bpy.context.scene.render.image_settings.color_mode
        output_dir = bpy.context.scene.render.filepath

        # Get the file name
        file_name = bpy.path.basename(bpy.context.blend_data.filepath)
               
        # Find the first object with an action
        first_object = None
        for obj in bpy.data.objects:
            if obj.animation_data is not None and obj.animation_data.action is not None:
                first_object = obj
                break  # No need to keep searching, so we can exit the loop

        # Get the action name
        action_name = first_object.animation_data.action.name if first_object else ""
 
        # Save the first camera to use later
        first_camera = None

        # Render each camera in the scene
        for camera_obj in bpy.data.objects:
            if camera_obj.type == 'CAMERA':
                # Save the first camera
                if first_camera is None:
                    first_camera = camera_obj
                # Set the active camera
                bpy.context.scene.camera = camera_obj
                # Set the output file name
                bpy.context.scene.render.filepath = f"{output_dir}{action_name} {file_name} {camera_obj.name} "
                # Set the frame range
                bpy.context.scene.frame_start = frame_start
                bpy.context.scene.frame_end = frame_end
                # Set the other rendering options
                bpy.context.scene.render.image_settings.file_format = file_format
                bpy.context.scene.render.ffmpeg.format = encoding_container
                bpy.context.scene.render.ffmpeg.codec = video_codec
                bpy.context.scene.render.image_settings.color_mode = color_depth
                # Render the scene and save the output
                bpy.ops.render.render(animation=True, write_still=True)

        # Set the active camera to the first




I got this error message: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 3.4\3.4\scripts\modules\addon_utils.py", line 333, in enable
    mod = __import__(module_name)
ModuleNotFoundError: No module named 'Render Cameras ver 0'

This error message indicates that the add-on is not being found by Blender. One possible reason for this is that the add-on script is not located in the correct directory.

In order to use an add-on in Blender, you need to place the script in one of the directories listed in the scripts/addons folder of your Blender installation. You can then enable the add-on in Blender by going to the Edit > Preferences menu and selecting the Add-ons tab. In the Add-ons tab, you can search for the name of the add-on and click the checkbox next to it to enable it.

If the add-on script is located in the correct directory and you are still encountering this error, it could be due to a typo in the bl_info dictionary. Make sure that the "name" value in the bl_info dictionary matches the name of the script file exactly (including the case).

I hope this helps! Let me know if you have any other questions.

Great success! Thank you.
You're welcome! I'm glad to have been able to help you. If you have any further questions or need additional assistance with creating Blender add-ons, don't hesitate to ask. I'm here to help!