kilacoda / chanim

Animation engine for explanatory chemistry videos

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

2022-12 updates necessary!

uwezi opened this issue · comments

I'm not good in handling forks on github, but here are the changes necessary to make chanim compatible with both the latest version of LaTeX/chemfig/Tikz and Manim v0.17:

in templates.py change the empty strings against "{}"
`
class ChemTemplate(TexTemplate):
def init(self,**kwargs):
super().init(**kwargs)
self.add_to_preamble("\usepackage{chemfig}")

def set_chemfig(
    self,
    atom_sep: str = "2em", ## all of these are the defaults in chemfig
    chemfig_style:str="{}",
    atom_style:str="{}",
    angle_increment:int=45,
    bond_offset:str="2pt",
    double_bond_sep:str="2pt",
    node_style:str="{}",
    bond_style:str="{}",
):
    set_chemfig = "\\setchemfig{atom sep=%s,chemfig style=%s, atom style=%s,angle increment=%d,bond offset=%s,double bond sep=%s, node style=%s, bond style=%s}" % (atom_sep,chemfig_style,atom_style,angle_increment,bond_offset,double_bond_sep,node_style,bond_style)
    self.add_to_preamble(set_chemfig)

`

in chem_objects.py recreate the class TexSymbol
from manim.mobject.text.tex_mobject import * from manim.mobject.svg.svg_mobject import VMobjectFromSVGPath class TexSymbol(VMobjectFromSVGPath): """Purely a renaming of SVGPathMobject.""" pass
At least I am able to render the first example again with these settings.

ok, some more corrections in templates.py

`

class ChemTemplate(TexTemplate):
def init(self,**kwargs):
super().init(**kwargs)
self.add_to_preamble("\usepackage{chemfig}")

def set_chemfig(
    self,
    atom_sep: str = "2em", ## all of these are the defaults in chemfig
    chemfig_style:str="",
    atom_style:str="",
    angle_increment:int=45,
    bond_offset:str="2pt",
    double_bond_sep:str="2pt",
    node_style:str="",
    bond_style:str="",
):
    set_chemfig = "\\setchemfig{{angle_increment={:d}".format(angle_increment)
    if atom_sep:
        set_chemfig += ",atom sep={:s}".format(atom_sep)
    if chemfig_style:
        set_chemfig += ",chemfig style={:s}".format(chemfig_style)
    if atom_style:
        set_chemfig += ",atom style={:s}".format(atom_style) 
    if bond_offset:
        set_chemfig += ",bond offset={:s}".format(bond_offset) 
    if double_bond_sep:
        set_chemfig += ",double bond sep={:s}".format(double_bond_sep) 
    if node_style:
        set_chemfig += ",node style={:s}".format(node_style)
    if bond_style:
        set_chemfig += ",bond style={:s}".format(bond_style)
    set_chemfig += "}"
    print(set_chemfig)
    self.add_to_preamble(set_chemfig)

class ChemReactionTemplate(TexTemplate):
def init(self,**kwargs):
super().init(**kwargs)
self.add_to_preamble("\usepackage{chemfig}")

def set_chemfig(
    self,

    ##Individual molecule params
    atom_sep: str = "2em", ## all of these are the defaults in chemfig
    chemfig_style:str="{}",
    atom_style:str="{}",
    angle_increment:int=45,
    bond_offset:str="2pt",
    double_bond_sep:str="2pt",
    node_style:str="{}",
    bond_style:str="{}",

    ## Reaction scheme params
    arrow_length:int=1,
    arrow_angle:int=0,
    arrow_style:str="",
    debug:str="false",
):
    set_chemfig = "\\setchemfig{{angle_increment={:d},arrow angle={:d},arrow coeff={:s}".format(angle_increment,arrow_angle,arrow_length)
    if atom_sep:
        set_chemfig += ",atom sep={:s}".format(atom_sep)
    if chemfig_style:
        set_chemfig += ",chemfig style={:s}".format(chemfig_style)
    if atom_style:
        set_chemfig += ",atom style={:s}".format(atom_style) 
    if bond_offset:
        set_chemfig += ",bond offset={:s}".format(bond_offset) 
    if double_bond_sep:
        set_chemfig += ",double bond sep={:s}".format(double_bond_sep) 
    if node_style:
        set_chemfig += ",node style={:s}".format(node_style)
    if bond_style:
        set_chemfig += ",bond style={:s}".format(bond_style)
    if debug:
        set_chemfig += ",scheme debug={:s}".format(debug)
    if arrow_style:
        set_chemfig += ",arrow styel={:s}".format(arrow_style)
            
    set_chemfig += "}"
    print(set_chemfig)


    self.add_to_preamble(set_chemfig)

def get_texcode_for_expression(self, expression):
    """Inserts expression verbatim into TeX template.

    Parameters
    ----------
    expression : :class:`str`
        The string containing the expression to be typeset, e.g. ``$\\sqrt{2}$``

    Returns
    -------
    :class:`str`
        LaTeX code based on current template, containing the given ``expression`` and ready for typesetting
    """
    return self.body.replace(self.placeholder_text, "\n\\schemestart\n"+expression+"\n\\schemestop\n\n")

def get_texcode_for_expression_in_env(self,expression,environment):
    """Inserts an expression wrapped in a given environment into the TeX template.

    Parameters
    ----------
    environment : :class:`str`
        The environment in which we should wrap the expression.
    expression : :class:`str`
        The string containing the expression to be typeset, e.g. ``"$\\sqrt{2}$"``

    Returns
    -------
    :class:`str`
        LaTeX code based on template, containing the given expression and ready for typesetting
    """
    print(environment)
    begin = r"\begin{" + environment + "}" + "\n\\schemestart"
    end = "\\schemestop\n" + r"\end{" + environment + "}"
    print(begin,end)
    return self.body.replace(
        self.placeholder_text, "{0}\n{1}\n{2}".format(begin,expression, end)
    )

`

After I modified the ChemTemplate and chem_objects.py code the way you did, the generated video (.mp4) has some problems, all the chemical element symbols overlap, but the generated gifs are correct, but I don't know why, sad.

This is almost too long ago for me to remember what I did, how and why.
Let me check later...