Skip to content

Commit

Permalink
ENH allow passing shrinking factor, store blender script in workdir
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdoc committed Apr 20, 2024
1 parent 43084a1 commit bd4407f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion blender_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def readstl(path, name):
bpy.data.objects['scan'].modifiers['Displace'].name = 'shrinking'
bpy.data.objects['scan'].modifiers['shrinking'].direction = 'NORMAL'
bpy.data.objects['scan'].modifiers['shrinking'].mid_level = 0
bpy.data.objects['scan'].modifiers['shrinking'].strength = 0.1
bpy.data.objects['scan'].modifiers['shrinking'].strength = {shrinking_factor}
try:
readstl('{customizations}', 'customizations')
Expand Down
30 changes: 24 additions & 6 deletions make_headcase.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Generate an MRI-compatible headcase from a 3D head model acquired with a Structure Sensor."""

import argparse
import os
import shlex
Expand Down Expand Up @@ -173,22 +174,29 @@ def align_scan(infile, outfile):
formats.write_stl(outfile, new_pts, new_polys)


def gen_case(scanfile, outfile, casetype="s32", nparts=4):
def gen_case(
scanfile, outfile, workdir=None, casetype="s32", nparts=4, expand_head_model=0.1
):
cwd, _ = os.path.split(__file__)
customizations = os.path.join(cwd, "stls", "default_customizations.stl")
casefile = dict(
s32="s32.stl", s64="s64.stl", n32="n32.stl", meg_ctf275="meg_ctf275.stl"
)
casefile = os.path.join(cwd, "stls", casefile[casetype])

tempdir = mkdtemp()
cleanup = False
if workdir is None:
workdir = mkdtemp()
cleanup = True

_call_blender(
blender_carve_model_template.format(
preview=casefile,
scan=scanfile,
customizations=customizations,
tempdir=tempdir,
tempdir=workdir,
nparts=nparts,
shrinking_factor=expand_head_model,
)
)

Expand All @@ -198,9 +206,10 @@ def gen_case(scanfile, outfile, casetype="s32", nparts=4):
}
with zipfile.ZipFile(outfile, mode="w") as pkg:
for fn in pieces[nparts]:
pkg.write(os.path.join(tempdir, fn), fn)
pkg.write(os.path.join(workdir, fn), fn)

shutil.rmtree(tempdir)
if cleanup:
shutil.rmtree(workdir)


def pymeshlab_version():
Expand Down Expand Up @@ -230,7 +239,7 @@ def pipeline(infile, outfile, casetype="s32", nparts=4, workdir=None):
print("Aligning head model")
align_scan(cleaned, aligned)
print("Making head case")
gen_case(aligned, outfile, casetype=casetype, nparts=nparts)
gen_case(aligned, outfile, working_dir, casetype=casetype, nparts=nparts)

if workdir is None:
shutil.rmtree(working_dir)
Expand All @@ -250,6 +259,15 @@ def pipeline(infile, outfile, casetype="s32", nparts=4, workdir=None):
type=str,
help="output headcase model (*.zip)",
)
parser.add_argument(
"--expand-head-model",
type=float,
default="0.1",
help="Expand the head model by this amount (in mm) before generating the "
"headcase. The default (0.1 mm) should work for most cases. If the resulting "
"headcase is too tight, one can try increasing this value. It is not "
"recommended to pass a value greater than 1 mm.",
)
parser.add_argument(
"--headcoil",
"-c",
Expand Down

0 comments on commit bd4407f

Please sign in to comment.