Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "order" dropdown for both spritesheet nodes #2878

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions backend/src/nodes/properties/inputs/generic_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,16 @@ def TileSizeDropdown(
class AudioStreamInput(BaseInput):
def __init__(self, label: str = "Audio Stream"):
super().__init__("AudioStream", label, kind="generic")


class OrderEnum(Enum):
ROW_MAJOR = 0
COLUMN_MAJOR = 1


def RowOrderDropdown() -> DropDownInput:
return EnumInput(
OrderEnum,
label="Order",
default=OrderEnum.ROW_MAJOR,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

from api import Collector, IteratorInputInfo
from nodes.properties.inputs import ImageInput, NumberInput
from nodes.properties.inputs import ImageInput, NumberInput, OrderEnum, RowOrderDropdown
from nodes.properties.outputs import ImageOutput

from .. import batch_processing_group
Expand All @@ -24,6 +24,17 @@
NumberInput("Number of columns (width)", min=1, default=1).with_docs(
"The number of columns to split the image into. The width of the image must be a multiple of this number."
),
RowOrderDropdown().with_docs(
"""The order in which the images are combined.
Examples:
```
Row major: Column major:
→ 0 1 2 ↓ 0 3 6
3 4 5 1 4 7
6 7 8 2 5 8
```""",
hint=True,
),
],
iterator_inputs=IteratorInputInfo(inputs=0),
outputs=[
Expand All @@ -42,17 +53,27 @@ def merge_spritesheet_node(
_: None,
rows: int,
columns: int,
order: OrderEnum,
) -> Collector[np.ndarray, np.ndarray]:
results = []

def on_iterate(tile: np.ndarray):
results.append(tile)

def on_complete():
result_rows = []
for i in range(rows):
row = np.concatenate(results[i * columns : (i + 1) * columns], axis=1)
result_rows.append(row)
return np.concatenate(result_rows, axis=0)
if order == OrderEnum.ROW_MAJOR:
result_rows = []
for i in range(rows):
row = np.concatenate(results[i * columns : (i + 1) * columns], axis=1)
result_rows.append(row)
return np.concatenate(result_rows, axis=0)
elif order == OrderEnum.COLUMN_MAJOR:
result_cols = []
for i in range(columns):
column = np.concatenate(results[i * rows : (i + 1) * rows], axis=0)
result_cols.append(column)
return np.concatenate(result_cols, axis=1)
else:
raise ValueError(f"Invalid order: {order}")

return Collector(on_iterate=on_iterate, on_complete=on_complete)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

from api import Iterator, IteratorOutputInfo
from nodes.properties.inputs import ImageInput, NumberInput
from nodes.properties.inputs import ImageInput, NumberInput, OrderEnum, RowOrderDropdown
from nodes.properties.outputs import ImageOutput, NumberOutput
from nodes.utils.utils import get_h_w_c

Expand All @@ -26,6 +26,17 @@
NumberInput("Number of columns (width)", min=1, default=1).with_docs(
"The number of columns to split the image into. The width of the image must be a multiple of this number."
),
RowOrderDropdown().with_docs(
"""The order in which the images are separated.
Examples:
```
Row major: Column major:
→ 0 1 2 ↓ 0 3 6
3 4 5 1 4 7
6 7 8 2 5 8
```""",
hint=True,
),
],
outputs=[
ImageOutput(
Expand All @@ -47,6 +58,7 @@ def split_spritesheet_node(
sprite_sheet: np.ndarray,
rows: int,
columns: int,
order: OrderEnum,
) -> Iterator[tuple[np.ndarray, int]]:
h, w, _ = get_h_w_c(sprite_sheet)
assert (
Expand All @@ -60,13 +72,24 @@ def split_spritesheet_node(
individual_w = w // columns

def get_sprite(index: int):
row = index // columns
col = index % columns
if order == OrderEnum.ROW_MAJOR:
row = index // columns
col = index % columns

sprite = sprite_sheet[
row * individual_h : (row + 1) * individual_h,
col * individual_w : (col + 1) * individual_w,
]
elif order == OrderEnum.COLUMN_MAJOR:
col = index // rows
row = index % rows

sprite = sprite_sheet[
row * individual_h : (row + 1) * individual_h,
col * individual_w : (col + 1) * individual_w,
]
sprite = sprite_sheet[
row * individual_h : (row + 1) * individual_h,
col * individual_w : (col + 1) * individual_w,
]
else:
raise ValueError(f"Invalid order: {order}")

return sprite, index

Expand Down
Loading