Hi everybody, I'm struggling with context, operators and selection. I would like to write a script that selects a single object, let's say a Cube, even though the current mode may be EDIT on some other object, let's say Cube.001. If you could provide the code to do so it would be great. I did two experiments, one that does not work in all cases, and one that looks like it is behaving correctly but I'm scared it may be doing something unstable too.
The first experiment fails exactly in the case in which Cube.001 is selected, it is in EDIT mode, and the script attempts to select Cube. What happens is that Blender switches to OBJECT mode on Cube, selecting it, but if you look at the ouliner for Cube.001, you will see that its mesh datablock of Cube.001, between the name and the visibility eye, remains selected. In the 3D View, you also see that Cube.001 still looks like it is in EDIT mode. If you execute my script, you will also notice that the duplicate operator fails because the context is incorrect.
The second experiment behaves correctly in this test case; the only difference is that I set EDIT mode on Cube before setting OBJECT.
| Code: |
import bpy
def select_only_v1(obj):
for other_obj in bpy.data.objects:
other_obj.select = False
obj.select = True
bpy.context.scene.objects.active = obj
bpy.ops.object.mode_set(mode='OBJECT')
def select_only_v2(obj):
for other_obj in bpy.data.objects:
other_obj.select = False
obj.select = True
bpy.context.scene.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.object.mode_set(mode='OBJECT')
obj = bpy.data.objects["Cube"]
#CHOSE EITHER select_only_v1(obj) OR select_only_v2(obj)
print(obj)
print(bpy.context.object)
bpy.ops.object.duplicate()
|
In terms of context, this is what is going on: when Cube.001 is selected, in edit mode, both object and edit_object context are pointing to it.
Hence before executing the script (version _v1) we have:
| Code: |
>>> bpy.context.object
bpy.data.objects['Cube.001']
>>> bpy.context.edit_object
bpy.data.objects['Cube.001']
|
After executing the script (version _v1) we have:
| Code: |
>>> bpy.context.object
bpy.data.objects['Cube']
>>> bpy.context.edit_object
bpy.data.objects['Cube.001']
|
And this leads to duplicate() poll error.
Can someone point me to proper documentation for bpy.context.edit_object?
More in general is there a good source of information on contexts and operators?
I have read the five subsections of Blender/Python Documentation at the page
http://www.blender.org/documentation/blender_python_api_2_63_17/contents.html and I feel like I am not mastering the topic yet.
Thank you.