1
$\begingroup$

I'm trying to accomplish with python script something like this:

  1. Take World Z position value of an object (let's call it OB1), which is parented to a pendulum(so it's World Z position is fluctuating).
  2. Make the script check OB1 World Z position every frame
  3. If certain OB1 World Z position (let's call it BOT) is reached, rotate OB2 by 90 degrees on Z axis.
  4. Make the OB2 keep new rotation until next BOT, which enables next rotation by 90 degrees on Z axis (so after 2 BOT, the rotation is 180 deg).
$\endgroup$
1
$\begingroup$

After a lot of googling, combining scraps of code and testing, I finally found the solution for this issue.I'm leaving it here cause it actually can be pretty useful in some situations.

See the docs about event handlers

import bpy
import math

sce = bpy.data.scenes[0]

# the function to call whenever the handler is envoked
def my_handler(sce):
    # contObj is controlling object, the one, which world Z position
    # I take to control rotation of "Target"
    contObj = bpy.data.objects["Controler"]

    # posOnZ is rounded world Z position of contObj 
    posOnZ = round(contObj.matrix_world.to_translation().z,3)

    # targObj is the object, which rotation will be changed, when posOnZ reach
    # certain value.
    targObj = bpy.data.objects["Target"]

    if posOnZ == 1.168:
        targObj.rotation_euler[2]+=6*(math.pi/180)

        # check if everything works (can be omitted)
        print(targObj.rotation_euler[2]*(180/math.pi) )

        #without sce.update() you can't see the changes in 3d view
        sce.update()

def register():
    bpy.app.handlers.frame_change_pre.append(my_handler)

my_handler(sce)  # optional, to test if the function is working..

register()
$\endgroup$
  • $\begingroup$ Thank You @zeffii for taking time to edit this post.Now it looks much better and is a lot more understandable. Although i think that my_handler(sce) (3 line from end) should be without indentation, as it's not a part of register() definition, but call of my_handler(sce) function. Please correct me if I'm wrong (which is totally probable) or edit your edit. $\endgroup$ – Mzidare Nov 2 '16 at 12:48
  • 1
    $\begingroup$ it's more appropriate to let the scene handler call my_handler() (see the docs), you don't need to call it for the handler to work.. but maybe if you want to see the result prior to starting the frame changing.. then maybe . $\endgroup$ – zeffii Nov 2 '16 at 13:37
  • $\begingroup$ I have no Idea how could one deduce all this info from documentation on handlers. For me it's just a list of handlers with short explanation, what each of them does.Anyway thank you so much for your time.I highly appreciate your help. $\endgroup$ – Mzidare Nov 2 '16 at 14:02

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.