Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
molecule = {
"camera": {
"position": [0.0000, 0.0000, 32.4760],
"rotation": [0.0000, 0.0000, 0.0000]
},
"atoms": [
{ "element": "C", "location": [ -0.7150, -1.2384, 0.0000 ] },
{ "element": "C", "location": [ 0.7150, -1.2384, 0.0000 ] },
{ "element": "C", "location": [ -1.4300, 0.0000, 0.0000 ] },
{ "element": "C", "location": [ -0.7150, 1.2384, 0.0000 ] },
{ "element": "C", "location": [ 0.7150, 1.2384, 0.0000 ] },
{ "element": "C", "location": [ 1.4300, 0.0000, 0.0000 ] },
{ "element": "H", "location": [ -1.2150, -2.1044, 0.0000 ] },
{ "element": "H", "location": [ 1.2150, -2.1044, 0.0000 ] },
{ "element": "H", "location": [ -2.4300, 0.0000, 0.0000 ] },
{ "element": "H", "location": [ -1.2150, 2.1044, 0.0000 ] },
{ "element": "H", "location": [ 1.2150, 2.1044, 0.0000 ] },
{ "element": "H", "location": [ 2.4300, 0.0000, 0.0000 ] }
],
"bonds": [
{ "atoms": [ 0, 1 ], "order": 1 },
{ "atoms": [ 0, 2 ], "order": 1 },
{ "atoms": [ 0, 6 ], "order": 1 },
{ "atoms": [ 1, 5 ], "order": 1 },
{ "atoms": [ 1, 7 ], "order": 1 },
{ "atoms": [ 2, 3 ], "order": 1 },
{ "atoms": [ 2, 8 ], "order": 1 },
{ "atoms": [ 3, 4 ], "order": 1 },
{ "atoms": [ 3, 9 ], "order": 1 },
{ "atoms": [ 4, 5 ], "order": 1 },
{ "atoms": [ 4, 10 ], "order": 1 },
{ "atoms": [ 5, 11 ], "order": 1 }
]
}
import bpy, bmesh, bpy_extras, time
t_start = time.time()
mesh = bpy.data.meshes.new("Atoms")
bm = bmesh.new()
for z in range(1000):
for atom in molecule['atoms']:
nv = bm.verts.new(atom['location'])
nv.co.z += z*0.2
bm.to_mesh(mesh)
mesh.update()
bpy_extras.object_utils.object_data_add(bpy.context, mesh)
points = bpy.context.object
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.mesh.primitive_uv_sphere_add()
bpy.ops.object.shade_smooth()
sphere = bpy.context.object
sphere.scale = (0.2,)*3
sphere.parent = points
points.dupli_type = "VERTS"
bpy.ops.curve.primitive_bezier_curve_add()
curve = bpy.context.object
curve.data.dimensions = '3D'
curve.data.fill_mode = 'FULL'
curve.data.bevel_depth = 0.05
curve.data.bevel_resolution = 4
c_splines = curve.data.splines
c_splines.remove(c_splines[0])
for z in range(1000):
for bond in molecule['bonds']:
spline = c_splines.new('BEZIER')
spline.bezier_points[0].co = molecule['atoms'][bond['atoms'][0]]['location']
spline.bezier_points[0].co.z += z*0.2
spline.bezier_points[0].handle_left_type = 'VECTOR'
spline.bezier_points[0].handle_right_type = 'VECTOR'
spline.bezier_points.add(1)
spline.bezier_points[1].co = molecule['atoms'][bond['atoms'][1]]['location']
spline.bezier_points[1].co.z += z*0.2
spline.bezier_points[1].handle_left_type = 'VECTOR'
spline.bezier_points[1].handle_right_type = 'VECTOR'
t_end = time.time()
objects_time_taken = t_end-t_start
print('That took',objects_time_taken)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment