Hi Pedro,
To insert a variable in a lilypond file:
import abjad score = abjad.Score() var = r"global = {}" ly = abjad.LilyPondFile(items=[var, score]) print(abjad.lilypond(ly))
\version "2.25.13" \language "english" global = {} \new Score <<
To reference it is complicated. Abjad LilyPond parser understands variable assigns (see: https://abjad.github.io/api/abjad/parsers/parser.html#abjad.parsers.parser.LilyPondParser) but you need to pass it together and the result is not what you want:
score = abjad.Score() parser = abjad.parser.LilyPondParser() string = r"global = {} \new Staff {\global}" staff = parser(string) score.append(staff) ly = abjad.LilyPondFile(items=[score]) print(abjad.lilypond(ly))
\version "2.25.13" \language "english" \new Score << \new Staff { { } }
I manage to get your expected result adding an empty container and attaching the variable as a LilyPondLiteral:
import abjad score = abjad.Score() var = r"global = {}" staff = abjad.Staff() cont = abjad.Container() staff.append(cont) abjad.attach(abjad.LilyPondLiteral(r"\global"), cont) score.append(staff) ly = abjad.LilyPondFile(items=[var, score]) print(abjad.lilypond(ly))
\version "2.25.13" \language "english" global = {} \new Score << \new Staff { \global { } }
Feels like hacking.
Best regards,
Davi
Pedro Faria <pedrofa...@gmail.com> writes:
I'm looking to use Abjad to help creating Lilypond templates (a simple use I know, but I'm still a beginner)Is there a way to, in abjad, define variables to be used in lilypond?For example, let's say that in my score block, instead of the music being typed out. I have only a reference to a variable, and that variable is defined elsewhere with the music to be engraved.Something like this:
global = (…)music = (…)} { Staff << >> { } } }