Views
No views yet
pip install -U transformers accelerate peft torch1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5BASE_MODEL = "meta-llama/Llama-3.2-3B-Instruct"
6LORA_ADAPTER = "rachelkluu/bioinspired3D"
7
8# Set this to your preferred device, e.g. "cuda:0" or "cpu"
9DEVICE_3D = "cuda:0"
10
11bio3d_tok = AutoTokenizer.from_pretrained(BASE_MODEL)
12
13base_model = AutoModelForCausalLM.from_pretrained(
14 BASE_MODEL,
15 torch_dtype=torch.float16,
16 device_map={"": DEVICE_3D},
17)
18
19bio3d_model = PeftModel.from_pretrained(base_model, LORA_ADAPTER)
20bio3d_model.eval()
21
22def format_input(prompt: str) -> str:
23 return (
24 "<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n"
25 "You are a helpful assistant<|eot_id|>"
26 "<|start_header_id|>user<|end_header_id|>\n\n"
27 f"{prompt}<|eot_id|>"
28 "<|start_header_id|>assistant<|end_header_id|>\n\n"
29 )1def extract_blender_code(model_out: str) -> str:
2 matches = list(re.finditer(r"```python\s*(.*?)```", model_out, flags=re.DOTALL))
3 if matches:
4 return matches[-1].group(1).strip()
5 pos = model_out.rfind("import bpy")
6 return model_out[pos:].strip() if pos != -1 else model_out.strip()
7
8
9def clean_blender_code(text: str) -> str:
10 if not text:
11 return "import bpy"
12 code = text.strip()
13 code = code.replace("```python", "").replace("```", "")
14 code = re.sub(r"[\x00-\x08\x0b-\x1f]", "", code)
15 if not code.lstrip().startswith("import bpy"):
16 code = "import bpy\n" + code
17 return code1prompt = """Write Blender code to make a cellular structure."""
2
3formatted = format_input(prompt)
4inputs = bio3d_tok(formatted, return_tensors="pt").to(bio3d_model.device)
5
6with torch.no_grad():
7 outputs = bio3d_model.generate(
8 **inputs,
9 max_new_tokens=2048,
10 do_sample=True,
11 temperature=0.1,
12 top_p=0.9,
13 )
14
15raw = bio3d_tok.decode(outputs[0], skip_special_tokens=True)
16raw_code = extract_blender_code(raw)
17blender_code = clean_blender_code(raw_code)
18
19print(blender_code)Write Blender code to make a cellular structure with smooth curves and layers on top and bottom1import bpy
2import addon_utils
3
4# Clear the scene
5bpy.ops.object.select_all(action='SELECT')
6bpy.ops.object.delete(use_global=False)
7
8# Parameters for the sandwich structure
9strut_thickness = 0.2 # Thickness of the struts
10smoothness_level = 3 # Level of smoothness
11slab_thickness = 0.1 # Thickness of the top and bottom slabs
12
13def create_cellular_sandwich():
14 # Function to create a smooth cellular sandwich structure.
15 # Steps:
16 # 1. Create a cube.
17 # 2. Fracture the cube into pieces.
18 # 3. Apply wireframe modifier for cellular effect.
19 # 4. Apply subsurface modifier for smoothing.
20 # 5. Create top and bottom slabs.
21
22 # Enable the Fracture Cell add-on
23 addon_utils.enable('object_fracture_cell')
24
25 # Create a cube
26 bpy.ops.mesh.primitive_cube_add()
27 cube = bpy.context.active_object
28
29 # Scale the cube
30 cube.scale = (1, 1, 0.75)
31 bpy.ops.object.transform_apply(scale=True)
32
33 # Fracture the cube
34 bpy.ops.object.add_fracture_cell_objects(
35 source={'PARTICLE_OWN'},
36 source_limit=100,
37 source_noise=0.5,
38 cell_scale=(1, 1, 1),
39 recursion=0,
40 recursion_source_limit=8,
41 recursion_clamp=250,
42 recursion_chance=0.25,
43 recursion_chance_select='SIZE_MIN',
44 use_smooth_faces=False,
45 use_sharp_edges=True,
46 use_sharp_edges_apply=True,
47 use_data_match=True,
48 use_island_split=True,
49 margin=0.001,
50 material_index=0,
51 use_interior_vgroup=False,
52 mass_mode='VOLUME',
53 mass=1,
54 use_recenter=True,
55 use_remove_original=True,
56 collection_name='',
57 use_debug_points=False,
58 use_debug_redraw=True,
59 use_debug_bool=False
60 )
61
62 # Remove the original cube
63 if cube and cube.name in bpy.data.objects:
64 bpy.data.objects.remove(cube, do_unlink=True)
65
66 # Join fractured pieces
67 bpy.ops.object.select_all(action='DESELECT')
68 for obj in bpy.context.scene.objects:
69 if obj.type == 'MESH':
70 obj.select_set(True)
71 bpy.context.view_layer.objects.active = obj
72 bpy.ops.object.join()
73
74 # Clean up geometry
75 active_mesh = bpy.context.view_layer.objects.active
76 bpy.ops.object.mode_set(mode='EDIT')
77 bpy.ops.mesh.select_all(action='SELECT')
78 bpy.ops.mesh.remove_doubles(threshold=0.2)
79 bpy.ops.mesh.dissolve_limited()
80 bpy.ops.object.mode_set(mode='OBJECT')
81
82 # Wireframe Modifier
83 wireframe_mod = active_mesh.modifiers.new(name='Wireframe', type='WIREFRAME')
84 wireframe_mod.thickness = strut_thickness
85 wireframe_mod.offset = -1
86 wireframe_mod.use_even_offset = False
87 wireframe_mod.use_relative_offset = True
88 wireframe_mod.use_replace = True
89 bpy.ops.object.convert(target='MESH')
90
91 # Subsurface Modifier
92 subsurf_mod = active_mesh.modifiers.new(name='Subdivision', type='SUBSURF')
93 subsurf_mod.levels = smoothness_level
94 subsurf_mod.render_levels = smoothness_level
95
96 # Create slabs
97 slab_height = 1.5
98 bottom_slab_z = -slab_height / 2 - slab_thickness / 2
99 top_slab_z = slab_height / 2 + slab_thickness / 2
100
101 # Bottom slab
102 bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, bottom_slab_z))
103 bottom_slab = bpy.context.active_object
104 bottom_slab.scale = (1, 1, slab_thickness / 2)
105 bpy.ops.object.transform_apply(scale=True)
106
107 # Top slab
108 bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, top_slab_z))
109 top_slab = bpy.context.active_object
110 top_slab.scale = (1, 1, slab_thickness / 2)
111 bpy.ops.object.transform_apply(scale=True)
112
113# Create the sandwich structure
114create_cellular_sandwich()
Write Blender code to make a helical bioinspired structure with flat rectangular struts in each layer1import bpy
2import math
3
4bpy.ops.object.select_all(action='SELECT')
5bpy.ops.object.delete(use_global=False)
6
7# Parameters for the helical fiber structure
8ply_count = 7 # Number of plies in the structure
9angle_increment = 10 # Rotation angle for each ply
10fiber_form ='rect' # Shape of the fibers
11
12# Dimensions of the structure
13structure_height = 2 # Total height of the structure
14plate_dimension = 2 # Width and length of each plate
15thickness_per_ply = structure_height / ply_count # Thickness of each ply
16fiber_gap = 0.01 # Gap between fibers
17
18def create_fiber(form, diameter, x, y, z, thickness):
19 if form =='rect':
20 bpy.ops.mesh.primitive_cube_add(size=1.0, location=(x, y, z), rotation=(0, math.radians(90), 0))
21 fiber = bpy.context.object
22 fiber.dimensions = (thickness, diameter, plate_dimension)
23 return fiber
24
25def generate_ply(index):
26 # Generate a ply of fibers in a helical arrangement.
27 z_position = index * thickness_per_ply
28 rotation_angle = index * angle_increment
29
30 bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, z_position))
31 empty_object = bpy.context.object
32
33 fiber_diameter = thickness_per_ply
34 fiber_distance = fiber_diameter + fiber_gap
35 fiber_count = max(1, int(plate_dimension / fiber_distance))
36
37 total_fiber_space = fiber_count * fiber_distance
38 start_y_position = -plate_dimension / 2 + fiber_distance / 2 + (plate_dimension - total_fiber_space) / 2
39
40 for i in range(fiber_count):
41 fiber_y_center = start_y_position + i * fiber_distance
42 fiber_instance = create_fiber(fiber_form, fiber_diameter, 0, fiber_y_center, z_position, thickness_per_ply)
43 fiber_instance.parent = empty_object
44 fiber_instance.matrix_parent_inverse = empty_object.matrix_world.inverted()
45
46 empty_object.rotation_euler[2] = math.radians(rotation_angle)
47 return empty_object
48
49# Create the helical structure
50for i in range(ply_count):
51 generate_ply(i)
1@article{luu2026bioinspired123d,
2 title={Bioinspired123D: Generative 3D Modeling System for Bioinspired Structures},
3 author={Luu, Rachel K. and Buehler, Markus J.},
4 year={2026}
5}