Equivalent to old script links ?

Hi !

I don’t know how we have to proceed now in the cases where we used script links before.
I want to ask an object to change its position depending on the frame count. In fact it should stick to the vertices of a mesh: frame 1, objet on 1st vertex. Frame 2, object on 2nd vertex, etc.
How would you do that ?


import bpy

start_pos = (0,0,0)
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, size=0.3, location=start_pos)
bpy.ops.object.shade_smooth()

positions = (0,0,2),(0,1,2),(3,2,1),(3,4,1),(1,2,1)
frame_num = 0
for position in positions:
    bpy.context.scene.frame_set(frame_num)
    bpy.context.active_object.location = position
    bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
    frame_num += 10

Mmmhh, you are adding keyframes i think.

I have in mind an example where the object “Camera” changed position on each frame, following the vertices of another mesh.

Here it is:

# 3DNPcam
# 
# Use this script with a scene scriptlink
# Don't forget to link your Camera to an Empty with a "Track To"-Constraint

import Blender
from Blender import Scene, NMesh

import math
from math import *

scene         = Scene.GetCurrent()
context       = scene.getRenderingContext()
obj           = Blender.Object.Get("Camera")
frame         = context.currentFrame() 
camspheremesh = NMesh.GetRaw("CameraBoss") 
camsphereobj  = Blender.Object.Get('CameraBoss')
vertamount    = len(camspheremesh.verts)
size          = camsphereobj.getSize() 

if frame <= vertamount:
    vert = camspheremesh.verts[frame-1]
else:
    vert = camspheremesh.verts[vertamount]

obj.setLocation (vert.co[0]*size[0], vert.co[1]*size[1], vert.co[2]*size[2]) 

No keyframes etc., but to be used with script links.

Is there a way now to “refresh” a script on each frame, or have it re-evaluated every time somthing is changed ?

oops, sorry i don’t know anything about script-links, that sounds interesting! Probably Atom knows [noparse]:)[/noparse]

I think the only way by the time is with drivers… see this example here
or yep, try Atom’s addon that works in a similar way

edit: found original explanation

The working word might well be “addon”.
I have to learn all over again with Python 3 and this new API.

Do you have a link to that Atom thing you are talking about ?
EDIT: That link of yours is spot on Liero ! Long live drivers !

Ohh. It seems way more complicated than it used to be then !
Strange that it doesn’t behave correctly when rendered !

yep, but see the updated example, not using context in the script seems to work…
you could even move camera from inside a function, not need to driver it

What updated example ? You mean Atom’s updated script ?
I think that is all too complicated for me: I have troubles with this API and this is much too complicated…
The torus example you posted: this SHOULD work, no ? If rendering makes the whole driver thing collapse there surely is a bug somewhere, isn’t it ?

this one is simpler, take a look… http://www.pasteall.org/blend/6753

and I updated the torus link to a fixed one as soon as I checked it and failed…
sorry if I was not clear enough

to clarify: I just changed ‘bpy.context.scene’ to ‘bpy.data.scenes[0]’ to avoid using context…

Oh, sorry I didn’t get it with the example update !
Fantastic !
Do you think this is a bug ? Have you considered reporting that ?

Thank you for your help: that will help me quite q lot believe me !

haha changed the file again, above is second link with sorted vertices -they didn’t make much sense before as they mixed when subdivided circle- found a way by converting to curve and back to mesh… about this being a bug I don’t think so, it is a limitation but things will get better for sure… glad it helped, if not at least I tried :wink:

I don’t understand a few things:
What is the role of the empy in the middle?

Looks like you need to put your driver somewhere, it could be the cam itself but seems nicer to have it on an empty… and a keyframe at least to make it work. What you can get rid off is the var in the driver.

OK, I’ve played with that a bit, and i feel a little bit better with all that python wizardry !