Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI enhancement: When recalling a promt, open the corresponding image … #403

Merged
merged 5 commits into from
Nov 24, 2022

Conversation

GottfriedHofmann
Copy link
Contributor

…in the image editor when there is an image with the same name as the seed value in the promt.

When there is an image datablock with the same name as the seed from the promt to be recalled, open it in the image editor. When there is more than one datablock with the seed in it's name (like 12345.001, 12345.002 etc.) do not change the image open in the image editor and give a notification to the user.

image_sync_with_promt_history.mp4

…in the image editor when there is an image with the same name as the seed value in the promt.
Copy link
Owner

@carson-katri carson-katri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple suggestions.

Comment on lines 46 to 48
image_names_no_suffix = [i.name.split('.')[0] for i in bpy.data.images]
image_names_no_suffix_seed_count = image_names_no_suffix.count(seed_string)
if image_names_no_suffix_seed_count == 1:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does an exact match catch it correctly:

Suggested change
image_names_no_suffix = [i.name.split('.')[0] for i in bpy.data.images]
image_names_no_suffix_seed_count = image_names_no_suffix.count(seed_string)
if image_names_no_suffix_seed_count == 1:
if (existing_image := bpy.data.images.get(seed_string, None)) is not None:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the proposed change is not equivalent. Consider the following situation. We have made three runs with constant seed of 1096871657 because we wanted to see how the result looked with different samplers. Thus we have three images: 1096871657, 1096871657.001 and 1096871657.002.
Now I want to recall the second promt. With your change, it would select the first of the three images, which is the wrong one. Unfortunately we do not know which of the three is the right image with just the seed. That's why I have decided to count the occurences of a seed in the prefixes of the names of all image datablocks (line 46 and 47) and only select an image if it is the only one with that seed as prefix (line 48).
When there is more than one occurence, it does not change the current image but reports that more than one has been found (lines 54 and 55).

I am aware that this is a heuristic and not perfect but I think I have an idea for a better solution. The problem is that the unique identifier for datablocks in Blender is the name, which is prone to be changed by the user and sideffects of Blender (.001 .002 .003 etc.). What we can do is the following: After an image is created, run hash() over the datablock and store the result as a custom property of said datablock and store it as well in dream_textures_prompt . That way we can definitely select the correct image no matter how it is named.

for area in context.screen.areas:
if area.type != 'IMAGE_EDITOR':
continue
area.spaces.active.image = bpy.data.images[seed_string]
Copy link
Owner

@carson-katri carson-katri Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes with the suggestion above if that works for you.

Suggested change
area.spaces.active.image = bpy.data.images[seed_string]
area.spaces.active.image = existing_image
self.report({'INFO'}, "Existing generated image opened in image editor")

Comment on lines 54 to 57
elif image_names_no_suffix_seed_count > 1:
self.report({'INFO'}, "More than one image found for seed " + seed_string + ". Not changing image editor contents.")
elif image_names_no_suffix_seed_count == 0:
self.report({'INFO'}, "No image found for seed " + seed_string + ". Not changing image editor contents.")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would rather show no message if an existing image isn't found, because users may think this is an error. I think it may be common to try reloading a prompt from a file that isn't open, so it may error more often than it doesn't.

Suggested change
elif image_names_no_suffix_seed_count > 1:
self.report({'INFO'}, "More than one image found for seed " + seed_string + ". Not changing image editor contents.")
elif image_names_no_suffix_seed_count == 0:
self.report({'INFO'}, "No image found for seed " + seed_string + ". Not changing image editor contents.")

image_names_no_suffix = [i.name.split('.')[0] for i in bpy.data.images]
image_names_no_suffix_seed_count = image_names_no_suffix.count(seed_string)
if image_names_no_suffix_seed_count == 1:
print("Image for seed " + seed_string + " is unique. Selecting image datablock.")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly remove the print statement to keep the logs clean.

Suggested change
print("Image for seed " + seed_string + " is unique. Selecting image datablock.")

…on can later be used to open the right image datablock in the Blender image editor when recalling the promt
… in the promt history as well. When recalling a prompt, load the image in the image editor if available
@GottfriedHofmann
Copy link
Contributor Author

I changed the way it works completely. Now a hash is created for each image and stored in a custom property of said image and in the promt history. When recalling a prompt from history and an image with said hash can be found in byp.data.images, show it in the image editor.

Copy link
Owner

@carson-katri carson-katri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@carson-katri carson-katri merged commit eacc3c9 into carson-katri:main Nov 24, 2022
JasonHoku pushed a commit to JasonHoku/dream-textures that referenced this pull request Dec 20, 2022
carson-katri#403)

* UI enhancement: When recalling a promt, open the corresponding image in the image editor when there is an image with the same name as the seed value in the promt.

* Add a hash of the resulting image as string to prompt. This information can later be used to open the right image datablock in the Blender image editor when recalling the promt

* Improvement: Create a hash for each image created and store that hash in the promt history as well. When recalling a prompt, load the image in the image editor if available

* Use hashlib instead of hash()

Co-authored-by: Carson Katri <Carson.katri@gmail.com>

* Fix: Need to import hashlib before we can use it

Co-authored-by: Carson Katri <Carson.katri@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants