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

Added Three Different Cases for Adding a New Instance #1859

Merged
merged 14 commits into from
Jul 26, 2024
Merged
33 changes: 31 additions & 2 deletions sleap/gui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,7 @@ def create_new_instance(
copy_instance=copy_instance,
new_instance=new_instance,
mark_complete=mark_complete,
location=location,
)

if has_missing_nodes:
Expand Down Expand Up @@ -2984,6 +2985,7 @@ def set_visible_nodes(
copy_instance: Optional[Union[Instance, PredictedInstance]],
new_instance: Instance,
mark_complete: bool,
7174Andy marked this conversation as resolved.
Show resolved Hide resolved
location: Optional[QtCore.QPoint],
7174Andy marked this conversation as resolved.
Show resolved Hide resolved
) -> bool:
"""Sets visible nodes for new instance.

Expand All @@ -3010,6 +3012,12 @@ def set_visible_nodes(
scale_width = new_size_width / old_size_width
scale_height = new_size_height / old_size_height

if location is not None:
reference_x = copy_instance[context.state["skeleton"].node_names[0]].x
reference_y = copy_instance[context.state["skeleton"].node_names[0]].y
offset_x = location.x() - reference_x
offset_y = location.y() - reference_y
7174Andy marked this conversation as resolved.
Show resolved Hide resolved

# Go through each node in skeleton.
for node in context.state["skeleton"].node_names:
# If we're copying from a skeleton that has this node.
Expand All @@ -3018,8 +3026,29 @@ def set_visible_nodes(
# We don't want to copy a PredictedPoint or score attribute.
x_old = copy_instance[node].x
y_old = copy_instance[node].y
x_new = x_old * scale_width
y_new = y_old * scale_height

# Handles if the new instance is created with right click
if (
location is not None
and offset_x is not None
and offset_y is not None
7174Andy marked this conversation as resolved.
Show resolved Hide resolved
):
x_old += offset_x
y_old += offset_y
7174Andy marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(copy_instance, PredictedInstance):
x_new = x_old
y_new = y_old
else:
if (x_old + 10) * scale_width <= (new_size_width - 10):
x_new = (x_old + 10) * scale_width
else:
x_new = x_old

if (y_old + 10) * scale_height <= (new_size_height - 10):
y_new = (y_old + 10) * scale_height
else:
y_new = y_old
7174Andy marked this conversation as resolved.
Show resolved Hide resolved

new_instance[node] = Point(
x=x_new,
Expand Down
5 changes: 4 additions & 1 deletion sleap/gui/widgets/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,10 @@ def show_contextual_menu(self, where: QtCore.QPoint):

menu.addAction("Add Instance:").setEnabled(False)

menu.addAction("Default", lambda: self.context.newInstance(init_method="best"))
menu.addAction(
"Default",
lambda: self.context.newInstance(init_method="best", location=scene_pos),
)

menu.addAction(
"Average",
Expand Down