-
-
Save zeffii/9306266 to your computer and use it in GitHub Desktop.
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 | |
import re | |
from urllib.request import urlopen | |
def create_obj_from_name(location="568003-Code-View-P001"): | |
''' | |
input: location string in format similar to default value | |
output: adds object to scene if success. | |
''' | |
bpy.context.scene.render.engine = 'CYCLES' | |
remote_location = "http://dribbble.com/shots/" + location | |
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 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(): | |
html_raw = urlopen(remote_location) | |
html_decoded = html_raw.readall().decode() | |
html = ''.join(html_decoded) | |
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() | |
create_objects_and_materials(color_group) | |
create_obj_from_name("568003-Code-View-P001") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment