Settinng co-ordinates etc

Hey guys I have another question, this time it is:

I was wondering what code I would need to retrieve the information on an object, mainly it’s co-ordinates.
For example; ob.getLocation(x,y,z) ???

Next, how would I then set certain co-ordinates (ob.setLocation(x,y,z)) for the objects through an event driven action. For example I want to the player to click on the first object and then when they click on the second object they move into each others’ co-ordiantes so they ‘swap places’.

What is the code that allows Blender to run after a certain event?

Thank you, Trussy

Hi there,

Here’s a short script that I made in the past:


# script made by Emil Lozanov, a.k.a. Abidos

from Blender import *
import bpy

#######################################

def Info_MeshObject():
    sce = bpy.data.scenes.active
    ob = sce.objects.active
    if ob.type == "Mesh":
        me = ob.getData(mesh=1)
        st = "Mesh name = "+me.name
        print st
        print

        ves = me.verts
        nv = len(ves)
        st = "Number of verts = "+str(nv)
        print st
        st = "List of verts:"
        print st
        for i in range(nv):
            print ves[i].co[:]
            st = str(i)+"   "+str(ves[i])
            print st
        print

        eds = me.edges
        ne = len(eds)
        st = "Number of edges = "+str(ne)
        print st
        st = "List of edges:"
        print st
        for i in range(ne):
            st = str(i)+"   "+str(eds[i])
            print st
        print

        fas = me.faces
        nf = len(fas)
        st = "Number of faces = "+str(nf)
        print st
        st = "List of faces:"
        print st
        for i in range(nf):
            st = str(i)+"   "+str(fas[i])
            print st
        print
        
        st = "Faces normals:"
        print st
        for i in range(nf):
            st = str(i)+"   "+str(fas[i].no)
            print st
        print
            
#######################################

def Fix_MeshInteger():
    sce = bpy.data.scenes.active
    ob = sce.objects.active
    if ob.type == "Mesh":
        me = ob.getData(mesh=1)
        vs = me.verts
        nv = len(vs)
        for i in range(nv):
            x = RoundFn(vs[i].co.x,2)
            vs[i].co.x = x
            y = RoundFn(vs[i].co.y,2)
            vs[i].co.y = y
            z = RoundFn(vs[i].co.z,2)
            vs[i].co.z = z
        
#######################################

def Run_Test():
    Info_MeshObject()
    Fix_MeshInteger()
    Info_MeshObject()

#######################################

def RoundFn(a,n): 
    n = round(n)  # Round numbers up to "n" digits after decimal point
    d = 10**n
    a = round(a*d)
    return a/d

################ Exception demo ################
    
def NewMesh(mesh_name):
    try:
        me = Mesh.Get(mesh_name)
    except NameError:
        me = Mesh.New()
    return me
        
#######################################################
####   MAIN PART   ####################################    
#######################################################

Run_Test()


When you run this in BLENDER with the “default” cube (the one that appears in all new BLENDER files or when you press SPACE and add a new cube from the menu), you may check differences in printed results before and after corrections made in Fix_MeshInteger() procedure. It appears that BLENDER still keeps non-precise coords even after such corrections made very explicitly. But this is another story… :wink:

The script obviously works with any other meshes that you’d like to have integer coords of vertices (as soon as there are small differences with an integer number due to float operations in BLENDER).

In the script above there is a proc. that does not really take part in coords coorection. It is def NewMesh(mesh_name): which shows you shortly how to take different actions when error messages are raised in the course of running a script. Basically, the proc is to make a new mesh if there is NO mesh avail with given name and if there is already a mesh with the tested name --> to use the existing mesh. For handling events - you prepare a special (or not-so-that-special proc) that reacts on triggering certain previously defined event number, OK?