# Coordinates of Meshes

Hello everyone!

I’m trying to connect Blender with Python to some external software.

The python script I need should check out the coordinates of some meshes with a certain frequency f (since these positions will change over time using the keyboard). Then some calculations are performed, that give angles between certain meshes.

But I’m a little uncertain how to apply them to my case. It would be very helpful for me If someone could give me a bit of help with how to start writing the script.

Jos van Schijndel.

I found this in the Blender API guide:

getLocation(space)

Parameters:
space (string) - The desired space for the location:
localspace: (default) location without other transformations
worldspace: location taking vertex parents, tracking and Ipos into account
Returns the object’s location (x, y, z).

Returns:
(x, y, z)
Example:

The example below works on the default scene. It retrieves all objects in the scene and prints the name and location of each object:

``````   import Blender
``````
``````   sce = Blender.Scene.GetCurrent()
``````
``````   for ob in sce.objects:
print obj.name
print obj.loc
``````

Note: the worldspace location is the same as ob.matrixWorld[3][0:3]

Isn’t this much easier to apply than to work with local coordinates and multiply them with a matrix to get the global coordinates?

it is easier to coding but multiplying by using ob.matrixWorld[3][0:3] may get faster cause it brings you directly to the result instead of multiplying by a matrix (includes several multiplications and sums). Pls note that ob.matrixWorld = ob.matrix = ob.mat

Regards,

But I have another question now: Is it also possible to get the coordinates of the Center of Gravity out of the Mesh’s data, so when I multiply these local (X,Y,Z) with ob.matrixWorld[3][0:3] I get the global coordinates of the CoG?
Or maybe to and Empty that is parented to a mesh so that it moves along while the mesh moves?

Or must I write a formula to get a specified point? For example: For a cube I could take the sum of X for all vertices, and divide them by 2. Doing this for Y and Z too, gives me the center of the cube.

EDIT: I tried both options (GetLocation and multiplying with matrix), but when I move the mesh, it still prints the location of the cube where it is when I start the game.

My total code:

import Blender
import bpy
from Blender import Mesh
from Blender import Object

sce = Blender.Scene.GetCurrent()
#sce = bpy.data.scenes.active
ob = Object.Get(“Cube”)
me = ob.getData(mesh=1)
Global_coordinates = {}
for v in me.verts:
Global_coordinates[v.index] = v.co * ob.mat
print Global_coordinates
print

print ob.name
print ob.loc

For the CoG you cant take the data from mesh’ data cause the object’s center may get moved in EDIT mode or some of the verts… So you need to calculate it. And it is NOT to divide by 2 for the cube but by 8!!! While you use the vector approach it may be easier. Try experimenting with this code:

``````from Blender import *

sce = Scene.GetCurrent()
ob = Object.GetSelected()[0]
me = ob.getData(mesh=1)

c = Mathutils.Vector(0,0,0)
for v in me.verts:
c += v.co
c_Global = c * ob.mat

print c
print c_Global
print

``````

And please wrap your further published code in CODE-/CODE tags (the right-most button above the text box having a sign “#” on it).

Regards,

Cooridinates question:

From zbrush to blender. I export high res texture and model and it works fine in blender. When I export low res model the high res texture doesn’t fit. Is this obvious? should the uv coordinates be the same? Confused!

I dont think it is a coordinate issue… rather then it’s related to number of verts N. There may be a pin-to-N-verts property so you’d need to re-work UV texture after import in Blender

Final last question: It keeps printing the coordinates of the cube’s starting position. I want it to print the cube’s current position.
For example: It keeps printing [0,0,0] (Cube starts in center of world) while I move the cube with the Game Engine (I used force and simple displacement).

Sorry for bothering you with so much questions!

Questions are not a problem… while the answers are!

I’ll answer when I can… or somebody else will.

To your question… Perhaps you dont update something after the move. Your scene, for example. So after moving try Scene.update(0). Also a Redraw(). This should work although I am not an expert in BGE.

Should it not work, I’d propose you later a stupid but always working sequence

Regards,

Sorry for robbing the thread but couldn’t get answer previously. I’ve a cracking texture made in zbrush but the poly count is very high on the mesh when imported to blender. Is there a way of getting a low poly mesh and fitting the high res image onto it?

The Blender.Redraw() function alone doesn’t give the wanted results.

The function Blender.Scene.update(0), I don’t get it to work, it keeps giving an error.
I also tried:
Blender.Scene.update(full=0)
Blender.Scene.update(1)
Scene.update

Blender.Scene.update() etc…

The error it gives is:
Python script error from controller “cont1#CONTR#1”:
Traceback (most recent call last):
File “Test”, line 29, in <module>
AttributeError: ‘module’ object has no attribute ‘update’

Which I find strange, since I searched through the API-documentation, and it says it exists.
And I (ofcourse) did the line: from Blender import Scene

@ Dundaglan: The more people post here, the more people will look at this thread since it gets to the top of the thread-list a lot XD I don’t mind =)

use the sce variable, not the Scene module… i.e.:

sce.update(0)

then

Redraw()

Regards,

I guess it’s not the right way, because it still doesn’t want to work.
My code is as follows:

``````import Blender
import bpy
#from Blender import Mesh
#from Blender import Object
#from Blender import Scene
from Blender import *

#sce = Blender.Scene.GetCurrent()
sce = bpy.data.scenes.active
sce.update(0)
Redraw()
ob = Object.Get("Cube")
me = ob.getData(mesh=1)
Global_coordinates = {}
for v in me.verts:
Global_coordinates[v.index] = v.co * ob.mat
print Global_coordinates
print

print ob.name
str = ob.loc
print str
``````

It is ran by pressing the space bar in the Game Engine.
I also have an actuator that moves the cube towards some direction with another key on the keyboard. Any other options?

Well, your code runs correctly but it practically does nothing.

At the default cube located at (0,0,0), try this:

``````from Blender import *

sce = Scene.GetCurrent()
ob = Object.Get("Cube")
me = ob.getData(mesh=1)
Global_coordinates = {}
for v in me.verts:
Global_coordinates[v.index] = v.co * ob.mat
print Global_coordinates
print

print ob.name
str = ob.loc
print str
print

ob.loc = (2,0,0)
sce.update(0)
Redraw()

New_Global_coordinates = {}
for v in me.verts:
New_Global_coordinates[v.index] = v.co * ob.mat
print New_Global_coordinates
print
print "=================================="
print

``````

You may see your cube moves and you may also see the difference in coordinates

Regards,

I now found out my problem is more of a GameLogic problem than a scripting-Problem…
I found a code (from 2006, http://blenderartists.org/forum/showthread.php?t=101298&highlight=getLocation ), that does follow the coordinates when the GameEngine moves an object. That is kind of what I want. It uses GameLogic variables…The problem is: Most functions are depreciated.

``````
g = GameLogic
L = g.getCurrentScene().getObjectList()
chuzzy = L["OBCube"].getPosition()
print chuzzy

``````

I already found in the API (There is a special GameEngine API on http://www.blender.org/documentation/249PythonDoc/GE/index.html ):
Under GameTypes.KX_GameObject:
Command: worldPosition
Returns: list [x, y, z]
Info: The object’s world position.

I hope you can help me one last time, because I don’t get a working code…

Not willing to disappoint you, but I mentioned already that I am not into BGE… and I wont get to it soon.

You need to adapt some of above codes.

Regards,

``````import GameLogic as g