Skip to content

Commit

Permalink
Merge pull request #466 from 7sDream/fix/pbrt/windows-path-escape
Browse files Browse the repository at this point in the history
fix(pbrt): escape string field when it's a file path
  • Loading branch information
howetuft authored Nov 29, 2024
2 parents 44b6d44 + c34be25 commit 6dabe49
Showing 1 changed file with 27 additions and 4 deletions.
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

0 comments on commit 6dabe49

Please sign in to comment.