Using ly:grob-object to access grobs with \tweak: Difference between revisions
Appearance
m Replace version="2.24.0" with version="2.24" now that the LilyWiki extension supports auto-selecting the latest release in a stable series |
m New category |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Some grobs can be accessed “laterally” from within another grob’s callback. These are usually listed as “layout objects” in the “Internal properties” section of a grob | Some grobs can be accessed “laterally” from within another grob’s callback. These are usually listed as “layout objects” in the “Internal properties” section of a grob interface. The function <code>ly:grob-object</code> is used to access these grobs. | ||
Demonstrated below are some ways of accessing grobs from within a NoteHead callback, but the technique is not limited to | Demonstrated below are some ways of accessing grobs from within a <code>NoteHead</code> callback, but the technique is not limited to <code>NoteHead</code>s. However, the <code>NoteHead</code> callback is particularly important, since it is the implicit callback used by the <code>\tweak</code> command. | ||
The example function | The console output of the example function below (<code>display-grobs</code>) is as follows. | ||
<pre> | |||
-------------------- | |||
#<Grob Accidental > | |||
() | |||
#<Grob Stem > | |||
</pre> | |||
It is probably not that useful, but it demonstrates that the grobs are indeed being accessed. | |||
<lilypond version="2.24"> | <lilypond version="2.24"> | ||
| Line 55: | Line 58: | ||
[[Category:Tweaks and overrides]] | [[Category:Tweaks and overrides]] | ||
[[Category:Included in the official documentation]] | [[Category:Included in the official documentation]] | ||
[[Category:Snippet]] | |||
Latest revision as of 23:20, 21 November 2025
Some grobs can be accessed “laterally” from within another grob’s callback. These are usually listed as “layout objects” in the “Internal properties” section of a grob interface. The function ly:grob-object is used to access these grobs.
Demonstrated below are some ways of accessing grobs from within a NoteHead callback, but the technique is not limited to NoteHeads. However, the NoteHead callback is particularly important, since it is the implicit callback used by the \tweak command.
The console output of the example function below (display-grobs) is as follows.
-------------------- #<Grob Accidental > () #<Grob Stem >
It is probably not that useful, but it demonstrates that the grobs are indeed being accessed.
\version "2.24"
#(define (notehead-get-accidental notehead)
;; notehead is grob
(ly:grob-object notehead 'accidental-grob))
#(define (notehead-get-arpeggio notehead)
;; notehead is grob
(let ((notecolumn (notehead-get-notecolumn notehead)))
(ly:grob-object notecolumn 'arpeggio)))
#(define (notehead-get-notecolumn notehead)
;; notehead is grob
(ly:grob-parent notehead X))
#(define (notehead-get-stem notehead)
;; notehead is grob
(let ((notecolumn (notehead-get-notecolumn notehead)))
(ly:grob-object notecolumn 'stem)))
#(define (display-grobs notehead)
;; notehead is grob
(let ((accidental (notehead-get-accidental notehead))
(arpeggio (notehead-get-arpeggio notehead))
(stem (notehead-get-stem notehead)))
(format (current-error-port) "~2&~a\n" (make-string 20 #\-))
(for-each
(lambda (x) (format (current-error-port) "~a\n" x))
(list accidental arpeggio stem))))
\relative c' {
%% display grobs for each note head:
%\override NoteHead.before-line-breaking = #display-grobs
<c
%% or just for one:
\tweak before-line-breaking #display-grobs
es
g>1\arpeggio
}