Discrepancy with what object is "active"

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

SamBushman
Posts: 18
Joined: Tue Jun 07, 2011 4:58 pm

Discrepancy with what object is "active"

Postby SamBushman » Mon Jun 11, 2012 6:19 pm

I have been thumbing through the Blender source code in order to determine why my context is incorrect for changing the mode in Blender (Object mode, Pose mode, etc) when using the Python API. My searching has led me to the ED_operator_object_active_editable function in screen_ops.c. This function seems to get a pointer to the current active object in the scene using the ED_object_active_context function found in object_edit.c. The function then checks the following with the active object:
1. Is the object pointer NULL?
2. Is the object's lib variable NULL? (Not sure that the "lib" variable does)
3. Is the object's restrictflag variable have its OB_RESTRICT_VIEW bit set? (I believe this mean "Is the object not visible?")

In my case, the third check fails. On further inspection I found that this makes some sense, as the object being referenced for these checks is indeed not visible. My problem is that the object being referenced is NOT the active object according to the Python API. I checked the name of the object referenced by the following variables in the Python API and they all matched:

Code: Select all

bpy.context.scene.objects.active.name
bpy.context.active_object.name
bpy.context.active_base.object.name

However, the object name associated with the object reference returned by ED_object_active_context differs from these calls. Does anyone have any ideas as to why this would happen? I am currently using Blender 2.63.10 from the SVN (revision 47576). I first observed this behavior with the Blender 2.63a Windows 32bit binary from the Blender website. I am running Windows 7 Professional 32-bit with Service Pack 1.

Thank you for your time,
Sam Bushman

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Tue Jun 12, 2012 9:01 am

can't help you with the C-code, but what are you trying to do with Python?

changing the mode shouldn't be problem, unless there is no context object (right after object deletion)
I'm sitting, waiting, wishing, building Blender in superstition...

SamBushman
Posts: 18
Joined: Tue Jun 07, 2011 4:58 pm

Postby SamBushman » Tue Jun 12, 2012 4:28 pm

In my addon I have a property dropdown list of items that are each associated with a bone on an armature. When one of these items are selected, I wish to select the bone associated with the selected item. Part of my addon also has an onscreen mesh that acts as a guide for configuring the items selectable from the property. This onscreen mesh occasionally has its visibility set to false. Anyhow, when I go to select the bone associated with a selected item, I go through the following procedure:
1. Deselect all objects in the scene using

Code: Select all

bpy.ops.object.select_all(action='DESELECT')

2. Select the armature that has the bone I wish to select:

Code: Select all

mArmature.select = True

3. Set the armature as the active object in the scene:

Code: Select all

bpy.context.scene.objects.active = mArmature

4. Change the mode to 'POSE' #Where I am having my issue with the function throwing an exception saying the context is incorrect.

Code: Select all

bpy.ops.object.mode_set(mode='POSE')

5. Select the bone on the armature.

Code: Select all

mArmature.pose.bones[bone_name].bone.select = True

6. Make the desired bone the active bone.

Code: Select all

mArmature.bones.active = mArmature.pose.bones[bone_name].bone


Just to ensure there is no confusion, I am not writing any c-code myself. I was simply using the c source code to determine WHY my context was incorrect when calling mode_set. I have determined that Blender throws the exception that my context is incorrect because the active object in the scene is not visible. My posted problem is that what the Blender C function sees as the active object in the scene differs from what I've set and observed to be the active object in my python addon. Ergo, if I can understand why this discrepancy exists I can ensure that I can change the mode in Blender without issue.

Hopefully this makes things clearer. Cheers,
Sam Bushman

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Wed Jun 13, 2012 2:16 pm

i see, it doesn't work if the armature is hidden

so how about unhiding it before changing mode?


mArmature.hide = False
bpy.ops.object.mode_set(mode='POSE')
I'm sitting, waiting, wishing, building Blender in superstition...

SamBushman
Posts: 18
Joined: Tue Jun 07, 2011 4:58 pm

Postby SamBushman » Wed Jun 13, 2012 4:12 pm

The armature is not hidden, that's the thing. The object that was selected and active before selecting the armature is hidden. The behavior I'm seeing seems to suggest that the updates the the scene context I am making in Python are not being pushed down into the c code immediately.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Sat Jun 16, 2012 4:54 pm

so armature is selected, active and not hidden...

maybe add bpy.context.scene.update() before mode_set?
I'm sitting, waiting, wishing, building Blender in superstition...

SamBushman
Posts: 18
Joined: Tue Jun 07, 2011 4:58 pm

Postby SamBushman » Tue Jun 19, 2012 11:40 pm

Just tried it, same result :( If it helps, the exception is thrown, but the mode is still set to pose whether the exception is handled or not.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Wed Jun 20, 2012 11:37 am

could be a real bug, maybe submit a bug report?

or unhide the object which is hidden and causing the error, then set mode and change hidden status back?
I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Coding Blender”

Who is online

Users browsing this forum: No registered users and 0 guests