When setting scad-extra-args buffer-locally in a scad-mode buffer (e.g. via .dir-locals.el, to pass project-specific -D variables), the setting has no effect on scad-preview.
Root cause: scad--preview-render executes with current-buffer bound to the *scad preview: ...* buffer (scad-preview-mode), not the originating scad-mode source buffer. Buffer-local variables set for scad-mode are therefore invisible when the command is assembled, and the global default (nil) is used instead.
Motivating use case: import() of external mesh files (STL etc.) resolves relative paths against the actual script file location — but scad--preview-render always writes the buffer to a fresh temp file elsewhere (make-temp-file), so relative imports always fail in preview even though they work fine when opening the file directly in the OpenSCAD app. Passing a project-relative -D project_dir="/abs/path" from the source buffer would be a reasonable workaround, but currently can't reach the preview process due to the buffer-context mismatch above.
Working local workaround (via advice, since scad--preview-render is private):
(advice-add 'scad--preview-render :around
(lambda (orig-fn &rest args)
(let* ((src-file (and (buffer-live-p scad--preview-buffer)
(buffer-local-value 'buffer-file-name scad--preview-buffer)))
(scad-extra-args
(if src-file
(append (list "-D" (format "project_dir=\"%s\""
(directory-file-name (file-name-directory src-file))))
scad-extra-args)
scad-extra-args)))
(apply orig-fn args))))
Suggested fix: When assembling scad-extra-args in scad--preview-render, additionally read the buffer-local value from scad--preview-buffer (the source buffer), not just from the current (preview) buffer — e.g. via buffer-local-value.
Environment
- scad-mode version: 20260519.1039 (revision 8798dca)
- Emacs: GNU Emacs 30.2 (build 1, aarch64-apple-darwin25.4.0), macOS 26.5.1
When setting
scad-extra-argsbuffer-locally in ascad-modebuffer (e.g. via.dir-locals.el, to pass project-specific-Dvariables), the setting has no effect onscad-preview.Root cause:
scad--preview-renderexecutes withcurrent-bufferbound to the*scad preview: ...*buffer (scad-preview-mode), not the originatingscad-modesource buffer. Buffer-local variables set forscad-modeare therefore invisible when the command is assembled, and the global default (nil) is used instead.Motivating use case:
import()of external mesh files (STL etc.) resolves relative paths against the actual script file location — butscad--preview-renderalways writes the buffer to a fresh temp file elsewhere (make-temp-file), so relative imports always fail in preview even though they work fine when opening the file directly in the OpenSCAD app. Passing a project-relative-D project_dir="/abs/path"from the source buffer would be a reasonable workaround, but currently can't reach the preview process due to the buffer-context mismatch above.Working local workaround (via advice, since
scad--preview-renderis private):Suggested fix: When assembling
scad-extra-argsinscad--preview-render, additionally read the buffer-local value fromscad--preview-buffer(the source buffer), not just from the current (preview) buffer — e.g. viabuffer-local-value.Environment