1
$\begingroup$

There are 2 cubes cube A and cube B. And cube B has a transformation constraint. target object is cube A. please see the picture.

enter image description here

cube B needs to move faster than cube A when it moves in positive X location but it needs to move with cube A when it moves in negative X location, but this constraint setting moves cube B at the same speed as cube A. Besides that cube B's default X location is set to X 10 when transformation constraint is active. it needs to be X 0. What should I do to make cube B move differently in negative and positive location?

$\endgroup$
0
1
$\begingroup$

you can do this with this script:

import bpy
from mathutils import Vector

def changePosition(frame):
    # triggered by a frame change, any code inside here gets run.

    a = bpy.data.objects['Cube A']
    b = bpy.data.objects['Cube B']
    
    fcurve = a.animation_data.action.fcurves

    x = fcurve[0].evaluate(frame-1)
    x1 = fcurve[0].evaluate(frame)
  
    deltaX = x1 - x
    
    if deltaX > 0:
        b.location = b.location + Vector((deltaX * 2,0,0))
    if deltaX < 0:
        b.location = b.location + Vector((deltaX,0,0))
    
    print("delta is", delta)

    return 0.0

bpy.app.driver_namespace['changePosition'] = changePosition

def my_handler(scene):
    changePosition(scene.frame_current)

bpy.app.handlers.frame_change_pre.append(my_handler)

Note: You have to "reset" your cube b after the end of your animation because it won't be resetted automatically.

Basically the script reads the animation information of cube a, checks whether the delta x movement is positive or negative and adds then 2 * x (in case of positive) or x (in case of negative) to cube b x position.

result:

enter image description here

$\endgroup$
2
  • $\begingroup$ nice. thank you $\endgroup$
    – Yokomizo
    Nov 26 '21 at 20:04
  • $\begingroup$ Glad I could help. Loved to figure it out😉 $\endgroup$
    – Chris
    Nov 26 '21 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.