0

Using Blender 2.82a, I am trying to advance an animation and then select an object or mesh that has been updated. I am using something similar to the old scene.update() (pre-2.8) as described in the Blender Wiki here and this question here. However, I am not sure how to properly select the object. Here is a minimal working example. I make a cloth above the starting cube. I advance several frames. Then, I use evaluated_get to get the updated cloth.

But now I'm trying to select it using select_set so that I can then make it the child of an armature (using code in this answer). If you run this Python script:

import bpy

Add collision modifier to starting cube to support cloth.

cube_obj = bpy.data.objects['Cube'] bpy.ops.object.modifier_add(type='COLLISION')

Make cloth at a height above the cube.

bpy.ops.mesh.primitive_plane_add(location=(0, 0, 2.0)) bpy.ops.object.editmode_toggle() bpy.ops.mesh.subdivide(number_cuts=20) bpy.ops.object.editmode_toggle() bpy.ops.object.modifier_add(type='CLOTH') cloth = bpy.context.object

Advance some frames to apply physics.

bpy.context.scene.frame_set(0) scene = bpy.context.scene for f in range(1, 10): scene.frame_set(f)

Get updated cloth.

depsgraph = bpy.context.evaluated_depsgraph_get() cloth_upd = cloth.evaluated_get(depsgraph)

At this point, bpy.context.selected_objects = [bpy.data.objects['Plane']]

Now try to select the updated cloth, but this does not work. I do this because

I will make an armature later, which is the parent of this updated cloth.

cloth_upd.select_set(True)

I get the error:

RuntimeError: Error: Object 'Plane' can't be selected because it is not in View Layer 'View Layer'!

When I print the cloth and cloth_upd, both result in <bpy_struct, Object("Plane")>, suggesting that they are of the same class. Does anyone know how to properly select the updated mesh or adjust the layer?

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
  • Thanks Ray for the better tags! Still getting used to them here. :) – ComputerScientist Jun 11 '20 at 12:32
  • 1
    i) since it was last added will already be selected ii) as mentioned before adding a grid removes need to editmode toggle and subdivide a plane to get same result iii) why select the (already selected) updated object? – batFINGER Jun 11 '20 at 12:33
  • Hi @batFINGER Thanks for clarity regarding (i) and (ii). I am doing this because if I use the original cloth and try to pin vertices, then the vertices will be pinned as if they were from frame 0 (and not a later frame after physics/animation is applied). This is a limitation that I have been trying to wrap my head around for two weeks. :) I put in a more extensive example here: https://blender.stackexchange.com/questions/180199/pin-cloth-vertices-at-a-specific-set-of-frames-and-un-pin-otherwise-in-python . Do you feel like this approach makes sense or am I missing something obvious? – ComputerScientist Jun 11 '20 at 13:01
  • There is no need to use an operator to set the object parent, see comment below linked answer Can simply ob.parent = parent and set parent inverse matrices if need be. removing the need to select. Similarly would never use the add modifiers op. Think of an evaluated object as a "virtual entity" for that frame If you print(repr(ob)) instead will see one as bpy.data.objects["Foo"] one as "Evaluated Object Foo" I doubt whether – batFINGER Jun 11 '20 at 14:31
  • changing the relations of an evaluated object is going to work, or work as you expect. – batFINGER Jun 11 '20 at 14:32
  • Thanks @batFINGER. I now know how to make a grid (so as to remove edit mode toggle) and also to make parenting easier without the use of an operator. I also confirm I see the difference in print(repr(ob)) with the object vs the evaluated object (or virtual entity). I am wondering if it's possible to "convert" from an evaluated object to a normal object so that we can create an object with the updated positions? (I can ask this as a separate question if you feel this would be too long for a comment.) Or is this an ill-posed question? – ComputerScientist Jun 14 '20 at 15:45
  • Just did some experiments, I can run the .original attribute from an evaluated object to get the original one. Unfortunately these will be based on the location at frame 0 upon creation, rather than with updated locations based on later frames. – ComputerScientist Jun 14 '20 at 16:07

0 Answers0