Search code examples
pythonblender

Blender 3.4 Crashing When Attempting to Run Python Script


I'm attempting to run a script that's meant to take an initial input for the amount of vertices I want on an object for it to make and a secondary string for the coordinates that I want it to set to each vertex. The problem is that it crashes right when I hit run. File named "setCoordinates.py".

import bpy

def create_object(num_vertices, coordinates_str):
    # Split into different strings for each vertex
    coordinate_strs = coordinates_str.strip().split("X: ")

    # Create a new mesh and object
    mesh = bpy.data.meshes.new("MyMesh")
    obj = bpy.data.objects.new("MyObject", mesh)
    bpy.context.collection.objects.link(obj)

    # Create a list to store the vertices
    vertices = []

    # Loop through the coordinate strings and add each vertex to the list
    for i in range(1, num_vertices + 1):
        # Split the current coordinate string into the X, Y, and Z components
        x, y, z = coordinate_strs[i].strip().split("Y: ")[1].strip().split("Z: ")
        
        # Add the vertex to the list
        vertices.append((float(x), float(y), float(z)))
        
    
    # Set the vertices of the mesh
    mesh.from_pydata(vertices, [], [])
    mesh.update()
    
# Get the number of vertices
num_vertices_str = input("Enter the number of vertices: ")
#Get the coordinate string
coordinates_str = input("Enter the vertex coordinates: ")

# Convert the number of vertices string to an integer
num_vertices = int(num_vertices_str)

create_object(num_vertices, coordinates_str)

I have an empty project with just an isosphere in it. I've tried to rearrange the code, and remove entire blocks, but it does the same thing. I've ran it in the Text Editor.


Solution

  • I tried running your script in Blender 3.4.1 and it worked. I think what happened is that Blender was waiting for your input so it looked like a crash. Since you're using the input Python function, the program won't continue until you enter the input in the terminal. There's a terminal window that automatically opens when you run Blender and the input prompts will appear there.

    Also I changed this part of the program (line 16) because there was an error:

    for coord_str in coordinate_strs[1:]:
        # Split the current coordinate string into the X, Y, and Z components
        coord_str = coord_str.split(" Y: ")
        x = coord_str[0]
        
        coord_str = coord_str[1].split(" Z: ")
        y = coord_str[0]
        
        z = coord_str[1]