-
-
Notifications
You must be signed in to change notification settings - Fork 138
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
Improve auto constraints #425
base: main
Are you sure you want to change the base?
Conversation
You add the constraints as normal, and when the context manager exits it checks if the sketch still solves. If it doesn't, it removes the last constraint and checks again. This continues until either the sketch solves, or all the constraints that were added inside the context manager are removed. The fact that the loop checks whether the sketch solves one more time after the list of new constraints is empty is deliberate; it makes sure the sketch is up to date. The loop doesn't check whether the list is empty, because when it's empty the sketch should solve (hence the check to make sure it solved before any of the constraints were added).
This operator was the simplest to add it to, and serves as an example of how to use the context manager.
The linter seems to dislike long lines. |
Great stuff! I see one major problem tho, since constraints.all is not sorted by creation date the contextmanager might remove constraints that weren't added in its context which is bad IMO. Furthermore checking if the system solves shouldn't change entities. Maybe we can add an option to the solver which doesn't apply changes. |
operators/utilities.py
Outdated
sketch = sketch or context.scene.sketcher.active_sketch | ||
constraints = constraints or context.scene.sketcher.constraints | ||
|
||
solve = solve_system(context, sketch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably avoid the initial solve check and just get the value from sketch.solver_state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I wasn't aware of that, I'll use it instead of calling the function then yeah.
Don't worry about the linter too much, it's more of an aid. |
I assumed that it'd be sorted, my bad. How would you go about getting a list that is sorted?
I'm not sure what you mean by this (probably because I don't know much about this code base). |
Instead of removing the newest constraint until the sketch solves, it just removes all of the new constraints if the sketch fails to solve. It calls solve again at the end to make sure it's updated.
It now only removes constraints that are actually new. |
I think that's a fair tradeoff and probably easy for the user to understand as the tool either creates constraints or not. |
The solver simply changes the position of geometric entities based on the constraints. We don't want that to happen always, usually only from the fini method of operators. With this PR the solver is triggered from the "main" method of the line_2d operator which runs every frame and causes the first point of the line to follow the vertical/horizontal linewhich is bad. |
Also added the report and update_entities arguments to solve_system and SlvsSketch.solve, so they're available regardless of how you call solve.
There's still two solve calls, but now they don't update entities at least. The first call is to see if any constraints failed, the second to update the UI after removing those constraints (otherwise it says "Inconsistent" during and after the operation).
Ah alright, makes sense.
I made it remove only the constraints where failed=True; though it might make sense to make this an option. |
False by default, meaning it will remove all new constraints if any of them failed. If set to True, it will only remove new constraints that failed, and keep successful new constraints.
Added the option, so by default it removes all new constraints if any of them failed; if enabled it only removes failed constraints and keeps the successful ones. |
Oh and would it be possible to set the linter to 120 line length? |
Looks good so far. Do you plan to change the other tools that add constraints as well with this PR? |
If you think the context manager is good now, I can start adding the other operators yeah. |
It looks good to me so far👍🏼 |
I could use some help coming up with cases where auto constraints fail actually. |
I think it's mainly a problem with some advanced tool like bevel/trim. If there are no cases where it fails with the simple tools we can just leave them as they are. |
I forgot there are other non-okay states that are not inconsistent, so the code doesn't work as it should in all cases. |
By using a context manager which prevents you from adding constraints that stop the sketch from solving.