[Python] How do I write a python code for Blender to refine a shape?

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am on the final steps on my project of creating a 3D model for CGRP staining and need some help with the coding. I have been trying to figure out how to generate a 3D model that connects the coordinate points, in order to create a hollow shape. The problem with my codes is that the coordinates do not connect the way I need them to. I need them to connect with the points above and below within layers I have created, but now they connect to all other coordinate points.

    I have included some of the codes I have been using in order to create the initial model. The first code is what I’ve used for the 2D outlines stacked and not connected:

    import bpy

    # List of coordinates (x, y, z)
    coordinates = [
    ]

    # Create a point at each coordinate
    for coord in coordinates:
    # Create an empty object at the given coordinate
    empty = bpy.data.objects.new("Point", None)
    empty.location = coord

    # Add empty to the scene
    bpy.context.collection.objects.link(empty)

    # Optionally set a display size (for visibility)
    empty.empty_display_size = 0.2 # Adjust as needed
    empty.empty_display_type = 'SPHERE' # Set the display type to make it more
    visible (SPHERE, CUBE, etc.)


    The second code creates a 3D shape, but it connects the points to all points, instead of nearby points:

    import bpy
    import bmesh
    from mathutils import Vector

    # Define your coordinates here as (x, y, z)
    coordinates = [
    ]

    # Create a new mesh and object
    mesh = bpy.data.meshes.new("CustomSurface")
    obj = bpy.data.objects.new("CustomObject", mesh)

    # Link the object to the scene
    bpy.context.collection.objects.link(obj)

    # Create a bmesh object to handle geometry
    bm = bmesh.new()

    # Add vertices
    vertex_map = {}
    for idx, coord in enumerate(coordinates):
    vertex = bm.verts.new(coord)
    vertex_map[idx] = vertex

    bm.verts.ensure_lookup_table()

    # Triangulation: Connect points as a convex hull
    # This ensures the points are connected in a valid surface
    try:
    bmesh.ops.convex_hull(bm, input=bm.verts)
    except RuntimeError:
    print("Failed to create a convex hull. Ensure points define a valid surface.")

    # Write the bmesh data into the mesh
    bm.to_mesh(mesh)
    bm.free()

    print("Custom surface with multiple faces created.")


    I have also tried to use this code, which would ideally create the figure we are going for, but it crashes my Blender application every time I try to use it:

    import bpy

    # List of coordinates (x, y, z)
    coordinates = [
    ]

    # Create a point at each coordinate
    for coord in coordinates:
    # Create an empty object at the given coordinate
    empty = bpy.data.objects.new("Point", None)
    empty.location = coord

    # Add empty to the scene
    bpy.context.collection.objects.link(empty)

    # Optionally set a display size (for visibility)
    empty.empty_display_size = 0.2 # Adjust as needed
    empty.empty_display_type = 'SPHERE' # Set the display type to make it more
    visible (SPHERE, CUBE, etc.)

    # Create lines to connect the points
    if len(coordinates) > 1:
    mesh_data = bpy.data.meshes.new(name="Line")
    line_obj = bpy.data.objects.new("Line", mesh_data)
    bpy.context.collection.objects.link(line_obj)

    # Create edges from the coordinates
    edges = [[i, (i + 1) % len(coordinates)] for i in range(len(coordinates))]
    mesh_data.from_pydata([coordinates[edge[0]] for edge in edges], [], edges)
    mesh_data.update()


    I have been copying and pasting the coordinates into the program and I have a word document of about 200 pages of coordinate points. Please let me know if this is something you can help with!

    Continue reading...

Compartilhe esta Página