Lompat ke konten Lompat ke sidebar Lompat ke footer

Widget HTML #1

Animations Python

Animations Python

Did you know that you could program in Blender to automatically create 3D objects and animations? Mina Pêcheux, Full stack web and game developer shares her best tips and trix to make it happen. Read the full guide here.

Blender is a well-known piece of software for 3D modeling, sculpting, texturing, animation and more. As the versions kept on coming out, this tool has slowly earned its place in the CGI industry, so much so that there are now a few long-feature films that are made entirely using Blender, and Youtube channels like Blender Guru entirely dedicated to learning the ins and outs of this software. And all this while it's completely free and open-source.

Animations

This has introduced a big change of mindset in the world of 3D, because it has showed people that anyone could have a go at this artform and achieve pretty incredible results.

Improving Time Series Animations In Matplotlib (from 2d To 3d)

So yes, at first, CGI software is primarily aimed at artists. But Blender, just like its competitors, also has another advantage for developers: a way to program your 3D scenes.

Today, I want to show how this programmatic approach to Blender allows you to instantly create a basic solar system, like this one:

Are you ready? Then let’s dive in! If you want to get the code directly, it's available as a Github gist 🚀

Tkinter Python Animation Create An Animation That

You might be wondering why it’s interesting to have a Python API for a 3D software. I mean, why embed a creative tool with stuff for coders?

There are, in truth, lots of use cases where it can be useful to automate a task: whether you want to quickly randomise your scene population algorithm, count objects and get custom stats on your scene – or even create an entire world from scratch that can be reproduced accurately with seeds. Having a way to integrate procedural generation or tailor-made tools directly into a CG context is just an amazing opportunity!

Moreover, the fact that it’s in Python, a language known for being easy-to-learn and for which there is just an endless number of tutorials online, makes it perfect for beginners to dive in to (without the fear of old C/C++-based APIs often requiring, in my opinion, higher coding skills).

Ic Python Api:animating A Character

By the way: this Python API is not just a shiny toy for wannabe-devs: it’s actually part of the Blender software itself and it’s used internally by the program as a core tool, although the user inputs and the results are wrapped in a user-friendly UI.

The API in itself,  bpy (for “Blender Python”), can be browsed on Blender’s specific docs; it is subdivided in several submodules, the 3 most important and commonly used being:

Today, we’ll focus on generating objects thanks to this API, so we’ll be doing some procedural generation. I’ve already discussed the benefits of this approach in other articles; roughly put, procedural generation is about defining a set of rules and a machine that uses those rules to automatically create valid instances. The big advantage is that, once you’re done making the engine, you can create as many instances as you want! Moreover, this generation can be pretty quick, dynamic depending on given conditions and infinite (for example for never-ending games like runners). But, of course, it’s usually a bit harder than hand-design because you need to teach your software the right and wrong patterns (i.e., the “rules”).

D Trajectory Animated Using Matplotlib (python)

Procedural generation is a very vast and complex topic. There are plenty of tools for generation engines – and to be honest, a large amount relies on randomness. The idea is to start off with random values and then somehow “control the craziness” so it is valid.

To see how powerful Blender’s Python API can be and how to use it, let’s work on a basic example: instantiating a bunch of planets around a sun (all being simple spheres) with random size, speed and colour, and having the planets rotate around the sun along circular orbits.

 be a physical simulation. We will pick all of our values at random and there will be no logical relationship whatsoever between the distance to the sun, the radius and the surface colour of the planets. The goal here is just to play around with Blender’s Python API, to see how to do some procedural generation and to have fun with glowy spheres.

Introducing Animatplot: Making Interactive Animated Plots With Matplotlib Easy (my First Library)

First things first: let’s see how to setup our Blender for Python and learn the different tools we have at our disposal.

Animation

Note: this tutorial uses screenshots from Blender 2.93. Also, I will be using the Python API from Blender 2.8+, so make sure your version matches.

When you want to work with Python in Blender, something that is quite useful is to start your Blender with the console attached. This will allow you to see some logs if your Python script errors, which is pretty essential for proper debugging!

