Need to link relative xyz, like synchronized swimming

I’m slowly learning python and game engine to help with animations. What I’m having a hard time finding is a way to link two objects xyz locations. Normally this would be done with parenting or constraints but I want it to receive this relitavely. Both object has their own unique starting rotation and location and when the first object is moved and rotated the secod object moves and is rotated the same ammount, like synchronized swimming. Also to consider is that I’ll be doing this with about 200 pairs of objects, not sure how to make that easier. (maybe a naming convension)

I have looked to a way to do this also, perhaps it could be added for 2.43

Mitso have a look at my swarm script:

It can do exactly that (This script is non game engine though, it makes IPOs, but look at it’s methods), I think you can also get the vector for the movement of each object in the game engine, there’s more about that in a thread in the Game engine section. You should be able to make them all match through python. If it’s efficient enough for real time though I’m not sure.

Thank you Macouno. I’m looking at you script and I belive it will help as soon as I learn some python scripting. While I’m doing that maybe some one can help me translate my logic process into python or point me to the correct properties and syntax to use.
I need to have it look for an object with “brick.001” string in the name. The brick will already have ipo animation from recorded physics. Take the location xyz vectors from frame one and the xyz vectors from frame 2 and find the difference. Add the respective xyz sum to “shard.001” at frame 2. Next frame… Then next brick and shard in numerical order. I know this is possible, it’s just going to take a little time to put together, and any help is appriciated.
The reson for this is that I found that bricks stack together nicely in the physics engine, much nicer than jagged shard like triangles. So I’m looking to translate the locational IPO info to the shards by using the bricks, after the bricks have gone through the simulation and produced the animation I want. Basic simple proxy. I can already do this in Maya and 3ds max, but having abandon both programs for Blender now I’m going to learn it here.

It seems that every python tutorial is seriously out of date. I found this one:

and it looks like it would tell me exactly what I need to know, but under blender 2.42 with python 2.4 the script returns an error. Can someone take a look at this and tell me what needs to be changed to make it work?

import Blender


mPlane= Blender.Object.Get(“mPlane”)

if mSpring.LocZ < (mPlane.LocZ + (mSpring.SizeY / 2 )):
mSpring.LocZ= (mPlane.LocZ + (mSpring.SizeY / 2 ))

if mSpring.LocZ < (mPlane.LocZ +mSpring.SizeY):
mSpring.SizeZ= ( mSpring.LocZ - mPlane.LocZ)

mSpring.SizeZ= mSpring.SizeY

Two possible explanations:

  • indention is wrong. Did you use spaces or tabs? Was the indention consistent throughout?
  • do mSpring and mPlane exist?

Also, without knowing what the error was, it is hard to tell what is wrong.


sorry, I was assuiming you could download and see the blend file on the tutorial page.

The error says “AttributeError: ‘NoneType’ object has no attribute ‘LocZ’”. I would guess that LocZ used to be a defined peramater and isn’t now. As for the other questions, the objects do exist, but it’s probably best to just download the blend file and see if you get the same results.

This error basically means that somehow, the python api is NOT getting one of the objects, meaning that something is not going right there.

Have you got scriptlinks enabled, and does the mSpring object have a scriptlink to this script?


Script links was turned off. It works now. Thank you.:smiley:

Next Question: How do you grab an object’s xyz location from frame 1 and then from frame 2 and find the difference for each xyz? I haven’t seen any examples of getting frame specific object location. (I’m not just loafing in here, I’m really looking and learning python, so any help is appriciated.)

Firstly, you don’t want to do this in a scriptlink…

I’m not sure whether the api has been updated so that there are alternatives, but basically, you do the following:

  1. make variables to store locations
  2. Blender.Set(‘curframe’, 1) or something like that to change to frame 1. Store location in the first of the variables
  3. do the same, except for the other frame, and store in the other variable
  4. then subtract each of the bits that correspond

Example code:
import Blender
from Blender import Object

get object

ob = Object.GetSelected()[0]

find first location

Blender.Set(‘curframe’, 1)
obLocA = ob.loc[:]

find second location

