1
$\begingroup$

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

enter image description here

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))
$\endgroup$
0

1 Answer 1

2
$\begingroup$

I made some research and found this answer

The problem is blender is not recalculating the matrix immediately after modifying the transformation.

You are missing an update bpy.context.view_layer.update() which you should call before get_variable_value(var)

var = scene_setup()
bpy.context.view_layer.update() # we need this update
print(get_variable_value(var))

Then you can get the cube's world space coordinates with:

loc = obj0.matrix_world.translation
$\endgroup$
2
  • 1
    $\begingroup$ Thank you, that's great $\endgroup$
    – X Y
    Commented Aug 2, 2022 at 3:33
  • $\begingroup$ you're welcome! :) $\endgroup$
    – Harry McKenzie
    Commented Aug 2, 2022 at 4:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .