Since it can't access the driver variable value (or I'm missing from the api), I am doing a function to get that value. Is there a better way to get them? I need some help in line 14, line 16 is also wrong
import bpy
def get_variable_value(var):
try:
if var.type == 'TRANSFORMS':
tar0 = var.targets[0]
obj0 = tar0.id
tar0_type = tar0.transform_type
tar0_space = tar0.transform_space
ch = tar0_type[0]
if ch == 'L': # location
loc = obj0.location
if tar0_space == 'WORLD_SPACE':
loc = ???
elif tar0_space == 'LOCAL_SPACE':
loc = (obj0.matrix_local @ obj0.matrix_parent_inverse.inverted()).decompose()[0]
else: # TRANSFORM_SPACE
loc = obj0.matrix_basis.decompose()[0]
if tar0_type == 'LOC_X': return loc.x
elif tar0_type == 'LOC_Y': return loc.y
else: return loc.z
else:
print("other case, no discussion this time")
else:
print("other case, no discussion this time")
except:
return None
def scene_setup():
import bpy
objs = bpy.data.objects
for obj in objs: objs.remove(obj)
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
cube = bpy.context.object
bpy.ops.object.empty_add(type='PLAIN_AXES', align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
empty = bpy.context.object
cube.select_set(True)
bpy.ops.object.parent_set(type='OBJECT')
empty.location.x += 1.1
bpy.ops.curve.primitive_bezier_curve_add(radius=1, enter_editmode=False, align='WORLD', location=(2, 2, 2), scale=(1, 1, 1))
bezier = bpy.context.object
bpy.context.view_layer.objects.active = cube
bpy.ops.object.constraint_add(type='FOLLOW_PATH')
cube.constraints[0].target = bezier
md = cube.modifiers.new("Bevel", "BEVEL")
fc = md.driver_add("angle_limit")
dr = fc.driver
vs = dr.variables
v = vs.new()
v.type = 'TRANSFORMS'
v.targets[0].id = cube
return v
# Set up the scene for testing, first delete all objects, and finally return a driver variable.
var = scene_setup()
print(get_variable_value(var))