Skip to content

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

add an automatic rigging feature using skintokens

sakalondb3b0d103d4e4
diff --git a/stablegen/core/callbacks.py b/stablegen/core/callbacks.py
index a6004a3..588b4ea 100644
--- a/stablegen/core/callbacks.py
+++ b/stablegen/core/callbacks.py
@@ -18,6 +18,7 @@ from .state import (
from .server_api import (
check_pbr_available,
check_server_availability,
+ check_skintokens_autorig_available,
check_trellis2_available,
)
from ..timeout_config import get_timeout
@@ -126,9 +127,11 @@ def update_combined(self, context):
if result['online']:
result['trellis2'] = check_trellis2_available(server_address, timeout=get_timeout('api'))
result['pbr'] = check_pbr_available(server_address, timeout=get_timeout('api'))
+ result['skintokens'] = check_skintokens_autorig_available(server_address, timeout=get_timeout('api'))
else:
result['trellis2'] = False
result['pbr'] = False
+ result['skintokens'] = False
return result
def _on_done(result):
@@ -140,6 +143,7 @@ def update_combined(self, context):
if hasattr(bpy.context, 'scene') and bpy.context.scene:
bpy.context.scene.trellis2_available = result.get('trellis2', False)
bpy.context.scene.pbr_nodes_available = result.get('pbr', False)
+ bpy.context.scene.skintokens_autorig_available = result.get('skintokens', False)
if not result.get('online', False):
print("[StableGen] ComfyUI server is not reachable.")
@@ -309,4 +313,3 @@ def update_palette_color(self, context):
def update_print_dithered(self, context):
check_print_preset_match(self.id_data)
-
diff --git a/stablegen/core/load_handlers.py b/stablegen/core/load_handlers.py
index 1c48b12..8e8bad7 100644
--- a/stablegen/core/load_handlers.py
+++ b/stablegen/core/load_handlers.py
@@ -128,7 +128,11 @@ def load_handler(dummy):
if prefs_wrapper:
server_address = prefs_wrapper.preferences.server_address
if server_address:
- from .server_api import check_trellis2_available, check_pbr_available
+ from .server_api import (
+ check_pbr_available,
+ check_skintokens_autorig_available,
+ check_trellis2_available,
+ )
from ..timeout_config import get_timeout
_timeout = get_timeout('api')
@@ -137,6 +141,7 @@ def load_handler(dummy):
result = {}
result['trellis2'] = check_trellis2_available(server_address, timeout=_timeout)
result['pbr'] = check_pbr_available(server_address, timeout=_timeout)
+ result['skintokens'] = check_skintokens_autorig_available(server_address, timeout=_timeout)
return result
def _apply_node_result(result):
@@ -145,5 +150,6 @@ def load_handler(dummy):
if hasattr(bpy.context, 'scene') and bpy.context.scene:
bpy.context.scene.trellis2_available = result.get('trellis2', False)
bpy.context.scene.pbr_nodes_available = result.get('pbr', False)
+ bpy.context.scene.skintokens_autorig_available = result.get('skintokens', False)
_state._run_async(_check_nodes, _apply_node_result)
diff --git a/stablegen/core/preferences.py b/stablegen/core/preferences.py
index c3dc72e..92306a1 100644
--- a/stablegen/core/preferences.py
+++ b/stablegen/core/preferences.py
@@ -18,6 +18,7 @@ from .server_api import (
_fetch_api_list,
check_pbr_available,
check_server_availability,
+ check_skintokens_autorig_available,
check_trellis2_available,
)
from .state import (
@@ -254,9 +255,11 @@ class CheckServerStatus(bpy.types.Operator):
if result['online']:
result['trellis2'] = check_trellis2_available(server_addr, timeout=get_timeout('api'))
result['pbr'] = check_pbr_available(server_addr, timeout=get_timeout('api'))
+ result['skintokens'] = check_skintokens_autorig_available(server_addr, timeout=get_timeout('api'))
else:
result['trellis2'] = False
result['pbr'] = False
+ result['skintokens'] = False
return result
def _on_done(result):
@@ -268,6 +271,7 @@ class CheckServerStatus(bpy.types.Operator):
if hasattr(bpy.context, 'scene') and bpy.context.scene:
bpy.context.scene.trellis2_available = result.get('trellis2', False)
bpy.context.scene.pbr_nodes_available = result.get('pbr', False)
+ bpy.context.scene.skintokens_autorig_available = result.get('skintokens', False)
if result.get('online', False):
print(f"[StableGen] ComfyUI server is online at {server_addr}.")
diff --git a/stablegen/core/properties.py b/stablegen/core/properties.py
index 3a0fe02..c39f720 100644
--- a/stablegen/core/properties.py
+++ b/stablegen/core/properties.py
@@ -1254,6 +1254,11 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
description="Whether PBR decomposition nodes (Marigold IID + StableDelight) are available on the ComfyUI server (auto-detected)",
default=False
)
+ bpy.types.Scene.skintokens_autorig_available = bpy.props.BoolProperty(
+ name="SkinTokens Auto-Rig Available",
+ description="Whether the StableGen SkinTokens auto-rig node is available on the ComfyUI server (auto-detected)",
+ default=False
+ )
bpy.types.Scene.show_trellis2_params = bpy.props.BoolProperty(
name="Show TRELLIS.2 Section", description="Toggle TRELLIS.2 Image-to-3D section", default=False
)
@@ -1269,6 +1274,9 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
bpy.types.Scene.show_trellis2_camera_settings = bpy.props.BoolProperty(
name="Show Camera Placement Settings", description="Toggle TRELLIS.2 camera placement settings", default=False
)
+ bpy.types.Scene.show_trellis2_autorig_settings = bpy.props.BoolProperty(
+ name="Show Auto-Rig Settings", description="Toggle TRELLIS.2 SkinTokens auto-rig settings", default=False
+ )
bpy.types.Scene.trellis2_last_input_image = bpy.props.StringProperty(
name="TRELLIS.2 Last Input Image",
description="Path to the image most recently used as TRELLIS.2 input (set automatically after generation)",
@@ -1496,6 +1504,56 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
description="Apply watertight capping and hollowing to delete internal geometry, saving UV and texture space",
default=False, update=update_parameters
)
+ bpy.types.Scene.trellis2_autorig_enabled = bpy.props.BoolProperty(
+ name="Auto-Rig",
+ description="Run the generated GLB through the StableGen SkinTokens ComfyUI node before importing it",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_use_transfer = bpy.props.BoolProperty(
+ name="Transfer Original",
+ description="Ask SkinTokens to transfer the generated rig back to the original mesh to preserve texture and scale",
+ default=True, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_use_skeleton = bpy.props.BoolProperty(
+ name="Use Existing Skeleton",
+ description="Ask SkinTokens to use an existing skeleton and only generate skin weights when the input already has one",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_use_postprocess = bpy.props.BoolProperty(
+ name="Skin Postprocess",
+ description="Enable SkinTokens voxel skin post-processing before export",
+ default=False, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_top_k = bpy.props.IntProperty(
+ name="Top K", description="SkinTokens sampling top-k",
+ default=5, min=1, max=200, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_top_p = bpy.props.FloatProperty(
+ name="Top P", description="SkinTokens nucleus sampling top-p",
+ default=0.95, min=0.1, max=1.0, step=0.01, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_temperature = bpy.props.FloatProperty(
+ name="Temperature", description="SkinTokens sampling temperature",
+ default=1.0, min=0.1, max=2.0, step=0.1, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_repetition_penalty = bpy.props.FloatProperty(
+ name="Repetition Penalty", description="SkinTokens repetition penalty",
+ default=2.0, min=0.5, max=3.0, step=0.1, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_num_beams = bpy.props.IntProperty(
+ name="Beams", description="SkinTokens beam count",
+ default=10, min=1, max=20, update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_model_ckpt = bpy.props.StringProperty(
+ name="Model Checkpoint",
+ description="Optional SkinTokens checkpoint path relative to SKINTOKENS_ROOT",
+ default="", update=update_parameters
+ )
+ bpy.types.Scene.trellis2_autorig_hf_path = bpy.props.StringProperty(
+ name="HF Path",
+ description="Optional Hugging Face path passed to SkinTokens",
+ default="", update=update_parameters
+ )
bpy.types.Scene.trellis2_auto_lighting = bpy.props.BoolProperty(
name="Auto Studio Lighting",
@@ -1806,10 +1864,10 @@ def unregister_properties(load_handler, _sg_queue_load_handler):
# --- TRELLIS.2 Properties ---
trellis2_props = [
- 'trellis2_available', 'pbr_nodes_available',
+ 'trellis2_available', 'pbr_nodes_available', 'skintokens_autorig_available',
'show_trellis2_params', 'show_trellis2_advanced',
'show_trellis2_mesh_settings', 'show_trellis2_texture_settings',
- 'show_trellis2_camera_settings',
+ 'show_trellis2_camera_settings', 'show_trellis2_autorig_settings',
'trellis2_last_input_image', 'qwen_use_trellis2_style', 'qwen_trellis2_style_initial_only',
'trellis2_pipeline_active', 'trellis2_pipeline_phase_start_pct',
'trellis2_pipeline_total_phases',
@@ -1839,6 +1897,12 @@ def unregister_properties(load_handler, _sg_queue_load_handler):
'trellis2_decimate_method', 'trellis2_remesh_method',
'trellis2_post_processing_enabled',
'trellis2_solidify',
+ 'trellis2_autorig_enabled', 'trellis2_autorig_use_transfer',
+ 'trellis2_autorig_use_skeleton', 'trellis2_autorig_use_postprocess',
+ 'trellis2_autorig_top_k', 'trellis2_autorig_top_p',
+ 'trellis2_autorig_temperature', 'trellis2_autorig_repetition_penalty',
+ 'trellis2_autorig_num_beams', 'trellis2_autorig_model_ckpt',
+ 'trellis2_autorig_hf_path',
'trellis2_auto_lighting',
'trellis2_skip_texture', 'trellis2_bg_removal', 'trellis2_background_color',
]
diff --git a/stablegen/core/server_api.py b/stablegen/core/server_api.py
index 43dcdaf..8d55148 100644
--- a/stablegen/core/server_api.py
+++ b/stablegen/core/server_api.py
@@ -105,6 +105,24 @@ def check_pbr_available(server_address, timeout=1.0):
return False
+def check_skintokens_autorig_available(server_address, timeout=1.0):
+ """Checks if the StableGen SkinTokens auto-rig custom node is installed."""
+ if not server_address:
+ return False
+ node_class = 'StableGenSkinTokensAutoRig'
+ try:
+ url = f"http://{server_address}/object_info/{node_class}"
+ response = requests.get(url, timeout=timeout)
+ if response.status_code == 200 and node_class in response.json():
+ print("[SkinTokens] Auto-detect: StableGen SkinTokens node found in ComfyUI.")
+ return True
+ print("[SkinTokens] Auto-detect: StableGen SkinTokens node NOT found in ComfyUI.")
+ return False
+ except Exception as e:
+ print(f"[SkinTokens] Auto-detect failed: {e}")
+ return False
+
+
# ── API data retrieval ─────────────────────────────────────────────────────
def fetch_from_comfyui_api(context, endpoint):
diff --git a/stablegen/mesh_gen/trellis2.py b/stablegen/mesh_gen/trellis2.py
index c843a38..1ad324d 100644
--- a/stablegen/mesh_gen/trellis2.py
+++ b/stablegen/mesh_gen/trellis2.py
@@ -85,6 +85,88 @@ class Trellis2Generate(bpy.types.Operator):
# Keep legacy _progress in sync for any code that reads it
self._progress = self._overall_progress
+ @staticmethod
+ def _clear_imported_custom_normals(mesh_objects):
+ """Clear TRELLIS/GLB custom normals without changing object hierarchy."""
+ if not mesh_objects:
+ return
+
+ previous_active = bpy.context.view_layer.objects.active
+ previous_selection = list(bpy.context.selected_objects)
+ cleared = 0
+
+ try:
+ if bpy.ops.object.mode_set.poll():
+ bpy.ops.object.mode_set(mode='OBJECT')
+ except Exception:
+ pass
+
+ try:
+ for obj in mesh_objects:
+ if obj.type != 'MESH':
+ continue
+ if hasattr(obj.data, "has_custom_normals") and not obj.data.has_custom_normals:
+ continue
+
+ bpy.ops.object.select_all(action='DESELECT')
+ obj.select_set(True)
+ bpy.context.view_layer.objects.active = obj
+ try:
+ bpy.ops.mesh.customdata_custom_splitnormals_clear()
+ cleared += 1
+ except Exception as err:
+ print(f"[TRELLIS2] Warning: Could not clear custom normals on {obj.name}: {err}")
+ finally:
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in previous_selection:
+ if bpy.context.scene.objects.get(obj.name):
+ obj.select_set(True)
+ if previous_active and bpy.context.scene.objects.get(previous_active.name):
+ bpy.context.view_layer.objects.active = previous_active
+
+ if cleared:
+ print(f"[TRELLIS2] Cleared custom split normals on {cleared} imported mesh object(s)")
+
+ @staticmethod
+ def _disable_not_exported_render_helpers():
+ """Disable glTF control/helper geometry for render and bake."""
+ disabled_objects = 0
+ disabled_collections = 0
+
+ def iter_collection_objects(collection):
+ seen = set()
+
+ def visit(col):
+ for obj in col.objects:
+ if obj.name in seen:
+ continue
+ seen.add(obj.name)
+ yield obj
+ for child in col.children:
+ yield from visit(child)
+
+ yield from visit(collection)
+
+ for collection in bpy.data.collections:
+ if "not_exported" not in collection.name.lower():
+ continue
+
+ if not collection.hide_render:
+ collection.hide_render = True
+ disabled_collections += 1
+
+ for obj in iter_collection_objects(collection):
+ if not obj.hide_render:
+ obj.hide_render = True
+ disabled_objects += 1
+
+ if disabled_objects or disabled_collections:
+ print(
+ "[TRELLIS2] Disabled render visibility for "
+ f"{disabled_objects} glTF not_exported helper object(s) "
+ f"across {disabled_collections} collection(s)."
+ )
+
@classmethod
def poll(cls, context):
if cls._is_running:
@@ -160,6 +242,7 @@ class Trellis2Generate(bpy.types.Operator):
self._progress = 0.0
self._stage = "Initializing"
self._texture_mode = tex_mode
+ self._autorig_enabled = False
from ..workflows import WorkflowManager
self.workflow_manager = WorkflowManager(self)
@@ -349,44 +432,51 @@ class Trellis2Generate(bpy.types.Operator):
# Import GLB into Blender
bpy.ops.import_scene.gltf(filepath=glb_path)
+ autorig_enabled = getattr(self, '_autorig_enabled', False)
# --- Clean hierarchy, unparent meshes, and remove GLTF/GLB structural empties ---
imported_objects = [obj for obj in context.selected_objects]
for obj in imported_objects:
obj["sg_generated"] = True
+ self._disable_not_exported_render_helpers()
mesh_objects = [obj for obj in imported_objects if obj.type == 'MESH']
empty_objects = [obj for obj in imported_objects if obj.type == 'EMPTY']
if mesh_objects:
- # 1. Unparent meshes while preserving their world space transforms and clearing broken custom normals
- for obj in mesh_objects:
- # Save world matrix
- world_mat = obj.matrix_world.copy()
- # Set the object as active so the operator can work on it
- bpy.context.view_layer.objects.active = obj
- # Clear custom split normals using the official operator to fix broken custom normals & convex shadowing artifacts
- try:
- bpy.ops.mesh.customdata_custom_splitnormals_clear()
- except Exception as err:
- print(f"[TRELLIS2] Warning: Could not clear custom normals on {obj.name}: {err}")
- # Unparent
- obj.parent = None
- # Restore world matrix
- obj.matrix_world = world_mat
-
- # 2. Delete the structural empties (e.g. "world", "geometry_0")
- if empty_objects:
+ # TRELLIS.2/GLB exports can carry broken custom split normals.
+ # Clear them for both plain and rigged imports; this only
+ # touches mesh data and leaves armature/object hierarchy intact.
+ self._clear_imported_custom_normals(mesh_objects)
+
+ if autorig_enabled:
+ # Rigged GLBs need their armature/object hierarchy intact.
bpy.ops.object.select_all(action='DESELECT')
- for obj in empty_objects:
+ for obj in imported_objects:
obj.select_set(True)
- bpy.ops.object.delete()
- print(f"[TRELLIS2] Cleaned up {len(empty_objects)} GLB parent empty objects")
+ bpy.context.view_layer.objects.active = mesh_objects[0]
+ else:
+ # 1. Unparent meshes while preserving their world space transforms
+ for obj in mesh_objects:
+ # Save world matrix
+ world_mat = obj.matrix_world.copy()
+ # Unparent
+ obj.parent = None
+ # Restore world matrix
+ obj.matrix_world = world_mat
+
+ # 2. Delete the structural empties (e.g. "world", "geometry_0")
+ if empty_objects:
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in empty_objects:
+ obj.select_set(True)
+ bpy.ops.object.delete()
+ print(f"[TRELLIS2] Cleaned up {len(empty_objects)} GLB parent empty objects")
- # 3. Reselect only the imported mesh objects for downstream processing
- bpy.ops.object.select_all(action='DESELECT')
- for obj in mesh_objects:
- obj.select_set(True)
- bpy.context.view_layer.objects.active = mesh_objects[0]
+ # 3. Reselect only the imported mesh objects for downstream processing
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in mesh_objects:
+ obj.select_set(True)
+ bpy.context.view_layer.objects.active = mesh_objects[0]
# --- Normalise imported mesh to a reasonable Blender-unit size ---
target_bu = getattr(context.scene, 'trellis2_import_scale', 2.0)
@@ -408,18 +498,32 @@ class Trellis2Generate(bpy.types.Operator):
)
if extent > 1e-6:
scale_factor = target_bu / extent
- for obj in imported_meshes:
- obj.scale *= scale_factor
- # Apply scale so downstream code sees unit scale
- bpy.ops.object.select_all(action='DESELECT')
- for obj in imported_meshes:
- obj.select_set(True)
- bpy.context.view_layer.objects.active = imported_meshes[0]
- bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
- print(f"[TRELLIS2] Scaled mesh to {target_bu} BU (factor {scale_factor:.4f})")
+ if autorig_enabled:
+ root_objects = [obj for obj in imported_objects if obj.parent is None]
+ if not root_objects:
+ root_objects = imported_objects
+ for obj in root_objects:
+ obj.scale *= scale_factor
+ print(
+ f"[TRELLIS2] Scaled rigged import to {target_bu} BU "
+ f"(factor {scale_factor:.4f}, unapplied)"
+ )
+ else:
+ for obj in imported_meshes:
+ obj.scale *= scale_factor
+ # Apply scale so downstream code sees unit scale
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in imported_meshes:
+ obj.select_set(True)
+ bpy.context.view_layer.objects.active = imported_meshes[0]
+ bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
+ print(f"[TRELLIS2] Scaled mesh to {target_bu} BU (factor {scale_factor:.4f})")
# --- Solidification ---
solidify_mesh = getattr(context.scene, 'trellis2_solidify', False)
+ if autorig_enabled and solidify_mesh:
+ print("[TRELLIS2] Skipping solidify because SkinTokens auto-rigging is enabled")
+ solidify_mesh = False
if solidify_mesh:
from ..texturing.print_export import _make_solid_mesh_object
imported_meshes = [obj for obj in context.selected_objects if obj.type == 'MESH']
@@ -440,7 +544,7 @@ class Trellis2Generate(bpy.types.Operator):
remesh_method = getattr(context.scene, 'trellis2_remesh_method', 'qdc')
imported_meshes = [obj for obj in context.selected_objects if obj.type == 'MESH']
- if imported_meshes:
+ if imported_meshes and not autorig_enabled:
# 1. Fallback decimation on main thread if background decimation was requested but didn't run
if decimate_method == 'collapse' and not getattr(self, '_trimesh_decimated', False):
import trimesh
@@ -474,6 +578,9 @@ class Trellis2Generate(bpy.types.Operator):
# 2. Local remeshing retopology
if remesh_method == 'quadriflow':
self._perform_retopology(context, imported_meshes, remesh_method)
+ elif imported_meshes and autorig_enabled and (
+ decimate_method == 'collapse' or remesh_method == 'quadriflow'):
+ print("[TRELLIS2] Skipping local decimation/retopology because SkinTokens auto-rigging is enabled")
# --- Apply shading mode to imported meshes ---
_shade_mode = getattr(context.scene, 'trellis2_shade_mode', 'flat')
@@ -897,6 +1004,7 @@ class Trellis2Generate(bpy.types.Operator):
# Store the final input image path for later use (IPAdapter/Qwen style)
self._input_image_path = image_path
+ self._autorig_enabled = bool(getattr(context.scene, 'trellis2_autorig_enabled', False))
result = self.workflow_manager.generate_trellis2(context, image_path)
@@ -908,7 +1016,7 @@ class Trellis2Generate(bpy.types.Operator):
self._error = result["error"]
else:
self._trimesh_decimated = False
- if decimate_method == 'collapse':
+ if decimate_method == 'collapse' and not self._autorig_enabled:
self._detail_stage = "Decimating mesh via trimesh"
self._phase_stage = "Trimesh decimation"
self._phase_progress = 90
@@ -923,6 +1031,8 @@ class Trellis2Generate(bpy.types.Operator):
except Exception as dec_err:
print(f"[TRELLIS2] Background trimesh decimation failed: {dec_err}")
# Fallback: result is unmodified raw GLB
+ elif decimate_method == 'collapse' and self._autorig_enabled:
+ print("[TRELLIS2] Skipping background trimesh decimation because SkinTokens auto-rigging is enabled")
self._glb_data = result
except Exception as e:
@@ -1228,4 +1338,4 @@ class StableGenLocalPostProcess(bpy.types.Operator):
self.report({'ERROR'}, f"Failed to run local post-processing: {e}")
import traceback
traceback.print_exc()
- return {'CANCELLED'}
\ No newline at end of file
+ return {'CANCELLED'}
diff --git a/stablegen/mesh_gen/workflows.py b/stablegen/mesh_gen/workflows.py
index fda9e29..68e7720 100644
--- a/stablegen/mesh_gen/workflows.py
+++ b/stablegen/mesh_gen/workflows.py
@@ -763,12 +763,26 @@ class _Trellis2WorkflowMixin:
# Configure export with unique prefix for identification
unique_prefix = f"trellis2_{uuid.uuid4().hex[:8]}"
+ autorig_enabled = bool(getattr(scene, 'trellis2_autorig_enabled', False))
+ if autorig_enabled and not getattr(scene, 'skintokens_autorig_available', False):
+ return {
+ "error": (
+ "SkinTokens auto-rigging is enabled, but the "
+ "StableGenSkinTokensAutoRig node was not detected in ComfyUI."
+ )
+ }
# When post-processing is disabled, maximize the decimation target
# and disable remeshing so the user gets the rawest possible mesh.
use_pp = getattr(scene, 'trellis2_post_processing_enabled', True)
decimate_method = getattr(scene, 'trellis2_decimate_method', 'server')
remesh_method = getattr(scene, 'trellis2_remesh_method', 'qdc')
+ if autorig_enabled and decimate_method == 'collapse':
+ print(
+ "[TRELLIS2] SkinTokens auto-rigging runs on the ComfyUI host; "
+ "using server-side decimation instead of local collapse decimation"
+ )
+ decimate_method = 'server'
if skip_texture:
bypass_server_simplify = (not use_pp) or (remesh_method != 'qdc' and decimate_method != 'server')
@@ -798,9 +812,6 @@ class _Trellis2WorkflowMixin:
prompt[NODES['shape_to_textured_mesh']]["inputs"]["tex_sampling_steps"] = scene.trellis2_tex_steps
# Configure GLB export via process/rasterize/export pipeline
- decimate_method = getattr(scene, 'trellis2_decimate_method', 'server')
- remesh_method = getattr(scene, 'trellis2_remesh_method', 'qdc')
-
bypass_server_process = (not use_pp) or (remesh_method != 'qdc' and decimate_method != 'server')
if bypass_server_process:
# Local decimation and/or local remeshing, or post-processing disabled: bypass server process_mesh entirely
@@ -819,6 +830,33 @@ class _Trellis2WorkflowMixin:
prompt[NODES['rasterize_pbr']]["inputs"]["texture_size"] = scene.trellis2_texture_size
prompt[NODES['export_glb']]["inputs"]["filename_prefix"] = unique_prefix
+ if autorig_enabled:
+ rig_node_id = str(
+ max(int(node_id) for node_id in prompt.keys() if str(node_id).isdigit()) + 1
+ )
+ prompt[rig_node_id] = {
+ "class_type": "StableGenSkinTokensAutoRig",
+ "inputs": {
+ "input_glb": [NODES[export_node_key], 0],
+ "input_path": "",
+ "output_prefix": f"{unique_prefix}_skintokens",
+ "install_policy": "auto",
+ "use_transfer": bool(getattr(scene, 'trellis2_autorig_use_transfer', True)),
+ "use_skeleton": bool(getattr(scene, 'trellis2_autorig_use_skeleton', False)),
+ "use_postprocess": bool(getattr(scene, 'trellis2_autorig_use_postprocess', False)),
+ "top_k": int(getattr(scene, 'trellis2_autorig_top_k', 5)),
+ "top_p": float(getattr(scene, 'trellis2_autorig_top_p', 0.95)),
+ "temperature": float(getattr(scene, 'trellis2_autorig_temperature', 1.0)),
+ "repetition_penalty": float(getattr(scene, 'trellis2_autorig_repetition_penalty', 2.0)),
+ "num_beams": int(getattr(scene, 'trellis2_autorig_num_beams', 10)),
+ "model_ckpt": getattr(scene, 'trellis2_autorig_model_ckpt', ''),
+ "hf_path": getattr(scene, 'trellis2_autorig_hf_path', ''),
+ },
+ }
+ NODES['autorig'] = rig_node_id
+ export_node_key = 'autorig'
+ print(f"[TRELLIS2] SkinTokens auto-rigging enabled via node {rig_node_id}")
+
# Save prompt for debugging
revision_dir = get_generation_dirs(context).get("revision", "")
if revision_dir:
@@ -877,6 +915,8 @@ class _Trellis2WorkflowMixin:
}
if not skip_bg:
NODE_PROGRESS[NODES['remove_bg']] = 5
+ if autorig_enabled:
+ NODE_PROGRESS[NODES['autorig']] = 99
# Wait for execution to complete via WebSocket
# Also capture 'executed' events which may contain the output path
@@ -896,6 +936,7 @@ class _Trellis2WorkflowMixin:
'simplify': 'Simplifying Mesh',
'export_trimesh': 'Exporting Mesh',
'export_glb': 'Exporting GLB',
+ 'autorig': 'Auto-Rigging',
}
# Build a lookup of known step counts so we can identify
diff --git a/stablegen/texturing/game_export.py b/stablegen/texturing/game_export.py
index 28b0649..e690faf 100644
--- a/stablegen/texturing/game_export.py
+++ b/stablegen/texturing/game_export.py
@@ -6,6 +6,95 @@ from ..utils import get_dir_path, sg_modal_active
_ADDON_PKG = __package__.rsplit('.', 1)[0]
+
+def _is_gltf_not_exported_helper(obj):
+ for collection in getattr(obj, "users_collection", []):
+ if "not_exported" in collection.name.lower():
+ return True
+ return False
+
+
+def _is_visible_export_object(obj):
+ if not obj or obj.hide_get() or _is_gltf_not_exported_helper(obj):
+ return False
+ try:
+ return obj.visible_get(view_layer=bpy.context.view_layer)
+ except TypeError:
+ try:
+ return obj.visible_get()
+ except Exception:
+ return True
+ except Exception:
+ return True
+
+
+def _armatures_for_mesh(mesh):
+ armatures = []
+ parent = mesh.parent
+ if parent and parent.type == 'ARMATURE':
+ armatures.append(parent)
+
+ for modifier in mesh.modifiers:
+ if modifier.type == 'ARMATURE' and modifier.object and modifier.object.type == 'ARMATURE':
+ armatures.append(modifier.object)
+
+ deduped = []
+ seen = set()
+ for armature in armatures:
+ if armature.name not in seen:
+ deduped.append(armature)
+ seen.add(armature.name)
+ return deduped
+
+
+def _mesh_uses_armature(mesh, armature):
+ if mesh.parent == armature:
+ return True
+ return any(
+ modifier.type == 'ARMATURE' and modifier.object == armature
+ for modifier in mesh.modifiers
+ )
+
+
+def _collect_game_export_objects(context, scope):
+ if scope == 'ALL_MESH':
+ meshes = [
+ obj for obj in context.view_layer.objects
+ if obj.type == 'MESH' and _is_visible_export_object(obj)
+ ]
+ selected_armatures = []
+ else:
+ selected = list(context.selected_objects)
+ meshes = [obj for obj in selected if obj.type == 'MESH']
+ selected_armatures = [
+ obj for obj in selected
+ if obj.type == 'ARMATURE' and _is_visible_export_object(obj)
+ ]
+
+ for armature in selected_armatures:
+ for obj in context.view_layer.objects:
+ if (
+ obj.type == 'MESH'
+ and _is_visible_export_object(obj)
+ and _mesh_uses_armature(obj, armature)
+ and obj not in meshes
+ ):
+ meshes.append(obj)
+
+ armatures = []
+ for mesh in meshes:
+ for armature in _armatures_for_mesh(mesh):
+ if _is_visible_export_object(armature) and armature not in armatures:
+ armatures.append(armature)
+
+ export_objects = []
+ for obj in [*armatures, *meshes]:
+ if obj not in export_objects:
+ export_objects.append(obj)
+
+ return meshes, armatures, export_objects
+
+
class ExportForGameEngine(bpy.types.Operator):
"""Export textured objects for game engines with PBR textures.
@@ -33,15 +122,15 @@ class ExportForGameEngine(bpy.types.Operator):
name="Objects",
description="Which objects to export",
items=[
- ('SELECTED', 'Selected', 'Export selected mesh objects'),
- ('ALL_MESH', 'All Mesh', 'Export all visible mesh objects'),
+ ('SELECTED', 'Selected', 'Export selected meshes and detected rigs'),
+ ('ALL_MESH', 'All Mesh', 'Export all visible meshes and detected rigs'),
],
default='SELECTED'
) # type: ignore
apply_transforms: bpy.props.BoolProperty(
name="Apply Transforms",
- description="Apply location, rotation and scale before export",
+ description="Apply location, rotation and scale before export. Skipped automatically for rigged exports",
default=True
) # type: ignore
@@ -77,20 +166,18 @@ class ExportForGameEngine(bpy.types.Operator):
original_selection = list(context.selected_objects)
original_active = context.view_layer.objects.active
- if self.export_scope == 'ALL_MESH':
- bpy.ops.object.select_all(action='DESELECT')
- meshes = [o for o in context.view_layer.objects
- if o.type == 'MESH' and not o.hide_get()]
- for o in meshes:
- o.select_set(True)
- if meshes:
- context.view_layer.objects.active = meshes[0]
- else:
- meshes = [o for o in context.selected_objects
- if o.type == 'MESH']
- if not meshes:
- self.report({'ERROR'}, "No mesh objects selected.")
- return {'CANCELLED'}
+ meshes, armatures, export_objects = _collect_game_export_objects(
+ context, self.export_scope)
+ if not meshes:
+ self.report({'ERROR'}, "No mesh objects to export.")
+ return {'CANCELLED'}
+
+ rigged_export = bool(armatures)
+
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in export_objects:
+ obj.select_set(True)
+ context.view_layer.objects.active = meshes[0]
# ── Output path ───────────────────────────────────────────────
output_base = get_dir_path(context, "baked")
@@ -108,7 +195,12 @@ class ExportForGameEngine(bpy.types.Operator):
filepath = os.path.join(output_base, f"{base_name}{ext}")
# ── Apply transforms if requested ─────────────────────────────
- if self.apply_transforms:
+ if self.apply_transforms and rigged_export:
+ print(
+ "[StableGen] Rigged export detected; skipping transform apply "
+ "to preserve armature bind pose"
+ )
+ elif self.apply_transforms:
for o in meshes:
o.select_set(True)
bpy.ops.object.transform_apply(
@@ -132,13 +224,21 @@ class ExportForGameEngine(bpy.types.Operator):
bpy.ops.export_scene.fbx(
filepath=filepath,
use_selection=True,
+ object_types={'MESH', 'ARMATURE'},
apply_scale_options='FBX_SCALE_ALL',
path_mode='COPY',
embed_textures=True,
+ add_leaf_bones=False,
bake_anim=self.export_animations,
+ bake_anim_use_all_bones=True,
mesh_smooth_type='FACE',
)
+ if rigged_export:
+ print(
+ f"[StableGen] Exported rigged asset with "
+ f"{len(meshes)} mesh object(s) and {len(armatures)} armature(s)"
+ )
self.report({'INFO'},
f"Exported to {filepath}")
except Exception as e:
@@ -159,4 +259,4 @@ class ExportForGameEngine(bpy.types.Operator):
except ReferenceError:
pass
- return {'FINISHED'}
\ No newline at end of file
+ return {'FINISHED'}
diff --git a/stablegen/texturing/rendering.py b/stablegen/texturing/rendering.py
index 8981601..9c1caae 100644
--- a/stablegen/texturing/rendering.py
+++ b/stablegen/texturing/rendering.py
@@ -1200,7 +1200,7 @@ def export_visibility(context, to_export, obj=None, camera_visibility=None, prep
mat = obj.active_material
if not mat:
return False
-
+
# Store original materials
original_materials[obj] = list(obj.data.materials)
original_active_material[obj] = obj.active_material # Store original active material
@@ -1457,7 +1457,7 @@ def export_visibility(context, to_export, obj=None, camera_visibility=None, prep
for mat in materials:
if mat != original_active_material[obj]:
obj.data.materials.append(mat)
-
+
# Clean up temporary materials
for _, temp_mat in temporary_materials.items():
if temp_mat and temp_mat.name in bpy.data.materials:
@@ -1586,6 +1586,281 @@ def prepare_baking(context):
# where we bake 5-7 channels of the same mesh back-to-back)
bpy.context.scene.render.use_persistent_data = True
+
+_SG_BAKE_UV_NAME = "BakeUV"
+_SG_PROJECTION_BUFFER_UV_NAME = "_SG_ProjectionBuffer"
+
+
+def _is_projection_or_buffer_uv(name):
+ return name.startswith("ProjectionUV") or name == _SG_PROJECTION_BUFFER_UV_NAME
+
+
+def _first_source_uv_name(obj):
+ if not obj or not obj.data.uv_layers:
+ return None
+ for uv in obj.data.uv_layers:
+ if uv.name != _SG_BAKE_UV_NAME and not _is_projection_or_buffer_uv(uv.name):
+ return uv.name
+ return None
+
+
+def _ensure_bake_uv(obj):
+ uv_layers = obj.data.uv_layers
+ bake_uv = uv_layers.get(_SG_BAKE_UV_NAME)
+ if bake_uv is None:
+ source_uv = _first_source_uv_name(obj)
+ if source_uv and uv_layers.get(source_uv):
+ uv_layers.active = uv_layers[source_uv]
+ bake_uv = uv_layers.new(name=_SG_BAKE_UV_NAME)
+ print(f"[StableGen] Created {_SG_BAKE_UV_NAME} on {obj.name}")
+ uv_layers.active = bake_uv
+ return bake_uv
+
+
+def _set_bake_uv_active(obj):
+ uv_layers = obj.data.uv_layers
+ bake_uv = uv_layers.get(_SG_BAKE_UV_NAME)
+ if bake_uv:
+ uv_layers.active = bake_uv
+ return bake_uv
+ for uv in uv_layers:
+ if not _is_projection_or_buffer_uv(uv.name):
+ uv_layers.active = uv
+ return uv
+ return _ensure_bake_uv(obj)
+
+
+def _pin_unlinked_image_textures_to_uv(nodes, links, uv_name, exclude_nodes=()):
+ if not uv_name:
+ return None
+
+ exclude_nodes = set(exclude_nodes or ())
+ image_nodes = [
+ node for node in nodes
+ if node.type == 'TEX_IMAGE'
+ and node not in exclude_nodes
+ and node.inputs.get("Vector")
+ and not node.inputs["Vector"].links
+ ]
+ if not image_nodes:
+ return None
+
+ uv_node = nodes.new("ShaderNodeUVMap")
+ uv_node.uv_map = uv_name
+ uv_node.label = f"StableGen Source UV ({uv_name})"
+ for image_node in image_nodes:
+ links.new(uv_node.outputs["UV"], image_node.inputs["Vector"])
+ print(
+ f"[StableGen] Pinned {len(image_nodes)} source image texture node(s) "
+ f"to UV map '{uv_name}' for baking"
+ )
+ return uv_node
+
+
+def _copy_material_slots_for_bake(obj):
+ original_materials = list(obj.data.materials)
+ original_active_index = obj.active_material_index
+ copied_materials = []
+
+ obj.data.materials.clear()
+ for mat in original_materials:
+ if mat:
+ mat_copy = mat.copy()
+ obj.data.materials.append(mat_copy)
+ copied_materials.append(mat_copy)
+
+ return original_materials, original_active_index, copied_materials
+
+
+def _restore_material_slots_after_bake(obj, original_materials, original_active_index, copied_materials):
+ obj.data.materials.clear()
+ for mat in original_materials:
+ if mat:
+ obj.data.materials.append(mat)
+ if obj.data.materials:
+ obj.active_material_index = min(max(0, original_active_index), len(obj.data.materials) - 1)
+ for mat in copied_materials:
+ if mat and mat.name in bpy.data.materials:
+ bpy.data.materials.remove(mat)
+
+
+def _first_principled_node(mat):
+ if not mat or not mat.use_nodes:
+ return None
+ for node in mat.node_tree.nodes:
+ if node.type == 'BSDF_PRINCIPLED':
+ return node
+ return None
+
+
+def _socket_gray_rgba(socket, fallback=0.0):
+ try:
+ value = socket.default_value
+ except Exception:
+ value = fallback
+ if isinstance(value, (float, int)):
+ scalar = float(value)
+ else:
+ scalar = float(value[0]) if value else float(fallback)
+ return (scalar, scalar, scalar, 1.0)
+
+
+def _pbr_channel_default_rgba(mat, channel_name):
+ principled = _first_principled_node(mat)
+ if channel_name == 'base_color':
+ if principled and principled.inputs.get("Base Color"):
+ return tuple(principled.inputs["Base Color"].default_value)
+ return (1.0, 1.0, 1.0, 1.0)
+ if channel_name == 'roughness':
+ if principled and principled.inputs.get("Roughness"):
+ return _socket_gray_rgba(principled.inputs["Roughness"], 0.5)
+ return (0.5, 0.5, 0.5, 1.0)
+ if channel_name == 'metallic':
+ if principled and principled.inputs.get("Metallic"):
+ return _socket_gray_rgba(principled.inputs["Metallic"], 0.0)
+ return (0.0, 0.0, 0.0, 1.0)
+ if channel_name == 'ao':
+ return (1.0, 1.0, 1.0, 1.0)
+ return (0.0, 0.0, 0.0, 1.0)
+
+
+def _is_bone_custom_shape_object(obj):
+ for armature in bpy.context.view_layer.objects:
+ if armature.type != 'ARMATURE' or not armature.pose:
+ continue
+ for bone in armature.pose.bones:
+ if bone.custom_shape == obj:
+ return True
+ return False
+
+
+def _is_gltf_not_exported_helper(obj):
+ for collection in getattr(obj, "users_collection", []):
+ if "not_exported" in collection.name.lower():
+ return True
+ return False
+
+
+def _iter_collection_objects_recursive(collection):
+ seen = set()
+
+ def visit(col):
+ for obj in col.objects:
+ if obj.name in seen:
+ continue
+ seen.add(obj.name)
+ yield obj
+ for child in col.children:
+ yield from visit(child)
+
+ yield from visit(collection)
+
+
+def _disable_not_exported_render_helpers(context=None):
+ """Prevent glTF helper/control geometry from occluding texture bakes."""
+ disabled_objects = 0
+ disabled_collections = 0
+
+ for collection in bpy.data.collections:
+ if "not_exported" not in collection.name.lower():
+ continue
+
+ if not collection.hide_render:
+ collection.hide_render = True
+ disabled_collections += 1
+
+ for obj in _iter_collection_objects_recursive(collection):
+ if not obj.hide_render:
+ obj.hide_render = True
+ disabled_objects += 1
+
+ if disabled_objects or disabled_collections:
+ print(
+ "[StableGen] Disabled render visibility for "
+ f"{disabled_objects} glTF not_exported helper object(s) "
+ f"across {disabled_collections} collection(s)."
+ )
+
+ if context:
+ context.view_layer.update()
+
+
+def _object_visible_in_view_layer(obj):
+ if obj.hide_get():
+ return False
+ try:
+ return obj.visible_get(view_layer=bpy.context.view_layer)
+ except TypeError:
+ try:
+ return obj.visible_get()
+ except Exception:
+ return True
+ except Exception:
+ return True
+
+
+def _is_texture_bake_target(obj):
+ if obj is None or obj.type != 'MESH':
+ return False
+ if not _object_visible_in_view_layer(obj) or obj.hide_render:
+ return False
+ if _is_gltf_not_exported_helper(obj):
+ return False
+ if _is_bone_custom_shape_object(obj):
+ return False
+ if not obj.data or not obj.data.polygons:
+ return False
+ return any(mat for mat in obj.data.materials)
+
+
+def _texture_bake_targets(objects):
+ targets = []
+ skipped = []
+ for obj in objects:
+ if _is_texture_bake_target(obj):
+ targets.append(obj)
+ elif obj and obj.type == 'MESH':
+ skipped.append(obj)
+ return targets, skipped
+
+
+def _activate_mesh_for_object_ops(obj):
+ """Select and activate a mesh object for bpy.ops calls."""
+ if obj is None or obj.type != 'MESH':
+ raise RuntimeError("Bake target is not a mesh object")
+
+ if not any(view_obj == obj for view_obj in bpy.context.view_layer.objects):
+ raise RuntimeError(
+ f"Bake target '{getattr(obj, 'name', '<unknown>')}' is not in the active view layer"
+ )
+
+ if obj.hide_get():
+ obj.hide_set(False)
+ if obj.hide_select:
+ obj.hide_select = False
+
+ try:
+ if bpy.ops.object.mode_set.poll():
+ bpy.ops.object.mode_set(mode='OBJECT')
+ except Exception:
+ pass
+
+ bpy.ops.object.select_all(action='DESELECT')
+ bpy.context.view_layer.objects.active = obj
+ obj.select_set(True)
+ bpy.context.view_layer.update()
+
+ if bpy.context.view_layer.objects.active != obj:
+ raise RuntimeError(f"Could not activate bake target '{obj.name}'")
+
+ return {
+ 'active_object': obj,
+ 'object': obj,
+ 'selected_objects': [obj],
+ 'selected_editable_objects': [obj],
+ }
+
+
def unwrap(obj, method, overlap_only):
"""
Unwraps the UVs of the given object using the selected method.
@@ -1595,85 +1870,67 @@ def unwrap(obj, method, overlap_only):
if method == 'none':
return
- # Deselect all objects
- bpy.ops.object.select_all(action='DESELECT')
- # Set object as active and select it
- bpy.context.view_layer.objects.active = obj
- obj.select_set(True)
-
- is_new = False
-
- # If all UV maps are ProjectionUV or buffer, add new one
- if all(["ProjectionUV" in uv.name or uv.name == "_SG_ProjectionBuffer" for uv in obj.data.uv_layers]):
- # Add a new UV map
- obj.data.uv_layers.new(name=f"BakeUV")
- is_new = True
- obj.data.uv_layers.active_index = len(obj.data.uv_layers) - 1
- # Set it for rendering
- obj.data.uv_layers.active = obj.data.uv_layers[-1]
- else:
- # Ensure the active UV is a non-ProjectionUV/buffer map
- for uv_layer in obj.data.uv_layers:
- if "ProjectionUV" not in uv_layer.name and uv_layer.name != "_SG_ProjectionBuffer":
- obj.data.uv_layers.active = uv_layer
- break
-
- bpy.ops.object.mode_set(mode='EDIT')
-
- # Ensure UV selection sync is OFF
- bpy.context.scene.tool_settings.use_uv_select_sync = False
-
- if overlap_only:
- # Deselect
- bpy.ops.uv.select_all(action='DESELECT')
- # Check if the object has overlapping UVs, if not, skip unwrapping
- bpy.ops.uv.select_overlap()
- # Get a BMesh representation of the mesh
- bm = bmesh.from_edit_mesh(obj.data)
- bm.faces.ensure_lookup_table()
- # Use the active UV layer (this is a BMUVLayer)
- uv_layer = bm.loops.layers.uv.active
- uv_layer = bm.loops.layers.uv.active
- if not uv_layer:
- bpy.ops.object.mode_set(mode='OBJECT')
- return
+ op_context = _activate_mesh_for_object_ops(obj)
+
+ _ensure_bake_uv(obj)
+
+ with bpy.context.temp_override(**op_context):
+ bpy.ops.object.mode_set(mode='EDIT')
+
+ # Ensure UV selection sync is OFF
+ bpy.context.scene.tool_settings.use_uv_select_sync = False
+
+ if overlap_only:
+ # Deselect
+ bpy.ops.uv.select_all(action='DESELECT')
+ # Check if the object has overlapping UVs, if not, skip unwrapping
+ bpy.ops.uv.select_overlap()
+ # Get a BMesh representation of the mesh
+ bm = bmesh.from_edit_mesh(obj.data)
+ bm.faces.ensure_lookup_table()
+ # Use the active UV layer (this is a BMUVLayer)
+ uv_layer = bm.loops.layers.uv.active
+ uv_layer = bm.loops.layers.uv.active
+ if not uv_layer:
+ bpy.ops.object.mode_set(mode='OBJECT')
+ return
+
+ # Check for ANY selected UV elements
+ if bpy.app.version >= (5, 0, 0):
+ # Blender 5.0+ uses loop.uv_select_vert
+ has_overlap = any(
+ loop.uv_select_vert
+ for face in bm.faces
+ for loop in face.loops
+ )
+ else:
+ has_overlap = any(
+ loop[uv_layer].select
+ for face in bm.faces
+ for loop in face.loops
+ )
+
+ if not has_overlap:
+ bpy.ops.object.mode_set(mode='OBJECT')
+ return
+
+ bpy.context.scene.tool_settings.use_uv_select_sync = True
+ # Select all faces
+ bpy.ops.mesh.select_all(action='SELECT')
- # Check for ANY selected UV elements
- if bpy.app.version >= (5, 0, 0):
- # Blender 5.0+ uses loop.uv_select_vert
- has_overlap = any(
- loop.uv_select_vert
- for face in bm.faces
- for loop in face.loops
- )
- else:
- has_overlap = any(
- loop[uv_layer].select
- for face in bm.faces
- for loop in face.loops
- )
+ if method == 'basic':
+ bpy.ops.uv.unwrap(method='ANGLE_BASED', margin=0.001)
+ elif method == 'smart':
+ bpy.ops.uv.smart_project()
+ elif method == 'cube':
+ bpy.ops.uv.cube_project(cube_size=1.0)
+ elif method == 'lightmap':
+ bpy.ops.uv.lightmap_pack()
+ elif method == 'pack':
+ bpy.ops.uv.pack_islands()
- if not has_overlap:
- bpy.ops.object.mode_set(mode='OBJECT')
- return
-
- bpy.context.scene.tool_settings.use_uv_select_sync = True
- # Select all faces
- bpy.ops.mesh.select_all(action='SELECT')
-
- if method == 'basic':
- bpy.ops.uv.unwrap(method='ANGLE_BASED', margin=0.001)
- elif method == 'smart':
- bpy.ops.uv.smart_project()
- elif method == 'cube':
- bpy.ops.uv.cube_project(cube_size=1.0)
- elif method == 'lightmap':
- bpy.ops.uv.lightmap_pack()
- elif method == 'pack':
- bpy.ops.uv.pack_islands()
-
- bpy.ops.uv.select_all(action='DESELECT')
- bpy.ops.object.mode_set(mode='OBJECT')
+ bpy.ops.uv.select_all(action='DESELECT')
+ bpy.ops.object.mode_set(mode='OBJECT')
def bake_texture(context, obj, texture_resolution, suffix = "", view_transform = 'Standard', output_dir = None):
"""
@@ -1682,6 +1939,10 @@ def bake_texture(context, obj, texture_resolution, suffix = "", view_transform =
:param obj: Blender object.
:return: True if successful, False otherwise.
"""
+ _disable_not_exported_render_helpers(context)
+ if _is_gltf_not_exported_helper(obj) or _is_bone_custom_shape_object(obj):
+ print(f"[StableGen] Skipping helper mesh during bake: {obj.name}")
+ return False
bpy.context.scene.display_settings.display_device = 'sRGB'
# Backup original view transform
original_view_transform = bpy.context.scene.view_settings.view_transform
@@ -1692,79 +1953,74 @@ def bake_texture(context, obj, texture_resolution, suffix = "", view_transform =
# Set the object to be rendered
obj.hide_render = False
+ source_uv_name = _first_source_uv_name(obj)
+ _set_bake_uv_active(obj)
+
# Create a new image for baking
image_name = f"{obj.name}_baked{suffix}"
image = bpy.data.images.new(name=image_name, width=texture_resolution, height=texture_resolution)
- # Create a new texture node in the object's material
if not obj.data.materials:
- # Cancel
print("[StableGen] No materials found")
return False
- else:
- # Temporarily remove all original materials
- mat = obj.active_material.copy()
- original_materials = list(obj.data.materials)
- original_active_material = obj.active_material
- obj.data.materials.clear()
- obj.data.materials.append(mat)
- mat.use_nodes = True
- nodes = mat.node_tree.nodes
- links = mat.node_tree.links
+ original_materials, original_active_index, copied_materials = _copy_material_slots_for_bake(obj)
+ if not copied_materials:
+ _restore_material_slots_after_bake(
+ obj, original_materials, original_active_index, copied_materials)
+ print("[StableGen] No material slots found")
+ return False
- tex_image = nodes.new("ShaderNodeTexImage")
- tex_image.image = image
- tex_image.location = (0, 0)
+ op_context = _activate_mesh_for_object_ops(obj)
+ _set_bake_uv_active(obj)
- # Deselect all objects
- bpy.ops.object.select_all(action='DESELECT')
- # Set object as active and select it
- bpy.context.view_layer.objects.active = obj
- obj.select_set(True)
- # Set the image node as the active bake target
- nodes.active = tex_image
-
- # Ensure the active UV is a non-ProjectionUV/buffer map for correct baking
- for uv_layer in obj.data.uv_layers:
- if "ProjectionUV" not in uv_layer.name and uv_layer.name != "_SG_ProjectionBuffer":
- obj.data.uv_layers.active = uv_layer
- break
+ for mat in copied_materials:
+ mat.use_nodes = True
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ tex_image = nodes.new("ShaderNodeTexImage")
+ tex_image.image = image
+ tex_image.location = (0, 0)
+ nodes.active = tex_image
+
+ _pin_unlinked_image_textures_to_uv(
+ nodes, links, source_uv_name, exclude_nodes={tex_image})
- # Check if there is a BSDF node before the output node
- output = None
- had_bsdf = False
- for node in nodes:
- if node.type == 'OUTPUT_MATERIAL':
- output = node
- break
-
- before_output = output.inputs[0].links[0].from_node if output and output.inputs[0].is_linked else None
- if before_output and before_output.type != 'BSDF_PRINCIPLED':
- bsdf_node = nodes.new("ShaderNodeBsdfPrincipled")
- bsdf_node.location = (before_output.location[0] + 200, before_output.location[1])
- # Connect the before output node to the BSDF node
- links.new(before_output.outputs[0], bsdf_node.inputs[0])
- # Connect the BSDF node to the output node
- links.new(bsdf_node.outputs[0], output.inputs[0])
-
- # Disconnect Metallic so the DIFFUSE color-only pass returns the
- # true Base Color. Principled BSDF multiplies diffuse by
- # (1 - Metallic), which would dim metallic areas incorrectly.
- for node in nodes:
- if node.type == 'BSDF_PRINCIPLED':
- met = node.inputs.get("Metallic")
- if met:
- for link in list(met.links):
- links.remove(link)
- met.default_value = 0.0
- break
+ # Check if there is a BSDF node before the output node
+ output = None
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output = node
+ break
+
+ before_output = output.inputs[0].links[0].from_node if output and output.inputs[0].is_linked else None
+ if before_output and before_output.type != 'BSDF_PRINCIPLED':
+ bsdf_node = nodes.new("ShaderNodeBsdfPrincipled")
+ bsdf_node.location = (before_output.location[0] + 200, before_output.location[1])
+ # Connect the before output node to the BSDF node
+ links.new(before_output.outputs[0], bsdf_node.inputs[0])
+ # Connect the BSDF node to the output node
+ links.new(bsdf_node.outputs[0], output.inputs[0])
+
+ # Disconnect Metallic so the DIFFUSE color-only pass returns the
+ # true Base Color. Principled BSDF multiplies diffuse by
+ # (1 - Metallic), which would dim metallic areas incorrectly.
+ for node in nodes:
+ if node.type == 'BSDF_PRINCIPLED':
+ met = node.inputs.get("Metallic")
+ if met:
+ for link in list(met.links):
+ links.remove(link)
+ met.default_value = 0.0
+ break
# Bake the texture
# Ensure OBJECT mode — bake fails silently in EDIT mode
- if bpy.context.mode != 'OBJECT':
- bpy.ops.object.mode_set(mode='OBJECT')
- bpy.ops.object.bake(type='DIFFUSE', width=texture_resolution, height=texture_resolution)
+ with bpy.context.temp_override(**op_context):
+ if bpy.context.mode != 'OBJECT':
+ bpy.ops.object.mode_set(mode='OBJECT')
+ bpy.ops.object.bake(type='DIFFUSE', width=texture_resolution, height=texture_resolution)
# Save the image if required
if not os.path.exists(output_dir):
@@ -1784,17 +2040,8 @@ def bake_texture(context, obj, texture_resolution, suffix = "", view_transform =
# Restore original render settings
obj.hide_render = original_render
- # Restore original materials
- obj.data.materials.clear()
- # First append the original active material
- if original_active_material:
- obj.data.materials.append(original_active_material)
- for mat_item in original_materials:
- if mat_item != original_active_material:
- obj.data.materials.append(mat_item)
-
- # Remove the temporary material copy (prevent leak)
- bpy.data.materials.remove(mat)
+ _restore_material_slots_after_bake(
+ obj, original_materials, original_active_index, copied_materials)
return True
@@ -1803,18 +2050,14 @@ def _has_non_projection_uv(obj):
for baking (not a StableGen projection UV or buffer)."""
if not obj or obj.type != 'MESH':
return False
- if not obj.data.uv_layers:
- return False
- for uv in obj.data.uv_layers:
- if (not uv.name.startswith("ProjectionUV")
- and uv.name != "_SG_ProjectionBuffer"):
- return True
- return False
+ return obj.data.uv_layers.get(_SG_BAKE_UV_NAME) is not None
def _uvs_likely_overlap(obj, uv_name=None):
me = obj.data
if not me.uv_layers:
return False
+ if uv_name is None:
+ uv_name = _SG_BAKE_UV_NAME
uv_layer = me.uv_layers.get(uv_name) if uv_name else me.uv_layers.active
if not uv_layer:
return False
@@ -1967,14 +2210,14 @@ def _has_pbr_channels(mat):
return any(ch in sources for ch in ('roughness', 'metallic', 'normal', 'emission', 'height', 'ao'))
-def _restore_materials(obj, original_materials, original_active):
- """Restore an object's material slots back to their original state."""
- obj.data.materials.clear()
- if original_active:
- obj.data.materials.append(original_active)
- for m in original_materials:
- if m != original_active:
- obj.data.materials.append(m)
+def _object_pbr_channels(obj):
+ channels = []
+ for mat in obj.data.materials:
+ if mat and _has_pbr_channels(mat):
+ for channel in _find_pbr_bake_sources(mat):
+ if channel not in channels:
+ channels.append(channel)
+ return channels
def bake_pbr_channel(context, obj, channel_name, texture_resolution, output_dir):
@@ -1990,11 +2233,18 @@ def bake_pbr_channel(context, obj, channel_name, texture_resolution, output_dir)
if not obj.data.materials or not obj.active_material:
return None
+ _disable_not_exported_render_helpers(context)
+ if _is_gltf_not_exported_helper(obj) or _is_bone_custom_shape_object(obj):
+ print(f"[StableGen] Skipping helper mesh during PBR bake: {obj.name}")
+ return None
+
# ── Save state ─────────────────────────────────────────────────
orig_vt = context.scene.view_settings.view_transform
orig_render = obj.hide_render
context.scene.view_settings.view_transform = 'Standard'
obj.hide_render = False
+ source_uv_name = _first_source_uv_name(obj)
+ _set_bake_uv_active(obj)
# ── Copy material onto the object ──────────────────────────────
original_materials = list(obj.data.materials)
@@ -2082,27 +2332,25 @@ def bake_pbr_channel(context, obj, channel_name, texture_resolution, output_dir)
nodes.active = tex_node
# ── Select object + non-projection UV ──────────────────────────
- bpy.ops.object.select_all(action='DESELECT')
- context.view_layer.objects.active = obj
- obj.select_set(True)
- for uv in obj.data.uv_layers:
- if "ProjectionUV" not in uv.name and uv.name != "_SG_ProjectionBuffer":
- obj.data.uv_layers.active = uv
- break
+ op_context = _activate_mesh_for_object_ops(obj)
+ _pin_unlinked_image_textures_to_uv(
+ nodes, links, source_uv_name, exclude_nodes={tex_node})
+ _set_bake_uv_active(obj)
# ── Bake ─────────────────────────────────────────────────────
# Ensure OBJECT mode — bake fails silently in EDIT mode
- if bpy.context.mode != 'OBJECT':
- bpy.ops.object.mode_set(mode='OBJECT')
- if bake_type == 'NORMAL':
- bpy.ops.object.bake(type='NORMAL',
- normal_space='TANGENT',
- width=texture_resolution,
- height=texture_resolution)
- else:
- bpy.ops.object.bake(type='EMIT',
- width=texture_resolution,
- height=texture_resolution)
+ with bpy.context.temp_override(**op_context):
+ if bpy.context.mode != 'OBJECT':
+ bpy.ops.object.mode_set(mode='OBJECT')
+ if bake_type == 'NORMAL':
+ bpy.ops.object.bake(type='NORMAL',
+ normal_space='TANGENT',
+ width=texture_resolution,
+ height=texture_resolution)
+ else:
+ bpy.ops.object.bake(type='EMIT',
+ width=texture_resolution,
+ height=texture_resolution)
# ── Save ───────────────────────────────────────────────────────
os.makedirs(output_dir, exist_ok=True)
@@ -2323,9 +2571,20 @@ class BakeTextures(bpy.types.Operator):
:return: {'RUNNING_MODAL'}
"""
if context.scene.texture_objects == 'all':
- self._objects = [obj for obj in context.view_layer.objects if obj.type == 'MESH' and not obj.hide_get()]
+ candidates = [obj for obj in context.view_layer.objects if obj.type == 'MESH']
else: # 'selected'
- self._objects = [obj for obj in context.selected_objects if obj.type == 'MESH']
+ candidates = [obj for obj in context.selected_objects if obj.type == 'MESH']
+ self._objects, skipped = _texture_bake_targets(candidates)
+ if skipped:
+ names = ", ".join(obj.name for obj in skipped[:5])
+ more = "" if len(skipped) <= 5 else f", +{len(skipped) - 5} more"
+ print(
+ f"[StableGen] Skipping {len(skipped)} non-texture helper mesh(es) "
+ f"during bake: {names}{more}"
+ )
+ if not self._objects:
+ self.report({'ERROR'}, "No textureable mesh objects found to bake.")
+ return {'CANCELLED'}
self._current_index = 0
self._phase = 'unwrap'
self._total_objects = len(self._objects)
@@ -2339,6 +2598,19 @@ class BakeTextures(bpy.types.Operator):
:return: {'RUNNING_MODAL'}
"""
+ _disable_not_exported_render_helpers(context)
+ self._objects, skipped = _texture_bake_targets(self._objects)
+ if skipped:
+ names = ", ".join(obj.name for obj in skipped[:5])
+ more = "" if len(skipped) <= 5 else f", +{len(skipped) - 5} more"
+ print(
+ f"[StableGen] Skipping {len(skipped)} non-texture helper mesh(es) "
+ f"during bake: {names}{more}"
+ )
+ if not self._objects:
+ self.report({'ERROR'}, "No textureable mesh objects found to bake.")
+ return {'CANCELLED'}
+
self.original_engine = bpy.context.scene.render.engine
if bpy.context.space_data and hasattr(bpy.context.space_data, 'shading'):
self.original_shading = bpy.context.space_data.shading.type
diff --git a/stablegen/ui/panel.py b/stablegen/ui/panel.py
index 94597be..a560e48 100644
--- a/stablegen/ui/panel.py
+++ b/stablegen/ui/panel.py
@@ -738,6 +738,16 @@ class StableGenPanel(bpy.types.Panel):
split.label(text="Texture Mode:")
split.prop(scene, "trellis2_texture_mode", text="")
+ split = params_container.split(factor=0.5)
+ split.label(text="Auto-Rig:")
+ rig_row = split.row(align=True)
+ rig_available = getattr(scene, 'skintokens_autorig_available', False)
+ rig_toggle = rig_row.row(align=True)
+ rig_toggle.enabled = rig_available
+ rig_toggle.prop(scene, "trellis2_autorig_enabled", text="", toggle=True, icon="ARMATURE_DATA")
+ if not rig_available:
+ rig_row.operator("stablegen.check_server_status", text="", icon="FILE_REFRESH")
+
# Prompt + native/none: show initial-image architecture & checkpoint
_prompt_needs_initial = (
scene.trellis2_generate_from == 'prompt'
@@ -924,6 +934,41 @@ class StableGenPanel(bpy.types.Panel):
split.prop(scene, "trellis2_shade_mode", text="")
content_box.separator()
+ # --- TRELLIS.2: Auto-Rigging Settings ---
+ if is_trellis2:
+ content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_autorig_settings", "Auto-Rigging (SkinTokens)", icon="ARMATURE_DATA")
+ if content_box:
+ rig_available = getattr(scene, 'skintokens_autorig_available', False)
+ row = content_box.row(align=True)
+ toggle_row = row.row(align=True)
+ toggle_row.enabled = rig_available
+ toggle_row.prop(scene, "trellis2_autorig_enabled", text="Auto-Rig", toggle=True, icon="ARMATURE_DATA")
+ if not rig_available:
+ row.operator("stablegen.check_server_status", text="", icon="FILE_REFRESH")
+
+ if rig_available and getattr(scene, 'trellis2_autorig_enabled', False):
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_autorig_use_transfer", text="Transfer Original", toggle=True)
+ row.prop(scene, "trellis2_autorig_use_postprocess", text="Skin Postprocess", toggle=True)
+ row = content_box.row()
+ row.prop(scene, "trellis2_autorig_use_skeleton", text="Use Existing Skeleton", toggle=True)
+
+ content_box.separator()
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_autorig_top_k", text="Top K")
+ row.prop(scene, "trellis2_autorig_top_p", text="Top P")
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_autorig_temperature", text="Temperature")
+ row.prop(scene, "trellis2_autorig_repetition_penalty", text="Repeat Penalty")
+ row = content_box.row()
+ row.prop(scene, "trellis2_autorig_num_beams", text="Beams")
+
+ content_box.separator()
+ row = content_box.row()
+ row.prop(scene, "trellis2_autorig_model_ckpt", text="Model Checkpoint")
+ row = content_box.row()
+ row.prop(scene, "trellis2_autorig_hf_path", text="HF Path")
+
# --- TRELLIS.2: Native Texture Settings ---
if is_trellis2 and trellis2_tex_mode == 'native':
content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_texture_settings", "Texture Settings (TRELLIS.2 Native)", icon="TEXTURE")
diff --git a/stablegen/ui/presets.py b/stablegen/ui/presets.py
index aa90ce8..df5ca47 100644
--- a/stablegen/ui/presets.py
+++ b/stablegen/ui/presets.py
@@ -163,6 +163,17 @@ GEN_PARAMETERS = [
"trellis2_decimate_method",
"trellis2_remesh_method",
"trellis2_post_processing_enabled",
+ "trellis2_autorig_enabled",
+ "trellis2_autorig_use_transfer",
+ "trellis2_autorig_use_skeleton",
+ "trellis2_autorig_use_postprocess",
+ "trellis2_autorig_top_k",
+ "trellis2_autorig_top_p",
+ "trellis2_autorig_temperature",
+ "trellis2_autorig_repetition_penalty",
+ "trellis2_autorig_num_beams",
+ "trellis2_autorig_model_ckpt",
+ "trellis2_autorig_hf_path",
"trellis2_bg_removal",
"trellis2_background_color",