

The output format can now be parsed by a library such as tinyxml2 in your application.

The output format is shown in the terminal window:

Print("%f %f %f "%tuple(model.vertices),end="") append the vertices to the class member of the Model class loop through each vertex and print it out each vertex This contains all the attributes of the model In line 8 we print out the result to the terminal. In line 7, we are going to store the vertices into the a member of the Model class, i.e. In the main code, we are going to create an instance of this class (Line 1). Let's first create a Model class which will contain all the attribute information about the model. Let's strip out the word Vector and fix the format output to something you can use in an application. The output format of the vertices is not very user friendly. If you run the script, the output is sent to the terminal window as shown below: loop through each vertex and print it out each vertexįor i,indices in enumerate(.data.loops): Line 4 loops through all the vertices of the model and prints them out to the terminal. Line 3 prints out the name of the model to the terminal. Since you are interested only in 3D models and not in lights or camera models, you need to check if the object is of type mesh (line 2). The first thing you need to do is loop through all the objects in the scene (see line 1 below). Now that you understand the Blender API, let's write a script which will loop through every object, get its name and print out vertex positions, normals and U-V coordinates to the terminal.
#Blender models for code#
In code this looks like this: #Obtaining the coordinate of a vertex of an objectīpy. Step 6. In the same manner, the vertices of the object can be found by going into the datablocks subtree and finding that this data is found under: i.e., to get the name of an object in the datablock, you had to click on Scene, then on Objects and then on the Name field. The path that you use in datablocks, is essentially the same path that you would use to access the name through the API. To access the Datablocks, locate the right-hand section of Blender and click on All Scenes and select Datablocks I was very confused with this API until I came across the Datablocks view. The Blender API can be a bit confusing at first. Let's print the vertex position to the terminal: import bpy For example, if you want to get the first vertex position of the object, you would access it as follows: #Accessing the first vertex position of the object.īpy. Now, if you want to access the object's data, you would need to access its data attributes. If you want to get the name of the object, you would access its name as follows: #accessing the name of an object in the sceneĪs a matter of a test, let's print out the name of your object to the terminal: import bpy If you want to access its first object in the scene, you would access it as follows: #accessing an object in blender For example if you want to access its scene, you would access its context and then its scene attribute: #accessing a scene in blender To do any of these operations you need to know the attributes/operators used in Blender.Įverything in Blender can be access through its context. You can get the name of a 3D model, orientation, position, etc. Let's do a simple test where you print out the name and vertices of an object.īlender allows you to control 3D models through its API. Let's get confortable using the Blender API.
