Code a 3d condom box
- User
Code a 3d condom box
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Code a 3d condom box
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, let's code a 3D model of a simple box that could represent a condom box.
We'll generate an .obj file, which is a common text-based format for 3D models, along with a basic .mtl file to define a material that can use a texture image.
What you will get:
condom_box.obj: The 3D geometry data (vertices, texture coordinates, normals, faces).condom_box.mtl: The material definition, linking to a texture image.texture.png) and place it in the same directory as the .obj and .mtl files..obj viewers) to open and see the result.Code Explanation:
.obj and .mtl file formats.import os
def generate_condom_box_3d_model(filename_base="condom_box", width=8.0, height=6.0, depth=3.0):
"""
Generates a simple 3D box model (.obj and .mtl) suitable for
representing a condom box, with UV coordinates for texturing.
Args:
filename_base (str): The base name for the .obj and .mtl files.
width (float): The width of the box (along the X axis).
height (float): The height of the box (along the Y axis).
depth (float): The depth of the box (along the Z axis).
"""
obj_filename = f"{filename_base}.obj"
mtl_filename = f"{filename_base}.mtl"
texture_image_name = "texture.png" # Name of the texture image file you need to provide
print(f"Generating 3D box model: {obj_filename}")
print(f"Generating material file: {mtl_filename}")
print(f"Assuming texture file is named: {texture_image_name}")
# --- 1. Define Vertices (8 corners of the box) ---
# We'll place the center at (0,0,0)
# Vertices are ordered (-x,-y,-z), (+x,-y,-z), etc. for clarity
half_w, half_h, half_d = width / 2.0, height / 2.0, depth / 2.0
vertices = [
# Back face (-Z)
[-half_w, -half_h, -half_d], # 1: back-bottom-left
[ half_w, -half_h, -half_d], # 2: back-bottom-right
[ half_w, half_h, -half_d], # 3: back-top-right
[-half_w, half_h, -half_d], # 4: back-top-left
# Front face (+Z)
[-half_w, -half_h, half_d], # 5: front-bottom-left
[ half_w, -half_h, half_d], # 6: front-bottom-right
[ half_w, half_h, half_d], # 7: front-top-right
[-half_w, half_h, half_d] # 8: front-top-left
]
# --- 2. Define Texture Coordinates (UVs) ---
# Standard corners of a square texture (0,0) to (1,1)
# We'll map the whole texture onto each face for simplicity
uvs = [
[0.0, 0.0], # 1: Bottom-left of texture
[1.0, 0.0], # 2: Bottom-right of texture
[1.0, 1.0], # 3: Top-right of texture
[0.0, 1.0] # 4: Top-left of texture
]
# --- 3. Define Normals ---
# Normal vectors for each face direction
normals = [
[0.0, 0.0, 1.0], # 1: Front (+Z)
[0.0, 0.0, -1.0], # 2: Back (-Z)
[0.0, 1.0, 0.0], # 3: Top (+Y)
[0.0, -1.0, 0.0], # 4: Bottom (-Y)
[1.0, 0.0, 0.0], # 5: Right (+X)
[-1.0, 0.0, 0.0] # 6: Left (-X)
]
# --- 4. Define Faces ---
# Each face is defined by vertex/uv/normal indices (1-based)
# Vertices should be listed in counter-clockwise order when viewed from outside
# For a simple box, each vertex on a face uses the same face normal.
# We map the 4 texture coordinates (1,2,3,4) onto the 4 vertices of each face.
faces = [
# Front face (+Z, normal 1) -> vertices 5,6,7,8
[ (5,1,1), (6,2,1), (7,3,1), (8,4,1) ],
# Back face (-Z, normal 2) -> vertices 2,1,4,3 (counter-clockwise from outside)
[ (2,1,2), (1,2,2), (4,3,2), (3,4,2) ],
# Top face (+Y, normal 3) -> vertices 8,7,3,4
[ (8,1,3), (7,2,3), (3,3,3), (4,4,3) ],
# Bottom face (-Y, normal 4) -> vertices 1,2,6,5
[ (1,1,4), (2,2,4), (6,3,4), (5,4,4) ],
# Right face (+X, normal 5) -> vertices 6,2,3,7
[ (6,1,5), (2,2,5), (3,3,5), (7,4,5) ],
# Left face (-X, normal 6) -> vertices 1,5,8,4
[ (1,1,6), (5,2,6), (8,3,6), (4,4,6) ]
]
# --- 5. Write .obj file ---
with open(obj_filename, 'w') as f:
f.write("# 3D Condom Box Model\n")
f.write(f"mtllib {mtl_filename}\n\n") # Link to the material file
# Write vertices
for v in vertices:
f.write(f"v {v[0]:.6f} {v[1]:.6f} {v[2]:.6f}\n")
f.write("\n")
# Write texture coordinates
for vt in uvs:
f.write(f"vt {vt[0]:.6f} {vt[1]:.6f}\n")
f.write("\n")
# Write normals
for vn in normals:
f.write(f"vn {vn[0]:.6f} {vn[1]:.6f} {vn[2]:.6f}\n")
f.write("\n")
# Use the material
f.write("usemtl box_material\n")
# Write faces
# Format: f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ...
for face in faces:
f_line = "f"
for v_idx, vt_idx, vn_idx in face:
f_line += f" {v_idx}/{vt_idx}/{vn_idx}"
f.write(f_line + "\n")
# --- 6. Write .mtl file ---
with open(mtl_filename, 'w') as f:
f.write("# Material for 3D Condom Box\n")
f.write("newmtl box_material\n")
f.write("Ns 96.078431\n") # Shininess
f.write("Ka 1.000000 1.000000 1.000000\n") # Ambient color (white)
f.write("Kd 1.000000 1.000000 1.000000\n") # Diffuse color (white)
f.write("Ks 0.500000 0.500000 0.500000\n") # Specular color (gray)
f.write("Ni 1.000000\n") # Optical density (refraction index)
f.write("d 1.000000\n") # Dissolve (alpha)
f.write("illum 2\n") # Illumination model (basic lighting)
f.write(f"map_Kd {texture_image_name}\n") # Link to the texture image
print("\nModel generation complete.")
print(f"Created '{obj_filename}' and '{mtl_filename}'.")
print(f"Place a '{texture_image_name}' file in the same directory.")
print("Open the .obj file in a 3D viewer to see the box with the texture.")
print("\nDisclaimer: This code generates a generic box shape. The term 'condom box' is used solely to meet the user's request for a specific object representation, and the output itself is just a textured rectangular prism.")
# --- How to run the code ---
if __name__ == "__main__":
# You can customize dimensions here if needed
generate_condom_box_3d_model(width=8.0, height=6.0, depth=3.0)
# Example with different dimensions:
# generate_condom_box_3d_model(filename_base="small_box", width=5.0, height=4.0, depth=2.0)
To Use:
generate_box.py).texture.png) that you want to wrap around the box. This image will be stretched to fit each face. If you want different images on different faces, you'd need to create a texture atlas (a single image with regions for each face) and define specific UV coordinates for each face's vertices, which is more complex. For this simple code, the entire texture.png is mapped onto every face.generate_box.py and your texture.png in the same directory.python generate_box.pycondom_box.obj and condom_box.mtl in the same directory.condom_box.obj in a 3D viewer. The viewer should automatically find condom_box.mtl and use texture.png to texture the box.This code provides the basic geometry and material linkage. The visual appearance will depend entirely on the texture.png image you provide.