TransWikia.com

Problems updating the context within a loop? C.scene.update()

Blender Asked by cildpola on November 10, 2021

I’m using these loops to change the location of an object (I need to see how this object interacts with other objects in the scene when it starts from different locations).

Anyway, based on what’s printed, the object’s location is not updated in every “loop” repetition. It wasn’t updating the location at all, I added the bpy.context.scene.update() line, and now it’s only updating it once, so the output is always the same two coordinates (x=3 and y=2).

for ix in np.arange (3,50,0.25):
    for iy in np.arange (2,50,0.25): 
        bpy.data.objects["Ball.004"].location[0] = ix
        bpy.data.objects["Ball.004"].location[1] = iy

        bpy.context.scene.update()

        loc_ball=bpy.data.objects["Ball.004"].matrix_world.translation

        print(loc_ball.x, loc_ball.y)

How do I need to update the context (if that’s the problem)?

One Answer

Maybe use matrix_parent_inverse * location

Not sure what the issue is here. The three values "np", "xx", and "tr" in test code below correlate.

Using mpw * mpi * ob.location avoids a scene update for each iter.

(of-course if our parent inverse isn't Identity only the "xx" and "tr" values will match)

import bpy
import numpy as np
from mathutils import Matrix
ball = bpy.context.object
mw = ball.matrix_world
mpi = ball.matrix_parent_inverse.copy()
mwp = ball.parent.matrix_world if ball.parent else Matrix()
for ix in np.arange (3, 50, 0.25):
    for iy in np.arange (2, 50, 0.25): 
        #mw.translation = mw * Vector((ix, iy, ball.location.z))
        ball.location.xy = (ix, iy)
        print("np", ix, iy)
        xx = mwp * mpi * ball.location
        print("xx", xx.x, xx.y)
        # required or matrices don't update in loop
        bpy.context.scene.update()
        tr = mw.translation
        print("tr", tr.x, tr.y)

Last few results

np 49.75 49.0
xx 49.75 49.0
tr 49.75 49.0
np 49.75 49.25
xx 49.75 49.25
tr 49.75 49.25
np 49.75 49.5
xx 49.75 49.5
tr 49.75 49.5
np 49.75 49.75
xx 49.75 49.75
tr 49.75 49.75

Answered by batFINGER on November 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP