I’m a novice trying to work out how to create a minimal Blender animation scene using a driver entirely in Python. Basically I have created an empty and a sphere, and I want the sphere’s Z-location to be driven by the empty’s Z-rotation, eg. Sphere’s z location = cos(empty’s z-rotation).
I expected it to be trivial, but…
Is there any comprehensive reference for Blender Python for thickheads like me, preferably with fully worked examples?:spin:
This is what I’ve got so far. The initial position of the sphere is correct but I don’t know how to make the sphere’s location change when the empty rotates. Presumably keyframes are involved but I haven’t found any example that details that part. (It’s probably obvious to you, but hey like I said: ‘thickhead’)
import bpy from math import pi scn = bpy.context.scene # Create an Empty empty = bpy.data.objects.new('Empty', None) empty.location = (0, 0, 0) empty.rotation_euler = (0, 0, pi) empty.empty_draw_size = 0.5 empty.empty_draw_type = 'PLAIN_AXES' empty.show_name = True scn.objects.link(empty) scn.update() # Create sphere bpy.ops.mesh.primitive_uv_sphere_add(segments=32, size=0.3, location=(1, 1, 0)) bpy.ops.object.shade_smooth() ob = bpy.context.active_object ob.name = "sphere" ob.show_name = True print(ob.name) # Create a driver for the sphere's Z-axis position fcurve = ob.driver_add("location", 2) fvar = fcurve.driver.variables.new() fvar.name = 'rotation' fvar.targets.id = empty fvar.targets.data_path = 'rotation.z' fcurve.driver.expression = 'cos(rotation)'