analitics

Pages

Monday, December 12, 2022

OpenAI knows how to write source code for Blender 3D.

I tested the new OpenAI chat from with this question:
Please write a 3d blender script in Python that moves a camera around an object 360 degrees facing it.

import bpy

# Set the object that the camera will orbit around
target_object = bpy.data.objects["MyObject"]

# Set the starting position for the camera
camera = bpy.data.objects["Camera"]
camera.location = (0, 0, 5)

# Set the number of degrees to rotate the camera around the object
degrees = 360

# Set the distance that the camera should be from the object
distance = 5

# Set the speed at which the camera should orbit
speed = 1

# Set the direction in which the camera should orbit (1 for clockwise, -1 for counter-clockwise)
direction = 1

# Calculate the number of frames needed for the camera to orbit
# around the object by the desired number of degrees
num_frames = degrees / speed

# Set the camera to track the object
bpy.ops.object.select_all(action="DESELECT")
camera.select_set(True)
bpy.context.view_layer.objects.active = camera
bpy.ops.object.track_set(type="TRACKTO")

# Animate the camera orbiting around the object
for frame in range(0, num_frames):
    # Set the current frame
    bpy.context.scene.frame_set(frame)

    # Calculate the new position for the camera based on its distance from the object
    x = distance * math.sin(math.radians(frame*speed*direction))
    y = distance * math.cos(math.radians(frame*speed*direction))
    camera.location = (x,y,0)
I tested with the latest version of Blender 3D and it works well with some small changes it can be improved.
This error for this script:
Python: Traceback (most recent call last):
...
TypeError: 'float' object cannot be interpreted as an integer
If you ask more than once you will get different source code and I encountered a network error.
I tried several times and the network error did not let me take the more complex and complete source code.