2
$\begingroup$

I have some points which represent polylines. I want to convert this polylines into geometry for which I am creating a curve and an object containing this object.

    curveData = bpy.data.curves.new('myCurve', type='CURVE')
    curveData.dimensions = '3D'
    curveData.bevel_resolution = 0
    curveData.bevel_depth = 1

    polyline = curveData.splines.new(type='POLY')
    polyline.points.add(len(yarn)-1)
    for point, node in zip(polyline.points, yarn):
        point.co = positions[node]
        point.radius = radii[node]

    # create Object
    obj = bpy.data.objects.new('yarn', curveData)  
    obj.active_material = mat

This code normally works fine, but for some data sets that I have it takes ages to generate all the geometry I need (~ 15 minutes).

I want to know if there is any way to send the points in my data set to the GPU and use a geometry shader to create the geoemtry I want to render. I would like to render it using the Principled BSDF shader, so the geometry shader should produce data for this shader.

$\endgroup$
  • $\begingroup$ How many spline points do you have in this context? $\endgroup$ – lemon Sep 25 at 14:52
  • $\begingroup$ I have around 20K splines. The number of control points for each spline may vary, but around 500 $\endgroup$ – jjcasmar Sep 25 at 14:53
  • $\begingroup$ I think you should generate all the splines in the same object. That will accelerate the generation. Though, that will be around 40 millions of vertices as result (so may be slow for several things) $\endgroup$ – lemon Sep 25 at 15:36
  • $\begingroup$ How can I generate all the splines in the same object? Im not sure how can I say where a spline end and where the next one begins. $\endgroup$ – jjcasmar Sep 25 at 15:46
3
$\begingroup$

A workaround can be generating all the splines in the same object.

Should be around 1 minute or less from the given information (20K splines of 500 vertices). Of course here, I've used generated coordinates.

import bpy

n_curves = 20000
n_points = 500

curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'
curveData.bevel_resolution = 0
curveData.bevel_depth = 1

for i in range( n_curves ):

    # splines.new adds a new spline inside the data (in the same object)
    polyline = curveData.splines.new(type='POLY')
    polyline.points.add(n_points)

    for n, point in zip( range( n_points ), polyline.points ):
        point.co = (n, 2*i, 0, 1)
        point.radius = 1 


# create Object
obj = bpy.data.objects.new('yarn', curveData)  
#obj.active_material = mat
bpy.context.scene.collection.objects.link(obj)

Note: I don't think you can use GPU to accelerate things. Even if possible don't think either that could be faster because of I/O needed between Blender's memory and gpu memory. (and there is no 'geometry generation shader').

$\endgroup$
  • $\begingroup$ My point with using the geometry shader was to send to the GPU the control points of the spline and generate the geoemtry in the geometry shader. No need to ahve the actual geometry on CPU side $\endgroup$ – jjcasmar Sep 25 at 16:13
  • $\begingroup$ I understand, but don't think this is possible in Blender... what do you mean by "geometry shader"? That does not exist. Eventually you can use Python gpu module, but no Principled BSDF here afaik. $\endgroup$ – lemon Sep 25 at 16:18
  • $\begingroup$ well, with geometry shader I mean the geometry shader stage in opengl $\endgroup$ – jjcasmar Sep 25 at 18:40
  • $\begingroup$ I have test this solution and works fine, the generation of all the geometry takes very little in comparison with my previous approach. What is the rationale behind this? $\endgroup$ – jjcasmar Sep 25 at 18:41
  • $\begingroup$ One object per spline makes a lot of objects in Blender in your case. This is slower than having one object with a lot of geometry. About opengl, Blender has (needs to have) another approach than game rendering engine (for instance) because geometry often changes when using Blender (and that's not, or not often, the case in real time rendering engines, like Unity or Godot, for instance). $\endgroup$ – lemon Sep 25 at 18:46

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.