2
$\begingroup$

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

        bpy.ops.anim.keyframe_insert_menu(type='Scaling')

        c += 1
$\endgroup$
3
  • $\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
    Commented Jan 18, 2017 at 8:44
  • $\begingroup$ @batFINGER Agreed; shouldn't that throw an exception without a pass body? $\endgroup$
    – Droppy
    Commented Jan 18, 2017 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
    Commented Jan 18, 2017 at 8:52

1 Answer 1

2
$\begingroup$

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
$\endgroup$
4
  • $\begingroup$ Many thanks. Can you explain why to start from frame 2(Still learning,Baby Steps) $\endgroup$
    – dvd
    Commented Jan 18, 2017 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
    Commented Jan 18, 2017 at 9:00
  • $\begingroup$ c = scene.frame_start , How I will use it?(Sorry for the ignorance) $\endgroup$
    – dvd
    Commented Jan 18, 2017 at 9:04
  • $\begingroup$ Edited answer accordingly. $\endgroup$
    – batFINGER
    Commented Jan 18, 2017 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .