Swapping Positions in Python

Hello Everyone I am trying to write an add on that swaps the positions of 2 components
but it is driving me crazy. I though it was going to be so simple but I can’t get it to work.

I tried the script below and the crazy thing is that when I print the position values of comp2
for the second time they do match the position values of comp1 in the output panel.

but the components do not move at all they stay in the same place.

Any ideas?

def ReplacePosition(comp1, comp2):
    print "Replacing Position"
    
    print comp1.Name
    
    
    print comp1.PositionMatrix.P.X
    print comp1.PositionMatrix.P.Y
    print comp1.PositionMatrix.P.Z
    
    print comp2.Name
    
    print comp2.PositionMatrix.P.X
    print comp2.PositionMatrix.P.Y
    print comp2.PositionMatrix.P.Z
    
    comp2.PositionMatrix = vcMatrix.new(comp1.PositionMatrix)
    
    print comp2.Name
    
    print comp2.PositionMatrix.P.X
    print comp2.PositionMatrix.P.Y
    print comp2.PositionMatrix.P.Z
    
    sim.update() 
    app.resetSimulation()

I got it to work with this code

def ReplacePosition(comp1, comp2):
    print "Replacing Position"
    
    pos1 = vcMatrix.new(comp1.PositionMatrix)
    pos2 = vcMatrix.new(comp2.PositionMatrix)
    
    comp1.PositionMatrix = pos2
    comp2.PositionMatrix = pos1
    
    sim.update() 
    app.resetSimulation()

This is the first code I wrote actually and it wasn’t working but I just restareted VC and it worked.

1 Like

You probably want to use WorldPositionMatrix so it works between components that are attached to different parents.