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

Issue 174 #175

Merged
merged 10 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions navis/meshes/b3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@
simp
Simplified mesh object.

Examples
--------
>>> import navis
>>> n = navis.example_neurons(1, kind="mesh")
>>> n_sm = simplify_mesh_blender(n,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea to provide some example here. Currently, this will cause an error during doctests because Blender is not installed on the Github worker.

You can either edit test-package.yml to install Blender before running pytest, or add the # doctest: +SKIP directive to exclude this line from the tests (see other such examples throughout the codebase). In theory, the former should be pretty straight forward: wget the linux distribution, unzip somewhere and add to PATH. In practice, Github Actions can be a pain in the neck to debug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's a great idea.

I had issue with the current installation with some mesa related libraries and removed graphviz-dev and mesa-vulkan-drivers from my test-package.yml. I also added the latest blender-LTS via moguri/setup-blender@v1 action.

I did not see the tests finish, but in at least one of the runs I saw the test from b3d pass. Let me know if this could work for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the solution to keeping the ubuntu libraries as they were before and installing the blender directly via wget, not relying on an external and potentially unnecessary complex action. The simplify_mesh_blender seems to pass more reliably now, the comparison between basic types and numpy types still fails, so the overall tests currently still fail.

Let me know if I should change anything else?

... F=0.2,
... inplace=False)
>>> n.n_vertices > n_sm.n_vertices
True

"""
if not tm.interfaces.blender.exists:
raise ModuleNotFoundError('No Blender 3D unavailable (executable not found).')

Check failure on line 52 in navis/meshes/b3d.py

View workflow job for this annotation

GitHub Actions / build (3.9)

No Blender 3D unavailable (executable not found).
_blender_executable = tm.interfaces.blender._blender_executable

if F > 1 or F < 0:
Expand Down
34 changes: 23 additions & 11 deletions navis/meshes/templates/blender_decimate.py.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ import os
if __name__ == '__main__':
# clear scene of default box
bpy.ops.wm.read_homefile()
try:
bpy.ops.object.mode_set(mode='OBJECT')
except BaseException:
pass
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=True)
if bpy.app.version > (3, 2, 0):
objs = [bpy.context.scene.objects['Camera'], bpy.context.scene.objects['Cube'], bpy.context.scene.objects['Light']]
with bpy.context.temp_override(selected_objects=objs):
bpy.ops.object.delete()
else:
try:
bpy.ops.object.mode_set(mode='OBJECT')
except BaseException:
pass
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=True)

# get temporary files from templated locations
mesh_pre = $MESH_PRE
mesh_post = os.path.abspath(r'$MESH_POST')

for filename in mesh_pre: # use data.objects instead of context.scene.objects
bpy.ops.import_mesh.stl(filepath=os.path.abspath(filename))

if bpy.app.version > (4, 0, 0):
bpy.ops.wm.stl_import(filepath=os.path.abspath(filename))
else:
bpy.ops.import_mesh.stl(filepath=os.path.abspath(filename))
mesh = bpy.data.objects[0]

# Make sure mesh is the active object
try:
# earlier than blender <2.8
Expand All @@ -29,13 +37,17 @@ if __name__ == '__main__':
bpy.context.view_layer.objects.active = mesh

# add decimate modifier

mod = mesh.modifiers.new('decimate', 'DECIMATE')
mod.decimate_type = 'COLLAPSE'
mod.ratio = $RATIO
mod.use_collapse_triangulate = True

bpy.ops.object.modifier_apply(modifier=mod.name)

bpy.ops.export_mesh.stl(
filepath=mesh_post,
use_mesh_modifiers=True)
if bpy.app.version > (4, 0, 0):
bpy.ops.wm.stl_export(filepath=mesh_post, apply_modifiers=True)
else:
bpy.ops.export_mesh.stl(
filepath=mesh_post,
use_mesh_modifiers=True)
Loading