I am attempting to control an object using a driver, which works fine,
but then I try to control the speed of the motion in a Panel . . . .
it fails every time, so I am either doing something wrong or missing something.
import bpy
from bpy.types import Panel, Menu
cube = bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(0, 0, 1))
bpy.context.object.name = "cube"
driv = bpy.context.object.driver_add('rotation_euler', 1)
driv.driver.type = 'SCRIPTED'
# Create a property, to control the speed using a custom property
bpy.context.object["speed"] = 1.0
speed = bpy.data.objects['cube'].values()[0]
driv.driver.expression = "frame * " +str(speed)+ " * .05"
OK, Now I want to add this to a panel, so I can change the speed.
class IA_Panel(bpy.types.Panel):
bl_label = "Controls"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
@classmethod
def poll(cls, context):
return (bpy.context.object is not None)
def draw(self, context):
layout = self.layout
ob = bpy.context.object
row = layout.row()
try:
ob["speed"]
layout.prop(ob, '["speed"]')
setSpeed(ob["speed"])
except:
pass
def register():
bpy.utils.register_class(IA_Panel)
def unregister():
bpy.utils.unregister_class(IA_Panel)
if __name__ == "__main__":
register()
def setSpeed(speed):
driv.driver.expression = "frame * " +str(speed)+ " * .05"
All of this code works in the command line, if I run the setSpeed() function in the command line,
it updates and the object speeds up.