# Problem with randomize transfom

first of all, i have absolutely no clue what i am doing here. A whole day of tutorials and try and error.

I want to make the cubes move randomly on the z-axis in a fixed range (loc=0,0,2) and write keyframes every 5 frames.

The Problem: How can i tell the operator to calculate always from the same location?

e.g.: The Cube is at location (0,0,0), the operator starts by (0,0,0) the keyframe is inserted with a location of (0,0,x). The operator starts again, but unfortunately by (0,0,x) and not by (0,0,0).

Best Regards, Rollo

import bpy
from random import randint

frame_number=0

for obj in bpy.data.collections['Cubes'].all_objects:
obj.select_set(True)

for i in range(0,50):
x = randint(0,1000)
bpy.context.scene.frame_set(frame_number)
bpy.ops.object.randomize_transform(random_seed=x, loc=(0, 0, 2))
frame_number +=5


Same thing using API methods.

Instead of using the operator calls can individually set each cube's z location to a random float value between 0 and 2, and insert a keyframe, in this case Z location at a designated frame. Notice not changing frame.

import bpy
from bpy import context
from random import uniform

#col = contxxt.collection
col = bpy.data.collections.get("Cubes")
if col:
for frame_number in range(0 ,50, 5):
for o in col.all_objects:
o.location.z = uniform(0, 2)
o.keyframe_insert("location", index=2, frame=frame_number) # z axis only


A transform, moves an object from where it is plus an amount, would be same as using

o.location.z += uniform(0, 2)


To have the value always zero on frame 0 could use

o.location.z = uniform(0, 2) if frame else 0


To insert all location keyframes, omit the index argument of Object.keyframe_insert

o.keyframe_insert("location", frame=frame_number)

• Do i understand it right that uniform(0,2) generates a random Number bewtween 0 and 2? First time for me seeing these API Methods. Is it possible that it also selects the objects in the collection? And this also works like a charme. Mar 21, 2021 at 18:54

try this:

import bpy
from random import randint

frame_number=0

for obj in bpy.data.collections['Cubes'].all_objects:
obj.select_set(True)
obj.location[2]  = 0

for i in range(0,50):
x = randint(0,1000)
bpy.context.scene.frame_set(frame_number)
bpy.ops.object.randomize_transform(random_seed=x, loc=(0, 0, 1))
for obj in bpy.data.collections['Cubes'].all_objects:
if obj.location[2] < 0:
obj.location[2] += 1

• I changed it to if obj.location[2]<-1: obj.location [2]=0 if obj.location[2]>1: obj.location[2]=0 and it works like a charme. Thank You Mar 21, 2021 at 18:46