Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.8 kB
2
Indexable
Never
import bpy

# Funkcja do tworzenia pojedynczego klocka LEGO 2x4
def create_lego_brick(width_count=2, length_count=4, height=1.2):
    stud_radius = 0.25  # Promień pojedynczego studa
    stud_height = 0.2   # Wysokość studa
    stud_spacing = 0.8  # Odległość między studami

    # Wymiary klocka
    brick_width = width_count * stud_spacing
    brick_length = length_count * stud_spacing
    brick_height = height

    # Tworzenie podstawy klocka (prostopadłościan)
    bpy.ops.mesh.primitive_cube_add(size=2)
    brick = bpy.context.object
    brick.scale = (brick_width / 2, brick_length / 2, brick_height / 2)
    brick.location.z = brick_height / 2

    # Nazwij klocek
    brick.name = f"LEGO_Brick_{width_count}x{length_count}"

    # Tworzenie studów na górze klocka
    for i in range(width_count):
        for j in range(length_count):
            bpy.ops.mesh.primitive_cylinder_add(radius=stud_radius, depth=stud_height)
            stud = bpy.context.object
            stud.location = (
                i * stud_spacing - (brick_width - stud_spacing) / 2, 
                j * stud_spacing - (brick_length - stud_spacing) / 2, 
                brick_height + stud_height / 2
            )
            stud.select_set(True)
            brick.select_set(True)
            bpy.context.view_layer.objects.active = brick
            bpy.ops.object.join()  # Łączenie studów z klockiem

    # Tworzenie otworów (recesses) pod spodem klocka
    recess_radius = stud_radius * 0.95
    recess_depth = stud_height * 1.5

    for i in range(width_count - 1):
        for j in range(length_count - 1):
            bpy.ops.mesh.primitive_cylinder_add(radius=recess_radius, depth=recess_depth)
            recess = bpy.context.object
            recess.location = (
                i * stud_spacing - (brick_width - stud_spacing) / 2 + stud_spacing / 2, 
                j * stud_spacing - (brick_length - stud_spacing) / 2 + stud_spacing / 2, 
                -brick_height / 2 - recess_depth / 2
            )
            recess.rotation_euler = (0, 0, 0)
            bpy.context.view_layer.objects.active = brick
            bpy.ops.object.modifier_add(type='BOOLEAN')
            bpy.context.object.modifiers["Boolean"].operation = 'DIFFERENCE'
            bpy.context.object.modifiers["Boolean"].object = recess
            bpy.ops.object.modifier_apply(modifier="Boolean")
            bpy.data.objects.remove(recess)  # Usunięcie wnęki po zastosowaniu modyfikatora
    
    return brick

# Usuwanie domyślnej kostki w scenie
if "Cube" in bpy.data.objects:
    bpy.data.objects['Cube'].select_set(True)
    bpy.ops.object.delete()

# Tworzenie klocka LEGO 2x4
create_lego_brick(2, 4, 1.2)  # Można zmieniać wymiary (np. 1x2, 2x2, 4x4 itp.)
Leave a Comment