Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 10, 2012

Cycles materials from dribbble generated swatch

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)
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)
view raw dbbb_to_b263.py hosted with ❤ by GitHub

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.

Programmed Lathing!

If we look at the previous snippet, it leaves a nasty taste because it amounts to no more than a macro coding. Let's see what a more low level coded interpretation looks like: using from_pydata