Tips For Using Python Libraries To Create 3d Animation

Basically, the idea is to launch Blender from your terminal rather than from your Application shortcut. The exact path to your Blender executable depends on your OS; I’m using Mac, so all I have to do is open a terminal and then run something like this:

To check that everything is working, simply click on the “+ New” button of the Text Editor, write a basic print('hello world') line in the Python script and click the “run” icon on the right (or use the shortcut ; make sure your mouse is hovering the Text Editor panel for this to work).

The parameters are pretty self-explanatory: we are creating a sphere with a radius of 3 at the origin point, with a normalised scale. (Feel free to browse the docs for more details on the available options)

How To Create Animations In Python?

Once again, run your code by clicking the “run” icon or with the shortcode. And tadaa! We’ve just created a simple UV sphere in our 3D scene purely via scripting!

Making N_PLANETS instances is straight-forward: we’ll simply wrap our primitive_uv_sphere_add() call in a for-loop and run it multiple times. To keep the script readable, we’ll actually extract this instantiation process into a util function,  create_sphere(), and we’ll pass it in the random radius and distance.

Create

Also, we should name our objects properly: this is interesting to keep our hierarchy clear and it will be essential when, later on, we automatically clean up the scene upon script initialization. I’ll simply call each object “Planet-00”, “Planet-01” and so on, for the N_PLANETS spheres.

Make Animations With Python

If you run this again, you should get a little set of well-aligned planets with various sizes that are all named in the right format:

In a similar way, we can re-use our create_sphere() method and make another one, called create_torus(), to add a sphere for the sun and some torus objects to show the planet orbits:

The sun is of course bigger than the planets, and the rings are simply placed at the same time as the planet spheres, using the distance to the origin for the torusradiusparameter:

Fun Python Animation Tutorial For Kids

This is great, but those objects are a bit morose, all gray and blocky. It’s time to work on two important 3D concepts: the shading and the materials of our objects!

Generally speaking,  the shading refers to how an object reacts to light and is drawn in the 3D view or in a render. Here however, I’m focusing on the “smooth” versus “flat” shading, which determines how 

 an object appears. It’s basically a second layer that further impacts how the object is rendered, but doesn’t change its actual geometry: it just looks smoother.

Animated

How To Animate Variable Fonts Using Python And Drawbot

Roughly put,  shaders are what give 3D objects their color, their shininess, their glossiness, their roughness… Think about it: in the real world, what makes you say that “hum, this is wood” is just a whole set of clues: the object is some tint of brown, it reflects light a bit but not with sharp light spots like metal, it doesn’t refract rays like glass and it doesn’t reflect your image like a mirror… Well – with the ever-improving CGI technology, all of these properties can now be modeled and reproduced in our 3D scenes!

Note: when you’re really aiming for realistic renders, you’ll probably have to dive into physical-based rendering or PBR. And that’s what Blender’s Cycles engine is for creating amazingly lifelike pictures that just “feel” real, mostly thanks to a bunch of complex and well-tweaked shaders.

In Blender, shaders are usually edited via the node-based graph editor (available in the “Shading” tab) that lets you chain and combine as many built-in nodes as you want to build more or less complex shading flows. In this tutorial

Early Look At A Python Program To Stream/record Facial Animation With Unreal And Osc

Materials then use those shaders and apply them to your 3D geometry. An object may have several material slots, i.e. it can use different shaders for different parts of the geometry, but we won’t get into that today, and we’ll stick with one material slot per object.

For this project,  I’m using the EEVEE engine that can also work with shader nodes, even though it doesn’t have all the same node types as the Cycles engine.

But it’s fine because, here, what I want is one that exists in both: the Emission shader. You can picture it as a big lightbulb that has an intensity (the “strength” parameter) and a color. It will make your 3D object emit light (so your object will become a light source in your scene that we’ll interact with the rest of the meshes!) and essentially make it “glow.

How To Create Matplotlib Animations: The Ultimate Guide

Let’s work on this step by step. We’ll start by making a

Python

Posting Komentar untuk "Animations Python"