Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active October 4, 2015 16:28
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/2667990 to your computer and use it in GitHub Desktop.
Save zeffii/2667990 to your computer and use it in GitHub Desktop.
from_pydata exhaustive
import bpy
import re
from urllib.request import urlopen
#usage
# - set to cycles first
# - unselect any objects.
# - run.
remote_location = "http://dribbble.com/shots/568003-Code-View-P001"
def get_mesh_data():
verts = [ (0.0, 0.0, 0.2), (0.0, 0.0, 0.8), (0.0, 0.0, 1.0),
(0.2, 0.0, 1.0), (1.0, 0.0, 1.0), (1.8, 0.0, 1.0),
(2.0, 0.0, 1.0), (2.0, 0.0, 0.8), (2.0, 0.0, 0.2)]
edges = [(i, i+1) for i in range(0,8)]
mesh_data = bpy.data.meshes.new("cylinder_profile")
mesh_data.from_pydata(verts, edges, [])
mesh_data.update()
return mesh_data
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 BSDF_color_from_hex(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):
mesh_data = get_mesh_data()
cylinder_name = "Cylinder_" + str(pos)
cylinder_object = bpy.data.objects.new(cylinder_name, mesh_data)
scene.objects.link(cylinder_object)
cylinder_object.select = True
scene.objects.active = bpy.data.objects[cylinder_name]
def append_modifiers(new_obj):
screw = new_obj.modifiers.new(type='SCREW', name="screw")
screw.axis = 'X'
subsurf = new_obj.modifiers.new(type='SUBSURF', name="subsurf")
def append_material(col, new_obj):
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
Diffuse_BSDF = col.node_tree.nodes['Diffuse BSDF']
Diffuse_BSDF.inputs[0].default_value = BSDF_color_from_hex(color)
make_object(idx, col)
new_obj = bpy.context.active_object
new_obj.location = (float(idx*2), 0.0, 0.0)
append_modifiers(new_obj)
append_material(col, new_obj)
bpy.ops.object.shade_smooth()
scene = bpy.context.scene
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