0
$\begingroup$

I'm making multiple changes to an object and rendering like this:

obj=bpy.data.objects["Cube"]
obj.rotation_euler.x=0.3
obj.location.x=10
...
bpy.ops.render.opengl()

after I'm done rendering I need to change the object back to it's original state
I tried using bpy.ops.ed.undo() but it's really unreliable so is there any other alternative?

Note: I'm not looking for a solution where I have to store the individual values in temporary variables like this:

obj=bpy.data.objects["Cube"]
temp_rotation_euler_x=obj.rotation_euler.x
obj.rotation_euler.x=0.3

temp_location_x=obj.location.x
obj.location.x=10
...

bpy.ops.render.opengl()

obj.rotation_euler.x = temp_rotation_euler_x
obj.location.x = temp_location_x
...
$\endgroup$
3
  • 1
    $\begingroup$ Hello, I think one reliable solution would be to save the file beforehand and revert it afterwards bpy.ops.wm.revert_mainfile() $\endgroup$
    – Gorgious
    Nov 21, 2022 at 7:53
  • $\begingroup$ @Gorgious yes this is exactly what I needed! thank you so much! but how efficient is it if it has to be used rapidly? (Also can you post this as an answer so I can mark it solved?) $\endgroup$
    – cak3_lover
    Nov 21, 2022 at 9:05
  • 1
    $\begingroup$ The efficiency will depend on the number of things to recalculate when the file is loaded. On an empty file it will be instantaneous, on a file with multiple rigs, modifiers etc. it can take several seconds. You can also explore animating the value. Please do post it as an answer with your completed code so others can benefit from it :) $\endgroup$
    – Gorgious
    Nov 21, 2022 at 14:59

1 Answer 1

1
$\begingroup$

Thanks to @Gorgious I found the solution:

import bpy

obj=bpy.data.objects["Cube"]

bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)

obj.rotation_euler.x=0.3
obj.location.x=10

bpy.ops.render.render(write_still = True)

bpy.ops.wm.revert_mainfile()

It ain't a pretty solution but it'll get the job done

$\endgroup$

You must log in to answer this question.

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