Skip to content

Allow negative batch_index on ImageFromBatch and LatentFromBatch (CORE-195)#13857

Merged
alexisrolland merged 4 commits into
Comfy-Org:masterfrom
drozbay:20260512a_frombatch_negative_index
May 15, 2026
Merged

Allow negative batch_index on ImageFromBatch and LatentFromBatch (CORE-195)#13857
alexisrolland merged 4 commits into
Comfy-Org:masterfrom
drozbay:20260512a_frombatch_negative_index

Conversation

@drozbay
Copy link
Copy Markdown
Contributor

@drozbay drozbay commented May 12, 2026

Adds negative-indexing support to batch_index on ImageFromBatch (comfy_extras/nodes_images.py:139) and LatentFromBatch (nodes.py:1224). Schema bounds change from (0, 4095) and (0, 63) respectively to (-MAX_RESOLUTION, MAX_RESOLUTION). Each method gains if batch_index < 0: batch_index += s_in.shape[0] before the existing upper clamp. length is unchanged. Each value verified against current code.

Similar newer indexing nodes (LatentCut, ReplaceVideoLatentFrames, LTXVAddGuide, TrimAudioDuration, VideoSlice) already support negative indexing. The two FromBatch nodes were the exceptions, so workflows that wanted the last N frames had to compute batch_size - N upstream. With this change, batch_index = -N, length = N works directly, which is the standard ergonomic pattern for windowing and first-last-frame loop flows.

The (-MAX_RESOLUTION, MAX_RESOLUTION) bound matches LatentCut.index and ReplaceVideoLatentFrames.index.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fe57a53a-4358-477a-aba3-11fd5c933d80

📥 Commits

Reviewing files that changed from the base of the PR and between dfd64db and 5886a58.

📒 Files selected for processing (1)
  • nodes.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • nodes.py

📝 Walkthrough

Walkthrough

This PR enables negative batch_index for ImageFromBatch and LatentFromBatch by widening their input ranges to -MAX_RESOLUTION..MAX_RESOLUTION. Both nodes convert a negative batch_index into a positive index by adding the batch size (e.g., batch_index += batch_size) before applying existing clamping and selection logic. Previously, indices were restricted to non-negative values.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: adding negative indexing support to two batch nodes with a reference to the issue tracker.
Description check ✅ Passed The description is directly related to the changeset, explaining what was changed, why it was changed, and providing context about consistency with similar nodes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@comfy_extras/nodes_images.py`:
- Around line 148-151: After converting a negative batch_index (batch_index +=
s_in.shape[0]) ensure you clamp it to a non-negative lower bound like
batch_index = max(0, batch_index) before computing length; in other words update
the block that manipulates batch_index and length in nodes_images.py so that
after the negative-index conversion you enforce batch_index = max(0,
batch_index) and then compute batch_index = min(s_in.shape[0] - 1, batch_index)
and length = min(s_in.shape[0] - batch_index, length) to prevent negative
indices and empty/unexpected slices when batch_index is more negative than the
batch size.

In `@nodes.py`:
- Around line 1235-1238: The negative-index conversion for batch_index can
produce values below 0 and bypass the upper clamp, causing empty slices; update
the logic around batch_index (the block that adjusts batch_index and length
using s_in.shape[0]) to clamp batch_index into [0, s_in.shape[0]-1] after adding
s_in.shape[0] for negatives (e.g., set batch_index = max(0, batch_index) before
the min(...) upper clamp or use batch_index = min(s_in.shape[0]-1, max(0,
batch_index))), and then recompute length = min(s_in.shape[0] - batch_index,
length) so slicing of s_in[batch_index:batch_index+length] cannot produce empty
results due to overly negative indices.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: eb2eaf3c-a15e-4ee4-b4d4-20a84ad63f60

📥 Commits

Reviewing files that changed from the base of the PR and between a5f7bc5 and a927795.

📒 Files selected for processing (2)
  • comfy_extras/nodes_images.py
  • nodes.py

Comment thread comfy_extras/nodes_images.py
Comment thread nodes.py
@alexisrolland
Copy link
Copy Markdown
Member

Tested and this works fine. I am not too sure how to test for Latent though...

{525C2E15-D2D7-4AC4-B3F7-BDE6DA20B813}

@alexisrolland
Copy link
Copy Markdown
Member

Tested with latents and it works fine.

{759D2234-FC9C-464E-9F8B-5F45539828A6}

Comment thread comfy_extras/nodes_images.py
@alexisrolland alexisrolland self-requested a review May 15, 2026 14:06
@alexisrolland alexisrolland merged commit 04856ac into Comfy-Org:master May 15, 2026
14 checks passed
@drozbay drozbay deleted the 20260512a_frombatch_negative_index branch May 15, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants