Skip to content

Commit

Permalink
New line tracing with Freestyle; move all colour theming into appropr…
Browse files Browse the repository at this point in the history
…iate section of config file; make arrow colour configurable; provide alternate colour scheme
  • Loading branch information
ajjackson committed Feb 22, 2016
1 parent 282d9c1 commit 91f95fb
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 13 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ The simplest way to get up and running is
## Requirements

- Some means of generating .ascii files. We like [Phonopy](http://phonopy.sourceforge.net).
- A recent version of Blender; development is currently with
Blender 2.73 and later, although some effort exists to preserve compatibility with earlier versions.
[Ubuntu repositories are often quite far behind...]
- A recent version of Blender; development is currently with Blender 2.76 and later.
- [Imagemagick](http://www.imagemagick.org) tools (specifically "convert" and "montage") are used for image conversion and tiling.
This is available in most package managers and may even be pre-installed with your Unix-like operating system.
- The GUI uses Tkinter with the python image library. On Linux this is typically packaged as `python-imaging-tk`. Mac OSX and Windows Python distributions tend to include Tkinter, but it may be necessary to also install a PIL implementation such as Pillow.
Expand Down
4 changes: 2 additions & 2 deletions addons/vsim2blender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"name": "ascii phonons",
"description": "Generate phonon mode visualisations from ASCII input files",
"author": "Adam J. Jackson",
"version": (0,4,1),
"blender": (2, 65, 0),
"version": (0,5,0),
"blender": (2, 67, 0),
"location": "",
"category": "Import-Export",
"tracker_url": "https://github.com/ajjackson/ascii-phonons/issues"
Expand Down
Binary file modified addons/vsim2blender/arrow_cylinder.blend
Binary file not shown.
116 changes: 113 additions & 3 deletions addons/vsim2blender/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def open_mode(ascii_file, mode_index, supercell=[2,2,2], animate=True, camera_ro
mass=mass,
rot_euler=vector_to_euler(arrow_vector),
scale=arrow_vector.length*arrow_magnitude)
bpy.data.materials['Arrow'].diffuse_color = string_to_list(config.get('colours','arrow',fallback='0. 0. 0.'))

# Position camera and colour world
# Note that cameras as objects and cameras as 'cameras' have different attributes,
Expand All @@ -324,12 +325,12 @@ def open_mode(ascii_file, mode_index, supercell=[2,2,2], animate=True, camera_ro
field_of_view=0.2, miller=miller, scene=bpy.context.scene)

bpy.context.scene.world = bpy.data.worlds['World']
bpy.data.worlds['World'].horizon_color = [float(x) for x in
config['general']['background'].split()]
bpy.data.worlds['World'].horizon_color = string_to_list(config.get(
'colours','background', fallback='0.5 0.5 0.5'))

def setup_render(start_frame=0, end_frame=None, n_frames=30, preview=False):
"""
Setup the render setting
Setup the render (old style)
:param n_frames: Animation length of a single oscillation cycle in frames
:type n_frames: Positive int
Expand Down Expand Up @@ -357,6 +358,115 @@ def setup_render(start_frame=0, end_frame=None, n_frames=30, preview=False):
bpy.context.scene.frame_start = start_frame
bpy.context.scene.frame_end = end_frame

def setup_render_freestyle(start_frame=0, end_frame=None, n_frames=30,
preview=False, config=False):
"""
Setup the render setting
:param n_frames: Animation length of a single oscillation cycle in frames
:type n_frames: Positive int
:param start_frame: The starting frame number of the rendered animation (default=0)
:type start_frame: int or None
:param end_frame: The ending frame number of the rendered animation (default=start_frame+n_frames-1)
:type end_frame: int or None
:param preview: Write to a temporary preview file at low resolution instead of the output. Use first frame only.
:type preview: str or Boolean False
:param config: Configuration settings -- this function makes use of
'box_thickness' and 'outline_thickness' keys in [general] section
and 'outline' and 'box' keys in [colours] section
:type config: configparser.ConfigParser
"""
if type(start_frame) != int:
start_frame = 0
if preview:
end_frame = start_frame
elif type(end_frame) != int:
end_frame = start_frame + n_frames - 1

if not config:
config = vsim2blender.read_config()

bpy.context.scene.render.resolution_x = 1080
bpy.context.scene.render.resolution_y = 1080
if preview:
bpy.context.scene.render.resolution_percentage = 20
else:
bpy.context.scene.render.resolution_percentage = 50

bpy.context.scene.frame_start = start_frame
bpy.context.scene.frame_end = end_frame

bpy.context.scene.render.use_freestyle=True

# Wireframe box and add to "Group" for exclusion from outlining
# Freestyle doesn't work with wireframes
bpy.data.materials['Bounding Box'].type = 'SURFACE'
bpy.data.objects['Bounding Box'].select = True

bpy.ops.object.modifier_add(type='SUBSURF')
mesh_to_wireframe(bpy.data.objects['Bounding Box'])
mark_edges(bpy.data.objects['Bounding Box'])
# bpy.ops.object.group_add()

# Bounding box line settings
bpy.ops.scene.freestyle_lineset_add()
boxlines = bpy.context.scene.render.layers['RenderLayer'].freestyle_settings.linesets.active
boxlines.linestyle.thickness = float(config.get('general','box_thickness', fallback=5))
boxlines.linestyle.color = string_to_list(config.get('colours', 'box', fallback='1. 1. 1.'))
# Bounding box tracer ignores everything but Freestyle marked edges
boxlines.select_silhouette = False
boxlines.select_border = False
boxlines.select_crease = False
boxlines.select_edge_mark = True

# Outline settings
bpy.ops.scene.freestyle_lineset_add()
atomlines = bpy.context.scene.render.layers['RenderLayer'].freestyle_settings.linesets.active
atomlines.linestyle.thickness = float(config.get('general','outline_thickness', fallback=3))
atomlines.linestyle.color = string_to_list(config.get('colours', 'outline', fallback='0. 0. 0.'))
# # Ignore members of 'Group' (i.e. the bounding box) when tracing thin black lines
# atomlines.group_negation = 'EXCLUSIVE'
# atomlines.group = bpy.data.groups['Group']

def string_to_list(string):
return [float(x) for x in string.split()]

def mesh_to_wireframe(bpy_object):
"""
Create and apply a wireframe modifier to a mesh object
:param bpy_object: Simple mesh object to convert to a wireframe
:type bpy_object: Blender object
:returns wire_object: Original object with applied wireframe
:rtype wire_object: Blender object
"""
bpy.context.scene.objects.active = bpy_object
bpy_object.select = True
bpy.ops.object.modifier_add(type='WIREFRAME')
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Wireframe")
return bpy_object

def mark_edges(bpy_object):
"""
Mark all the edges of an object's mesh for Freestyle
:param bpy_object: Object to mark. Must have a mesh.
:type bpy_object: Blender object
:returns marked_object: Original object with Freestyle marks
:rtype marked_object: Blender object
"""
bpy.context.scene.objects.active = bpy_object
bpy_object.select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.mark_freestyle_edge()
bpy.ops.object.mode_set(mode='OBJECT')
return bpy_object


def render(scene=False, output_file=False, preview=False):
"""
Render the scene
Expand Down
6 changes: 6 additions & 0 deletions addons/vsim2blender/settings.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[general]
box_thickness = 5
outline_thickness = 3

[colours]
background = 0.5 0.5 0.5
box = 1. 1. 1.
outline = 0. 0. 0.

2 changes: 1 addition & 1 deletion ascii_phonons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def call_blender(input_file, blender_bin=False, mode_index=0, supercell=(2,2,2),
start_frame={start_frame}, end_frame={end_frame},
preview={preview}, do_mass_weighting={do_mass_weighting},
camera_rot={camera_rot}, miller={miller}, zoom={zoom})
vsim2blender.plotter.setup_render(n_frames={3}, start_frame={start_frame}, end_frame={end_frame}, preview={preview})
vsim2blender.plotter.setup_render_freestyle(n_frames={3}, start_frame={start_frame}, end_frame={end_frame}, preview={preview}, config=config)
vsim2blender.plotter.render(output_file='{out_file}', preview={preview})
""".format(input_file, mode_index, animate, n_frames, vectors,
scale_factor, vib_magnitude, arrow_magnitude,
Expand Down
18 changes: 16 additions & 2 deletions docs/Config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ Parameters and section headers are *case-sensitive*, and are lower-case except f
Settings
--------

**settings.conf** currently hosts one section, ``[general]``, with the following parameters:
**settings.conf**, which lies inside the **vsim2blender** package, contains default settings that are not related to specific elements.

* ``background = R G B`` The background colour is set with three floating point RGB values. These range between 0 and 1.
::

[general]
box_thickness = 5
outline_thickness = 3

[colours]
background = 0.5 0.5 0.5
box = 1. 1. 1.
outline = 0. 0. 0.
Elements
--------

Expand Down Expand Up @@ -56,6 +65,11 @@ welcome for a more mainstream pallette. The values are RGB tuples, with values r
[colours]
Cu = 0.8 0.3 0.1

User configuration
------------------
An example user configuration file, with an alternative colour scheme, is included in the main project directory as **example.conf**. Note that the colour information for elements and for other parts of the image may be mixed freely.


.. [1] www.nist.gov/pml/data/comp J. S. Coursey, D. J. Schwab, J. J. Tsai, and R. A. Dragoset, NIST Physical Measurement Laboratory
.. [2] http://dx.doi.org/10.1039/B801115J B. Cordero *et al.* (2008) *Dalton Trans.* **2008** (21) 2832-2838
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
# built documents.
#
# The short X.Y version.
version = u'0.4'
version = u'0.5'
# The full version, including alpha/beta/rc tags.
release = u'0.4.1'
release = u'0.5.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
14 changes: 14 additions & 0 deletions example.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[general]

box_thickness = 8
outline_thickness = 2

[colours]

background = 0.0 0.02 0.03
box = 0.3 0.5 1.0
outline = 1.0 1.0 1.0
arrow = 1.0 1.0 1.0

S = 0.6 0.6 0.0
Sn = 0.3 0.5 0.4

0 comments on commit 91f95fb

Please sign in to comment.