Percent(LEFT) / Percent(DOWN) create an object without `length`, failing later with AttributeError
Description of bug / unexpected behavior
unit.Percent only sets self.length when the axis exactly equals X_AXIS or Y_AXIS (and raises for Z_AXIS). Negative directions such as LEFT or DOWN, or any other vector, skip every branch in __init__, so the object is created without a length attribute. The error only surfaces later in __mul__, far from the actual mistake.
Expected behavior
Percent(LEFT) should behave like Percent(X_AXIS) (and Percent(DOWN) like Percent(Y_AXIS)), and any unsupported axis should raise a clear ValueError at construction.
How to reproduce the issue
Code for reproducing the problem
from manim import DOWN, LEFT, RIGHT, UP
from manim.utils import unit
print("RIGHT:", unit.Percent(RIGHT) * 10) # works
print("UP:", unit.Percent(UP) * 10) # works
print("LEFT:", unit.Percent(LEFT) * 10) # fails
Additional media files
Images/GIFs
Not applicable: no rendering is involved.
Logs
Terminal output
RIGHT: 1.4222222222222223
UP: 0.8
Traceback (most recent call last):
File ".../manim/percent_bug.py", line 6, in <module>
print("LEFT:", unit.Percent(LEFT) * 10) # fails
~~~~~~~~~~~~~~~~~~~^~~~
File ".../manim/manim/utils/unit.py", line 31, in __mul__
return val / 100 * self.length
^^^^^^^^^^^
AttributeError: 'Percent' object has no attribute 'length'
System specifications
System Details
- OS: macOS 27.0.1 (26A434), Apple Silicon (arm64)
- RAM: 8 GB
- Python version: 3.14.7
- Installed modules: development install of Manim Community v0.22.0 from
mainviauv sync(dependencies as pinned inuv.lock, e.g. numpy 2.4.1, pycairo 1.29.0)
Not pasted in full; the bug is in pure Python and independent of dependency versions.
LaTeX details
- LaTeX distribution: not installed (not needed to reproduce)
- Installed LaTeX packages: not applicable
Additional comments
#4841 included a ValueError for invalid axes but was closed together with its docs changes. Would a small standalone fix (with tests in tests/module/utils/test_units.py) be welcome? I'd be happy to open a PR.
Hi! I'd like to work on this issue. I plan to fix the handling of LEFT and DOWN directions and add tests. Is this approach okay? Thank you!
- Hi! I'd like to work on this issue. I plan to fix the handling of LEFT and DOWN directions and add tests. Is this approach okay? Thank you!
Feel free to open a pull requests which fixes the bug.
Hi @GniLudio As mentioned in my bug report, I'd planned to fix this myself and have already started working on it locally (reproduction, test setup and the fix itself), as part of a university course. I've seen that #5089 was opened in the meantime. Would it be okay for me to open a PR with my fix as well? I'll reference #5089 in it so the two can be compared.
@manan21bhalla I would be happy to see a PR from you, but a small remark:
Currently, Percent has three hardcoded cases, one for each primary axis.
I would expect a fix to allow passing any Vector3D, instead of just introducing more hardcoded cases.
And of course, tests are very welcome.
Thanks @GniLudio, that makes sense. To make sure I implement the behaviour you have in mind: for an arbitrary direction v, I'd define the length as the extent of the frame through its centre along v, i.e. for a unit vector (dx, dy) the length is min(frame_width / |dx|, frame_height / |dy|). That gives frame_width for LEFT/RIGHT and frame_height for UP/DOWN, as now.
Some details I'd like to confirm:
- Should
vbe normalised, so thatPercent(2 * RIGHT) == Percent(RIGHT)? - For vectors with a z component, should only the x/y part be used, with
OUT/INstill raisingNotImplementedErrorsince the frame has no depth? - Should the zero vector raise a
ValueError?
I'll start on this with tests in the meantime and adjust to whatever you prefer.
Should v be normalised, so that Percent(2 * RIGHT) == Percent(RIGHT)?
No. I think passing 2*RIGHT should result in double the length than passing RIGHT.
For vectors with a z component, should only the x/y part be used, with OUT/IN still raising NotImplementedError since the frame has no depth?
I think it's better to change that behaviour and multipy the z-component with a depth of 0.
Should the zero vector raise a ValueError?
No. Same as mentioned above.
Thanks @GniLudio, that's clear. I'll scale each component by the frame size, i.e. (frame_width, frame_height, 0), and use the length of the result, so 2 * RIGHT gives double, UR gives the frame diagonal, and OUT or the zero vector give 0. Percent would still return a number (as now) rather than a vector; let me know if you'd prefer otherwise.
Percentwould still return a number (as now) rather than a vector; let me know if you'd prefer otherwise.
You probably mean Percent.length: Yes, Percent(...).length should stay as a number.
I'd like to push back on both this issue and @GniLudio's suggestion that Percent should accept arbitrary vectors, if I may.
The fact that Percent takes a vector input seems to be almost incidental - the input is used effectively as an enum value. The intended use is something like mob.shift(RIGHT * 25 * Percent(X_AXIS)), meaning "shift mob right by 25% of the screen's width". As I understand it, the syntax might as well have been 25 * Percent(AxisEnum.X). The class effectively just returns one of two floats (config.frame_[width | height] / 100) depending on which of its two expected inputs it receives.
So this particular issue's suggestion (Percent(LEFT) should behave like Percent(X_AXIS) and Percent(DOWN) like Percent(Y_AXIS)) seems like it is addressed simply by moving the negative sign elsewhere (e.g. writing mob.shift(LEFT * 25 * Percent(X_AXIS)) instead). At least to me, this seems more intuitive.
As for the other suggestion (allow passing any Vector3D), I feel like the semantics of the Percent class start to break down in this case. What does Percent([8, -5, 0]) represent in terms of screen-relative distance? What does the vector [8, -5, 0] represent in the first place? How do we expect a user to actually use this? Those are some of the questions I think I'd need answers to before I really buy this implementation change.
I'd like to hear both of your thoughts on this! Just to be 100% clear, I think you're both identifying valid enough problems. For example, it seems like constructing a vector representing "1/3rd of both the width and height of the screen" is currently done easiest with something like
33.33 * (RIGHT * Percent(X_AXIS) + UP * Percent(Y_AXIS))
so clearly there's room for improvement here.
Thanks @nikolajmunk, that's a fair point, and I agree the main use is 25 * Percent(X_AXIS) with the direction kept outside.
To answer your question: in #5098, Percent([8, -5, 0]) is the length of that vector after scaling x by the frame width and y by the frame height. So 33.33 * Percent(UR) gives a third of the frame diagonal. I'll admit the use case is narrower than for a single axis.
One thing I think holds either way: at the moment Percent(LEFT) builds an object with no length, and the error only appears later as an AttributeError. So whichever design is preferred, I think it should fail (or work) at construction.
The options I can see:
- Keep
PercenttoX_AXISandY_AXISonly, and raise a clearValueErrorfor anything else (e.g. suggestingLEFT * Percent(X_AXIS)). - Also accept negative axes (
LEFT,DOWN) as the same length as their positive axis, and raise for the rest. - Accept any vector, as currently in #5098.
@GniLudio, @nikolajmunk, could you agree on which you'd prefer? I'm happy to update #5098 to match, and I'll hold off on changes until then.
OK, I conferred with @GniLudio behind the scenes and I'm making an executive decision here: For now, let's just stick to your option 1, i.e. accepting X_AXIS and Y_AXIS and raising an exception for other input.
The main reason for this is that PR #4989 is going to be merged fairly soon. One of the added changes is that any 2D camera may now be moved and scaled, not just MovingCamera. This leaves Percent in a weird spot where config.frame_width/height is only accurate until you modify the camera. IOW we'll probably need to rethink the future of the Percent class in this brave new world where any camera can be resized, so there's no reason to drastically change its implementation right now. Feel free to update your existing PR or to create a new one! :)
Thanks @nikolajmunk and @GniLudio, that makes sense given #4989. I've updated #5098 to option 1:
X_AXISandY_AXISwork as before.Z_AXISstill raisesNotImplementedError, as onmain.- Any other input now raises a
ValueErrorat construction, with a message suggesting putting the direction outside (e.g.LEFT * 25 * Percent(X_AXIS)).
I also added a docstring (including a note that Percent uses the config frame size rather than the current camera) and tests for the rejected inputs. Happy to adjust if you'd prefer a different message or exception.