2
$\begingroup$

I created a Python script that I execute on an Ubuntu server. The script loads a big PLY file (around 100MB), decimates it and smooths it.

Currently, the script is quite slow (around 10 minutes to finish). As my server has 2 great GPUs (Tesla P100), I'm wondering if it's possible to use GPU instead of CPU for this script. If yes, how?

Here is the executed script:

import bpy as bpy
print("Importing PLY")
bpy.ops.import_mesh.ply(filepath="input.ply")
print("PLY imported")

print("Add decimate modifier 0.03")
bpy.ops.object.modifier_add(type='DECIMATE')
bpy.context.object.modifiers[-1].ratio = 0.03
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Decimate")
print("Decimate modifier applied")

print("Add Laplacial smooth modifier 10 1.0 0")
bpy.context.object.modifiers["Laplacian Smooth"].use_y = False
bpy.context.object.modifiers["Laplacian Smooth"].use_x = False
bpy.context.object.modifiers["Laplacian Smooth"].lambda_border = 0
bpy.context.object.modifiers["Laplacian Smooth"].iterations = 10
bpy.context.object.modifiers["Laplacian Smooth"].lambda_factor = 1.0
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Laplacian Smooth")
print("Laplacial modifier applied")

print("Exporting STL")
bpy.ops.export_mesh.stl(filepath="output.stl",   use_selection=True)
print("STL exported")
$\endgroup$
5
  • $\begingroup$ I hope you don't mind my edit. I hope it will make the question easier to understand at first glance. $\endgroup$ – Martynas Žiemys Apr 16 '19 at 10:48
  • 1
    $\begingroup$ As far as I know Blender cannot use GPU for processing geometry so Blender's Python API is not suitable for such a task. Blender's script could trigger some other software to do the processing, but then even sending large amounts of data around with Python would take a lot of time, so it seems to me what you want may not be possible/practical to achieve. $\endgroup$ – Martynas Žiemys Apr 16 '19 at 10:55
  • $\begingroup$ I agree with @MartinZ, I think Blender uses the CPU for pretty much anything except rendering (and even then only when the GPU is set up to as the rendering device) $\endgroup$ – TLousky Apr 16 '19 at 12:03
  • 1
    $\begingroup$ Imagine this question bumped this question blender.stackexchange.com/questions/115992/… Duplicate? $\endgroup$ – batFINGER Apr 16 '19 at 14:11
  • $\begingroup$ @batFINGER I agree. $\endgroup$ – Sazerac Apr 16 '19 at 23:58
2
$\begingroup$

Not an answer re using GPU.

Not sure re using GPU, can however suggest stripping this back to have only import and export operators and use API methods to add modifiers, create modified mesh, remove modifiers, swap in modified mesh.

May not speed it up too much, will if looping multiple imported objects.

import bpy
context = bpy.context
# import 
ob = context.object

#add decimate modifer
dm = ob.modifiers.new("decimate", 'DECIMATE')
dm.ratio = 0.03

# Add Laplacial smooth modifier 10 1.0 0"
lsm = ob.modifiers.new("ls", 'LAPLACIANSMOOTH')
lsm.use_y = False
lsm.use_x = False
lsm.lambda_border = 0
lsm.iterations = 10
lsm.lambda_factor = 1.0
# create modified mesh
context.scene.update()
me = ob.to_mesh(context.depsgraph, True)
# remove modifiers
ob.modifiers.clear()
# swap in the mesh
ob.data = me
# export

Note this is for 2.8. Check the docs for ob.to_mesh(..) in prior versions.

A modified mesh can also be created from object via bmesh module.

If there are materials will need to link those to new mesh too.

$\endgroup$
1
  • $\begingroup$ Thanks a lot for this sharing :-) I'll implement and test it. I m still interrested by the GPU topic. $\endgroup$ – Maxime Apr 16 '19 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.