DEV Community

Discussion on: Create NFT 3D Collectibles Using Blender Scripting

 
hideckies profile image
hideckies

In that case, add multiple materials(textures) to the object (e.g. material names are "material_1", "material_2", so on) in advance, then you can write for example:

# gen_metadata.py

# List
list_background = [black, red, green, ...]
list_head = [material_1, material_2, ...]

random_background = random.choice(list_background)
# ...

attributes = [
    {
        "trait_type": "Background",
        "value": random_background
    },
    # ...
]
Enter fullscreen mode Exit fullscreen mode
# gen_model.py

# Assign background material
def assign_background_material(material_name):
    # Get the object named "background" in the "misc" collection from the scene
    background_object = bpy.data.objects["background"]

    # Set the material you want to assign
    target_material = bpy.data.materials.get(material_name)

    # Assign material
    background_object.data.materials[0] = target_material

# Append head
def append_head(trait_value):
    # Set the material name (e.g. "material_1", "material_2", etc)
    material_name = trait_value

    # Append "head" collection in the scene
    path = PARTS_DIR + "head/head.blend/Collection/"
    bpy.ops.wm.append(filename="head", directory=path)

    # Get the "head" collection
    head_col = bpy.data.collections["head"]

     # Assign material
    target_material = bpy.data.materials.get(material_name)
    for head in head_col.objects:
        head.data.materials[0] = target_material

# Generate
def generate(id, metadata):
    # Background
   if attr in metadata["attributes"]:
        if attr["trait_type"] == "Background" and attr["value"] != "":
            assign_background_material(attr["value"])
    # ....
Enter fullscreen mode Exit fullscreen mode