# How can I use for loop (or while loop) in python to move 50 objects with random beginning positions to specific positions

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 random import randint

#create random tubulins#
n=0
while n < 36:
x = randint(-3, 3)
y = randint(-3, 3)
z = randint(-3, 3)
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.context.object.rotation_euler[0] = 1.5708

for l in range(0,12):
for k in range(0,4):
bpy.context.object.rotation_euler[0] = 1.5708

for k in range(15):
bpy.data.objects[k].select_set(True)
$$$$

• 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? Aug 10 '20 at 6:17
• @ 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. Aug 10 '20 at 15:57

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
points = []
for i in range(amount):
t = twist * 2 * pi * i/amount
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
`
• Thank you man. Your code inspires me. I have an idea now. Thanks again. Aug 10 '20 at 16:10