3
$\begingroup$

I am a beginner of Blender. I want to use the blender to show how proteins form a microtubule. My code creates a piece of microtubule and some proteins on random positions. I want to put the random proteins on the microtubule.

My plan was using python to read the positions of my proteins and changing their positions to wherever I want, but the API is kind of confusing for me to read. I don't know how to express the 3D position of a random target in bpy.

Can someone please show me some material or the code to do this? Thank you very much.

import math
import bpy
from math import cos
from math import sin
from math import radians
from random import randint



#create random tubulins#
n=0
while n < 36:
    x = randint(-3, 3)
    y = randint(-3, 3)
    z = randint(-3, 3)
    bpy.ops.mesh.primitive_cylinder_add(radius=0.12, depth=0.176, enter_editmode=False, location=(x,y,z))
    bpy.context.object.rotation_euler[0] = 1.5708
    n = n + 1

#create microtubule#
for j in range(0, 5):
    for i in range(0,12):
        bpy.ops.mesh.primitive_cylinder_add(radius=0.12, depth=0.176, enter_editmode=False, location=( 0.5*math.cos(math.radians(i*30)), 0.2*2*j, 0.5*math.sin(math.radians(i*30))))
        bpy.context.object.rotation_euler[0] = 1.5708
        
        





for l in range(0,12):
    for k in range(0,4):
        bpy.ops.mesh.primitive_cylinder_add(radius=0.12, depth=0.176, enter_editmode=False, location=( 0.5*math.cos(math.radians(l*30)), 0.2*(2*k+1), 0.5*math.sin(math.radians(l*30))))
        bpy.context.object.rotation_euler[0] = 1.5708






##add tubulins onto the microtubule, aka: adding key frames ##
 
for k in range(15):
    bpy.data.objects[k].select_set(True)
```    
$\endgroup$
2
  • $\begingroup$ So, you want to 1. Create randomly position proteins; 2. Let those proteins go stick on micro-tubule(what is this...). And those proteins should go straight forward to tube without penetrating any mesh? $\endgroup$
    – HikariTW
    Aug 10 '20 at 6:17
  • $\begingroup$ @ HikariTW Yes, that is right. My code already creates randomly position proteins (I used cylinder to stand for the protein in my code), but I don't know how to use bpy to add those proteins onto my tube (extent the tube). Microtubules are polymers of tubulin that form part of the cytoskeleton and provide structure and shape to eukaryotic cells. $\endgroup$
    – kinesin
    Aug 10 '20 at 15:57
3
$\begingroup$

This script creates random points and mixes with points of helix. Here i used lerp() from mathutils to mix two points based on frame. You can replace helix points with vertices of cylinder.

import bpy
from random import randint
from mathutils import Vector
from math import sin, cos, pi

# create helix points
def helix(amount, radius, height, twist):
    points = []
    for i in range(amount):
        t = twist * 2 * pi * i/amount
        x = radius * sin(t)
        y = radius * cos(t)
        z = height * radius * t / amount
        points.append(Vector((x,y,z)))
    return points

# generate random points
def random_points(amount, min, max):
    randpoints = []
    for i in range(amount):
        x = randint(min, max)
        y = randint(min, max)
        z = randint(min, max)
        randpoints.append(Vector((x, y, z)))
    return randpoints
 
random_points = random_points(50, -3, 3)
final_points = helix(50, 0.2, 8, 8)

# get objects from collection
coll = bpy.data.collections.get("spheres")
objects = coll.objects

# clear previous animation data
for obj in objects:
    obj.animation_data_clear()
        
start = 1
end = 100
for f in range(start, end+1):    
    bpy.context.scene.frame_set(f)
    for i, obj in enumerate(objects):
        obj.scale = (0.1,0.1,0.1)
        obj.location = random_points[i].lerp(final_points[i], f/end) # vector1.lerp(vector2, factor)
        obj.keyframe_insert("location", frame = f) # insert location keyframe
$\endgroup$
1
  • $\begingroup$ Thank you man. Your code inspires me. I have an idea now. Thanks again. $\endgroup$
    – kinesin
    Aug 10 '20 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.