Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/balprak2.py
Created March 3, 2014 14:58
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/9326737 to your computer and use it in GitHub Desktop.
Save zeffii/9326737 to your computer and use it in GitHub Desktop.
import bpy
def create_obj_from_name():
'''
input: location string in format similar to default value
output: adds object to scene if success.
'''
bpy.context.scene.render.engine = 'CYCLES'
cols = [
(0.11764, 0.14117, 0.14901, 1.0),
(0.27450, 0.27450, 0.27843, 1.0),
(0.96862, 0.96862, 0.96862, 1.0),
(0.61568, 0.61960, 0.62352, 1.0)
]
def get_mesh_data():
verts = [
(0.0, 0.0, 0.0), (1.0, 0.0, 0.0),
(1.0, 1.0, 0.0), (0.0, 1.0, 0.0)
]
edges = [(i, i+1) if i<3 else (3,0) for i in range(0,4)]
mesh_data = bpy.data.meshes.new("cylinder_profile")
mesh_data.from_pydata(verts, edges, [[0,1,2,3]])
mesh_data.update()
return mesh_data
def make_object(pos, col, 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_material(col, new_obj, idx):
new_obj.data.materials.append(col)
new_obj.active_material_index = idx
# new_obj.material_slots[col.name].link = 'OBJECT'
def create_objects_and_materials(col_name='swatch'):
for idx, color in enumerate(cols):
mesh_data = get_mesh_data()
col_rename = col_name + str(idx)
col = pymat.new(col_rename)
col.use_nodes = True
Diffuse_BSDF = col.node_tree.nodes['Diffuse BSDF']
Diffuse_BSDF.inputs[0].default_value = color
make_object(idx, col, mesh_data)
new_obj = bpy.context.active_object
new_obj.location = (float(idx*2), 0.0, 0.0)
append_material(col, new_obj, idx)
bpy.ops.object.shade_smooth()
scene = bpy.context.scene
pymat = bpy.data.materials
create_objects_and_materials()
create_obj_from_name()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment