Skip to content

Transform your 3D texturing workflow with the power of generative AI, directly within Blender!

add reproject operator, fix union controlnet bug when there is also a non-union controlnet present

sakalond24c1cd348366
diff --git a/stablegen/__init__.py b/stablegen/__init__.py
index adcf5a1..a23591c 100755
--- a/stablegen/__init__.py
+++ b/stablegen/__init__.py
@@ -3,7 +3,7 @@ import bpy # pylint: disable=import-error
from .stablegen import StableGenPanel, ApplyPreset, SavePreset, DeletePreset, get_preset_items, update_parameters
from .render_tools import BakeTextures, AddCameras, SwitchMaterial, ExportOrbitGIF, CollectCameraPrompts, CameraPromptItem
from .utils import AddHDRI, ApplyModifiers, CurvesToMesh
-from .generator import ComfyUIGenerate
+from .generator import ComfyUIGenerate, Reproject
import os
from bpy.app.handlers import persistent
@@ -15,6 +15,24 @@ bl_info = {
'blender': (4, 2, 0)
}
+classes = [
+ StableGenPanel,
+ ApplyPreset,
+ SavePreset,
+ DeletePreset,
+ BakeTextures,
+ AddCameras,
+ SwitchMaterial,
+ ExportOrbitGIF,
+ CollectCameraPrompts,
+ CameraPromptItem,
+ AddHDRI,
+ ApplyModifiers,
+ CurvesToMesh,
+ ComfyUIGenerate,
+ Reproject,
+]
+
def update_combined(self, context): # Combined with load_handler to load controlnet unit on first setup
update_parameters(self, context)
load_handler(None)
@@ -77,6 +95,7 @@ class StableGenAddonPreferences(bpy.types.AddonPreferences):
layout.prop(self, "controlnet_mapping")
layout.prop(self, "save_blend_file")
+
def get_models_from_directory(base_path: str, model_type_subdir: str, valid_extensions: tuple, include_subdirs: bool = True):
"""
Helper function to scan a directory (and optionally subdirectories) for model files.
@@ -519,24 +538,18 @@ def load_handler(dummy):
if new_lora and hasattr(new_lora, 'name') and scene.lora_units and new_lora.name in scene.lora_units:
scene.lora_units.remove(scene.lora_units.find(new_lora.name))
+classes_to_append = [StableGenAddonPreferences, ControlNetUnit, LoRAUnit, AddControlNetUnit, RemoveControlNetUnit, AddLoRAUnit, RemoveLoRAUnit]
+for cls in classes_to_append:
+ classes.append(cls)
+
def register():
"""
Registers the addon.
:return: None
"""
- bpy.utils.register_class(StableGenAddonPreferences)
- bpy.utils.register_class(StableGenPanel)
- bpy.utils.register_class(ComfyUIGenerate)
- bpy.utils.register_class(BakeTextures)
- bpy.utils.register_class(AddCameras)
- bpy.utils.register_class(SwitchMaterial)
- bpy.utils.register_class(AddHDRI)
- bpy.utils.register_class(ApplyModifiers)
- bpy.utils.register_class(CurvesToMesh)
- bpy.utils.register_class(ControlNetUnit)
- bpy.utils.register_class(LoRAUnit)
- bpy.utils.register_class(CameraPromptItem)
- bpy.utils.register_class(CollectCameraPrompts)
+ for cls in classes:
+ bpy.utils.register_class(cls)
+
bpy.types.Scene.comfyui_prompt = bpy.props.StringProperty(
name="ComfyUI Prompt",
description="Enter the text prompt for ComfyUI generation",
@@ -1115,21 +1128,18 @@ def register():
update=update_parameters
)
+ bpy.types.Scene.project_only = bpy.props.BoolProperty(
+ name ="Project Only",
+ description="""Internal flag to only project existing textures onto the model""",
+ default=False,
+ update=update_parameters
+ )
# IPADAPTER parameters
-
bpy.types.Scene.controlnet_units = bpy.props.CollectionProperty(type=ControlNetUnit)
- bpy.utils.register_class(AddControlNetUnit)
- bpy.utils.register_class(RemoveControlNetUnit)
bpy.types.Scene.lora_units = bpy.props.CollectionProperty(type=LoRAUnit)
- bpy.utils.register_class(AddLoRAUnit)
- bpy.utils.register_class(RemoveLoRAUnit)
bpy.types.Scene.controlnet_units_index = bpy.props.IntProperty(default=0)
bpy.types.Scene.lora_units_index = bpy.props.IntProperty(default=0)
- bpy.utils.register_class(ApplyPreset)
- bpy.utils.register_class(SavePreset)
- bpy.utils.register_class(DeletePreset)
- bpy.utils.register_class(ExportOrbitGIF)
bpy.app.handlers.load_post.append(load_handler)
def unregister():
@@ -1211,27 +1221,11 @@ def unregister():
del bpy.types.Scene.show_image_guidance_settings
del bpy.types.Scene.show_masking_inpainting_settings
del bpy.types.Scene.show_mode_specific_settings
- bpy.utils.unregister_class(ApplyPreset)
- bpy.utils.unregister_class(DeletePreset)
- bpy.utils.unregister_class(SavePreset)
- bpy.utils.unregister_class(CurvesToMesh)
- bpy.utils.unregister_class(ApplyModifiers)
- bpy.utils.unregister_class(AddHDRI)
- bpy.utils.unregister_class(SwitchMaterial)
- bpy.utils.unregister_class(AddCameras)
- bpy.utils.unregister_class(BakeTextures)
- bpy.utils.unregister_class(ComfyUIGenerate)
- bpy.utils.unregister_class(StableGenPanel)
- bpy.utils.unregister_class(StableGenAddonPreferences)
- bpy.utils.unregister_class(ControlNetUnit)
- bpy.utils.unregister_class(AddControlNetUnit)
- bpy.utils.unregister_class(RemoveControlNetUnit)
- bpy.utils.unregister_class(LoRAUnit)
- bpy.utils.unregister_class(AddLoRAUnit)
- bpy.utils.unregister_class(RemoveLoRAUnit)
- bpy.utils.unregister_class(ExportOrbitGIF)
- bpy.utils.unregister_class(CameraPromptItem)
- bpy.utils.unregister_class(CollectCameraPrompts)
+ del bpy.types.Scene.project_only
+
+ for cls in reversed(classes):
+ bpy.utils.unregister_class(cls)
+
# Remove the load handler for default controlnet unit
if load_handler in bpy.app.handlers.load_post:
bpy.app.handlers.load_post.remove(load_handler)
diff --git a/stablegen/generator.py b/stablegen/generator.py
index 52d52b3..d731fc7 100644
--- a/stablegen/generator.py
+++ b/stablegen/generator.py
@@ -25,6 +25,106 @@ def redraw_ui(context):
for area in context.screen.areas:
area.tag_redraw()
+class Reproject(bpy.types.Operator):
+ """Rerun projection of existing images
+ - Uses the Generate operator to reproject images, new textures will respect new Viewpoint Blending Settings
+ - Will not work with textures which used refine mode with the preserve parameter enabled"""
+ bl_idname = "object.stablegen_reproject"
+ bl_label = "Reproject Images"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ _original_method = None
+ _original_overwrite_material = None
+ _timer = None
+ @classmethod
+ def poll(cls, context):
+ """
+ Polls whether the operator can be executed.
+ :param context: Blender context.
+ :return: True if the operator can be executed, False otherwise.
+ """
+ # Check for other modal operators
+ operator = None
+ if context.scene.output_timestamp == "":
+ return False
+ for window in context.window_manager.windows:
+ for op in window.modal_operators:
+ if op.bl_idname == 'OBJECT_OT_add_cameras' or op.bl_idname == 'OBJECT_OT_bake_textures' or\
+ op.bl_idname == 'OBJECT_OT_collect_camera_prompts' or op.bl_idname == 'OBJECT_OT_test_stable' or\
+ op.bl_idname == 'OBJECT_OT_stablegen_reproject' or context.scene.generation_status == 'waiting':
+ operator = op
+ break
+ if operator:
+ break
+ if operator:
+ return False
+ return True
+
+ def execute(self, context):
+ """
+ Executes the operator.
+ :param context: Blender context.
+ :return: {'FINISHED'}
+ """
+ # Search for largest material id
+ meshes = [obj for obj in bpy.context.scene.objects if obj.type == 'MESH']
+ max_id = -1
+ for obj in meshes:
+ mat_id = get_last_material_index(obj)
+ if mat_id > max_id:
+ max_id = mat_id
+
+ cameras = [obj for obj in bpy.context.scene.objects if obj.type == 'CAMERA']
+ for i, _ in enumerate(cameras):
+ # Check if the camera has a corresponding generated image
+ image_path = get_file_path(context, "generated", camera_id=i, material_id=max_id)
+ if not os.path.exists(image_path):
+ self.report({'ERROR'}, f"Camera {i} does not have a corresponding generated image.")
+ print(f"{image_path} does not exist")
+ return {'CANCELLED'}
+
+ self._original_method = context.scene.generation_method
+ self._original_overwrite_material = context.scene.overwrite_material
+ # Set the flag to reproject
+ context.scene.project_only = True
+ # Set the generation method to 'separate' to avoid generating new images
+ context.scene.generation_method = 'separate'
+ context.scene.overwrite_material = True
+ # Set timer to 1 seconds to give some time for the generate to start
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(1.0, window=context.window)
+ # Run the generation operator
+ bpy.ops.object.test_stable('INVOKE_DEFAULT')
+
+ # Switch to modal and wait for completion
+ print("Going modal")
+ return {'RUNNING_MODAL'}
+
+ def modal(self, context, event):
+ """
+ Handles modal events.
+ :param context: Blender context.
+ :param event: Blender event.
+ :return: {'PASS_THROUGH'}
+ """
+ if event.type == 'TIMER':
+ running = False
+ if ComfyUIGenerate._is_running:
+ running = True
+ if not running:
+ # Reset the generation method and overwrite material flag
+ context.scene.generation_method = self._original_method
+ context.scene.overwrite_material = self._original_overwrite_material
+ # Reset the project only flag
+ context.scene.project_only = False
+ # Remove the modal handler
+ context.window_manager.event_timer_remove(self._timer)
+ # Report completion
+ self.report({'INFO'}, "Reprojection complete.")
+ return {'FINISHED'}
+ return {'PASS_THROUGH'}
+
+
class ComfyUIGenerate(bpy.types.Operator):
"""Generate textures using ComfyUI (to all mesh objects using all cameras in the scene)
@@ -115,7 +215,8 @@ class ComfyUIGenerate(bpy.types.Operator):
return {'FINISHED'}
# Timestamp for output directory
- context.scene.output_timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
+ if not context.scene.project_only:
+ context.scene.output_timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
# If UV inpainting and we're in prompt collection mode, collect prompts first.
if context.scene.generation_method == 'uv_inpaint' and self.show_prompt_dialog:
@@ -177,7 +278,10 @@ class ComfyUIGenerate(bpy.types.Operator):
if any(obj.type == 'CURVE' for obj in bpy.context.scene.objects):
self.report({'WARNING'}, "Curves detected in the scene. This may cause issues with the generation process. Consider removing them before proceeding.")
- print(f"Generating images for {len(self._cameras)} cameras")
+ if context.scene.project_only:
+ print(f"Reprojecting images for {len(self._cameras)} cameras")
+ else:
+ print(f"Generating images for {len(self._cameras)} cameras")
uv_slots_needed = len(self._cameras)
@@ -245,33 +349,34 @@ class ComfyUIGenerate(bpy.types.Operator):
self._cameras.append(camera)
bpy.context.scene.collection.objects.link(camera)
- # If there is depth controlnet unit
- if any(unit["unit_type"] == "depth" for unit in controlnet_units):
- if context.scene.generation_method != 'uv_inpaint':
- # Export depth maps for each camera
- for i, camera in enumerate(self._cameras):
- bpy.context.scene.camera = camera
- self.export_depthmap(context, camera_id=i)
- if context.scene.generation_method == 'grid':
- self.combine_maps(context, self._cameras, type="depth")
- # If there is canny controlnet unit
- if any(unit["unit_type"] == "canny" for unit in controlnet_units):
- if context.scene.generation_method != 'uv_inpaint':
- # Export canny maps for each camera
- for i, camera in enumerate(self._cameras):
- bpy.context.scene.camera = camera
- export_canny(context, camera_id=i, low_threshold=context.scene.canny_threshold_low, high_threshold=context.scene.canny_threshold_high)
- if context.scene.generation_method == 'grid':
- self.combine_maps(context, self._cameras, type="canny")
- # If there is normal controlnet unit
- if any(unit["unit_type"] == "normal" for unit in controlnet_units):
- if context.scene.generation_method != 'uv_inpaint':
- # Export normal maps for each camera
- for i, camera in enumerate(self._cameras):
- bpy.context.scene.camera = camera
- self.export_normal(context, camera_id=i)
- if context.scene.generation_method == 'grid':
- self.combine_maps(context, self._cameras, type="normal")
+ if not context.scene.project_only:
+ # If there is depth controlnet unit
+ if any(unit["unit_type"] == "depth" for unit in controlnet_units):
+ if context.scene.generation_method != 'uv_inpaint':
+ # Export depth maps for each camera
+ for i, camera in enumerate(self._cameras):
+ bpy.context.scene.camera = camera
+ self.export_depthmap(context, camera_id=i)
+ if context.scene.generation_method == 'grid':
+ self.combine_maps(context, self._cameras, type="depth")
+ # If there is canny controlnet unit
+ if any(unit["unit_type"] == "canny" for unit in controlnet_units):
+ if context.scene.generation_method != 'uv_inpaint':
+ # Export canny maps for each camera
+ for i, camera in enumerate(self._cameras):
+ bpy.context.scene.camera = camera
+ export_canny(context, camera_id=i, low_threshold=context.scene.canny_threshold_low, high_threshold=context.scene.canny_threshold_high)
+ if context.scene.generation_method == 'grid':
+ self.combine_maps(context, self._cameras, type="canny")
+ # If there is normal controlnet unit
+ if any(unit["unit_type"] == "normal" for unit in controlnet_units):
+ if context.scene.generation_method != 'uv_inpaint':
+ # Export normal maps for each camera
+ for i, camera in enumerate(self._cameras):
+ bpy.context.scene.camera = camera
+ self.export_normal(context, camera_id=i)
+ if context.scene.generation_method == 'grid':
+ self.combine_maps(context, self._cameras, type="normal")
# Prepare for generating
if context.scene.generation_method == 'grid':
@@ -316,7 +421,10 @@ class ComfyUIGenerate(bpy.types.Operator):
self.prompt_text = context.scene.comfyui_prompt
self._progress = 0.0
- self._stage = "Starting"
+ if context.scene.project_only:
+ self._stage = "Reprojecting"
+ else:
+ self._stage = "Starting"
redraw_ui(context)
self._current_image = 0
self._total_images = len(self._cameras)
@@ -365,7 +473,8 @@ class ComfyUIGenerate(bpy.types.Operator):
remove_empty_dirs(context)
context.scene.generation_status = 'idle'
return {'CANCELLED'}
- self.report({'INFO'}, "Generation complete.")
+ if not context.scene.project_only:
+ self.report({'INFO'}, "Generation complete.")
context.scene.display_settings.display_device = 'sRGB'
context.scene.view_settings.view_transform = 'Standard'
context.scene.generation_status = 'idle'
@@ -428,8 +537,6 @@ class ComfyUIGenerate(bpy.types.Operator):
:param context: Blender context.
:return: None
"""
- output_dir = context.preferences.addons[__package__].preferences.output_dir
-
self._error = None
try:
mesh_objects = None
@@ -441,7 +548,7 @@ class ComfyUIGenerate(bpy.types.Operator):
normal_path = None
mask_path = None
render_path = None
- while self._threads_left > 0:
+ while self._threads_left > 0 and not context.scene.project_only:
if context.scene.steps != 0:
# Generate image without ControlNet if needed
if camera_id == 0 and (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate' or context.scene.generation_method == 'refine')\
@@ -853,6 +960,7 @@ class ComfyUIGenerate(bpy.types.Operator):
controlnet_units = getattr(context.scene, "controlnet_units", [])
current_pos = pos_input
current_neg = neg_input
+ has_union = False
for idx, unit in enumerate(controlnet_units):
# Generate unique keys for nodes in this chain unit.
load_key = str(200 + idx * 3) # LoadImage node
@@ -910,10 +1018,11 @@ class ComfyUIGenerate(bpy.types.Operator):
base_prompt["239"]["inputs"]["type"] = "canny/lineart/anime_lineart/mlsd"
elif unit.unit_type == "normal":
base_prompt["239"]["inputs"]["type"] = "normal"
- else:
- # Remove the node
- if "239" in base_prompt:
- del base_prompt["239"]
+ has_union = True
+ if not has_union:
+ # Remove the node
+ if "239" in base_prompt:
+ del base_prompt["239"]
return base_prompt, current_pos
diff --git a/stablegen/stablegen.py b/stablegen/stablegen.py
index b15a323..1bf6bf7 100755
--- a/stablegen/stablegen.py
+++ b/stablegen/stablegen.py
@@ -796,6 +796,10 @@ class StableGenPanel(bpy.types.Panel):
row = tools_box.row()
row.operator("object.export_orbit_gif", text="Export Orbit GIF/MP4", icon="RENDER_ANIMATION")
+ if width_mode == 'narrow':
+ row = tools_box.row()
+ row.operator("object.stablegen_reproject", text="Reproject Textures", icon="FILE_REFRESH")
+
layout.separator()