accounted for width/height/crop/alignment

This commit is contained in:
Nico Melone
2025-05-20 15:28:46 -05:00
parent 6213aa5c13
commit 5bbee7c328

View File

@@ -7,11 +7,11 @@ direction_x = random.choice([-1, 1])
direction_y = random.choice([-1, 1])
# Initialize global position
position_x = 0.0
position_y = 0.0
position_x = 1920/2
position_y = 1080/2
# Global variables holding the values of data settings / properties
source_name = "Spaceship" # Name of the source to shake
source_name = "Spaceship" # Name of the source to bounce
speed_x = 5.0 # Speed of movement in the X direction
speed_y = 3.0 # Speed of movement in the Y direction
@@ -40,6 +40,7 @@ def get_scene_item_from_source(source):
scene_item = obs.obs_scene_find_source(scene, source_name)
return scene_item
# Function to update position
def update_position():
global position_x, position_y, direction_x, direction_y
@@ -54,19 +55,63 @@ def update_position():
return
# Get canvas size
width = 490 #obs.obs_source_get_width(source)
height = 275 #obs.obs_source_get_height(source)
source_crop = obs.obs_sceneitem_crop()
obs.obs_sceneitem_get_crop(scene_item,source_crop)
source_transform = obs.obs_transform_info()
obs.obs_sceneitem_get_info(scene_item, source_transform)
print(source_transform.alignment)
width = (obs.obs_source_get_width(source) - source_crop.left - source_crop.right) * source_transform.scale.x
height = (obs.obs_source_get_height(source) - source_crop.top - source_crop.bottom) * source_transform.scale.y
#print(width, height)
canvas_width = 1920
canvas_height = 1080
print(width, height, canvas_width, canvas_height)
#print(width, height, canvas_width, canvas_height)
# Calculate new position
position_x += direction_x * speed_x
position_y += direction_y * speed_y
print(position_x, position_y)
alignment = source_transform.alignment
#print(position_x, position_y)
#Alignment map top_left:5,top_center:4, top_right:6,center_left:1,center: 0, center_right: 2, bottom_left: 9, bottom_center: 8, bottom_right: 10
# Calculate position offsets based on alignment
if alignment == 5: # Top Left
position_x_offset = 0
position_y_offset = 0
elif alignment == 4: # Top Center
position_x_offset = width / 2
position_y_offset = 0
elif alignment == 6: # Top Right
position_x_offset = width
position_y_offset = 0
elif alignment == 1: # Center Left
position_x_offset = 0
position_y_offset = height / 2
elif alignment == 0: # Center
position_x_offset = width / 2
position_y_offset = height / 2
elif alignment == 2: # Center Right
position_x_offset = width
position_y_offset = height / 2
elif alignment == 9: # Bottom Left
position_x_offset = 0
position_y_offset = height
elif alignment == 8: # Bottom Center
position_x_offset = width / 2
position_y_offset = height
elif alignment == 10: # Bottom Right
position_x_offset = width
position_y_offset = height
else: # Default to top left if alignment is unknown
position_x_offset = 0
position_y_offset = 0
# Bounce off edges
if position_x <= 0 or position_x + width >= canvas_width:
if position_x - position_x_offset <= 0 or position_x + width - position_x_offset >= canvas_width:
print(
f"Bounce off left/right: {position_x} | {position_x+width} | {canvas_width} | {direction_x}")
direction_x *= -1
if position_y <= 0 or position_y + height >= canvas_height:
if position_y - position_y_offset <= 0 or position_y + height - position_y_offset >= canvas_height:
print(
f"Bounce off top/bottom: {position_y} | {position_y+height} | {canvas_height} | {direction_y}")
direction_y *= -1
# Apply new position
@@ -134,7 +179,10 @@ def script_tick(seconds):
# Callback for the hotkey
def on_dvd_hotkey(pressed):
global is_active
is_active = pressed
if pressed and not is_active:
is_active = True
elif pressed and is_active:
is_active = False
# Identifier of the hotkey set by OBS
@@ -150,8 +198,6 @@ def script_load(settings):
obs.obs_data_array_release(hotkey_save_array)
# Called before data settings are saved
def script_save(settings):
obs.obs_save_sources()