Skip to content

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

describe objects for the views that see them and open a view full size

sakalond7c8744dd0110
diff --git a/stablegen/cameras/overlays.py b/stablegen/cameras/overlays.py
index 086bf71..17cec02 100644
--- a/stablegen/cameras/overlays.py
+++ b/stablegen/cameras/overlays.py
@@ -420,6 +420,9 @@ def _sg_draw_view_labels():
context = _draw_context()
if context is None or context.region_data.view_perspective == 'CAMERA':
return
+ from ..ui.studio import preview_open
+ if preview_open(context.window, context.area): # a camera's full-size view covers this viewport
+ return
settings = context.scene.sg_camera_display
if settings.labels == 'NONE':
return
diff --git a/stablegen/cameras/visibility.py b/stablegen/cameras/visibility.py
new file mode 100644
index 0000000..d8d4dbe
--- /dev/null
+++ b/stablegen/cameras/visibility.py
@@ -0,0 +1,189 @@
+"""Which objects each camera sees, for object prompts.
+
+A grid of rays through the camera's own frame (its per-camera resolution, shift and sensor fit) counts the object
+each ray hits first; an object's share is its fraction of the rays. A view's prompt then carries the prompts of the
+described objects it sees, largest first. Sampling runs from a timer for the panel, so drawing never waits for it,
+and synchronously when a generation builds its view plan.
+"""
+import math
+
+import bpy # pylint: disable=import-error
+import numpy as np
+from mathutils import Vector # pylint: disable=import-error
+
+from .geometry import _get_camera_resolution
+
+RAYS = 6000 # per view: about 0.07 s on a 2M-triangle scene, an object from 0.2 % of the frame is 12 rays
+DETAIL_RAYS = 40000 # the open view's outline of one object
+MIN_SHARE = 0.002 # an object counts as seen from 0.2 % of the frame
+# The reference categorical palette's dark steps in their fixed order (validated on the card colour, adjacent
+# pairs); the described objects take them by how much of the views they fill.
+COLORS = ('#3987e5', '#d95926', '#199e70', '#c98500', '#d55181', '#008300', '#9085e9')
+
+_cache = {} # (scene, camera, rays) -> (key, grid)
+_pending = [] # (scene name, camera name, rays) waiting for the timer
+_timer = None
+
+
+class Grid:
+ """One camera's sample: ``labels`` (rows bottom first) index ``names``; -1 where the ray hits nothing."""
+
+ def __init__(self, names, labels):
+ self.names, self.labels = names, labels
+ counts = np.bincount(labels[labels >= 0].ravel(), minlength=len(names)) if len(names) else np.zeros(0)
+ self.shares = sorted(((names[i], float(counts[i] / labels.size)) for i in range(len(names)) if counts[i]),
+ key=lambda item: -item[1])
+
+ def mask(self, name):
+ return self.labels == self.names.index(name) if name in self.names else np.zeros(self.labels.shape, bool)
+
+
+def _camera_key(scene, camera):
+ data = camera.data
+ return (tuple(v for row in camera.matrix_world for v in row), data.type, data.lens, data.ortho_scale,
+ data.shift_x, data.shift_y, data.sensor_fit, data.sensor_width, data.sensor_height,
+ _get_camera_resolution(camera, scene))
+
+
+def _scene_key(scene):
+ """What else changes what a camera sees: the meshes, where they are and whether they show."""
+ return hash(tuple((obj.name, obj.visible_get(), tuple(v for row in obj.matrix_world for v in row))
+ for obj in scene.objects if obj.type == 'MESH'))
+
+
+def sample(scene, depsgraph, camera, rays=RAYS):
+ """Cast about *rays* rays through the camera's frame; returns a Grid."""
+ rx, ry = _get_camera_resolution(camera, scene)
+ aspect = rx / max(1, ry)
+ columns = max(8, round(math.sqrt(rays * aspect)))
+ rows = max(8, round(rays / columns))
+ inverse = camera.calc_matrix_camera(depsgraph, x=rx, y=ry, scale_x=scene.render.pixel_aspect_x,
+ scale_y=scene.render.pixel_aspect_y).inverted_safe()
+ world = camera.matrix_world
+ ortho = camera.data.type == 'ORTHO'
+ forward = (world.to_3x3() @ Vector((0, 0, -1))).normalized()
+ origin = world.translation
+ names, index = [], {}
+ labels = np.full((rows, columns), -1, np.int32)
+ for row in range(rows):
+ for column in range(columns):
+ near = inverse @ Vector(((column + .5) / columns * 2 - 1, (row + .5) / rows * 2 - 1, -1, 1))
+ point = world @ (near.xyz / near.w)
+ start, direction = (point, forward) if ortho else (origin, (point - origin).normalized())
+ hit, _location, _normal, _face, obj, _matrix = scene.ray_cast(depsgraph, start, direction)
+ if not hit or obj is None:
+ continue
+ name = getattr(obj, 'original', obj).name
+ if name not in index:
+ index[name] = len(names)
+ names.append(name)
+ labels[row, column] = index[name]
+ return Grid(names, labels)
+
+
+def cached(scene, camera, rays=RAYS, scene_key=None):
+ """The camera's Grid if it is current, else None."""
+ entry = _cache.get((scene.name, camera.name, rays))
+ key = (_camera_key(scene, camera), scene_key if scene_key is not None else _scene_key(scene))
+ return entry[1] if entry and entry[0] == key else None
+
+
+def detect(context, cameras, rays=RAYS):
+ """Sample every camera whose result is out of date, now; returns {camera name: Grid}."""
+ scene = context.scene
+ depsgraph = context.evaluated_depsgraph_get()
+ scene_key = _scene_key(scene)
+ result = {}
+ for camera in cameras:
+ grid = cached(scene, camera, rays, scene_key)
+ if grid is None:
+ grid = sample(scene, depsgraph, camera, rays)
+ _cache[(scene.name, camera.name, rays)] = ((_camera_key(scene, camera), scene_key), grid)
+ result[camera.name] = grid
+ return result
+
+
+def request(scene, cameras, rays=RAYS):
+ """Cached Grids now, the rest sampled by a timer that redraws the 3D views when they are ready."""
+ global _timer
+ scene_key = _scene_key(scene)
+ result = {}
+ for camera in cameras:
+ grid = cached(scene, camera, rays, scene_key)
+ if grid is not None:
+ result[camera.name] = grid
+ elif (scene.name, camera.name, rays) not in _pending:
+ _pending.append((scene.name, camera.name, rays))
+ if _pending and _timer is None:
+ _timer = _tick
+ bpy.app.timers.register(_tick, first_interval=0.05)
+ return result
+
+
+def _tick():
+ """Sample pending cameras for up to ~0.15 s per call, so the interface stays responsive."""
+ global _timer
+ import time
+ deadline = time.perf_counter() + 0.15
+ context = bpy.context
+ try:
+ while _pending and time.perf_counter() < deadline:
+ scene_name, camera_name, rays = _pending.pop(0)
+ scene, camera = bpy.data.scenes.get(scene_name), bpy.data.objects.get(camera_name)
+ if scene is None or camera is None or camera.type != 'CAMERA' or context.scene != scene:
+ continue
+ detect(context, [camera], rays)
+ except Exception as error: # a timer must not raise; the next request retries
+ print(f"[StableGen] Object prompts: could not sample the cameras' views: {error}")
+ _pending.clear()
+ for window in context.window_manager.windows:
+ for area in window.screen.areas:
+ if area.type == 'VIEW_3D':
+ area.tag_redraw()
+ if _pending:
+ return 0.02
+ _timer = None
+ return None
+
+
+def colors(grids):
+ """{object name: colour} for the described objects in these Grids, the largest overall first."""
+ totals = {}
+ for grid in grids:
+ for name, share in grid.shares:
+ totals[name] = totals.get(name, 0) + share
+ described = [name for name in sorted(totals, key=lambda n: -totals[n]) if object_prompt(name)]
+ return {name: COLORS[i % len(COLORS)] for i, name in enumerate(described)}
+
+
+def object_prompt(name):
+ obj = bpy.data.objects.get(name)
+ return obj.sg_object_prompt.strip() if obj is not None and hasattr(obj, 'sg_object_prompt') else ''
+
+
+def described(grid):
+ """The described objects a view sees enough of, largest first: [(name, share, prompt)]."""
+ return [(name, share, object_prompt(name)) for name, share in grid.shares
+ if share >= MIN_SHARE and object_prompt(name)]
+
+
+def view_prompt(scene, camera, grid=None):
+ """What a view adds before the texture prompt: its view prompt (Use view prompts) and the prompts of the
+ described objects it sees (Use object prompts)."""
+ parts = []
+ if scene.use_camera_prompts:
+ item = scene.camera_prompts.get(camera.name)
+ if item and item.prompt.strip():
+ parts.append(item.prompt.strip())
+ if getattr(scene, 'use_object_prompts', False) and grid is not None:
+ parts += [prompt for _name, _share, prompt in described(grid)]
+ return ', '.join(parts)
+
+
+def clear():
+ global _timer
+ _cache.clear()
+ _pending.clear()
+ if _timer is not None and bpy.app.timers.is_registered(_tick):
+ bpy.app.timers.unregister(_tick)
+ _timer = None
diff --git a/stablegen/core/properties.py b/stablegen/core/properties.py
index 1460752..c97c310 100644
--- a/stablegen/core/properties.py
+++ b/stablegen/core/properties.py
@@ -1345,6 +1345,17 @@ def register_properties(update_model_list, ControlNetUnit, LoRAUnit,
name="Use view prompts", description="Include each camera’s view prompt with the shared generation prompt. Use view prompts for viewing direction or details belonging to that view.",
default=True, update=update_parameters
)
+ bpy.types.Scene.use_object_prompts = bpy.props.BoolProperty(
+ name="Use object prompts",
+ description="Add each described object's prompt to the views that see it, largest first. Describe an object "
+ "from an open camera card's Objects in view",
+ default=False, update=update_parameters
+ )
+ bpy.types.Object.sg_object_prompt = bpy.props.StringProperty(
+ name="Object prompt",
+ description="Describes this object. With Use object prompts, every view that sees it includes this text",
+ default=""
+ )
bpy.types.Scene.sg_camera_order = bpy.props.CollectionProperty(
type=CameraOrderItem, name="Camera Generation Order",
description="Defines the order in which cameras are processed during generation"
@@ -2217,6 +2228,7 @@ def unregister_properties(load_handler, _sg_queue_load_handler):
'output_timestamp',
'camera_prompts',
'use_camera_prompts',
+ 'use_object_prompts',
'sg_camera_order',
'sg_camera_order_index',
'sg_use_custom_camera_order',
@@ -2271,6 +2283,8 @@ def unregister_properties(load_handler, _sg_queue_load_handler):
for prop in _simple_scene_props:
if hasattr(bpy.types.Scene, prop):
delattr(bpy.types.Scene, prop)
+ if hasattr(bpy.types.Object, 'sg_object_prompt'):
+ del bpy.types.Object.sg_object_prompt
# --- PBR Decomposition Properties ---
pbr_props = [
diff --git a/stablegen/texturing/generator.py b/stablegen/texturing/generator.py
index 3089533..8e633c1 100644
--- a/stablegen/texturing/generator.py
+++ b/stablegen/texturing/generator.py
@@ -1071,16 +1071,17 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._view_plan = None
if use_camera_plan:
view_records = []
+ # The plan freezes what each view adds to the texture prompt: its view prompt and the prompts of the
+ # described objects it sees, sampled now with the cameras where they will render from.
+ from ..cameras.visibility import detect, view_prompt as compose_view_prompt
+ grids = (detect(context, self._cameras)
+ if getattr(context.scene, 'use_object_prompts', False) else {})
for index, cam in enumerate(self._cameras):
if "sg_view_uuid" not in cam:
cam["sg_view_uuid"] = uuid.uuid4().hex
cam_res_x = int(cam.get("sg_res_x", render.resolution_x))
cam_res_y = int(cam.get("sg_res_y", render.resolution_y))
- prompt_item = next(
- (item for item in context.scene.camera_prompts if item.name == cam.name),
- None,
- )
- view_prompt = prompt_item.prompt if prompt_item and prompt_item.prompt else ""
+ view_prompt = compose_view_prompt(context.scene, cam, grids.get(cam.name))
view_id = str(cam["sg_view_uuid"])
matrix_world = tuple(
tuple(float(value) for value in row) for row in cam.matrix_world
diff --git a/stablegen/texturing/workflows.py b/stablegen/texturing/workflows.py
index d382279..2e959e4 100644
--- a/stablegen/texturing/workflows.py
+++ b/stablegen/texturing/workflows.py
@@ -52,12 +52,15 @@ _ADDON_PKG = __package__.rsplit('.', 1)[0]
def _immutable_view_prompt(operator):
- """Return the prompt frozen into the canonical generation view plan."""
+ """Return what the view adds to the texture prompt, frozen into the generation's view plan: its view prompt
+ (Use view prompts) and its described objects' prompts (Use object prompts)."""
index = getattr(operator, '_current_image', -1)
plan = getattr(operator, '_view_plan', ()) or ()
if 0 <= index < len(plan):
return plan[index].prompt or ""
- # Legacy camera-prompt lookup when the canonical-plan experiment is off.
+ # Without a plan (Masked Edit): the camera's view prompt alone, with Use view prompts.
+ if not bpy.context.scene.use_camera_prompts:
+ return ""
cameras = getattr(operator, '_cameras', ()) or ()
if 0 <= index < len(cameras):
camera_name = cameras[index].name
@@ -1296,11 +1299,10 @@ class _TexturingWorkflowMixin:
remove_context = False
# --- Camera Prompt Injection ---
- if context.scene.use_camera_prompts:
- view_desc = _immutable_view_prompt(self.operator)
- if view_desc:
- # Prepend the view description
- user_prompt = f"{view_desc}, {user_prompt}"
+ view_desc = _immutable_view_prompt(self.operator)
+ if view_desc:
+ # Prepend the view description
+ user_prompt = f"{view_desc}, {user_prompt}"
# --- Handle Context Render (Image 3) ---
# This is only active in sequential mode after the first image
@@ -1678,10 +1680,9 @@ class _TexturingWorkflowMixin:
# --- Base user prompt + camera injection ---
user_prompt = _texturing_prompt(context.scene)
camera_suffix = ""
- if context.scene.use_camera_prompts:
- view_desc = _immutable_view_prompt(self.operator)
- if view_desc:
- camera_suffix = f", {view_desc}"
+ view_desc = _immutable_view_prompt(self.operator)
+ if view_desc:
+ camera_suffix = f", {view_desc}"
# ─── Collect reference images ────────────────────────────────
ref_images = []
@@ -2877,7 +2878,7 @@ class _TexturingWorkflowMixin:
base_prompt_text = _texturing_prompt(context.scene)
# Camera Prompt Injection
- if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
+ if context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
view_desc = _immutable_view_prompt(self.operator)
if view_desc:
# Prepend the view description
@@ -3322,7 +3323,7 @@ class _TexturingWorkflowMixin:
base_prompt_text = _texturing_prompt(context.scene)
# Camera Prompt Injection
- if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
+ if context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
view_desc = _immutable_view_prompt(self.operator)
if view_desc:
# Prepend the view description
@@ -3556,7 +3557,7 @@ class _TexturingWorkflowMixin:
base_prompt_text = _texturing_prompt(context.scene)
# Camera Prompt Injection
- if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
+ if context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit']:
view_desc = _immutable_view_prompt(self.operator)
if view_desc:
# Prepend the view description
@@ -3759,7 +3760,7 @@ class _TexturingWorkflowMixin:
base_prompt_text = _texturing_prompt(context.scene)
# Camera Prompt Injection
- if context.scene.use_camera_prompts and context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit', 'grid']:
+ if context.scene.generation_method in ['separate', 'sequential', 'refine', 'local_edit', 'grid']:
view_desc = _immutable_view_prompt(self.operator)
if view_desc:
# Prepend the view description
diff --git a/stablegen/ui/panel.py b/stablegen/ui/panel.py
index 6b914b4..865987e 100644
--- a/stablegen/ui/panel.py
+++ b/stablegen/ui/panel.py
@@ -1382,6 +1382,11 @@ class StableGenPanel(bpy.types.Panel):
if content_box:
row = content_box.row(align=True)
row.prop(scene, "use_camera_prompts", text="Use view prompts", toggle=True, icon="OUTLINER_OB_CAMERA")
+ row = content_box.row(align=True)
+ row.prop(scene, "use_object_prompts", text="Use object prompts", toggle=True, icon="OBJECT_DATA")
+ active = context.active_object
+ if scene.use_object_prompts and active is not None and active.type == 'MESH':
+ content_box.prop(active, "sg_object_prompt", text=active.name)
# ── Camera Generation Order ──
row = content_box.row(align=True)
diff --git a/stablegen/ui/presets.py b/stablegen/ui/presets.py
index 3a141cb..693f35e 100644
--- a/stablegen/ui/presets.py
+++ b/stablegen/ui/presets.py
@@ -626,6 +626,7 @@ GEN_PARAMETERS = [
'mask_blocky',
'architecture_mode',
'use_camera_prompts',
+ 'use_object_prompts',
'sg_use_custom_camera_order',
'generation_mode',
'texture_objects',
diff --git a/stablegen/ui/studio.py b/stablegen/ui/studio.py
index b78003e..a0d8195 100644
--- a/stablegen/ui/studio.py
+++ b/stablegen/ui/studio.py
@@ -13,6 +13,7 @@ import time
import bpy
import blf
+import numpy as np
from bpy.app.handlers import persistent
from . import studio_model as model
@@ -102,6 +103,7 @@ _cursor_owners = {}
_window_mouse = {}
_pending = set()
_handler = None
+_preview_handler = None
_registered = False
_font_id = None
_font_path = ''
@@ -145,10 +147,72 @@ def _undo(message):
_log.debug('Undo push unavailable', exc_info=True)
+def _view_aspect(scene, camera, image):
+ """A camera's view shape: its generated image's, else its resolution's."""
+ if image is not None and image.size[0] > 0 and image.size[1] > 0:
+ return image.size[0] / image.size[1]
+ from ..cameras.geometry import _get_camera_resolution
+ width, height = _get_camera_resolution(camera, scene)
+ return width / max(1, height)
+
+
+def _open_view_layout(x, y, width, aspect, fields_height=200):
+ """Where an open camera card shows its view and its fields: a portrait or square view beside the fields, as tall
+ as they are (200 to 420 px, at most 240 px wide); a landscape view (or any view when the fields would get
+ narrower than 220 px) across the card above them, at most 260 px tall. Returns (view rect, (fields x, fields y,
+ fields width))."""
+ if aspect <= 1.25:
+ view_width = min(240, max(200, min(420, fields_height)) * aspect)
+ if width - 24 - view_width - 16 >= 220:
+ return ((x + 12, y + 43, view_width, view_width / aspect),
+ (x + 12 + view_width + 16, y + 43, width - 24 - view_width - 16))
+ height = min(260, (width - 24) / aspect)
+ view_width = height * aspect
+ return ((x + 12 + (width - 24 - view_width) / 2, y + 43, view_width, height),
+ (x + 12, y + 43 + height + 12, width - 24))
+
+
+def _object_tag(p, x, y, name, hue, *, share='', height=15, size=9, selected=False, hover=False):
+ """The panel's tag, led by the object's colour when it is described; undescribed objects are dimmed."""
+ width = p.measure(name, size) + 10 + (9 if hue else 0) + (p.measure(share, size) + 4 if share else 0)
+ p.outline(x, y, width, height, 'selected' if selected else 'hover_soft',
+ 'accent' if selected else 'hover_border' if hover else 'line', 3)
+ tx = x + 5
+ if hue:
+ p.rect(tx, y + (height - 5) / 2, 5, 5, hue, 1)
+ tx += 9
+ p.text(name, tx, y + (height - size) / 2 - .5, size, 'secondary' if hue else 'disabled')
+ if share:
+ p.text(share, tx + p.measure(name, size) + 4, y + (height - size) / 2 - .5, size, 'muted')
+ return width
+
+
def _binding_key(binding):
return f'{binding.root}:{binding.name}:{binding.path}:{binding.property}'
+def prompt_wording(scene, prop):
+ """(label, placeholder) for a prompt field, by what the prompt does now: a new texture, a restyle of the
+ current one, an edit of part of it, an image for TRELLIS.2. The placeholder shows, greyed, while it is empty.
+ ('', '') for other text fields."""
+ if prop == 'comfyui_negative_prompt':
+ return 'Negative prompt', 'What to keep out, e.g. blurry, text, watermark'
+ if prop == 'sg_masked_prompt':
+ return 'Edit instructions', 'What to change inside the painted area, e.g. make the jacket red leather'
+ if prop == 'texture_prompt':
+ return 'Texture prompt', 'The surface for the mesh, e.g. weathered bronze with green patina'
+ if prop != 'comfyui_prompt':
+ return '', ''
+ if scene.architecture_mode == 'trellis2':
+ return 'Mesh image prompt', 'The object to generate, e.g. a wooden treasure chest with iron bands'
+ method = scene.qwen_generation_method if scene.model_architecture in QWEN_ARCHITECTURES else scene.generation_method
+ return {
+ 'refine': ('Refine prompt', 'How the whole texture should change, e.g. hand-painted style, warmer colors'),
+ 'local_edit': ('Edit prompt', 'What to change where the detail cameras look, e.g. add gold lettering to the sign'),
+ 'uv_inpaint': ('Texture prompt', 'What the untextured areas should show, e.g. the same dark wood grain'),
+ }.get(method, ('Texture prompt', 'The texture to generate, e.g. a weathered bronze statue with green patina'))
+
+
class StudioSession:
def __init__(self, context):
self.window, self.area, self.region = context.window, context.area, context.region
@@ -195,6 +259,15 @@ class StudioSession:
self.busy = False
self.ctrl = False
self.camera_images = {}
+ # The display chosen in Diagnostics, and the settings of the one shown (Update view applies a difference)
+ self.diagnostic_pick = None
+ self.diagnostic_shown = None
+ # The object whose prompt an open camera card is editing, and the outline drawn over that card's view
+ self.object_prompt = None
+ self.object_outline = None
+ self.open_fields_height = {} # camera -> height of its open card's fields, for the view beside them
+ # The camera whose view fills the 3D viewport after a click on its image; any click closes it
+ self.preview = None
self.card_bounds = {}
# Narrowest width while the widen callout was shown in this session.
self.callout_width = None
@@ -440,7 +513,12 @@ class StudioSession:
else:
value = float(text) if prop.type == 'FLOAT' else int(text) if prop.type == 'INT' else text
if binding.value(bpy.context) != value:
+ # A renamed object keeps its open section (Blender may adjust the name: .001)
+ renamed = (binding.data(bpy.context) if binding.root == 'object' and binding.property == 'name'
+ and binding.name == self.object_prompt else None)
self.write_binding(binding, value)
+ if renamed is not None:
+ self.object_prompt = renamed.name
self.refresh()
except (ValueError, TypeError, ReferenceError, RuntimeError) as exc:
self.tell('Could not apply this value: ' + str(exc).split('\n')[0])
@@ -855,7 +933,14 @@ class StudioSession:
if not self.active():
self.commit()
self.menu = None
+ self.preview = None
return {'PASS_THROUGH'}
+ if self.preview:
+ closing = (event.value == 'PRESS' and event.type in {'LEFTMOUSE', 'RIGHTMOUSE', 'ESC'})
+ if closing or event.type == 'LEFTMOUSE' and event.value in {'RELEASE', 'DOUBLE_CLICK'}:
+ if closing:
+ self.close_preview()
+ return {'RUNNING_MODAL'} if self.in_area() else {'PASS_THROUGH'}
if self.camera_drag:
if event.type in {'ESC', 'RIGHTMOUSE'} and event.value == 'PRESS':
self.finish_camera_drag(cancel=True)
@@ -2132,8 +2217,16 @@ class StudioSession:
y += 29
def setup(cx, cy, cw):
cy += self.draw_binding(p, self.binding('use_camera_prompts'), 'Use view prompts', cx, cy, cw)
+ cy += self.draw_binding(p, self.binding('use_object_prompts'), 'Use object prompts', cx, cy, cw)
return cy + self.draw_binding(p, self.binding('sg_use_custom_camera_order'), 'Custom generation order', cx, cy, cw)
y = cards.group(p, 'View setup', x, y, width, setup, 'camera')
+ object_prompts = getattr(scene, 'use_object_prompts', False)
+ grids = visibility.request(scene, cameras) if object_prompts and not self.busy else {}
+ hues = {name: color(hue.lstrip('#')) for name, hue in visibility.colors(grids.values()).items()}
+ images = {camera.name: self.camera_images.get(camera.name) for camera in cameras}
+ aspects = {camera.name: _view_aspect(scene, camera, images[camera.name]) for camera in cameras}
+ # One frame width for every row: as wide as the widest view needs at the rows' 60 px height
+ column = round(min(104, max(40, max((60 * aspect for aspect in aspects.values()), default=60))))
if cameras:
# The displayed camera cards are the order editor. Avoid a second,
# disconnected native list and its up/down-button cluster.
@@ -2151,38 +2244,66 @@ class StudioSession:
p.begin_group()
selected = camera.select_get()
hovered = self.hovered('camera:' + camera.name) and not self.busy
- tx = x + 32 if selected else x + 103
+ tx = x + 32 if selected else x + 30 + column + 13
p.text(f'{i + 1:02} · {camera.name}', tx, y + 16, FONT_SIZE, 'text', x + width - tx - 44)
- image = self.camera_images.get(camera.name)
+ image = images[camera.name]
prompt = scene.camera_prompts.get(camera.name)
+ grid = grids.get(camera.name)
if not selected:
title = prompt.prompt if prompt and prompt.prompt else 'Generated' if image else 'No generated image'
- p.text(title, tx, y + 36, 10, 'secondary', width - 146)
+ p.text(title, tx, y + 36, 10, 'secondary', x + width - tx - 44)
+ if grid is not None:
+ self.draw_object_tags(p, grid, hues, tx, y + 53, x + width - tx - 44)
p.target('camera:' + camera.name, (x + 25, y + 1, width - 67, 38 if selected else 82),
lambda name=camera.name: self.call('stablegen.studio_select_camera', {'camera_name': name, 'extend': self.ctrl}),
not self.busy, camera.name + ' · Ctrl click to add to selection')
+ # Without a custom order the list shows the alphabetical order generation uses, so it cannot be dragged
grip = (x + 4, y + 5, 23, 28)
- p.icon('grip', grip[0] + 4, grip[1] + 7, 14, 'disabled' if self.busy else 'accent' if self.hovered('camera-drag:' + camera.name) else 'secondary')
- p.target('camera-drag:' + camera.name, grip, ('camera-drag', camera.name), not self.busy,
- 'Drag to change generation order. Dropping enables custom order; Esc cancels.')
+ draggable = scene.sg_use_custom_camera_order and not self.busy
+ p.icon('grip', grip[0] + 4, grip[1] + 7, 14, 'accent' if draggable and self.hovered('camera-drag:' + camera.name)
+ else 'secondary' if draggable else 'disabled')
+ p.target('camera-drag:' + camera.name, grip, ('camera-drag', camera.name), draggable,
+ 'Drag to change the generation order; Esc cancels' if scene.sg_use_custom_camera_order
+ else 'Turn on Custom generation order (View setup) to drag views into the order they generate in')
p.button('look:' + camera.name, (x + width - 37, y + (10 if selected else 28), 25, CONTROL_HEIGHT), '',
('operator', 'stablegen.studio_look_camera', {'camera_name': camera.name}), icon='eye', enabled=not self.busy,
hint='Look through ' + camera.name)
- image_rect = (x + 12, y + 43, 70, 160) if selected else (x + 30, y + 12, 60, 60)
- p.outline(*image_rect, 'menu', 'menu_border', 2)
- if image:
- p.image(image, image_rect)
+ if selected:
+ # A view beside the fields matches their height, measured on the previous draw
+ fields_height, settled = self.open_fields_height.get(camera.name, (200, 0))
+ view_rect, (fx, fy, fw) = _open_view_layout(x, y, width, aspects[camera.name], fields_height)
+ fields_top = fy
+ vx, vy, vw, vh = view_rect
+ frame = (vx - 1, vy - 1, vw + 2, vh + 2)
else:
- ix, iy, iw, ih = image_rect
+ # Every row's frame is as wide as the widest view; a narrower one sits centred in it.
+ frame = (x + 30, y + 12, column, 60)
+ view_rect = (x + 31, y + 13, column - 2, 58)
+ # The whole frame opens the preview, so a narrow view is as easy to click as a wide one
+ preview_key = 'camera-preview:' + camera.name
+ hover = image is not None and self.hovered(preview_key)
+ p.outline(*frame, 'menu', 'hover_border' if hover else 'menu_border', 2)
+ shown = p.image(image, view_rect, 8 if selected else 4) if image else None
+ if shown:
+ p.target(preview_key, frame, lambda name=camera.name: self.open_preview(name), not self.busy,
+ 'View full size · click again or Esc to close')
+ if not shown:
+ ix, iy, iw, ih = view_rect
p.icon('camera', ix + (iw - 25) / 2, iy + (ih - 25) / 2, 25)
if selected:
- y += 43
- y += self.draw_binding(p, model.Binding.from_data(camera, 'name'), 'Camera name', x + 98, y, width - 110)
+ fy += self.draw_binding(p, model.Binding.from_data(camera, 'name'), 'Camera name', fx, fy, fw)
if prompt and scene.use_camera_prompts:
- y += self.draw_binding(p, model.Binding.from_data(prompt, 'prompt'), 'View prompt', x + 98, y, width - 110,
- multiline=True, height=93)
- y = max(y, image_rect[1] + image_rect[3]) + 12
+ fy += self.draw_binding(p, model.Binding.from_data(prompt, 'prompt'), 'View prompt', fx, fy, fw,
+ multiline=True, height=56 if object_prompts else 93)
+ if object_prompts:
+ fy = self.draw_view_objects(p, camera, grid, grids, hues, fx, fy, fw, shown)
+ # Wrapping text can trade height for width: settle within a few redraws
+ if abs((fy - fields_top) - fields_height) > 1 and settled < 4:
+ self.open_fields_height[camera.name] = (fy - fields_top, settled + 1)
+ self.area.tag_redraw()
+ y = max(fy, view_rect[1] + view_rect[3]) + 12
else:
+ self.open_fields_height.pop(camera.name, None)
y += 84
self.camera_rows.append((camera.name, (x, start, width, y - start)))
p.end_group((x, start, width, y - start), 'subbox', 'accent' if selected else 'hover_border' if hovered else 'section_border', 3)
@@ -2196,9 +2317,206 @@ class StudioSession:
p.line([(sx, sy), (sx + sw, sy), (sx + sw, sy + sh), (sx, sy + sh), (sx, sy)], 'accent')
for section in self.document.sections:
if section.page == 'CAMERAS' and section.key not in {'camera_tools', 'show_camera_options'}:
- y = self.draw_section(p, section, x, y + 8, width, omit={'use_camera_prompts','sg_use_custom_camera_order'})
+ y = self.draw_section(p, section, x, y + 8, width, omit={'use_camera_prompts', 'use_object_prompts',
+ 'sg_object_prompt', 'sg_use_custom_camera_order'})
return y
+ def draw_object_tags(self, p, grid, hues, x, y, width):
+ """A closed camera card's described objects, largest first, on one line."""
+ right = x + width
+ for name, _share, _prompt in visibility.described(grid):
+ tag_width = p.measure(name, 9) + 19
+ if x + tag_width > right:
+ break
+ _object_tag(p, x, y, name, hues.get(name), height=15, size=9)
+ x += tag_width + 4
+
+ def draw_view_objects(self, p, camera, grid, grids, hues, x, y, width, shown):
+ """An open camera card's objects: every object the view sees with its share (described ones in their
+ colour), the prompt of the one being edited, and the prompt this view adds to the texture prompt."""
+ p.text('Objects in view', x, y, FONT_SIZE)
+ if grid is None:
+ p.text('Finding what this view sees…', x, y + 17, 10, 'muted', width)
+ return y + 17 + 22
+ seen = [(name, share) for name, share in grid.shares if share >= visibility.MIN_SHARE]
+ described = [name for name, _share in seen if visibility.object_prompt(name)]
+ note = f'{len(described)} described · {len(seen) - len(described)} more'
+ p.text(note, x + width - p.measure(note, 10) - 2, y + 1, 10, 'muted')
+ y += 17
+ target = self.object_prompt if self.object_prompt in dict(seen) else None
+ # The selected object's rename button ends the first row; the tags leave room for it
+ tags_width = width - 30 if target else width
+ tx, ty = x, y
+ for name, share in sorted(seen, key=lambda item: (item[0] not in described, -item[1])):
+ label = f'{share:.1%}' if share < .01 else f'{share:.0%}'
+ tag_width = p.measure(name, 10) + p.measure(label, 10) + 14 + (9 if name in described else 0)
+ rename_key = 'object-name:' + name
+ renaming = self.edit if self.edit and self.edit['key'] == rename_key else None
+ if renaming:
+ # While renamed, the tag is its object's name field, growing with the name
+ tag_width = max(tag_width, p.measure(renaming['buffer'].text) + 2 * TEXT_PADDING + 8)
+ if tx > x and tx + tag_width > x + tags_width:
+ tx, ty = x, ty + 22
+ key = 'object-prompt:' + name
+ if renaming:
+ rect = (tx, ty - 2, min(tag_width, x + tags_width - tx), 22)
+ lines, scroll = p.text_field(rename_key, rect, renaming['buffer'].text, ('edit', renaming['binding'], False),
+ editing=renaming['buffer'], hint='Enter renames · Esc cancels')
+ renaming.update(rect=rect, lines=lines, scroll=scroll)
+ else:
+ _object_tag(p, tx, ty, name, hues.get(name), share=label, height=18, size=10, selected=name == target,
+ hover=self.hovered(key))
+ p.target(key, (tx, ty, tag_width, 18), lambda name=name: self.edit_object_prompt(name), not self.busy,
+ f'Describe {name}; every view that sees it uses the description')
+ tx += tag_width + 4
+ if target:
+ button_key = 'object-rename:' + target
+ p.button(button_key, (x + width - 24, y - 2, 24, CONTROL_HEIGHT), '', lambda: self.rename_object(target),
+ icon='rename', enabled=not self.busy, hover=self.hovered(button_key), hint=f'Rename {target}')
+ y = ty + 18 + 9
+ if target:
+ obj = bpy.data.objects.get(target)
+ count = sum(1 for g in grids.values() if dict(g.shares).get(target, 0) >= visibility.MIN_SHARE)
+ note = f'in {count} of {len(grids)} views'
+ p.text(note, x + width - p.measure(note, 10) - 2, y + 1, 10, 'muted')
+ y += self.draw_binding(p, model.Binding.from_data(obj, 'sg_object_prompt'), f'Object prompt · {target}',
+ x, y, width)
+ p.text('Every view that sees it uses this text', x, y - 2, 10, 'muted', width)
+ y += 16
+ if shown:
+ self.draw_object_outline(p, camera, target, hues.get(target), shown)
+ return self.draw_view_prompt(p, camera, grid, hues, x, y, width)
+
+ def draw_object_outline(self, p, camera, name, hue, rect):
+ """Outline the object being described in the open view, from a finer ray sample of that one view."""
+ scene = bpy.context.scene
+ detail = visibility.request(scene, [camera], visibility.DETAIL_RAYS).get(camera.name)
+ if detail is None:
+ return
+ key = (camera.name, name, id(detail), hue)
+ if not self.object_outline or self.object_outline[0] != key:
+ mask = detail.mask(name)
+ inner = mask.copy()
+ for _ in range(2): # a two-sample edge reads at the panel's scale
+ eroded = inner.copy()
+ eroded[1:, :] &= inner[:-1, :]
+ eroded[:-1, :] &= inner[1:, :]
+ eroded[:, 1:] &= inner[:, :-1]
+ eroded[:, :-1] &= inner[:, 1:]
+ inner = eroded
+ rgba = np.zeros(mask.shape + (4,), np.float32)
+ rgba[..., :3] = (hue or p.colors['accent'])[:3]
+ rgba[..., 3] = np.where(mask & ~inner, 1.0, np.where(mask, .22, 0.0))
+ self.object_outline = (key, rgba)
+ p.overlay(self.object_outline[1], rect)
+
+ def draw_view_prompt(self, p, camera, grid, hues, x, y, width):
+ """What this view sends: its view prompt, its described objects' prompts (underlined in their colours) and
+ the texture prompt, as the generation's view plan composes them."""
+ from ..texturing.workflows import _texturing_prompt
+ scene = bpy.context.scene
+ parts = []
+ item = scene.camera_prompts.get(camera.name)
+ if scene.use_camera_prompts and item and item.prompt.strip():
+ parts.append((item.prompt.strip(), None))
+ parts += [(prompt, hues.get(name)) for name, _share, prompt in visibility.described(grid)]
+ parts.append((_texturing_prompt(scene).strip(), 'texture'))
+ text, spans = '', []
+ for part, hue in parts:
+ if not part:
+ continue
+ if text:
+ text += ', '
+ spans.append((len(text), len(text) + len(part), hue))
+ text += part
+ p.text('Prompt for this view', x, y, FONT_SIZE)
+ y += 17
+ lines = wrap(text, width - 16, p.measure)
+ shown_lines = min(len(lines), 6)
+ height = shown_lines * TEXT_LINE_HEIGHT + 2 * TEXT_PADDING - 2
+ p.outline(x, y, width, height, 'menu', 'menu_border')
+ for row, (start, end, line) in enumerate(lines[:shown_lines]):
+ ly = y + TEXT_PADDING - 1 + row * TEXT_LINE_HEIGHT
+ cuts = sorted({0, len(line)} | {min(max(s - start, 0), len(line)) for s, e, _ in spans}
+ | {min(max(e - start, 0), len(line)) for s, e, _ in spans})
+ for a, b in zip(cuts, cuts[1:]):
+ span = next((hue for s, e, hue in spans if s <= start + a < e), None)
+ piece = line[a:b]
+ px = x + TEXT_PADDING + p.measure(line[:a])
+ p.text(piece, px, ly, FONT_SIZE, 'secondary' if span in (None, 'texture') else 'text')
+ if span not in (None, 'texture') and piece.strip():
+ p.rect(px, ly + 14, p.measure(piece.rstrip()), 2, span)
+ words = len(text.split())
+ p.text('View prompt + objects in view + texture prompt', x, y + height + 5, 10, 'muted', width - 68)
+ p.text(f'{words} words', x + width - 55, y + height + 5, 10, 'muted')
+ return y + height + 24
+
+ def open_preview(self, name):
+ self.preview = name
+ self.clear_hover(leave=True)
+ self.area.tag_redraw()
+
+ def close_preview(self):
+ self.preview = None
+ self.area.tag_redraw()
+
+ def in_area(self):
+ x, y = self.mouse
+ area = self.area
+ return area.x <= x < area.x + area.width and area.y <= y < area.y + area.height
+
+ def draw_preview(self, region):
+ """A camera's view across the 3D viewport, left of the sidebar when it overlaps the viewport."""
+ camera = bpy.data.objects.get(self.preview or '')
+ image = self.camera_images.get(self.preview or '')
+ if camera is None or image is None:
+ self.preview = None
+ return
+ scale = max(.8, bpy.context.preferences.system.ui_scale)
+ p = Painter(region, scale, _font_id or 0)
+ try:
+ width, height = region.width / scale, region.height / scale
+ # With Region Overlap the header, toolbar and sidebar draw over the viewport: keep clear of them.
+ left, top, right, bottom = 0, 0, width, height
+ for other in self.area.regions:
+ if other == region or other.width <= 1 or other.height <= 1:
+ continue
+ if not (region.x <= other.x < region.x + region.width or region.x < other.x + other.width <= region.x + region.width):
+ continue
+ if other.type == 'TOOLS' and other.x <= region.x + 1:
+ left = max(left, (other.x + other.width - region.x) / scale)
+ elif other.type == 'UI' and other.x + other.width >= region.x + region.width - 1:
+ right = min(right, (other.x - region.x) / scale)
+ elif other.type in {'HEADER', 'TOOL_HEADER'} and other.y >= region.y + region.height / 2:
+ top = max(top, (region.y + region.height - other.y) / scale)
+ elif other.type in {'HEADER', 'TOOL_HEADER', 'ASSET_SHELF', 'ASSET_SHELF_HEADER'}:
+ bottom = min(bottom, (other.y + other.height - region.y) / scale)
+ # The viewport blends in linear light: 94 % black reads as a dimmed backdrop (82 % looked half-dark)
+ p.rect(0, 0, width, height, (0, 0, 0, .94))
+ cameras = self.ordered_cameras()
+ index = next((i for i, c in enumerate(cameras) if c.name == camera.name), 0)
+ margin = 24
+ p.text(f'{index + 1:02} · {camera.name}', left + margin, top + 14, 13, 'text', right - left - 2 * margin)
+ note = f'{image.size[0]} × {image.size[1]} · click or Esc to close'
+ p.text(note, right - margin - p.measure(note, 10), top + 17, 10, 'muted')
+ p.image(image, (left + margin, top + 40, right - left - 2 * margin, bottom - top - 40 - margin), 12)
+ finally:
+ p.finish()
+
+ def rename_object(self, name):
+ """Turn the selected object's tag into its name field, the name selected."""
+ obj = bpy.data.objects.get(name)
+ if obj is None:
+ return
+ from .studio_widgets import Target
+ field = Target('object-name:' + name, (0, 0, 0, 0), ('edit', model.Binding.from_data(obj, 'name'), False))
+ self.begin_edit(field, select=True)
+
+ def edit_object_prompt(self, name):
+ self.object_prompt = None if self.object_prompt == name else name
+ self.object_outline = None
+ self.area.tag_redraw()
+
def menu_items(self):
if self.menu.get('kind') == 'lora':
return lora.menu_items(self.menu)
@@ -2712,6 +3030,25 @@ def _draw():
session.error = str(exc)
+def preview_open(window, area):
+ """Whether the StableGen panel in this viewport shows a camera's view over it."""
+ session = _sessions.get((window.as_pointer(), area.as_pointer())) if window and area else None
+ return bool(session and session.preview)
+
+
+def _draw_preview():
+ context = bpy.context
+ if context.area is None or context.region is None or context.region.type != 'WINDOW':
+ return
+ session = _sessions.get((context.window.as_pointer(), context.area.as_pointer()))
+ if session and session.preview and session.active():
+ try:
+ session.draw_preview(context.region)
+ except Exception:
+ _log.exception('Studio preview drawing error')
+ session.preview = None
+
+
def _tick():
if not _registered:
return None
@@ -2744,7 +3081,7 @@ STUDIO_UI_CLASSES = diagnostics.CLASSES + [lora.SG_OT_StudioLoRA, SG_OT_StudioSe
def register_studio_ui():
- global _registered, _handler, _font_id, _font_path
+ global _registered, _handler, _preview_handler, _font_id, _font_path
_registered = True
diagnostics.register()
_font_id = None
@@ -2759,19 +3096,23 @@ def register_studio_ui():
_font_path = str(path)
break
_handler = bpy.types.SpaceView3D.draw_handler_add(_draw, (), 'UI', 'POST_PIXEL')
+ _preview_handler = bpy.types.SpaceView3D.draw_handler_add(_draw_preview, (), 'WINDOW', 'POST_PIXEL')
bpy.app.timers.register(_tick, first_interval=.05, persistent=True)
if _load_pre not in bpy.app.handlers.load_pre:
bpy.app.handlers.load_pre.append(_load_pre)
def unregister_studio_ui():
- global _registered, _handler
+ global _registered, _handler, _preview_handler
_registered = False
diagnostics.unregister()
_load_pre(None)
if _handler:
bpy.types.SpaceView3D.draw_handler_remove(_handler, 'UI')
_handler = None
+ if _preview_handler:
+ bpy.types.SpaceView3D.draw_handler_remove(_preview_handler, 'WINDOW')
+ _preview_handler = None
if bpy.app.timers.is_registered(_tick):
bpy.app.timers.unregister(_tick)
if _load_pre in bpy.app.handlers.load_pre:
diff --git a/stablegen/ui/studio_cards.py b/stablegen/ui/studio_cards.py
index c0c5ddb..6ecdc86 100644
--- a/stablegen/ui/studio_cards.py
+++ b/stablegen/ui/studio_cards.py
@@ -249,6 +249,7 @@ LABELS = {
'generation_method': 'Texture workflow',
'qwen_generation_method': 'Texture workflow',
'use_camera_prompts': 'Use view prompts',
+ 'use_object_prompts': 'Use object prompts',
'denoise': 'Edit strength',
'sg_masked_strength': 'Edit strength',
'scheduler': 'Noise schedule',
diff --git a/stablegen/ui/studio_widgets.py b/stablegen/ui/studio_widgets.py
index 5351cf4..983e36b 100644
--- a/stablegen/ui/studio_widgets.py
+++ b/stablegen/ui/studio_widgets.py
@@ -1,6 +1,7 @@
"""GPU widgets for Studio's sidebar. Bounds also define their hit targets."""
from dataclasses import dataclass
import math
+import os
import bpy
import blf
@@ -150,6 +151,150 @@ def _icon_texture(name, size, scale):
return _icon_textures[key]
+_thumbnail_sources = {} # (image name, size cap) -> (image key, premultiplied RGBA uint16, rows bottom first)
+_thumbnail_textures = {}
+_overlay_textures = {}
+THUMBNAIL_SOURCE = 1024 # longest side kept for an image's thumbnails; a full-size preview keeps up to 4096
+
+
+def _image_key(image):
+ """Changes when the image's pixels may have: another file version, an edit, a resize."""
+ path = bpy.path.abspath(image.filepath, library=image.library) if image.filepath else ''
+ try:
+ stamp = os.path.getmtime(path) if path else 0.0
+ except OSError:
+ stamp = 0.0
+ return image.name, image.as_pointer(), tuple(image.size), image.is_dirty, stamp
+
+
+def _thumbnail_source(image, cap=THUMBNAIL_SOURCE):
+ """The image as premultiplied, display-referred RGBA at most *cap* pixels long. Premultiplied, so
+ resampling never pulls in the colour a generator leaves under transparent pixels (Qwen's purple)."""
+ import cv2
+ key = _image_key(image)
+ cached = _thumbnail_sources.get((image.name, cap))
+ if cached and cached[0] == key:
+ return cached[1]
+ width, height = image.size
+ channels = image.channels
+ if width <= 0 or height <= 0 or channels not in (1, 3, 4):
+ return None
+ pixels = np.empty(width * height * channels, np.float32)
+ image.pixels.foreach_get(pixels)
+ pixels = pixels.reshape(height, width, channels)
+ if channels == 1:
+ pixels = np.repeat(pixels, 3, axis=2)
+ rgb, alpha = pixels[..., :3], (pixels[..., 3:4] if channels == 4 else np.ones((height, width, 1), np.float32))
+ if image.is_float and 'srgb' not in image.colorspace_settings.name.lower():
+ # Float buffers hold scene-linear values; byte buffers are already display-encoded.
+ rgb = np.where(rgb <= 0.0031308, rgb * 12.92, 1.055 * np.power(np.maximum(rgb, 0), 1 / 2.4) - 0.055)
+ rgba = np.concatenate([np.clip(rgb, 0, 1) * alpha, np.clip(alpha, 0, 1)], axis=2)
+ scale = min(1.0, cap / max(width, height))
+ if scale < 1:
+ rgba = cv2.resize(rgba, (max(1, round(width * scale)), max(1, round(height * scale))),
+ interpolation=cv2.INTER_AREA)
+ source = (rgba * 65535).round().astype(np.uint16)
+ if len(_thumbnail_sources) > 64:
+ _thumbnail_sources.clear()
+ _thumbnail_sources[(image.name, cap)] = (key, source)
+ return source
+
+
+def _file_key(path):
+ try:
+ stat = os.stat(path)
+ except OSError:
+ return None
+ return os.path.normcase(os.path.abspath(path)), stat.st_mtime_ns, stat.st_size
+
+
+def _file_source(path, cap=THUMBNAIL_SOURCE):
+ """An image file as _thumbnail_source gives a Blender image (premultiplied RGBA, bottom row first), read
+ straight from the file: drawing may not load image datablocks. None when it can't be read."""
+ import cv2
+ key = _file_key(path)
+ if key is None:
+ return None
+ cached = _thumbnail_sources.get((key[0], cap))
+ if cached and cached[0] == key:
+ return cached[1]
+ try:
+ pixels = cv2.imdecode(np.fromfile(path, np.uint8), cv2.IMREAD_UNCHANGED) # works with any path's letters
+ except (OSError, ValueError, cv2.error):
+ pixels = None
+ if pixels is None:
+ return None
+ depth = 65535.0 if pixels.dtype == np.uint16 else 1.0 if pixels.dtype.kind == 'f' else 255.0
+ pixels = pixels.astype(np.float32) / depth
+ if pixels.ndim == 2:
+ pixels = pixels[..., None]
+ if pixels.shape[2] == 1:
+ pixels = np.repeat(pixels, 3, axis=2)
+ if pixels.shape[2] == 3:
+ pixels = np.concatenate([pixels, np.ones(pixels.shape[:2] + (1,), np.float32)], axis=2)
+ rgba = np.clip(pixels[::-1, :, [2, 1, 0, 3]], 0, 1) # OpenCV's BGRA, top row first
+ rgba = np.concatenate([rgba[..., :3] * rgba[..., 3:4], rgba[..., 3:4]], axis=2)
+ height, width = rgba.shape[:2]
+ scale = min(1.0, cap / max(width, height))
+ if scale < 1:
+ rgba = cv2.resize(rgba, (max(1, round(width * scale)), max(1, round(height * scale))),
+ interpolation=cv2.INTER_AREA)
+ source = (rgba * 65535).round().astype(np.uint16)
+ if len(_thumbnail_sources) > 64:
+ _thumbnail_sources.clear()
+ _thumbnail_sources[(key[0], cap)] = (key, source)
+ return source
+
+
+def _thumbnail_texture(image, width, height, check):
+ """A width x height device-pixel texture of the image (a Blender image, or an image file's path) over the
+ theme's transparency checkerboard."""
+ import cv2
+ ui = bpy.context.preferences.themes[0].user_interface
+ primary = tuple(ui.transparent_checker_primary)
+ secondary = tuple(ui.transparent_checker_secondary)
+ from_file = isinstance(image, str)
+ key = (_file_key(image) if from_file else _image_key(image), width, height, check, primary, secondary)
+ if key not in _thumbnail_textures:
+ source = (_file_source if from_file else _thumbnail_source)(
+ image, THUMBNAIL_SOURCE if max(width, height) <= THUMBNAIL_SOURCE else 4096)
+ if source is None:
+ return None
+ small = cv2.resize(source, (width, height), interpolation=cv2.INTER_AREA).astype(np.float32) / 65535
+ rows, columns = np.mgrid[0:height, 0:width]
+ board = np.where((((rows // check) + (columns // check)) % 2 == 0)[..., None],
+ np.float32(primary), np.float32(secondary))
+ rgba = np.ones((height, width, 4), np.float32)
+ rgba[..., :3] = small[..., :3] + board * (1 - small[..., 3:4])
+ if len(_thumbnail_textures) > 128:
+ _thumbnail_textures.clear()
+ data = _float_buffer(rgba)
+ _thumbnail_textures[key] = gpu.types.GPUTexture((width, height), format='RGBA16F', data=data)
+ return _thumbnail_textures[key]
+
+
+def _float_buffer(array):
+ """A GPU float buffer of a numpy array, without the per-element list a full-size preview would stall on."""
+ flat = np.ascontiguousarray(array, np.float32).ravel()
+ try:
+ return gpu.types.Buffer('FLOAT', flat.size, flat)
+ except (TypeError, ValueError):
+ return gpu.types.Buffer('FLOAT', flat.size, flat.tolist())
+
+
+def _array_texture(rgba):
+ """A texture of a small RGBA float array (rows bottom first), cached while the same array is drawn."""
+ key = (id(rgba), rgba.shape)
+ cached = _overlay_textures.get(key)
+ if cached is None or cached[0] is not rgba:
+ if len(_overlay_textures) > 16:
+ _overlay_textures.clear()
+ height, width = rgba.shape[:2]
+ data = _float_buffer(rgba)
+ cached = _overlay_textures[key] = (rgba, gpu.types.GPUTexture((width, height), format='RGBA16F', data=data))
+ return cached[1]
+
+
def _logo_texture(size, scale):
"""Return a cached (texture, extent) for the full-colour logo at this device pixel size."""
extent = round(size * scale)
@@ -483,19 +628,50 @@ class Painter:
# blf.draw leaves blending off; translucent shapes and icon edges need it.
gpu.state.blend_set('ALPHA')
- def image(self, image, rect):
- self.emit(self.draw_image, image, rect)
-
- def draw_image(self, image, rect):
+ def image(self, image, rect, check=8):
+ """Draw *image* (a Blender image, or an image file's path) fitted inside *rect*, transparent parts over the
+ theme's checkerboard (*check* UI pixels a square); returns the rectangle it fills, None for a file it can't
+ read."""
x, y, width, height = rect
- iw, ih = image.size
+ if isinstance(image, str):
+ source = _file_source(image)
+ if source is None:
+ return None
+ ih, iw = source.shape[:2]
+ else:
+ iw, ih = image.size
if iw <= 0 or ih <= 0:
- return
+ return None
ratio = min(width / iw, height / ih)
- w, h = iw * ratio, ih * ratio
- x, y = x + (width - w) / 2, y + (height - h) / 2
+ w = max(1, round(iw * ratio * self.scale)) / self.scale
+ h = max(1, round(ih * ratio * self.scale)) / self.scale
+ x = round((x + (width - w) / 2) * self.scale) / self.scale
+ y = round((y + (height - h) / 2) * self.scale) / self.scale
+ self.emit(self.draw_image, image, (x, y, w, h), check)
+ return x, y, w, h
+
+ def draw_image(self, image, rect, check):
+ x, y, w, h = rect
+ texture = _thumbnail_texture(image, round(w * self.scale), round(h * self.scale),
+ max(1, round(check * self.scale)))
+ if texture is None:
+ return
+ shader = gpu.shader.from_builtin('IMAGE')
+ shader.bind()
+ shader.uniform_sampler('image', texture)
+ positions = [(px * self.scale, (self.height - py) * self.scale)
+ for px, py in ((x, y+h), (x+w, y+h), (x+w, y), (x, y))]
+ batch_for_shader(shader, 'TRI_FAN', {'pos': positions,
+ 'texCoord': [(0,0), (1,0), (1,1), (0,1)]}).draw(shader)
+
+ def overlay(self, rgba, rect):
+ """Draw an RGBA float array (rows bottom first, straight alpha) stretched over *rect*."""
+ self.emit(self.draw_overlay, rgba, rect)
+
+ def draw_overlay(self, rgba, rect):
+ x, y, w, h = rect
+ texture = _array_texture(rgba)
shader = gpu.shader.from_builtin('IMAGE')
- texture = gpu.texture.from_image(image)
shader.bind()
shader.uniform_sampler('image', texture)
positions = [(px * self.scale, (self.height - py) * self.scale)