3
$\begingroup$

Ok, what I'm asking could not be so mainstream. I'm writing an addon for blender that will add a better particle system, and I am in the process of reating an operator that spawns particles and animates them during time. Untill now I've used this procedure to create a particle object:

  • I created a plane,
  • Merged it at the center
  • removed doubles

So That I would have only a vertex. And then to add datas like particle age, the frame it was born, etc, I used object Custom Properties.

the problem is that Blender struggles to keep upp with thousands of different objects to the point where it doesn't even play the animation at all or just crashes.

But I noticed that it could hold more than 20000 particles at a time without any problem. I want to state that I know python but not so much C++ or C, so excuse me if the following argument is wrong, but I think that blender cannot hold so many object cause they have much more to them than a single particle, for example an objects has Material Slot, Particle Systems, Location, And Much More and It's also treated in a completely different way than a particle which on the other side only has few properties.

given the Argument is true (Escuse me if it isn't) could I create a simple class in a c++ file like this:

class better_particle{
    public;
    int age;
    vector Location;
    }

And create instead of a normal object a better_particle object to represent each particle, and also important draw that better_particle Class into the viewport using for example a Rectagle (Like For Example blender's Default Particle)?

I think this is possible modifying the Blender source code to fit my needs, but I want to keep it a Blender addon and not just a custom Build.

So is this possible?

EDIT: I Cannot use Animation Nodes as it hasn't the flexibility of an addon, and The Tree as long as I know it's executed each frame, so particle will not be able to spawn or move in subframes and this would lead to lagging streaks of particles. The same problem that there's in blender by default.

WHAT I'm TRYING TO ACHIEVE:

I've Written this simple Python Code:

class particle:
pass

created_particles = []
for f in range(0,40): #For each of 40 Frames
    for i in range(0, 30): # Create 30 Particle
        pp = particle() ##Create New Particle Object
        pp.location = (0,0,0) #Add location variable
        pp.spawn_frame = f
        created_particles.extend([pp]), 

As you can see it just creates some particle object ad assign them some variables. Now I need to Create the particle class in c++ with something like the lines up above and then in Python do something like:

p = bpy.ops.add_particle()  
p.location = (0,0,0)
etc  

and the Last Thing I need to do it's to create a drawing function in c++ do display these particle objects.

$\endgroup$
  • $\begingroup$ Are you by any chance using operators to create plane, merge vertices and remove doubles? $\endgroup$ – batFINGER May 12 '18 at 17:13
  • $\begingroup$ I'm using a function that does the creating, merging and removing doubles. And that function is inserted into a modal operator run every frame. $\endgroup$ – Fabrizio May 12 '18 at 17:16
  • 1
    $\begingroup$ I mean, if all is inside a blender object, finally, you need only one blender object. So, your code can associate (create) verts/polys you want inside of it and follow the particles age/location. In other words, your code can maintain a link between each particle and corresponding verts/polys. $\endgroup$ – lemon May 14 '18 at 12:38
  • 1
    $\begingroup$ you could save the size color etc in vertexgroups and vertexcolors. It would be comparably easy to implement. If you later decide to replace the vertices with cube you can do so with the duplication feature I think as long as you dont want rotating particles (could make trouble in subframes I didn't try) $\endgroup$ – HenrikD May 14 '18 at 19:49
  • 2
    $\begingroup$ AFAIK you can't use C# at all without custom build unless you hook into external executable somehow. Not trying to be smart but how is this a better particle? What are you trying to achieve that you can't do by python scripting the existing particle system? You definitely don't want to create an object per particle. Make a single mesh and store particles as vertices. You can store surprisingly lot of data per vertex natively and what you can't (like velocity) you could store as custom data array built in vertex order. But it won't perform better than native C# particles, probably worse. $\endgroup$ – kheetor May 15 '18 at 10:28
2
+50
$\begingroup$

I suggest writing your code in Python first to keep it simple and get results quickly (especially when you have little experience with where are the bottlenecks).

Then you need to benchmark it and find where the bottlenecks are.

  • if they are on Blender's side, porting your code to c/c++ will have little to no effect. Building your own Blender is the solution.

  • if they are on your code's side consider using Numpy module rather than writing custom C/C++ code first. If you want speedup on arrays or math, it is often enough. Threading can also help.

    For most use cases when you need to wrap C/C++ classes the ctypes module is all you need.

    You can also use Cython to expose parts of C/C++ library to Python.

    For another solution for Python binding to C/C++ code look into Boost.Python:

    The Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes, functions and objects to Python, and vice-versa, using no special tools

Last thing to consider is that Blender 2.8 version is behind corner and a lot of things will change including the particle system. Some coding time on an addon this big might be wasted when it will release and you will have to re-work it or it becomes redundant by the new native system.

$\endgroup$
  • $\begingroup$ Hi Jaroslav, are you able to help with wrapping C++ calls in blender? $\endgroup$ – Denis Sep 24 '18 at 18:29
  • $\begingroup$ @Denis Yes, when you write your question just link it here or ping me in chat, I'll take a look. $\endgroup$ – Jaroslav Jerryno Novotny Sep 24 '18 at 20:52
  • $\begingroup$ I added some info here chat.stackexchange.com/rooms/83603/… $\endgroup$ – Denis Sep 24 '18 at 21:21

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .