Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pbrt): escape string field when it's a file path #466

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions Render/renderers/Pbrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
#
# (same as povray)

import itertools
import os
import re
import itertools
import textwrap

import FreeCAD as App
Expand Down Expand Up @@ -111,7 +111,7 @@ def write_mesh(name, mesh, material, **kwargs):
{matval.write_textures()}
{material}
Shape "plymesh"
"string filename" [ "{plyfile}" ]
"string filename" [ "{_pbrt_escape_string(plyfile)}" ]
AttributeEnd
# ~Object '{name}'
"""
Expand Down Expand Up @@ -219,7 +219,7 @@ def write_imagelight(name, image, **_):
LightSource "infinite" "string filename" "{m}"
AttributeEnd
# ~Imagelight '{n}'\n"""
return snippet.format(n=name, m=image)
return snippet.format(n=name, m=_pbrt_escape_string(image))


def write_distantlight(name, color, power, direction, angle, **kwargs):
Expand Down Expand Up @@ -466,7 +466,7 @@ def _write_texture(**kwargs):

# Compute snippet (transformation is in uv...)
snippet = f""" Texture "{texname}" "{textype}" "imagemap"
"string filename" "{filebasename}"
"string filename" "{_pbrt_escape_string(filebasename)}"
"string mapping" "uv"
"string encoding" "{encoding}"
"""
Expand Down Expand Up @@ -585,6 +585,29 @@ def _write_texref(**kwargs):
# ===========================================================================


def _pbrt_escape_string(s):
"""
Escapes special characters in a string to ensure it can be safely written
as a string in pbrt file.

Character list comes from: https://github.com/mmp/pbrt-v4/blob/master/src/pbrt/parser.cpp#L100
"""
return s.translate(
str.maketrans(
{
"\b": r"\b",
"\f": r"\f",
"\n": r"\n",
"\r": r"\r",
"\t": r"\t",
"\\": r"\\",
"'": r"\'",
'"': r"\"",
}
)
)


def _format_list(inlist, elements_per_line, indentation=6):
"""Format list of numbers, to improve readability."""
elements_per_line = int(elements_per_line)
Expand Down