0
$\begingroup$

In this python script:

import bpy

import math

 # Place the reference of the selected 3D object in a variable

mon_objet = bpy.context.object

" We fix the object to the desired position "

mon_objet.location[0] = 2.2

mon_objet.location[1] = 0

frame_num = 0




entre_chaque_keyframe = 1

bpy.context.scene.frame_start = 0

bpy.context.scene.frame_end = 135000


for n in range(0,6300):

    x=4.4*math.cos(n)/(math.exp(50*n)+math.exp(-50*n))

    y=4.4*math.sin(n)/(math.exp(50*n)+math.exp(-50*n))

    z=2.2*(math.exp(50*n)-math.exp(-50*n))/(math.exp(50*n)+math.exp(-50*n))


    # We are on the right frame, frame_num is 0 at the beginning

    bpy.context.scene.frame_set(frame_num)

    #We create a location keyframe

    mon_objet.location=(x,y,z)

    bpy.ops.anim.keyframe_insert_menu(type='Location')


    # We increase the value of the next frame to fix

    frame_num = frame_num+entre_chaque_keyframe

my goal is to insert a position key at each new location of the object. The successive positions of the object are in a spherical helix; In general, this kind of script worked very well (no error) for other parametric equations but here I do not see the error. The python console says: "syntax error: bpy.context.scene.frame_set (frame_num)" Why ?

Thank you for your help

$\endgroup$
3
$\begingroup$

You forgot the closing bracket here at the end:

z=2.2*(math.exp(50*n)-math.exp(-50*n))/(math.exp(50*n)+math.exp(-50*n)
$\endgroup$
0
$\begingroup$

Where to begin

As pointed out there was a syntax error with a missing brace.

Added to that

Blender uses radians as a unit of rotation. Stepping 1 radian every frame is equivalent of approx 57 degrees (math.degrees(1)) each frame, probably not what you want.

math.exp is going to grow exponentially and quite likely give math domain errors. Try crunching math.exp(50 * 6300) into your python interpreter and see if it gives a result. Might need numpy or use 1 for (a - b) / (a + b) when a gets large. Or re-visit your equations for a spiral.

Break down large formulas into smaller parts.

There is no need to either call the anim insert menu operator (IMO Never use this in a script) , consider that more of use in UI. Use keyframe_insert(...) method of the ID object. Can also pass the frame taking away the need to set frame each iteration too.

Below is somethat of a clean up of your script. Notice it will throw a NaN (not a number) result for z after a number of frames.

import bpy

from math import exp, sin, cos, radians
from mathutils import Vector

ob = bpy.context.object

entre_chaque_keyframe = 1

frame = 0

for n in range(3600):
    alpha = radians(n)
    m = 0.01
    a = exp(m * alpha)
    b = 1 / a
    c = 2.2 / (a + b)


    v = c * Vector((2 * cos(alpha), 
                    2 * sin(alpha), 
                    (a - b)))

    ob.location = v  
    ob.keyframe_insert('location', frame=frame)

    frame += entre_chaque_keyframe
$\endgroup$

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.