Transform your 3D texturing workflow with the power of generative AI, directly within Blender!
regenerate selected views from the projection, not a bake or an edit layer
sakalonde2bd2637577b
diff --git a/stablegen/texturing/generator.py b/stablegen/texturing/generator.pyindex 4c65c3f..44df300 100644--- a/stablegen/texturing/generator.py+++ b/stablegen/texturing/generator.py@@ -29,7 +29,7 @@ from ..util.workflow_templates import prompt_text, prompt_text_img2img, prompt_tfrom .rendering import export_emit_image, export_visibility, export_canny, bake_texture, prepare_baking, unwrap, export_render, export_viewport, render_edge_feather_mask, apply_uv_inpaint_texture, flatten_projection_material_for_refine, _RenderStateTransaction, _legacy_render_dither, _prepare_compositor_render, _finish_compositor_renderfrom ..cameras.geometry import _SGCameraResolution, _get_camera_resolution, _get_resolution_alignfrom ..cameras.overlays import _sg_restore_square_display, _sg_remove_crop_overlay, _sg_ensure_crop_overlay, _sg_hide_label_overlay, _sg_restore_label_overlay-from .projection import project_image, reinstate_compare_nodes, _bake_visibility_weights, reset_projection_material_nodes, _is_projection_mesh, pop_svm_stack_warning, pop_eevee_attribute_warning+from .projection import project_image, reinstate_compare_nodes, activate_projection_material, _bake_visibility_weights, reset_projection_material_nodes, _is_projection_mesh, pop_svm_stack_warning, pop_eevee_attribute_warningfrom .jobs import operation, RenderRequest, MainThreadOperation, run_syncfrom ..utils import get_last_material_index, get_generation_dirs, get_file_path, get_dir_path, remove_empty_dirs, get_compositor_node_tree, new_compositor_math, configure_output_node_paths, get_eevee_engine_id, sg_modal_activefrom ..util.mirror_color import MirrorReproject, _get_viewport_ref_np, _apply_color_match_to_file, restore_unmatched_image@@ -194,9 +194,10 @@ class Regenerate(bpy.types.Operator):return {'CANCELLED'}else: # allself._to_texture = [obj for obj in bpy.context.view_layer.objects if obj.type == 'MESH' and not obj.hide_get()]- # Revert discard angle+ # Revert discard angle (in the projection material, not a bake's)new_discard_angle = context.scene.discard_factorfor obj in self._to_texture:+ activate_projection_material(obj)if not obj.active_material or not obj.active_material.use_nodes:continue@@ -1150,8 +1151,9 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):self._to_texture = [obj for obj in bpy.context.view_layer.objects if obj.type == 'MESH' and not obj.hide_get()]# Find all mesh objects, check their material ids and store the highest one- # (a reprojection reads generated views, which Masked Edit layers do not have)- reproject = context.scene.generation_mode == 'project_only'+ # (a reprojection or regeneration reads and replaces generated views,+ # which Masked Edit layers do not have)+ reproject = context.scene.generation_mode in {'project_only', 'regenerate_selected'}for obj in self._to_texture:for slot in obj.material_slots:material_id = get_last_material_index(obj, include_edits=not reproject)@@ -1302,6 +1304,11 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):# Regenerate mode preparationif context.scene.generation_mode == 'regenerate_selected':+ # After Bake Textures the bake is the active material; the views to+ # reset, and the context of the first regenerated one, are in the+ # projection material.+ for obj in self._to_texture:+ activate_projection_material(obj)if context.scene.generation_method == 'sequential':# Sequential regeneration: reset all cameras from the first# selected onward so the projection sequence replays correctly.@@ -1964,6 +1971,28 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):else:image = self.workflow_manager.generate(context, controlnet_info=controlnet_info, ipadapter_ref_info=ipadapter_ref_info)else:+ if (context.scene.generation_mode == 'regenerate_selected'+ and self._current_image == min(self._selected_camera_ids)):+ # Regeneration starts mid-sequence: project the+ # views before it, as the run did, so its context+ # holds those and not the views being regenerated.+ self._stage = "Projecting Image"+ def earlier_project_callback():+ try:+ yield from project_image(+ context, self._to_texture, self._material_id,+ stop_index=self._current_image - 1,+ cameras=self._cameras,+ view_plan=self._view_plan,+ _steps=True,+ )+ except Exception as e:+ self._error = str(e)+ traceback.print_exc()+ return None+ self._run_on_main_thread(earlier_project_callback, finish_on_cancel=True)+ if self._error:+ returnself._stage = "Preparing Next Camera"self._progress = 0def context_callback():diff --git a/stablegen/texturing/projection.py b/stablegen/texturing/projection.pyindex 52b7fef..0a86838 100644--- a/stablegen/texturing/projection.py+++ b/stablegen/texturing/projection.py@@ -1894,6 +1894,28 @@ def is_baked_material(material):return bool(material.get('sg_baked')) or re.search(r'_baked(\.\d+)?( Masked Edit)*$', material.name) is not None+def activate_projection_material(obj):+ """Make the object's projection material active again when a bake's material is.++ A bake's material holds its final texture and is the active one after Bake+ Textures; projecting into it, or reading a run's views from it, replaced or+ missed the projection. Faces move to the projection material (a new one if+ the object has none); the bake stays for the user to switch back to.+ """+ if obj.active_material is None or not is_baked_material(obj.active_material):+ return obj.active_material+ baked = obj.active_material+ index = next((k for k, m in enumerate(obj.data.materials)+ if m and m is not baked and not is_baked_material(m)), -1)+ if index < 0:+ obj.data.materials.append(bpy.data.materials.new(name="ProjectionMaterial"))+ index = len(obj.data.materials) - 1+ obj.active_material_index = index+ obj.data.polygons.foreach_set("material_index", [index] * len(obj.data.polygons))+ obj.data.update()+ return obj.active_material++def _get_voronoi_placeholder(color=(1.0, 0.0, 1.0)):"""Get or create a 1x1 solid-colour image used as placeholder fornon-generated cameras in Voronoi projection mode. The colour is@@ -2872,26 +2894,13 @@ def project_image(context.view_layer.objects.active = objobj.select_set(True)- # A bake's material holds its final texture. Projecting into it (it is- # the active material after Bake Textures) replaced the bake with a new- # projection graph; project into the object's projection material- # again, or a new one, and leave the bake for the user to switch back to.# Local edits (Masked Edit, local refine) edit the active material on# purpose, a bake included, and keep doing so.local_edit = (context.scene.generation_method == 'local_edit'or (context.scene.model_architecture in QWEN_ARCHITECTURESand context.scene.qwen_generation_method == 'local_edit'))- if (not local_edit and obj.active_material is not None- and is_baked_material(obj.active_material)):- baked = obj.active_material- index = next((k for k, m in enumerate(obj.data.materials)- if m and m is not baked and not is_baked_material(m)), -1)- if index < 0:- obj.data.materials.append(bpy.data.materials.new(name="ProjectionMaterial"))- index = len(obj.data.materials) - 1- obj.active_material_index = index- obj.data.polygons.foreach_set("material_index", [index] * len(obj.data.polygons))- obj.data.update()+ if not local_edit:+ activate_projection_material(obj)# Create the materialto_switch = False