3
$\begingroup$

How do I get the keyframe_insert to work with frame_offset? When using it from the image below, you can see that I can add the keyframe by hand, but with "Copy Data Path" I can't make it work with the code though. Keyframing Offset

def preview_enum_update(wm, context):
print("wm.my_previews = %s" % wm.my_previews)

#load image - first of all
dividedImageBeforeDot, dividedImageAfterSecondDot = os.path.splitext(wm.my_previews)
dividedImageBeforeDot, dividedImageAfterDot = os.path.splitext(dividedImageBeforeDot)

materialName = "Testing"

myMaterial = bpy.data.materials.get(materialName) or bpy.data.materials.new(materialName)
myMaterial.use_nodes = True

node_tree = myMaterial.node_tree

node = node_tree.nodes.get("Image Texture") or node_tree.nodes.new("ShaderNodeTexImage")

node.image_user.frame_duration = 1
node.image_user.frame_start = 1
node.image_user.frame_offset = int(dividedImageAfterDot.split('.')[1])-1
myMaterial.keyframe_insert('nodes["Image Texture"].image_user.frame_offset')
$\endgroup$

1 Answer 1

3
$\begingroup$

In general, set the property's value and THEN insert the keyframe with that value (mentioning the frame number). The value needs to match the property type. In your case:

Step 1 -

bpy.data.materials["Testing"].node_tree.nodes["ShaderNodeTexImage"].image_user.frame_offset = 1

Step 2 -

bpy.data.materials["Testing"].node_tree.nodes["ShaderNodeTexImage"].image_user.keyframe_insert("frame_offset", frame=1)

Which sets the frame offset to 1 for ShaderNodeTexImage at frame 1, for Testing material.

For objects, it's a bit different, but the concept is the same -

bpy.data.objects["obj_name"].hide_render = True
bpy.data.objects["obj_name"].keyframe_insert("hide_render", frame = 100)

which hides the object in render, at frame 100

The API documentation is not explicit about all this, but you have to connect the dots by understanding how Blender API is structured and how Python works. Refer:

https://docs.blender.org/api/current/bpy.types.ImageUser.html?highlight=frame_offset#bpy.types.ImageUser.frame_offset https://docs.blender.org/api/current/bpy.types.bpy_struct.html?highlight=keyframe_insert#bpy.types.bpy_struct.keyframe_insert

This module is used for all Blender/Python access: https://docs.blender.org/api/current/bpy.data.html

Play around and practice, self learning is best learning :)

$\endgroup$

You must log in to answer this question.

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