0
$\begingroup$

I'm creating a human baby AI in Blender, and am going to have it randomly generate actions and simultaneously move all its body motors.

Will python in Blender allow parallel programming?

$\endgroup$
  • 2
    $\begingroup$ The question seems to suggest that "simultaneously move all its body motors" would require parallel programming. Is this what you are intending to use parallel programming for? $\endgroup$ – ideasman42 Jun 18 '17 at 14:41
2
$\begingroup$

In Blender if you want to make use of multiple CPU cores for your scripts - look into multi-processing.

Since Python's threading does not take advantage of multiple CPU's and is mainly used so multiple tasks can run without one task blocking all the others.


Note that its questionable if you even need multi-processing for this task.

Also take a look at numba (jit accelerated Python which is compatible with Blender), it can give real speedups for numeric tasks and easier to integrate then alternatives.

$\endgroup$
1
$\begingroup$

I was looking at the question, and @Doyousketch2's answer and the subsequent discussion, and I think you are both talking around the issue. @FriendlyPerson44 is right that there are plenty of tasks that are computationally infeasible to do single-threaded (especially in real time) on currently modern hardware. @Doyousketch2 is also right that Blender shouldn't have to care because it should really only be communicating with anything (except for the isolated rendering pipeline) sequentially.

Your specific question: I don't know if Blender supports in process multi-threaded Python extensions, but I wouldn't recommend using them. Put anything multithreaded outside the Blender process.

Summary answer:

  • Your primary capability concern should be if your GPU(s?) can service your concurrent algorithm and the Blender rendering pipeline simultaneously. (All the rest are implementation details.)
  • Your concurrent algorithm should probably be written in OpenCL, not Python, and use Python to "direct traffic" (data and execution flow control).
  • To save your sanity, even if Blender allows multi-threaded in-process Python extensions (which I doubt), you are probably best served keeping such messiness OUTSIDE the Blender process, and use IPC to pass data to your parallel application.
  • Protip: This isn't a project for newcomers to concurrency programming. Start with smaller projects and build to this.

Details:

  • Python does support multi threading: https://www.tutorialspoint.com/python/python_multithreading.htm
  • It's likely that Python embedded in Blender doesn't support multi-threading because supporting it properly is rather messy in any program. In this case, I think multi-threaded Python is rather beside the point here. (See below).
  • Even if multithreaded embedded Python is supported, it's probably more maintainable and portable to keep anything multithreaded in a separate process and use IPC to send data back and forth.
  • If you want to run on the GPU, Python being multithreaded is really besides the point, because Python doesn't run on any GPUs. You need a language like OpenCL (or CUDA), but you can run the OpenCL code from Python, and then use Python mainly for directing traffic, which is generally your main concern when doing concurrent software development.
  • Be aware that concurrent programming, whether multi-threaded on the CPU, or distributed on a GPU, is a very different kettle of fish than sequential software. Your main concerns should include:
    • Preventing any thread from modifying data while another thread is trying to access the same data.
    • Preventing multiple threads from deadlocking, waiting for resources each other is holding: See The Dining Philosophers Problem
    • Several other synchronization issues that can often lead to resource starvation.
    • Dividing up the workload, and reassembling the results into a linear stream.
  • As a highly skilled developer, I will assume you are familiar with Test Driven Development, and simply state that I highly recommend it for intricate interactions like concurrency.
  • Within the context of TDD, I'll share these pearls of wisdom that I attribute mostly to Robert C. Martin in Chapter 13 (entitled Concurrency) of his book Clean Code:
    • Isolate all your algorithmic code from all of your concurrency code
    • Test all the algorithmic code, that you intend to run concurrently, in a completely single-threaded fashion.
    • Test all your concurrency code independently as well.
    • He also discusses several ways of reproducing several common concurrency issues that are normally hard to test for.

I hope this helps answer your concurrency questions with Blender

$\endgroup$
0
$\begingroup$

Just set it to iterate through each bone during every frame.

Move each one a fraction of the desired direction,
based upon the expected amount of frames for that motion.

Say his arm moves for 10 sec's @ 30 fps ...so 300 frames.

Lets say it starts with rotation X at 0°, and it'll end up at 60°
Every frame it moves 1/300th of 60° ...or 0.2°

If you want to get fancy, you can write a function
to ease the motion in and out.

But yeah, the trick to do it is called iteration, not parallel programming.

$\endgroup$
  • $\begingroup$ What if I 100% need the parallel O.O $\endgroup$ – Friendly Person 44 Mar 23 '17 at 13:24
  • $\begingroup$ Movement is an illusion. Look at the way any game is programmed. They move sprites between frames. You only see the update. Simply keep track of the motion of each individual bone with variables, deltaX, deltaY, deltaZ. If it's positive, the bone will move that amount each frame. Negative, in the opposite dir. 0 = no motion. $\endgroup$ – Doyousketch2 Mar 23 '17 at 13:36
  • $\begingroup$ I'm creating a massive neural network that requires massive parallelism on a GPU. $\endgroup$ – Friendly Person 44 Mar 23 '17 at 15:08
  • $\begingroup$ ...and you don't know how to iterate through objects??? $\endgroup$ – Doyousketch2 Mar 23 '17 at 16:21
  • $\begingroup$ I don't understand that. I'm only begging you to tell me if Blender Python allows parallel so I could have a ex. neural network brain with 8 million operations firing about. Later I'll (again) try googling if python is parallel, but blender may not allow many things to happen at the same moment. I searched online and it doesn't look good, then again I don't know that because code (and installing/finding installer!) is annoyingly messy combined with "googling" (EV3 programming blocks I could instantly fluently use fast). $\endgroup$ – Friendly Person 44 Mar 23 '17 at 17:28

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.