# loop works only for 4 frames any idea why?

I'm trying to use scripting to create jumpy cone for fire purpose, and the loop works only for 4 frames, any idea why? Thanks in advance

import bpy
import random

for object in bpy.data.objects:

bpy.ops.object.select_all(action = 'DESELECT')

string1 = object.name

for o in string1:

if (string1.find("Cone",0)!=(-1)):

object.select = True

c = 2

a = random.randrange(10,30)

b = random.randrange(3,6)

a /= b

# make each object that string1 find, "ACTIVE"
bpy.context.scene.objects.active = bpy.data.objects[object.name]

object.scale.z = a

bpy.context.scene.frame_current = c

c += 1

• "Cone" has 4 letters. for o in "Cone" will give values of o of "C", "o", "n", "e" Added an answer with what I think is the result you are after. Or there was only 4 objects in your bpy.data, hard to tell as there is a syntax error around for o in string1: – batFINGER Jan 18 '17 at 8:44
• @batFINGER Agreed; shouldn't that throw an exception without a pass body? – Droppy Jan 18 '17 at 8:48
• My assumption is either the for loop can be removed as o isn't used, or the if block needs to be indented. Either way c will always be 2. – batFINGER Jan 18 '17 at 8:52

Can do this without operators. I've set script to run on all objects in scene with a name starting with "Cone".

A common blender technique is to select an object and simply use

cone = context.active_object


Here is test code that adds a random z scale for cone on each frame from scene.frame_start to scene.end_frame.

import bpy
from random import randrange
from bpy import context
scene = context.scene
cones = [o for o in scene.objects if o.name.startswith("Cone")]

for cone in cones:
c = scene.frame_start
while c <= scene.frame_end:
a = randrange(10,30)
b = randrange(3,6)

cone.scale.z = a / b
cone.keyframe_insert("scale", index=2, frame=c)
c += 1

• Many thanks. Can you explain why to start from frame 2(Still learning,Baby Steps) – dvd Jan 18 '17 at 8:58
• Used your c = 2 is the only reason for 2. For all frames in scene frame range use c = scene.frame_start instead. – batFINGER Jan 18 '17 at 9:00
• c = scene.frame_start , How I will use it?(Sorry for the ignorance) – dvd Jan 18 '17 at 9:04
• Edited answer accordingly. – batFINGER Jan 18 '17 at 9:07