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
  • $\begingroup$ "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: $\endgroup$ – batFINGER Jan 18 '17 at 8:44
  • $\begingroup$ @batFINGER Agreed; shouldn't that throw an exception without a pass body? $\endgroup$ – Droppy Jan 18 '17 at 8:48
  • $\begingroup$ 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. $\endgroup$ – 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
|improve this answer|||||
  • $\begingroup$ Many thanks. Can you explain why to start from frame 2(Still learning,Baby Steps) $\endgroup$ – dvd Jan 18 '17 at 8:58
  • $\begingroup$ Used your c = 2 is the only reason for 2. For all frames in scene frame range use c = scene.frame_start instead. $\endgroup$ – batFINGER Jan 18 '17 at 9:00
  • $\begingroup$ c = scene.frame_start , How I will use it?(Sorry for the ignorance) $\endgroup$ – dvd Jan 18 '17 at 9:04
  • $\begingroup$ Edited answer accordingly. $\endgroup$ – batFINGER Jan 18 '17 at 9:07

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.