convert sceneitem commands

This commit is contained in:
onyx-and-iris 2025-07-24 03:46:11 +01:00
parent e8664f0117
commit 1ce832dfde
2 changed files with 103 additions and 114 deletions

View File

@ -33,6 +33,7 @@ for sub_app in (
'replaybuffer', 'replaybuffer',
'scene', 'scene',
'scenecollection', 'scenecollection',
'sceneitem',
): ):
module = importlib.import_module(f'.{sub_app}', package=__package__) module = importlib.import_module(f'.{sub_app}', package=__package__)
app.command(module.app) app.command(module.app)

View File

@ -2,41 +2,42 @@
from typing import Annotated, Optional from typing import Annotated, Optional
import typer from cyclopts import App, Argument, Parameter
from rich.table import Table from rich.table import Table
from . import console, util, validate from . import console, util, validate
from .alias import SubTyperAliasGroup from .context import Context
from .enum import ExitCode
from .error import OBSWSCLIError
app = typer.Typer(cls=SubTyperAliasGroup) app = App(name='sceneitem', help='Commands for controlling scene items in OBS.')
@app.callback()
def main():
"""Control items in OBS scenes."""
@app.command('list | ls') @app.command('list | ls')
def list_( def list_(
ctx: typer.Context,
scene_name: Annotated[ scene_name: Annotated[
Optional[str], Optional[str],
typer.Argument( Argument(
show_default='The current scene', hint='Scene name to list items for',
help='Scene name to list items for',
), ),
] = None, ] = None,
uuid: Annotated[bool, typer.Option(help='Show UUIDs of scene items')] = False, /,
uuid: Annotated[bool, Parameter(help='Show UUIDs of scene items')] = False,
*,
ctx: Annotated[Context, Parameter(parse=False)],
): ):
"""List all items in a scene.""" """List all items in a scene."""
if not scene_name: if not scene_name:
scene_name = ctx.obj['obsws'].get_current_program_scene().scene_name scene_name = ctx.client.get_current_program_scene().scene_name
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
console.err.print(f'Scene [yellow]{scene_name}[/yellow] not found.') console.err.print(f'Scene [yellow]{scene_name}[/yellow] not found.')
raise typer.Exit(1) raise OBSWSCLIError(
f'Scene [yellow]{scene_name}[/yellow] not found.',
exit_code=ExitCode.ERROR,
)
resp = ctx.obj['obsws'].get_scene_item_list(scene_name) resp = ctx.client.get_scene_item_list(scene_name)
items = sorted( items = sorted(
( (
( (
@ -52,10 +53,10 @@ def list_(
) )
if not items: if not items:
console.out.print( raise OBSWSCLIError(
f'No items found in scene {console.highlight(ctx, scene_name)}.' f'No items found in scene [yellow]{scene_name}[/yellow].',
exit_code=ExitCode.SUCCESS,
) )
raise typer.Exit()
table = Table( table = Table(
title=f'Items in Scene: {scene_name}', title=f'Items in Scene: {scene_name}',
@ -138,36 +139,39 @@ def list_(
def _validate_sources( def _validate_sources(
ctx: typer.Context, ctx: Context,
scene_name: str, scene_name: str,
item_name: str, item_name: str,
group: Optional[str] = None, group: Optional[str] = None,
) -> bool: ):
"""Validate the scene name and item name.""" """Validate the scene name and item name."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
console.err.print(f'Scene [yellow]{scene_name}[/yellow] not found.') raise OBSWSCLIError(
return False f'Scene [yellow]{scene_name}[/yellow] not found.',
exit_code=ExitCode.ERROR,
)
if group: if group:
if not validate.item_in_scene_item_list(ctx, scene_name, group): if not validate.item_in_scene_item_list(ctx, scene_name, group):
console.err.print( raise OBSWSCLIError(
f'Group [yellow]{group}[/yellow] not found in scene [yellow]{scene_name}[/yellow].' f'Group [yellow]{group}[/yellow] not found in scene [yellow]{scene_name}[/yellow].',
exit_code=ExitCode.ERROR,
) )
return False
else: else:
if not validate.item_in_scene_item_list(ctx, scene_name, item_name): if not validate.item_in_scene_item_list(ctx, scene_name, item_name):
console.err.print( raise OBSWSCLIError(
f'Item [yellow]{item_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow]. Is the item in a group? ' f'Item [yellow]{item_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow]. Is the item in a group? '
f'If so use the [yellow]--group[/yellow] option to specify the parent group.\n' f'If so use the [yellow]--group[/yellow] option to specify the parent group.\n'
'Use [yellow]obsws-cli sceneitem ls[/yellow] for a list of items in the scene.' 'Use [yellow]obsws-cli sceneitem ls[/yellow] for a list of items in the scene.',
exit_code=ExitCode.ERROR,
) )
return False
return True
def _get_scene_name_and_item_id( def _get_scene_name_and_item_id(
ctx: typer.Context, scene_name: str, item_name: str, group: Optional[str] = None ctx: Context,
scene_name: str,
item_name: str,
group: Optional[str] = None,
): ):
"""Get the scene name and item ID for the given scene and item name.""" """Get the scene name and item ID for the given scene and item name."""
if group: if group:
@ -178,10 +182,10 @@ def _get_scene_name_and_item_id(
scene_item_id = item.get('sceneItemId') scene_item_id = item.get('sceneItemId')
break break
else: else:
console.err.print( raise OBSWSCLIError(
f'Item [yellow]{item_name}[/yellow] not found in group [yellow]{group}[/yellow].' f'Item [yellow]{item_name}[/yellow] not found in group [yellow]{group}[/yellow].',
exit_code=ExitCode.ERROR,
) )
raise typer.Exit(1)
else: else:
resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name) resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
scene_item_id = resp.scene_item_id scene_item_id = resp.scene_item_id
@ -189,21 +193,20 @@ def _get_scene_name_and_item_id(
return scene_name, scene_item_id return scene_name, scene_item_id
@app.command('show | sh') @app.command(name=['show', 'sh'])
def show( def show(
ctx: typer.Context, scene_name: Annotated[str, Argument(hint='Scene name the item is in')],
scene_name: Annotated[
str, typer.Argument(..., show_default=False, help='Scene name the item is in')
],
item_name: Annotated[ item_name: Annotated[
str, str,
typer.Argument(..., show_default=False, help='Item name to show in the scene'), Argument(hint='Item name to show in the scene'),
], ],
group: Annotated[Optional[str], typer.Option(help='Parent group name')] = None, /,
group: Annotated[Optional[str], Parameter(help='Parent group name')] = None,
*,
ctx: Annotated[Context, Parameter(parse=False)],
): ):
"""Show an item in a scene.""" """Show an item in a scene."""
if not _validate_sources(ctx, scene_name, item_name, group): _validate_sources(ctx, scene_name, item_name, group)
raise typer.Exit(1)
old_scene_name = scene_name old_scene_name = scene_name
scene_name, scene_item_id = _get_scene_name_and_item_id( scene_name, scene_item_id = _get_scene_name_and_item_id(
@ -231,21 +234,20 @@ def show(
) )
@app.command('hide | h') @app.command(name=['hide', 'h'])
def hide( def hide(
ctx: typer.Context, scene_name: Annotated[str, Argument(hint='Scene name the item is in')],
scene_name: Annotated[
str, typer.Argument(..., show_default=False, help='Scene name the item is in')
],
item_name: Annotated[ item_name: Annotated[
str, str,
typer.Argument(..., show_default=False, help='Item name to hide in the scene'), Argument(hint='Item name to hide in the scene'),
], ],
group: Annotated[Optional[str], typer.Option(help='Parent group name')] = None, /,
group: Annotated[Optional[str], Parameter(help='Parent group name')] = None,
*,
ctx: Annotated[Context, Parameter(parse=False)],
): ):
"""Hide an item in a scene.""" """Hide an item in a scene."""
if not _validate_sources(ctx, scene_name, item_name, group): _validate_sources(ctx, scene_name, item_name, group)
raise typer.Exit(1)
old_scene_name = scene_name old_scene_name = scene_name
scene_name, scene_item_id = _get_scene_name_and_item_id( scene_name, scene_item_id = _get_scene_name_and_item_id(
@ -272,36 +274,30 @@ def hide(
) )
@app.command('toggle | tg') @app.command(name=['toggle', 'tg'])
def toggle( def toggle(
ctx: typer.Context, scene_name: Annotated[str, Argument(hint='Scene name the item is in')],
scene_name: Annotated[ item_name: Annotated[str, Argument(hint='Item name to toggle in the scene')],
str, typer.Argument(..., show_default=False, help='Scene name the item is in') /,
], group: Annotated[Optional[str], Parameter(help='Parent group name')] = None,
item_name: Annotated[ *,
str, ctx: Annotated[Context, Parameter(parse=False)],
typer.Argument(
..., show_default=False, help='Item name to toggle in the scene'
),
],
group: Annotated[Optional[str], typer.Option(help='Parent group name')] = None,
): ):
"""Toggle an item in a scene.""" """Toggle an item in a scene."""
if not _validate_sources(ctx, scene_name, item_name, group): _validate_sources(ctx, scene_name, item_name, group)
raise typer.Exit(1)
old_scene_name = scene_name old_scene_name = scene_name
scene_name, scene_item_id = _get_scene_name_and_item_id( scene_name, scene_item_id = _get_scene_name_and_item_id(
ctx, scene_name, item_name, group ctx, scene_name, item_name, group
) )
enabled = ctx.obj['obsws'].get_scene_item_enabled( enabled = ctx.client.get_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(scene_item_id), item_id=int(scene_item_id),
) )
new_state = not enabled.scene_item_enabled new_state = not enabled.scene_item_enabled
ctx.obj['obsws'].set_scene_item_enabled( ctx.client.set_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(scene_item_id), item_id=int(scene_item_id),
enabled=new_state, enabled=new_state,
@ -333,30 +329,26 @@ def toggle(
) )
@app.command('visible | v') @app.command(name=['visible', 'v'])
def visible( def visible(
ctx: typer.Context, scene_name: Annotated[str, Argument(hint='Scene name the item is in')],
scene_name: Annotated[
str, typer.Argument(..., show_default=False, help='Scene name the item is in')
],
item_name: Annotated[ item_name: Annotated[
str, str, Argument(hint='Item name to check visibility in the scene')
typer.Argument(
..., show_default=False, help='Item name to check visibility in the scene'
),
], ],
group: Annotated[Optional[str], typer.Option(help='Parent group name')] = None, /,
group: Annotated[Optional[str], Parameter(help='Parent group name')] = None,
*,
ctx: Annotated[Context, Parameter(parse=False)],
): ):
"""Check if an item in a scene is visible.""" """Check if an item in a scene is visible."""
if not _validate_sources(ctx, scene_name, item_name, group): _validate_sources(ctx, scene_name, item_name, group)
raise typer.Exit(1)
old_scene_name = scene_name old_scene_name = scene_name
scene_name, scene_item_id = _get_scene_name_and_item_id( scene_name, scene_item_id = _get_scene_name_and_item_id(
ctx, scene_name, item_name, group ctx, scene_name, item_name, group
) )
enabled = ctx.obj['obsws'].get_scene_item_enabled( enabled = ctx.client.get_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(scene_item_id), item_id=int(scene_item_id),
) )
@ -377,68 +369,62 @@ def visible(
) )
@app.command('transform | t') @app.command(name=['transform', 't'])
def transform( def transform(
ctx: typer.Context, scene_name: Annotated[str, Argument(hint='Scene name the item is in')],
scene_name: Annotated[ item_name: Annotated[str, Argument(hint='Item name to transform in the scene')],
str, typer.Argument(..., show_default=False, help='Scene name the item is in') /,
], group: Annotated[Optional[str], Parameter(help='Parent group name')] = None,
item_name: Annotated[
str,
typer.Argument(
..., show_default=False, help='Item name to transform in the scene'
),
],
group: Annotated[Optional[str], typer.Option(help='Parent group name')] = None,
alignment: Annotated[ alignment: Annotated[
Optional[int], typer.Option(help='Alignment of the item in the scene') Optional[int], Parameter(help='Alignment of the item in the scene')
] = None, ] = None,
bounds_alignment: Annotated[ bounds_alignment: Annotated[
Optional[int], typer.Option(help='Bounds alignment of the item in the scene') Optional[int], Parameter(help='Bounds alignment of the item in the scene')
] = None, ] = None,
bounds_height: Annotated[ bounds_height: Annotated[
Optional[float], typer.Option(help='Height of the item in the scene') Optional[float], Parameter(help='Height of the item in the scene')
] = None, ] = None,
bounds_type: Annotated[ bounds_type: Annotated[
Optional[str], typer.Option(help='Type of bounds for the item in the scene') Optional[str], Parameter(help='Type of bounds for the item in the scene')
] = None, ] = None,
bounds_width: Annotated[ bounds_width: Annotated[
Optional[float], typer.Option(help='Width of the item in the scene') Optional[float], Parameter(help='Width of the item in the scene')
] = None, ] = None,
crop_to_bounds: Annotated[ crop_to_bounds: Annotated[
Optional[bool], typer.Option(help='Crop the item to the bounds') Optional[bool], Parameter(help='Crop the item to the bounds')
] = None, ] = None,
crop_bottom: Annotated[ crop_bottom: Annotated[
Optional[float], typer.Option(help='Bottom crop of the item in the scene') Optional[float], Parameter(help='Bottom crop of the item in the scene')
] = None, ] = None,
crop_left: Annotated[ crop_left: Annotated[
Optional[float], typer.Option(help='Left crop of the item in the scene') Optional[float], Parameter(help='Left crop of the item in the scene')
] = None, ] = None,
crop_right: Annotated[ crop_right: Annotated[
Optional[float], typer.Option(help='Right crop of the item in the scene') Optional[float], Parameter(help='Right crop of the item in the scene')
] = None, ] = None,
crop_top: Annotated[ crop_top: Annotated[
Optional[float], typer.Option(help='Top crop of the item in the scene') Optional[float], Parameter(help='Top crop of the item in the scene')
] = None, ] = None,
position_x: Annotated[ position_x: Annotated[
Optional[float], typer.Option(help='X position of the item in the scene') Optional[float], Parameter(help='X position of the item in the scene')
] = None, ] = None,
position_y: Annotated[ position_y: Annotated[
Optional[float], typer.Option(help='Y position of the item in the scene') Optional[float], Parameter(help='Y position of the item in the scene')
] = None, ] = None,
rotation: Annotated[ rotation: Annotated[
Optional[float], typer.Option(help='Rotation of the item in the scene') Optional[float], Parameter(help='Rotation of the item in the scene')
] = None, ] = None,
scale_x: Annotated[ scale_x: Annotated[
Optional[float], typer.Option(help='X scale of the item in the scene') Optional[float], Parameter(help='X scale of the item in the scene')
] = None, ] = None,
scale_y: Annotated[ scale_y: Annotated[
Optional[float], typer.Option(help='Y scale of the item in the scene') Optional[float], Parameter(help='Y scale of the item in the scene')
] = None, ] = None,
*,
ctx: Annotated[Context, Parameter(parse=False)],
): ):
"""Set the transform of an item in a scene.""" """Set the transform of an item in a scene."""
if not _validate_sources(ctx, scene_name, item_name, group): _validate_sources(ctx, scene_name, item_name, group)
raise typer.Exit(1)
old_scene_name = scene_name old_scene_name = scene_name
scene_name, scene_item_id = _get_scene_name_and_item_id( scene_name, scene_item_id = _get_scene_name_and_item_id(
@ -478,10 +464,12 @@ def transform(
transform['scaleY'] = scale_y transform['scaleY'] = scale_y
if not transform: if not transform:
console.err.print('No transform options provided.') raise OBSWSCLIError(
raise typer.Exit(1) 'No transform options provided. Use at least one of the transform options.',
exit_code=ExitCode.ERROR,
)
transform = ctx.obj['obsws'].set_scene_item_transform( transform = ctx.client.set_scene_item_transform(
scene_name=scene_name, scene_name=scene_name,
item_id=int(scene_item_id), item_id=int(scene_item_id),
transform=transform, transform=transform,