Materials and Nodes
Dribbble generates .aco (swatch) schemes from any upload, that's great for archiving my favourite color schemes. To avoid headache and lowlevel python i've resorted to leeching the color values directly from the html, instead of decoding the .aco file. Currently the snippet creates cycles node materials from swatches using urllib and extracts it using regex. The idea is to keep things simple in the event that dribbble changes their html.This will facilitate using cool palettes to generate abstract visuals (but cycles only)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from urllib.request import urlopen | |
import re | |
#usage | |
# - set to cycles first | |
# - unselect any objects. | |
# - run. | |
remote_location = "http://dribbble.com/shots/550651-Dune-Runner" | |
def regex_this(html): | |
pattern = '<a href="\/colors\/(.*)"' | |
matches = re.findall(pattern, html) | |
return [i[:6] for i in matches] | |
def get_html(remote_location): | |
html_raw = urlopen(remote_location) | |
html = html_raw.readall().decode() | |
html = ''.join(html) | |
return html | |
def color_hex_to_float(group): | |
rgb = [group[i:i+2] for i in range(0, 6, 2)] | |
r, g, b = [(int(col, 16)/255) for col in rgb] | |
return r, g, b | |
def create_materials(color_group, col_name='swatch'): | |
for idx, color in enumerate(color_group): | |
r, g, b = color_hex_to_float(color) | |
col = pymat.new(col_name + str(idx)) | |
col.use_nodes = True | |
Diffuse_BSDF = col.node_tree.nodes['Diffuse BSDF'] | |
Diffuse_BSDF.inputs[0].default_value = [r, g, b, 1] | |
pymat = bpy.data.materials | |
html = get_html(remote_location) | |
color_group = regex_this(html) | |
create_materials(color_group) |
But Wait! There is more!
The following version also creates primitive cubes and assigns them each a swatch colour.becomes
And more!
Here's another version It's a small rewrite to get an idea of what kind of code juggling is permitted within python - it's a bit more modular but might take more intuition to read. This was an intermediate step to the next iteration.This revision demonstrates the evils of using ops, i would prefer to make the geometry mathematically or part math part lathe operation.