Created
May 11, 2012 17:50
-
-
Save zeffii/2661327 to your computer and use it in GitHub Desktop.
get swatch and assign materials
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'): | |
material_names = [] | |
for idx, color in enumerate(color_group): | |
r, g, b = color_hex_to_float(color) | |
material_name = col_name + str(idx) | |
col = pymat.new(material_name) | |
col.use_nodes = True | |
Diffuse_BSDF = col.node_tree.nodes['Diffuse BSDF'] | |
Diffuse_BSDF.inputs[0].default_value = [r, g, b, 1] | |
material_names.append(material_name) | |
return material_names | |
def make_objects_and_assign_materials(material_names): | |
for pos, material in enumerate(material_names): | |
bpy.ops.mesh.primitive_cube_add() | |
new_obj = bpy.context.active_object | |
new_obj.location = (float(pos*2), 0.0, 0.0) | |
new_obj.data.materials.append(pymat[material]) | |
pymat = bpy.data.materials | |
html = get_html(remote_location) | |
color_group = regex_this(html) | |
material_names = create_materials(color_group) | |
make_objects_and_assign_materials(material_names) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment