Skip to content

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

fix: batch generation robustness and cleanup

sakalond9a0913ad1474
diff --git a/stablegen/__init__.py b/stablegen/__init__.py
index e744fa9..7dd9ff1 100644
--- a/stablegen/__init__.py
+++ b/stablegen/__init__.py
@@ -36,7 +36,6 @@ from .texturing.generator import (
)
from .mesh_gen.trellis2 import Trellis2Generate
from .mesh_gen.batch import (
- batch_classes as _batch_classes,
TRELLIS2_OT_BatchSelectFolder, TRELLIS2_OT_BatchGenerate,
TRELLIS2_OT_BatchCancel, TRELLIS2_OT_BatchClear,
)
@@ -161,6 +160,9 @@ def register():
def unregister():
+ from .mesh_gen.batch import unregister_batch
+ unregister_batch()
+
unregister_properties(
load_handler=load_handler,
_sg_queue_load_handler=_sg_queue_load_handler,
diff --git a/stablegen/mesh_gen/__init__.py b/stablegen/mesh_gen/__init__.py
index e8ae342..d20c0fe 100644
--- a/stablegen/mesh_gen/__init__.py
+++ b/stablegen/mesh_gen/__init__.py
@@ -1,4 +1,9 @@
"""mesh_gen – 3D mesh generation (TRELLIS.2)."""
from .trellis2 import Trellis2Generate # noqa: F401
-from .batch import batch_classes, TRELLIS2_OT_BatchSelectFolder, TRELLIS2_OT_BatchGenerate, TRELLIS2_OT_BatchCancel, TRELLIS2_OT_BatchClear # noqa: F401
+from .batch import ( # noqa: F401
+ batch_classes,
+ TRELLIS2_OT_BatchSelectFolder, TRELLIS2_OT_BatchGenerate,
+ TRELLIS2_OT_BatchCancel, TRELLIS2_OT_BatchClear,
+ unregister_batch,
+)
diff --git a/stablegen/mesh_gen/batch.py b/stablegen/mesh_gen/batch.py
index ec359d9..ed169f4 100644
--- a/stablegen/mesh_gen/batch.py
+++ b/stablegen/mesh_gen/batch.py
@@ -14,12 +14,12 @@ _batch_state = {
'total': 0,
'cancelled': False,
'rename_meshes': True,
- 'pre_objects': set(), # object names present before current generation
+ 'pre_objects': set(), # object ids present before current generation
'settling': False, # waiting a few ticks for Trellis2Generate to start
'settle_count': 0,
}
-_SETTLE_TICKS = 4 # ticks to wait after invoking the operator
+_SETTLE_TICKS = 12 # ticks to wait after invoking the operator
_TICK_INTERVAL = 0.5 # seconds between timer callbacks
@@ -63,9 +63,10 @@ def _rename_new_objects(image_path):
stem = os.path.splitext(os.path.basename(image_path))[0]
try:
scene = bpy.context.scene
+ pre_ids = state['pre_objects']
new_objs = [
obj for obj in scene.objects
- if obj.name not in state['pre_objects']
+ if id(obj) not in pre_ids
]
mesh_objs = [o for o in new_objs if o.type == 'MESH']
if not mesh_objs:
@@ -89,9 +90,9 @@ def _trigger_next():
state = _batch_state
image_path = state['images'][state['index']]
- # Snapshot current objects so we can identify new ones after import
+ # Snapshot current object IDs so we can identify new ones after import
try:
- state['pre_objects'] = {obj.name for obj in bpy.context.scene.objects}
+ state['pre_objects'] = {id(obj) for obj in bpy.context.scene.objects}
except Exception: # noqa: BLE001
state['pre_objects'] = set()
@@ -105,12 +106,12 @@ def _trigger_next():
return
try:
- bpy.ops.object.trellis2_generate('INVOKE_DEFAULT')
+ bpy.ops.object.trellis2_generate('EXEC_DEFAULT')
state['settling'] = True
state['settle_count'] = 0
except Exception as exc: # noqa: BLE001
- print(f"[BatchGen] Error invoking generate: {exc}")
- state['cancelled'] = True
+ print(f"[BatchGen] Error invoking generate for image "
+ f"{state['index'] + 1}: {exc}, skipping")
def _batch_tick():
@@ -130,7 +131,12 @@ def _batch_tick():
# ── Settling: waiting for Trellis2Generate to start running ─────────────
if state['settling']:
state['settle_count'] += 1
- if state['settle_count'] >= _SETTLE_TICKS:
+ if Trellis2Generate._is_running:
+ # Operator started successfully, stop settling
+ state['settling'] = False
+ elif state['settle_count'] >= _SETTLE_TICKS:
+ # Timed out waiting for operator — skip this image
+ print(f"[BatchGen] Image {state['index'] + 1} failed to start, skipping")
state['settling'] = False
return _TICK_INTERVAL
@@ -267,3 +273,14 @@ batch_classes = [
TRELLIS2_OT_BatchCancel,
TRELLIS2_OT_BatchClear,
]
+
+
+def unregister_batch():
+ """Stop any running batch timer. Called from addon unregister."""
+ _batch_state['cancelled'] = True
+ _batch_state['active'] = False
+ try:
+ if bpy.app.timers.is_registered(_batch_tick):
+ bpy.app.timers.unregister(_batch_tick)
+ except Exception: # noqa: BLE001
+ pass
diff --git a/stablegen/ui/panel.py b/stablegen/ui/panel.py
index 80755b4..8cb49ca 100644
--- a/stablegen/ui/panel.py
+++ b/stablegen/ui/panel.py
@@ -235,7 +235,7 @@ class StableGenPanel(bpy.types.Panel):
if getattr(context.window_manager, 'sg_batch_running', False) and _bt > 0:
progress_col.progress(
text=f"Batch: Model {_bi}/{_bt}",
- factor=max(0.0, min((_bi - 1) / _bt, 1.0))
+ factor=max(0.0, min(_bi / _bt, 1.0))
)
# Bar 1 — Overall
@@ -672,7 +672,7 @@ class StableGenPanel(bpy.types.Panel):
img_row = split.row(align=True)
img_row.prop(scene, "trellis2_input_image", text="")
img_row.operator("object.trellis2_batch_select_folder",
- text="", icon="FILE_FOLDER")
+ text="", icon="OUTLINER_COLLECTION")
# Batch folder status row
batch_folder = getattr(scene, 'trellis2_batch_folder', '')