Manually selecting a manim.cfg when NOT running from ClI
See original GitHub issueTitle.
It’s not possible rn, as the .cfg
files are chosen first when importing manim by a CLI flag.
This would be useful for tests, and for (maybe) a potential migrating toward a manim that can be run from python files.
Pinging @leotrs
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
Configuration - Manim Community v0.17.1
cfg . Currently, Manim does not support config files with any other name. The config file must start with the section header [CLI] ......
Read more >CLI flags and configuration - manim documentation
flag abbr function
‑‑help ‑h Show the help message and exit
‑‑version ‑v Display the version of manimgl
‑‑write_file ‑w Render the scene as a movie...
Read more >First example command returns error (get_monitors) #1389
Describe the error. I want to execute : manimgl example_scenes.py OpeningManimExample. Code and Error. Code: example_scenes.py. Error:
Read more >python - Is it possible to run manim programmatically (and not ...
The "manim" command is just a Python script, so clearly the answer is yes. You can poke through the source to find their...
Read more >Manim Tutorial Series E02: Positioning and Configuration
Manim is a free and open-source, community-maintained Python library for creating (mathematical) animations originally started by Grant ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m working on this. The tempconfig was the first step toward that goal
Ok I’ve been thinking about this, and I’ll make it my priority for now because it’s holding up other things (#318, #317). If manim could be ran programmatically (and not only from the command line), testing would be much easier too. Also, I just realized that the configuration is actually being parsed (at least) twice, because the
config._run_config()
function is being called multiple times… So here’s my plan to do this. cc @Aathish04 @huguesdevimeux @naveen521kk @eulertour @PgBiel please share your thoughts.I propose that we implement the following ways of rendering a scene using manim:
From the command line (already supported):
From a python script:
MyScene().render()
. For this one to work, we need to implement aScene.render
method. Right now, a Scene is automatically rendered when it is constructed, but this can be easily changed by moving a few lines fromScene.__init__
toScene.render
, and callingrender
inside__main__.main()
so that manim can still be called from the command line without any changes. This is an easy PR that we could implement ASAP (and, I think, regardless of the rest of this proposal). Note that this alone would simplify testing a good deal.manim.render(MyScene)
. For this one to work, we can implement arender
method in__init__.py
that just calls the newScene.render
(see the previous paragraph).manim.render(some_module)
. When called in this way, this just searches anyScene
classes insome_module
and creates them and calls theirrender
method. This is pretty much what the current__main__.main()
does.Now, the main problem (and going back to the present issue) is how to specify a config file when rendering programmatically, in any of the previous three ways. I suggest that we implement them as:
The
config
argument is a dictionary that will temporarily update the global config dict. This means that if I domanim.render(MyScene, config={"background_color": WHITE})
, then the global config will be used, but the"background_color"
option will be overriden byWHITE
. Right before returning, this function will restore the global config dict. Callingmanim.render(MyScene)
without aconfig
argument just uses the global config dict untouched. Using the other argument,config_file
, will read the specified file and add it as the highest priority to the cascading file system, and update the global config dict. The global config is again restored after the call returns. (We can perhaps cache the resulting overriden config dicts in case they are needed again in the future.)Now, all of this pivots on the fact that executing
import manim
will correctly setup the global config dict. In particular, recall thatimport manim
first executes__init__.py
which in turn executesimport config
. So the order of events would be the following:import manim
, which executesmanim/__init__.py
. This can be a user scene script or our ownmanim/__main__.py
.__init__.py
, the first line of code isimport config
which executesconfig.py
.config.py
, as much as possible of the config dict should be defined. In particular, all config files must be read and parsed. Another change I’m proposing is thatconfig.py
also parses the sections of the configuration corresponding to the logger, and exports a new dictionary calledlogger_config
. All ofconfig
,camera_config
,file_writer_config
, andlogger_config
are defined and exported. NOTE the logger is NOT set here.__init__.py
, the next line should beimport logger
. This executeslogger.py
, which will executefrom config import logger_config
, and use it to set the logger.manim.render(Scene, config=some_config_dict)
. If they do so, then the functionmanim.render
will callconfig.udpate(some_config_dict)
, render the Scene, and then restore the original global config dict. Execution ends here. (Note this is also the case for tests or docs builds which require to import manim.)__main__
because manim has been called from the command line,__init__.py
returns execution to__main__.main
. Now, this function must deal with parsing command line flags and then updating the global config dict permanently. This can easily be done by e.g.manim.config.parse_cli(args)
. Note this will remove the problem we’ve had in the past whereconfig.py
had to determine whether it had been called from the command line. Once the CLI flags have been parsed and the config updated,__main__.main
simply decides what to execute next, depending on the subcommand used. Thus, a big chunk ofcfg_subcommands
will be ported into__main__.py
. In the event that the console command requires manim to render a scene,__main__.main
can just callmanim.render(module_passed_on_the_cli)
.Note that in the process of making these changes, we will eliminate the hacky function
_from_command_line
, as well as eliminating the need of calling_run_config
twice, and there will be even more consolidation of the whole mess that is the config system. (I’m 100% guilty of it being a mess.)I hope this made some sense. I wanted to ran it by everyone before going into it because it will require a certain degree of surgery and being very careful.
Are there any objections? Did I miss anything?