Previous Thread  Next Thread

chat icon Discrepancy with what object is "active"

SamBushman

Posted: Mon Jun 11, 2012 5:19 pm
Joined: 07 Jun 2011
Posts: 18
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:
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
Reply with quote


CoDEmanX

Posted: Tue Jun 12, 2012 8:01 am
Joined: 05 Apr 2009
Posts: 894
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...
Reply with quote


SamBushman

Posted: Tue Jun 12, 2012 3:28 pm
Joined: 07 Jun 2011
Posts: 18
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:
bpy.ops.object.select_all(action='DESELECT')

2. Select the armature that has the bone I wish to select:
Code:
mArmature.select = True

3. Set the armature as the active object in the scene:
Code:
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:
bpy.ops.object.mode_set(mode='POSE')

5. Select the bone on the armature.
Code:
mArmature.pose.bones[bone_name].bone.select = True

6. Make the desired bone the active bone.
Code:
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
Reply with quote


CoDEmanX

Posted: Wed Jun 13, 2012 1:16 pm
Joined: 05 Apr 2009
Posts: 894
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...
Reply with quote


SamBushman

Posted: Wed Jun 13, 2012 3:12 pm
Joined: 07 Jun 2011
Posts: 18
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.
Reply with quote


CoDEmanX

Posted: Sat Jun 16, 2012 3:54 pm
Joined: 05 Apr 2009
Posts: 894
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...
Reply with quote


SamBushman

Posted: Tue Jun 19, 2012 10:40 pm
Joined: 07 Jun 2011
Posts: 18
Just tried it, same result Sad If it helps, the exception is thrown, but the mode is still set to pose whether the exception is handled or not.
Reply with quote


CoDEmanX

Posted: Wed Jun 20, 2012 10:37 am
Joined: 05 Apr 2009
Posts: 894
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...
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group