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

Add File Batch and Re-Enable Iterations #332

Merged
merged 6 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def register():
check_for_updates()

bpy.types.Scene.dream_textures_prompt = PointerProperty(type=DreamPrompt)
bpy.types.Scene.dream_textures_prompt_file = PointerProperty(type=bpy.types.Text)
bpy.types.Scene.init_img = PointerProperty(name="Init Image", type=bpy.types.Image)
bpy.types.Scene.init_mask = PointerProperty(name="Init Mask", type=bpy.types.Image)
def get_selection_preview(self):
Expand Down
18 changes: 11 additions & 7 deletions generator_process/intents/prompt_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,17 @@ def update(self, n=1):
self.send_info("Starting")

tmp_stderr = sys.stderr = StringIO() # prompt2image writes exceptions straight to stderr, intercepting
generator.prompt2image(
# a function or method that will be called each step
step_callback=view_step,
# a function or method that will be called each time an image is generated
image_callback=image_writer,
**args
)
prompt_list = args['prompt'] if isinstance(args['prompt'], list) else [args['prompt']]
for prompt in prompt_list:
generator_args = args.copy()
generator_args['prompt'] = prompt
generator.prompt2image(
# a function or method that will be called each step
step_callback=view_step,
# a function or method that will be called each time an image is generated
image_callback=image_writer,
**generator_args
)
if tmp_stderr.tell() > 0:
tmp_stderr.seek(0)
s = tmp_stderr.read()
Expand Down
31 changes: 25 additions & 6 deletions operators/dream_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,24 @@ def invoke(self, context, event):
return {'CANCELLED'}

def execute(self, context):
history_entry = context.preferences.addons[StableDiffusionPreferences.bl_idname].preferences.history.add()
for prop in context.scene.dream_textures_prompt.__annotations__.keys():
if hasattr(history_entry, prop):
setattr(history_entry, prop, getattr(context.scene.dream_textures_prompt, prop))
history_entries = []
is_file_batch = context.scene.dream_textures_prompt.prompt_structure == file_batch_structure.id
file_batch_lines = []
if is_file_batch:
file_batch_lines = [line for line in context.scene.dream_textures_prompt_file.lines if len(line.body.strip()) > 0]
history_entries = [context.preferences.addons[StableDiffusionPreferences.bl_idname].preferences.history.add() for _ in file_batch_lines]
else:
history_entries = [context.preferences.addons[StableDiffusionPreferences.bl_idname].preferences.history.add() for _ in range(context.scene.dream_textures_prompt.iterations)]
for i, history_entry in enumerate(history_entries):
for prop in context.scene.dream_textures_prompt.__annotations__.keys():
if hasattr(history_entry, prop):
setattr(history_entry, prop, getattr(context.scene.dream_textures_prompt, prop))
if is_file_batch:
history_entry.prompt_structure = custom_structure.id
history_entry.prompt_structure_token_subject = file_batch_lines[i].body

if is_file_batch:
context.scene.dream_textures_prompt.iterations = 1

def bpy_image(name, width, height, pixels):
image = bpy.data.images.new(name, width=width, height=height)
Expand All @@ -53,7 +67,9 @@ def bpy_image(name, width, height, pixels):
screen = context.screen
scene = context.scene

iteration = 0
def image_writer(shared_memory_name, seed, width, height, upscaled=False):
nonlocal iteration
global last_data_block
# Only use the non-upscaled texture, as upscaling is currently unsupported by the addon.
if not upscaled:
Expand All @@ -75,8 +91,9 @@ def image_writer(shared_memory_name, seed, width, height, upscaled=False):
if area.type == 'IMAGE_EDITOR':
area.spaces.active.image = image
scene.dream_textures_prompt.seed = str(seed) # update property in case seed was sourced randomly or from hash
history_entry.seed = str(seed)
history_entry.random_seed = False
history_entries[iteration].seed = str(seed)
history_entries[iteration].random_seed = False
iteration += 1

def view_step(step, width=None, height=None, shared_memory_name=None):
scene.dream_textures_progress = step + 1
Expand Down Expand Up @@ -209,6 +226,8 @@ def save_temp_image(img, path=None):

args = headless_prompt.generate_args()
args.update(headless_args)
if headless_prompt.prompt_structure == file_batch_structure.id:
args['prompt'] = [line.body for line in scene.dream_textures_prompt_file.lines if len(line.body.strip()) > 0]
args['init_img'] = init_img_path

def step_callback(step, width=None, height=None, shared_memory_name=None):
Expand Down
10 changes: 10 additions & 0 deletions prompt_engineering.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,21 @@ def custom_prompt(tokens):
custom_prompt
)

def file_batch_prompt(tokens):
return f""
file_batch_structure = PromptStructure(
'file_batch',
"File Batch",
[],
file_batch_prompt
)

prompt_structures = [
texture_structure,
photography_structure,
concept_art_structure,
custom_structure,
file_batch_structure
]

def map_structure(x):
Expand Down
2 changes: 1 addition & 1 deletion property_groups/dream_prompt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bpy
from bpy.props import FloatProperty, IntProperty, EnumProperty, BoolProperty, StringProperty, PointerProperty
from bpy.props import FloatProperty, IntProperty, EnumProperty, BoolProperty, StringProperty
from ..prompt_engineering import *

sampler_options = [
Expand Down
14 changes: 12 additions & 2 deletions ui/panels/dream_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from ...operators.view_history import ImportPromptFile
from ..space_types import SPACE_TYPES
from ...property_groups.dream_prompt import DreamPrompt
import types

def dream_texture_panels():
for space_type in SPACE_TYPES:
Expand Down Expand Up @@ -93,7 +92,10 @@ def draw(self, context):
enum_cases = DreamPrompt.__annotations__[enum_prop].keywords['items']
if len(enum_cases) != 1 or enum_cases[0][0] != 'custom':
segment_row.prop(get_prompt(context), enum_prop, icon_only=is_custom)
layout.prop(get_prompt(context), "seamless")
if get_prompt(context).prompt_structure == file_batch_structure.id:
layout.template_ID(context.scene, "dream_textures_prompt_file", open="text.open")
else:
layout.prop(get_prompt(context), "seamless")
yield PromptPanel

class NegativePromptPanel(sub_panel):
Expand All @@ -102,6 +104,10 @@ class NegativePromptPanel(sub_panel):
bl_label = "Negative"
bl_parent_id = PromptPanel.bl_idname

@classmethod
def poll(self, context):
return get_prompt(context).prompt_structure != file_batch_structure.id

def draw_header(self, context):
layout = self.layout
layout.prop(get_prompt(context), "use_negative_prompt", text="")
Expand Down Expand Up @@ -225,6 +231,10 @@ def draw(self, context):
super().draw(context)
layout = self.layout
layout.use_property_split = True

iterations_row = layout.row()
iterations_row.enabled = get_prompt(context).prompt_structure != file_batch_structure.id
iterations_row.prop(get_prompt(context), "iterations")

row = layout.row()
row.scale_y = 1.5
Expand Down