Skip to content

Commit

Permalink
Added (optional) paths to blender & inkscape binaries in config file …
Browse files Browse the repository at this point in the history
…to make the inkscape / blender dependencies on macos work better.
  • Loading branch information
marklescroart committed Jun 21, 2018
1 parent 6bf8b58 commit 028f1d4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
14 changes: 11 additions & 3 deletions cortex/blender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

import numpy as np

from .. import options
from .. import freesurfer
from .. import dataset
from .. import utils

default_blender = options.config.get('dependency_paths', 'blender')

_base_imports = """import sys
sys.path.insert(0, '{path}')
import xdrlib
Expand All @@ -19,18 +22,18 @@
from bpy import data as D
""".format(path=os.path.split(os.path.abspath(__file__))[0])

def _call_blender(filename, code):
def _call_blender(filename, code, blender_path=default_blender):
"""Call blender, while running the given code. If the filename doesn't exist, save a new file in that location.
New files will be initially cleared by deleting all objects.
"""
with tempfile.NamedTemporaryFile() as tf:
print("In new named temp file: %s"%tf.name)
startcode=_base_imports
endcode = "\nbpy.ops.wm.save_mainfile(filepath='{fname}')".format(fname=filename)
cmd = "blender -b {fname} -P {tfname}".format(fname=filename, tfname=tf.name)
cmd = "{blender_path} -b {fname} -P {tfname}".format(blender_path=blender_path, fname=filename, tfname=tf.name)
if not os.path.exists(filename):
startcode += "blendlib.clear_all()\n"
cmd = "blender -b -P {tfname}".format(tfname=tf.name)
cmd = "{blender_path} -b -P {tfname}".format(blender_path=blender_path, tfname=tf.name)

tf.write((startcode+code+endcode).encode())
tf.flush()
Expand Down Expand Up @@ -131,6 +134,11 @@ def gii_cut(fname, subject, hemi):
def fs_cut(fname, subject, hemi, freesurfer_subject_dir=None):
"""Cut freesurfer surface using blender interface
Parameters
----------
fname : str
file path for new .blend file (must end in ".blend")
if `freesurfer_subject_dir` is None, it defaults to SUBJECTS_DIR environment variable
"""
wpts, polys, curv = freesurfer.get_surf(subject, hemi, 'smoothwm', freesurfer_subject_dir=freesurfer_subject_dir)
Expand Down
3 changes: 2 additions & 1 deletion cortex/blender/blendlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _repack(linear, n=3):
For example, _repack([1, 2, 3, 4, 5, 6], n=3) -> [[1, 2, 3], [4, 5, 6]]
Good for unravelling ravelled data
"""
return list(zip(*[iter(linear)]*3))
return list(zip(*[iter(linear)] * n))

def clear_all():
bpy.ops.object.select_all(action='SELECT')
Expand Down Expand Up @@ -112,6 +112,7 @@ def write_patch(filename, pts, edges=None):
fp.write(struct.pack('>i3f', i+1, *pt))

def save_patch(fname, mesh='hemi'):
"""Saves patch to file that can be read by pycortex"""
if isinstance(mesh, str):
mesh = D.meshes[mesh]

Expand Down
13 changes: 13 additions & 0 deletions cortex/defaults.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ default_cmap = RdBu_r
default_cmap2D = RdBu_covar
fsl_prefix = fsl5.0-

[dependency_paths]
# The following specify paths to the binary executable files
# for blender and inkscape (which are external dependencies of
# pycortex). "inkscape" and "blender" alone should work on Linux,
# since the binaries should already be on the system path.
# For MacOS, unless you have done some manual configuration on
# your own, these will need to be something like:
# blender = /Applications/Blender/blender.app/Contents/MacOS/blender
# inkscape = /Applications/Inkscape.app/Contents/MacOS/Inkscape
# Windows is not currently supported.
inkscape = inkscape
blender = blender

[mayavi_aligner]
line_width = 1
point_size = 2
Expand Down

0 comments on commit 028f1d4

Please sign in to comment.