issue156:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue156:python [2020/04/25 14:18] – créée auntiee | issue156:python [2020/04/29 14:46] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | As I sit in self-imposed isolation, I have once again struggled with what to present to you this month. I'm fairly certain that I won't be repeating anything that I have presented before. This month, we will explore Blender and its Python scripting possibilities. | + | **As I sit in self-imposed isolation, I have once again struggled with what to present to you this month. I'm fairly certain that I won't be repeating anything that I have presented before. This month, we will explore Blender and its Python scripting possibilities. |
- | If you aren't aware of Blender, I'll give you a quick introduction as to what it is, what it does, and how to do something simple, before we get into the programming aspects. I will say from the start, I am just learning how to do things with Blender from Python (and I'm not really that good with Blender by itself), so bear with me. | + | If you aren't aware of Blender, I'll give you a quick introduction as to what it is, what it does, and how to do something simple, before we get into the programming aspects. I will say from the start, I am just learning how to do things with Blender from Python (and I'm not really that good with Blender by itself), so bear with me.** |
- | From their website (blender.org), | + | Alors que je m' |
+ | |||
+ | Si vous ne connaissez pas Blender, je vous présenterai succinctement ce dont il s' | ||
+ | |||
+ | **From their website (blender.org), | ||
I'm sure that you have heard of "Big Buck Bunny", | I'm sure that you have heard of "Big Buck Bunny", | ||
Ligne 10: | Ligne 14: | ||
Blender runs on almost any platform. The latest version is 2.8.2a, and you can download it at | Blender runs on almost any platform. The latest version is 2.8.2a, and you can download it at | ||
- | https:// | + | https:// |
+ | |||
+ | D' | ||
+ | |||
+ | Je suis sûr que vous avez déjà entendu parler de « Big Buck Bunny », mais avez-vous entendu parler de l' | ||
+ | |||
+ | https:// | ||
+ | |||
+ | Blender tourne sur à peu près toutes les plateformes. La dernière version est la 2.8.2a et vous pouvez la télécharger sur https:// | ||
+ | |||
+ | **One other thing, before we get started. Blender has always gone through many changes, and the tutorials out there don't often catch up. In fact, many of the tutorials (and many books) are written and published based on a preview or pre-release version that many times is changed multiple times before the actual version release. A good example is when a tutorial (for Blender 2.8.x) refers to changing things in preferences by accessing the File | User Preferences menu. That no longer exists. It's actually located under Edit | Preferences. I, at this point, don't have a good place to point you to in order to find all the changes like this. Many API calls were changed as well, so you should keep the API documentation close at hand when attempting to learn from a tutorial or book on the subject. The Blender 2.8.2a API reference is located at https:// | ||
+ | https:// | ||
- | One other thing, before we get started. Blender | + | Autre chose, avant de commencer. Blender |
- | https:// | + | |
- | Now, on with the fun! | + | **Now, on with the fun! |
Download Blender and un-pack it in a convenient folder and run it from the terminal command: | Download Blender and un-pack it in a convenient folder and run it from the terminal command: | ||
Ligne 25: | Ligne 39: | ||
This is the default ‘new project’, and contains three objects: a cube, a camera and a light. | This is the default ‘new project’, and contains three objects: a cube, a camera and a light. | ||
- | I should tell you, right now, that Blender is NOT something you can learn in a single day or week. However, if you really want to learn it, within a week, with the right tutorials (and this is NOT one of those), you can become very conversant and effective. You will ALWAYS need to have the keyboard and mouse very close, since Blender uses both for most any action. We'll get into the programming aspect in a couple of minutes. For now, let's do something with our Blender scene (bottom left). | + | I should tell you, right now, that Blender is NOT something you can learn in a single day or week. However, if you really want to learn it, within a week, with the right tutorials (and this is NOT one of those), you can become very conversant and effective. You will ALWAYS need to have the keyboard and mouse very close, since Blender uses both for most any action. We'll get into the programming aspect in a couple of minutes. For now, let's do something with our Blender scene (bottom left).** |
- | I took a screenshot of Blender, opened it into Inkscape, and labeled the camera and light for you (I think the cube is fairly obvious), and showed the three axes for you in order to help you understand the ways things will move. | + | Maintenant, amusons-nous ! |
+ | |||
+ | Téléchargez Blender et extrayez-le dans le dossier convenable, puis lancez-le avec une commande dans le terminal : | ||
+ | |||
+ | $ ./blender | ||
+ | |||
+ | Le premier écran que vous devriez voir est quelque chose comme celui montré ci-dessous. | ||
+ | |||
+ | C'est le « nouveau projet » par défaut, et il contient trois objets : un cube, une caméra et un éclairage. | ||
+ | |||
+ | Je dois vous dire, tout de suite, que Blender N'est PAS quelque chose que vous pouvez apprendre en un jour ou une semaine. Cependant, si vous voulez vraiment l' | ||
+ | |||
+ | **I took a screenshot of Blender, opened it into Inkscape, and labeled the camera and light for you (I think the cube is fairly obvious), and showed the three axes for you in order to help you understand the ways things will move. | ||
If we take a VERY close look at the cube, which is our default object when we start Blender, you will see something interesting… | If we take a VERY close look at the cube, which is our default object when we start Blender, you will see something interesting… | ||
- | See that little circle with an “X” and a dot in the cube? That’s the 3D cursor. This is the origin of all the objects that we put into our blender scene. Even though it doesn’t look like it, it is at the intersection of the Green and Red layout lines, and the origin is in the center of the cube. That means that only the “top” half of the cube is above the imaginary plane which is the grid that is shown on the layout screen. | + | See that little circle with an “X” and a dot in the cube? That’s the 3D cursor. This is the origin of all the objects that we put into our blender scene. Even though it doesn’t look like it, it is at the intersection of the Green and Red layout lines, and the origin is in the center of the cube. That means that only the “top” half of the cube is above the imaginary plane which is the grid that is shown on the layout screen.** |
+ | |||
+ | J'ai pris une copie d' | ||
+ | |||
+ | Si vous regardez très PRÉCISÉMENT le cube, qui est notre objet par défaut au démarrage de Blender, vous verrez quelque chose d' | ||
+ | |||
+ | Voyez-vous le petit cercle avec un « X » et un point dans le cube ? C'est le curseur 3D. C'est l' | ||
- | Let’s do some coding... | + | **Let’s do some coding... |
Change your screen to the Scripting window tab, which should be close to the center of the screen at the top. You should see something like the image bottom right. | Change your screen to the Scripting window tab, which should be close to the center of the screen at the top. You should see something like the image bottom right. | ||
Ligne 41: | Ligne 73: | ||
Normally, we would expect File | New, File | Open, and File | Save options, but these are located under ‘Text’. | Normally, we would expect File | New, File | Open, and File | Save options, but these are located under ‘Text’. | ||
- | LARGE DISCLAIMER: While I've been a user off and on of Blender for many years and a Python programmer for almost 15 years, until I started working on this month' | + | LARGE DISCLAIMER: While I've been a user off and on of Blender for many years and a Python programmer for almost 15 years, until I started working on this month' |
- | We’ll start our first program. I found a few small programs for Blender at: | + | Effectuons un peu de codage |
+ | |||
+ | Sur votre écran, passez sur l' | ||
+ | |||
+ | L' | ||
+ | |||
+ | Normalement, | ||
+ | |||
+ | GRAND AVERTISSEMENT : Bien que je sois un utilisateur intermittent de Blender depuis des années et un programmeur en Python depuis presque 15 ans, avant que je commence à travailler sur cet article, je n' | ||
+ | |||
+ | **We’ll start our first program. I found a few small programs for Blender at: | ||
https:// | https:// | ||
Ligne 52: | Ligne 94: | ||
import bpy | import bpy | ||
- | Of course, we need to import the Blender API library. Next, we’ll create a function (below) that will delete everything in the default scene, so we can put what we want where we want it. | + | Of course, we need to import the Blender API library. Next, we’ll create a function (below) that will delete everything in the default scene, so we can put what we want where we want it.** |
- | We need to code it this way, since we have to delete each of the existing objects one at a time. There is no “delete all objects” command that I could find. The bpy.data.objects command will provide a “list” of all the objects, and we can step through that, one at a time, to remove or delete each of the objects. | + | Nous commencerons notre premier programme. J'ai trouvé quelques petits programmes pour Blender sur : https:// |
+ | |||
+ | Cependant, du fait du changement dans les API depuis que ça a été écrit, quand j'ai essayé de les faire tourner, il a refusé ; aussi, je l'ai transformé en une version plus simple. | ||
+ | |||
+ | Utilisez Texte > Nouveau pour que l' | ||
+ | |||
+ | import bpy | ||
+ | |||
+ | Evidemment, nous devons importer la bibliothèque des API de Blender. Ensuite, nous créerons une fonction (ci-dessous) qui effacera toute la scène par défaut ; ainsi, nous pourrons y mettre ce que nous voulons où nous voulons. | ||
+ | |||
+ | **We need to code it this way, since we have to delete each of the existing objects one at a time. There is no “delete all objects” command that I could find. The bpy.data.objects command will provide a “list” of all the objects, and we can step through that, one at a time, to remove or delete each of the objects. | ||
Next, we’ll define two variables, the first for the size of the object and one for the size of the “world” that we will be creating. | Next, we’ll define two variables, the first for the size of the object and one for the size of the “world” that we will be creating. | ||
Ligne 62: | Ligne 114: | ||
extents = 8.0 | extents = 8.0 | ||
- | Next, we’ll create a single cube (yes, I know we just deleted one, but this shows how to create a new object) and set its location. Notice that we set the cube at Z axis of 1, so it’s above the “floor”, | + | Next, we’ll create a single cube (yes, I know we just deleted one, but this shows how to create a new object) and set its location. Notice that we set the cube at Z axis of 1, so it’s above the “floor”, |
- | That’s our code. Now save the code as “test1.py” and click on the ‘Run Script’ button. The script will run and you will see the result in the upper left layout window. If you press the {F-12} keyboard button, you will see the scene rendered. | + | Nous avons besoin de la coder de cette façon car nous devons effacer un par un chacun des objets existants. Il n'y a pas de commande « effacer tous les objets » disponible par défaut. La commande bpy.data.objects nous fournira une « liste » de tous les objets, et nous pouvons la parcourir pour enlever ou supprimer, un par un, chacun des objets. |
+ | |||
+ | Ensuite, nous définirons deux variables, la première pour la taille de l' | ||
+ | |||
+ | sz = 2 | ||
+ | |||
+ | extents = 8.0 | ||
+ | |||
+ | Ensuite, nous créerons un simple cube (oui, je sais, nous venons juste d'en effacer un, mais ça nous montre comment créer un nouvel objet) et définirons sa position. Notez que nous définissons que le cube est à 1 sur l'axe Z ; il est donc au-dessus du « plancher », qui dans notre cas, est imaginaire ; mais nous pourrons en créer un plus tard si nous voulons (voir ci-dessus). | ||
+ | |||
+ | **That’s our code. Now save the code as “test1.py” and click on the ‘Run Script’ button. The script will run and you will see the result in the upper left layout window. If you press the {F-12} keyboard button, you will see the scene rendered. | ||
It’s pretty boring, so I won’t even show it here, but it was important to show how to create an object from code. | It’s pretty boring, so I won’t even show it here, but it was important to show how to create an object from code. | ||
Ligne 72: | Ligne 134: | ||
We’ll call this Test2.py. | We’ll call this Test2.py. | ||
- | We’ll go ahead and import the library and use the clear_scene function we created in test1.py (see next page, top left). | + | We’ll go ahead and import the library and use the clear_scene function we created in test1.py (see next page, top left).** |
+ | |||
+ | Voici notre code. Maintenant, sauvegardez-le dans « test1.py » et cliquez sur le bouton « Lancer le Script ». Le script sera lancé et vous verrez le résultat dans la fenêtre de mise en scène en haut à gauche. Si vous appuyez sur la touche {F12} du clavier, vous verrez le rendu de la scène. | ||
+ | |||
+ | C'est plutôt sans intérêt ; aussi, je ne le montre pas ici, mais c'est important de montrer comment créer un objet à partir du code. | ||
+ | |||
+ | Maintenant, démarrons un nouveau script pour quelque chose de beaucoup plus intéressant. | ||
+ | Nous l' | ||
+ | |||
+ | Nous commencerons par importer la bibliothèque et utiliserons la fonction clear_scene que nous avons créée dans test1.py (voir page suivante, en haut à gauche). | ||
- | Now we need to define a number of variables for our program. This will include the extents (size of our “world”), | + | **Now we need to define a number of variables for our program. This will include the extents (size of our “world”), |
# Size of grid | # Size of grid | ||
Ligne 83: | Ligne 154: | ||
padding = 0.005 | padding = 0.005 | ||
# Size of each cube | # Size of each cube | ||
+ | sz = (extents / count) - padding** | ||
+ | |||
+ | Maintenant, nous devons définir quelques variables pour notre programme. Cela incluera extents (la taille de notre « monde »), le nombre de cubes que nous créerons (sur chaque calque), l' | ||
+ | |||
+ | # Taille de la grille | ||
+ | extents = 8.0 | ||
+ | # Nombre de cubes | ||
+ | count = 10 | ||
+ | # Espacement entre cubes | ||
+ | padding = 0.005 | ||
+ | # Taille de chaque cube | ||
sz = (extents / count) - padding | sz = (extents / count) - padding | ||
- | # To convert abstract grid position within loop to real-world coordinates. | + | **# To convert abstract grid position within loop to real-world coordinates. |
iprc = 0.0 | iprc = 0.0 | ||
jprc = 0.0 | jprc = 0.0 | ||
Ligne 102: | Ligne 184: | ||
centerx = 0.0 | centerx = 0.0 | ||
- | Now we’ll create our cube of cubes (shown below). | + | Now we’ll create our cube of cubes (shown below).** |
- | The rest of our program will be just like the end of test1.py, in that we create a lamp and camera into the scene (next page). | + | # Pour convertir la position abstraite de la grille dans la boucle en coordonnées du monde réel. |
+ | iprc = 0.0 | ||
+ | jprc = 0.0 | ||
+ | kprc = 0.0 | ||
+ | countf = 1.0 / (count - 1) | ||
+ | diff = extents * 2 | ||
+ | |||
+ | # Position de chaque cube. | ||
+ | z = 0.0 | ||
+ | y = 0.0 | ||
+ | x = 0.0 | ||
+ | |||
+ | # Centre de la grille. | ||
+ | centerz = 0.0 | ||
+ | centery = 0.0 | ||
+ | centerx = 0.0 | ||
+ | |||
+ | Maintenant, nous créerons notre cube de cubes (voir ci-dessous). | ||
+ | |||
+ | **The rest of our program will be just like the end of test1.py, in that we create a lamp and camera into the scene (next page). | ||
Now, when we run the program, it will take some time. On my machine, it took over a minute and a half. Nothing happens on the blender screen, but if you look at the terminal window, you will see the output of the print statements that shows something is actually happening. Remember, we are creating 1000 cubes (10 x 10 x 10) along with the light and camera. | Now, when we run the program, it will take some time. On my machine, it took over a minute and a half. Nothing happens on the blender screen, but if you look at the terminal window, you will see the output of the print statements that shows something is actually happening. Remember, we are creating 1000 cubes (10 x 10 x 10) along with the light and camera. | ||
- | Shown right is what the result of test2.py looks like when rendered. | + | Shown right is what the result of test2.py looks like when rendered.** |
- | As always, I have uploaded these two programs onto pastebin… | + | Le reste de notre programme sera comme la fin de test1.py, parce que nous créons une lampe et une caméra dans la scène (page suivante). |
+ | |||
+ | Quand nous lançons notre programme, ça prend un peu de temps. Sur ma machine, ça dure environ une minute et demi. Rien ne se passe sur l' | ||
+ | |||
+ | À droite, le résultat de test2.py est présenté tel qu'il est rendu. | ||
+ | |||
+ | **As always, I have uploaded these two programs onto pastebin… | ||
Test1.py | Test1.py | ||
Ligne 117: | Ligne 224: | ||
https:// | https:// | ||
- | Until next month; stay safe, healthy, positive and creative! | + | Until next month; stay safe, healthy, positive and creative!** |
+ | |||
+ | Comme toujours, j'ai téléversé ces deux programmes sur pastebin : | ||
+ | |||
+ | Test1.py | ||
+ | https:// | ||
+ | Test2.py | ||
+ | https:// | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | // | ||
+ | |||
+ | **This creates a cube at location 0, 0 and 1 unit above the imaginary “ground” plane. | ||
+ | |||
+ | Now we’ll create a lamp and a camera for the scene...** | ||
+ | |||
+ | Ceci crée un cube à l' | ||
+ | |||
+ | Maintenant, nous créons une lampe et une caméra pour la scène... | ||
issue156/python.1587817107.txt.gz · Dernière modification : 2020/04/25 14:18 de auntiee