Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created May 12, 2012 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/2666465 to your computer and use it in GitHub Desktop.
Save zeffii/2666465 to your computer and use it in GitHub Desktop.
another alternative _ ugly using ops
import bpy
import re
from urllib.request import urlopen
import math
#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()
return ''.join(html)
def color_hex_to_BSDF_input(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, 1.0
def get_colors_from_html(remote_location):
html = get_html(remote_location)
return regex_this(html)
def make_object(pos, col):
## GEOMETRY, the ugly way - but perhaps it serves a purpose to illustrate the process.
# create circle with up axis being x, size 0.2
bpy.ops.mesh.primitive_circle_add( vertices=32, radius=.2, rotation=(0.0, 0.5*math.pi, 0.0))
bpy.ops.object.editmode_toggle()
# extrude scale circle to .8 in y/z direction
bpy.ops.mesh.extrude_region_move()
bpy.ops.transform.resize( value=(0.0, 4.0, 4.0))
# extrude scale circle to 1.0 in y/z direction
bpy.ops.mesh.extrude_region_move()
bpy.ops.transform.resize( value=(0.0, 1.25, 1.25))
# extrude and translate to provide edge loops for the subdiv smoothing
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(.2,0,0)})
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(.8,0,0)})
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(.8,0,0)})
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(.2,0,0)})
# extrude scale circle to 0.8 in y/z direction
bpy.ops.mesh.extrude_region_move()
bpy.ops.transform.resize( value=(0.0, 0.8, 0.8))
# extrude scale circle to 0.2 in y/z direction
bpy.ops.mesh.extrude_region_move()
bpy.ops.transform.resize( value=(0.0, .25, .25))
# unify normals, set smooth shading
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.object.editmode_toggle()
bpy.ops.object.shade_smooth()
## MODIFIERS
bpy.ops.object.modifier_add(type='SUBSURF')
## MATERIAL
new_obj = bpy.context.active_object
new_obj.location = (float(pos*2), 0.0, 0.0)
new_obj.data.materials.append(col)
def create_objects_and_materials(color_group, col_name='swatch'):
for idx, color in enumerate(color_group):
col = pymat.new(col_name + str(idx))
col.use_nodes = True
rgb_values = color_hex_to_BSDF_input(color)
Diffuse_BSDF = col.node_tree.nodes['Diffuse BSDF']
Diffuse_BSDF.inputs[0].default_value = rgb_values
make_object(idx, col)
pymat = bpy.data.materials
color_group = get_colors_from_html(remote_location)
create_objects_and_materials(color_group)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment