I had this problem too, if you have found a proper way to update template_preview please share, meanwhile I have found a workaround that works for me. As you noticed when you manually resize the image it gets updated. you can do that also programmatically inside the draw function for example: col.scale_y = 1.01 and also I had to add reload() and update() functions so I created a switch inside my draw function:
updater = True
def draw(self, context):
layout = self.layout
prop_group = context.scene.my_prop_group
split = layout.split(factor=0.5)
# First column for the image
col = split.column()
col.label(text="Image Preview:")
tex = bpy.data.textures.get("TempScreenImg")
try:
tex.image.reload()
tex.image.update()
except:
print("no data")
slot = getattr(context, "texture_slot", None) #tried this because that's how blender does template_preview but as far as I understand this is optional for what we try to do.
# Use the texture for preview
if tex:
col.template_preview(tex, slot=slot)
if self.updater:
col.scale_y = 1.0
self.updater = not self.updater
else:
col.scale_y = 1.01
self.updater = not self.updater
Now the template_preview will update whenever the panel is redrawn but this still is not good enough that is why if you need to trigger the update whenever you change some properties you can add update function to the property like this:
def update_func(self, context):
context.area.tag_redraw()
def register():
#or inside propertygroup
....
bpy.types.Scene.my_prop = bpy.props.IntProperty(update=update_func)
....
This approach will automatically resize your template_preview whenever you modify properties that should trigger an update. While this will introduce some extra refresh cycles, making it more noticeable in the template_preview, it's a functional workaround. I haven't found a better solution yet, other than potentially creating custom panels that bypass Blender's native UI.
Additionally, I'm encountering update-related issues with other template_ui elements as well. If anyone has insights into how to properly handle these templates, I'd be interested in hearing your suggestions.
image
type has anupdate
function itself (docs), perhaps it helps you out? So something liketexture.image.update()
. $\endgroup$