Blender.Set('curframe, 2)
obLocB = ob.loc[:]

find difference in location

obLocDiff = [(b-a) for a,b in zip(obLocA, obLocB)] # note: this only works with Python 2.4+


Learning python from the gound up is not practical for me right now. So I need some help. Here is a .blend file that has object “cube” with a recorded IPO from physics sim. A script that would translate the location information to the object “plane”, keeping “plane” in it’s own starting piont but moving exactly the same xyz distances each frame is what I"m looking for. “plane” already has a rotational constraint that copies the rotation of “cube” to it. If seen several requests for a script of this type around the forum. Eventually I’d like to build it into a script more like I talked about before, but if I can get it to work on just one instance, that will be a lot of help.

This is the kind of thing that I think belongs in the modifier stack. I’m working on a problem that requires a similar solution.

But until such a feature is available, why not use scriptlinks? I ask in earnest because I don’t have the experience to know much about this issue in Blender. Here’s the solution I envision (imagine parenting or other relationships as you desire):


Forgive me if the details/syntax aren’t quite right, I’m new to all this. The basic idea is that the foundation location for all the “swarm” of objects is set and fairly permanent, as specified by the Empty.n objects. You can save or load them all as a list of rot-trans matrices, saved relative to Empty. Likewise, the Object (which is the master object, the one manipulated) has a rot-trans relative to Empty. Using a script, you can apply the Object-rel-Empty matrix to each individual object. Two matrices describe the entire system: one (actually, a list of many) for creating the swarm and a second for the motion. It should be extremely easy to use the parenting system to track down all the objects which need to have their locations updated, rather than rely upon naming conventions.

For my project, I’ve also been having a bugger of a time working with animations having simultaneous rotations around all three coordinates. Keyframes give me x,y,z rotations, but the IPO curves are triply flawed. First, there’s the lack of a -360/0/360 equality in creating smooth curves. Second, x,y,z curves are calculated independently, with no consideration for each other. Finally, there’s gimbal lock.
To solve this, I’m thinking of using scriptlinks similar to the swarm issue. Create four dummy objects which have properties assigned from true object’s rotational matrix (interpreted as a quat), and create the IPO curves for those objects. The true object can then have a correct rotation applied at any time, and position can be done in the standard way.

I bring this up because I notice that in the example file, you’ve got the same kind of free-form motion that has thus far required lots of manual attention for me: this just won’t be possible when dealing with the clusters of objects we’re talking about.

Surprisingly, it doesn’t seem that there is a built-in getChildren.

This seems to work:

import Blender
from Blender import *

def getChildren(targetObj):
    outlist = []
    scene = Scene.GetCurrent()
    allObj = scene.getChildren()
    for object in allObj:
        if object.getParent() == targetObj:
    return outlist

children = getChildren(Object.GetSelected()[0])

Ok, I’ve got a working .blend file, but no host. I’ll send it to someone if they’d like to see it.

Here’s the code that I’ve got:

import Blender
from Blender import *

def getChildren(targetObj):
    outlist = []
    scene = Scene.GetCurrent()
    allObj = scene.getChildren()
    for object in allObj:
        if object.getParent() == targetObj:
    return outlist
def ApplyMatrixCopies(targetObj):
    children = getChildren(targetObj)

    foundMat = False
    for obj in children:
        name = obj.getName()
        matchName = "Empty"
        if name.find(matchName) == -1:
            childMatrix = obj.getMatrix("localspace")
            foundMat = True
    #print "  "
    #print childMatrix
    for obj in children:
        name = obj.getName()
        if name.find(matchName) &lt;&gt; -1:
            lclChildren = getChildren(obj)
            for lclObj in lclChildren:
root = Object.Get("root")
#print "end"

What it’s missing is a good way to link the script to the system that requires the transformation propagation. I got it working, but more by trial and error than anything else. The hard-code using the “root” name isn’t the way I’d prefer to have it done.

Also, there’s some sort of residual translation offset between the copied object and the empty that defines their transformation localspace. It is probably related to the offset that existed when I performed the parenting, scripting, or some other quirk of Blender.

I use, it seems fairly decent.

I’m always looking for example .blend files :slight_smile:

Mike is pretty straight forward for hosting any type of file.

No feedback?

Your cubes are swimming! Awsome…

I works, and very simple. But what about two independent groups? or 5?