Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Last active December 1, 2022 07:33
Show Gist options
  • Save CGArtPython/c33f52773b9cb59d62ad102d208c16e7 to your computer and use it in GitHub Desktop.
Save CGArtPython/c33f52773b9cb59d62ad102d208c16e7 to your computer and use it in GitHub Desktop.
Beginner Blender Python Tutorial: Python Dictionaries Example 1 (used in tutorial: https://youtu.be/FkJ2XanNBR4)
# give Python access to Blender's functionality
import bpy
# extend Python functionality to generate random numbers
import random
def create_plane_with_color(color, location):
# add a plane
bpy.ops.mesh.primitive_plane_add()
plane_object = bpy.context.active_object
plane_object.location = location
# create a new material
material = bpy.data.materials.new(name=f"diffuse_material")
material.diffuse_color = color
# add material to object
plane_object.data.materials.append(material)
####################
####################
# create a dict of colors
my_colors = {
"Brick Red": (0.4019, 0.068478, 0.0578054, 1.0),
"Neon Red": (1.0, 0.0307132, 0.0307135, 1.0),
"Pastel Red": (0.955973, 0.351532, 0.351533, 1.0)
}
####################
# example 1A
color = my_colors["Brick Red"]
create_plane_with_color(color, location=(0, 0, 0))
####################
# example 1B
create_plane_with_color(my_colors["Neon Red"], location=(0, 3, 0))
####################
# example 1C
# create a list of keys
list_of_keys = list(my_colors.keys())
# select a random key
random_key = random.choice(list_of_keys)
color = my_colors[random_key]
create_plane_with_color(color, location=(0, 6, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment