2
$\begingroup$

I have a series of .obj objects which I want to play in series to create an animation. The obj files are named according to the time steps. My idea was to import one file, show it, wait for half a second, delete it and show the next file and so on to make it look like an animation. I created a python file to do this by putting these operations it in a loop, but it is not working. As soon as I use bpy.ops.object.delete() it deletes everything and I can't see the meshes. I used time.sleep(0.5) to give a bit a of time before deleting the mesh, but still I can't see any of the imported meshes.

I tried the method shown in this question: how to correctly update a mesh while animating series of VTP files?

But it is not working. How can I do this? Please help.

$\endgroup$
4
  • 1
    $\begingroup$ You call an operator (byp.ops....). These are functions for user interaction and they work with a context object that contains additional info (which objects are selected? What mode is active (Object, Edit, Weight Paint, ...)? and so on). In your context (bpy.context probably, context can be overwritten) you have selected all objects. Now, if you call the operator delete() they all get deleted. Solution: make sure that only the object is selected that you want to delete (as you would do as a user). $\endgroup$
    – Blunder
    Jan 15 at 20:51
  • 1
    $\begingroup$ Check bpy.context.selected_objects in your script to see how many and which objects are selected before you delete them. Also, have a look here about operators and how to override the context: docs.blender.org/api/current/bpy.ops.html#overriding-context $\endgroup$
    – Blunder
    Jan 15 at 20:54
  • 2
    $\begingroup$ This could also be done in geonodes by using a collection and picking a single instance from the collection based on current frame... $\endgroup$ Jan 16 at 12:27
  • $\begingroup$ @MarkusvonBroady do you have a sample code ? Please do share if you have something similar. $\endgroup$ Jan 22 at 21:53

1 Answer 1

2
$\begingroup$

You can create the illusion of animation by showing/hiding objects at the right times. For example, the first object might be shown when the current frame is in [0,3), the next one when in [3,6), and so on.

Probably the easiest way to do this is with drivers.

import bpy

# Path to OBJ files, replace with your own files
obj_paths = [
    'my-file.%03d.obj' % i
    for i in range(1, 5)
]

# The frame when the OBJs should begin playing
frame_start = 1

# How many frames to show each OBJ for
frame_duration = 3

for i, path in enumerate(obj_paths):
    bpy.ops.wm.obj_import(filepath=path)

    # Frame when OBJ first appears
    f_begin = frame_start + i * frame_duration
    # Frame when OBJ disappears
    f_end = f_begin + frame_duration

    for ob in bpy.context.selected_objects:
        # Use a driver to hide it (both in viewport and render)
        # when frame is not in [f_start, f_end)
        for prop in ['hide_viewport', 'hide_render']:
            ob.driver_add(prop).driver.expression = (
                f'frame < {f_begin} or frame >= {f_end}'
            )
$\endgroup$
1
  • $\begingroup$ This worked, thank you so much :) $\endgroup$ Jan 20 at 10:57

You must log in to answer this question.

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