Page MenuHome

Fix T78646: Incorrect defaults for image_user when creating texture nodes from Python
AbandonedPublic

Authored by Vincent Blankfield (vvv) on Jul 5 2020, 9:35 PM.

Details

Summary

Currently when creating ShaderNodeTexImage nodes like this:

import bpy
import os

filepath = os.path.expanduser("~/Desktop/test.png")
img = bpy.data.images.load(filepath)
mat = bpy.data.materials.new("Frame Duration Test")
mat.use_nodes = True

node = mat.node_tree.nodes.new(type = "ShaderNodeTexImage")
node.image = img
node.name = img.name

print(f"frame_offset: {node.image_user.frame_offset}. Should be 0")
print(f"frame_start: {node.image_user.frame_start}. Should be 1")
print(f"frame_duration: {node.image_user.frame_duration}. Should be 1")
print(f"frame_current: {node.image_user.frame_current}. Should be 1")

the image_user for the node will be configured with the following settings:

frame_offset: 0. Should be 0
frame_start: 1. Should be 1
frame_duration: 100. Should be 1
frame_current: 0. Should be 1

which leads to the image being used like an animated texture.

And in case of rendering an animation, this image_user configuration makes ImageLoader comparisons fail in ImageManager::add_image_slot (frame will differ). This way the image manager will allocate new image slots for the misconfigured textures each frame. In case of Persistent Images option enabled, old slots will never be freed, so the memory usage will grow with insane rate each rendered frame (as described in T78537).

This patch proposes to set the image_user defaults to:

frame_offset: 0
frame_start: 1
frame_duration: 1
frame_current: 1

suited for the single image textures, as the most used texture type. With this configuration the insane memory use issues will be avoided when rendering animation that use textures imported by script like in the example above. The wrong image_user configuration may be easily fixed in the script that create the nodes. But considering there are existing popular scripts that do it this way (never settings the appropriate image_user settings), and the fact that the high memory use issues cause isn't obvious, I think it's better to change the defaults.

The reason for the hardcoded default frame count of 100 is unknown for me. And I leave this question for the reviewers. It seems to be introduced in rB0a4cded91eed.

Diff Detail

Repository
rB Blender

Event Timeline

Vincent Blankfield (vvv) requested review of this revision.Jul 5 2020, 9:35 PM
Vincent Blankfield (vvv) created this revision.
Vincent Blankfield (vvv) edited the summary of this revision. (Show Details)Jul 5 2020, 9:41 PM

Remove redundant set to 0.

I don't consider these defaults to be wrong by themselves, see T78646: image_user.frame_duration = 100 when creating single image node in Cycles. For cases where we can't automatically detect duration, it is also convenient to have some initial duration to show that the sequence works.

The fix should be in the calculation of the current frame or the way Cycles uses it.