I have a for loop that each time it will move or rotate several objects randomly, if some conditions not satisfied, it will undo all operations.
for obj in group_obj.objects:
move_rotate(obj, T, T_0)
accessableArea(obj)
new_c = total_cost(group_obj)
if i == 0:
old_c = c_0
if Metropolis(old_c, new_c):
old_c = new_c
else:
for i in range(len(group_obj.objects)):
bpy.ops.ed.undo()
This is move & rotate function
def move_rotate(object, T, T_0):
"""
Description:move and rotate object randomly
"""
if len(object.name.split("_")) < 3:
std = math.sqrt(T/10)
m = np.random.normal(0, std, 1)
if np.random.randint(2):
object.location.x += m
else:
object.location.y += m
# rootate
std = math.pi * (T/T_0)
r = np.random.normal(0, std, 1)
object.rotation_euler.z += r
It will report this error
RuntimeError: Operator bpy.ops.ed.undo.poll() failed, context is incorrect
Another question is, it seems blneder considered all operations down by script as one operation, Each time I run over the script press undo the sence will return to origin instead of last step.
Update: Based on @X Y's answer, I made some improvement:
while T > T_F:
for i in range(num):
count = 0
for obj in group.objects:
bpy.ops.ed.undo_push()
# repeat move&rotate until it won't cross the wall
while True:
move_rotate(obj, T, T_0)
if check_if_cross_wall(obj):
bpy.ops.ed.undo()
continue
break
count += 1
accessableArea(obj)
new_c = total_cost(group)
if i == 0:
old_c = c_0
if Metropolis(old_c, new_c):
old_c = new_c
else:
bpy.ops.ed.undo()
T = T * alpha
move & rotate function keep it as is.
report StructRNA of type Object has been removed.
bpy.ops.ed.undo_push()
before you do some action. $\endgroup$bpy.ops
operation in itself so the undo would reverse everything done by the Run Script action. You can see this action as Run Script under menu Edit > Undo History $\endgroup$bpy.ops.ed.undo_history(i)
? $\endgroup$Operator bpy.ops.ed.undo.poll() failed, context is incorrect
$\endgroup$obj = bpy.context.object
, and then if you runbpy.ops.ed.undo()
,obj
is invalid. So you need get the object again. $\endgroup$