Blender 2.6x: Questions for exporter (matrix, mesh data, …)

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

Blender 2.6x: Questions for exporter (matrix, mesh data, …)

Postby dom107 » Tue May 29, 2012 2:35 pm

I’m working on an exporter script and I would like some confirmations about some functions or general principles.

Thanks in advance for your help.

By looking at various exporter2.6x scripts, I found:

1) Usually scripts start with something like:
global_matrix = mathutils.Matrix()
What is this actually doing? Is it not enough, before converting local coordinates to world coordinates, to use the instruction at point 2:

2) obj_matrix = obj.matrix_world.copy()
I read that the copy suffix is compulsory otherwise it would change the model. What would be changed?

3) There is also obj.matrix_local
I believe this is the same as obj.matrix_world.inverted()
Is there alse the need to rather use obj.matrix_local?copy()?

4) As UV coordinates are relative to a vertex, they don’t need any coordinates system conversion?

5) To scan the model, the typical mesh access to its data would be:

Code: Select all

   # Create a Mesh datablock with modifiers applied (Applying modifiers 'preview' settings)
    data = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
   data = None

if data:

   obj_matrix = obj.matrix_world.copy()

   data.calc_tessface() # Compute and update the list of tessellated faces (faces of 3 or 4 vertices)

   ... (do something)...

   for face_num, face in enumerate(data.tessfaces):
      f_v = face.vertices
      f_mat = data.materials[face.material_index]
      uf =[face_num]
      f_uv = uf.uv
      for vert_num in range(3):
         vertex_indices_list = (f_v[0], f_v[1], f_v[2])
         facesUV_list = (f_uv[0], f_uv[1], f_uv[2]) # UV coordinates for each vertex
         normals_list = (data.vertices[f_v[0]].normal, data.vertices[f_v[1]].normal, data.vertices[f_v[2]].normal)
         points_local_coord_list = (data.vertices[f_v[0]].co, data.vertices[f_v[1]].co, data.vertices[f_v[2]].co)
   ... (do something)...

         # convert local object coordinates to world coordinates
         world_vertex = data.vertices[f_v[vert_num]].co * obj_matrix
   ... (do something)...
      if len(f_v) == 4:  # Quad: add another triangle
         vertex_indices_list = (f_v[0], f_v[2], f_v[3])
         facesUV_list = (f_uv[0], f_uv[2], f_uv[3]) # UV coordinates
         normals_list = (data.vertices[f_v[0]].normal, data.vertices[f_v[2]].normal, data.vertices[f_v[3]].normal)
         points_coord_list = (data.vertices[f_v[0]].co, data.vertices[f_v[2]].co, data.vertices[f_v[3]].co)
   ... (do something)...

Posts: 3646
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Postby stiv » Tue May 29, 2012 7:19 pm

In order to write code for blender, it helps to understand the data structures you are trying to manipulate.

Blender objects consist of two parts:

* an Object which holds spatial information like location, rotation and size

* a ObjData part which holds the geometry - vertices, edges and faces.

an ObjData may belong to many Objects (linked duplicates). Think about a row of telephone poles. Each one has its own location, but they all can share the same geometry.

An Object can be parented to another Object, which means if you move the parent, the child follows.

Materials can belong to either the Object or the ObjData.

More info is available on the blender wiki.

Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

Postby dom107 » Tue May 29, 2012 11:27 pm

I've read a lot and started to test the export which core is based on the above sample code .

So at his point, I thought I'd need a little help to stay on the right tracks and get some advice from experienced people.

It is certain that I missed some points.

As for my questions:
Point 1: it's just the identity matrix

About the use of '.copy()' in a matrix, I understand that the lack of it might update the model if '.invert()' is used. But for just 'obj_matrix = obj.matrix_world ', I don't see.

Things like:
data = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
were found in some export scripts and the explanation was found in Blender python documentation.

Posts: 3646
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Postby stiv » Wed May 30, 2012 2:10 am

About the use of '.copy()' in a matrix

Python variables hold references to objects. If you do
a = Foo()
you get variable a refering to a new instance of class Foo.
If you do
b = a
then both variables, a and b, point to the same object.
b = a.copy()
gives you a new instance of Foo, identical to the one variable a refers to.

Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

Thanks for the clarification.

Postby dom107 » Wed May 30, 2012 10:01 am

Thanks for the clarification.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests