bpy.data.armatures[0] vs bpy.context.scene.objects.['Armatur

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

caibbor
Posts: 10
Joined: Thu Sep 20, 2012 11:43 pm

bpy.data.armatures[0] vs bpy.context.scene.objects.['Armatur

Postby caibbor » Thu Feb 14, 2013 1:11 am

I'm trying to cycle through all the armatures to export them to a file. this seems easiest using bpy.data.armatures[x] instead of going through bpy.context.scene.objects and checking if each object's type is an armature. the problem I'm having is that I can't export the proper world-translated positions for each bone this way... why is there no bpy.data.armatures[0].matrix_world ?

Code: Select all


>>> bpy.data.armatures[0].bones[0].head_local
Vector((-4.505608330873656e-09, 0.0, 0.0))

>>> bpy.data.objects['Armature'].data.bones[0].head_local
Vector((-4.505608330873656e-09, 0.0, 0.0))
                                   
>>> bpy.data.objects['Armature'].matrix_world
Matrix(((1.0, 0.0, 0.0, -0.8924787044525146),
        (0.0, 1.0, 0.0, 1.8488168716430664),
        (0.0, 0.0, 1.0, 5.216436862945557),
        (0.0, 0.0, 0.0, 1.0)))

>>> bpy.data.armatures[0].matrix_world
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'Armature' object has no attribute 'matrix_world'

caibbor
Posts: 10
Joined: Thu Sep 20, 2012 11:43 pm

Postby caibbor » Thu Feb 14, 2013 3:58 pm

Code: Select all

>>> bpy.data.armatures[0].name
'Armature'

>>> bpy.data.objects['Armature'].name
'Armature'

>>> bpy.data.objects['Armature'].type
'ARMATURE'

>>> bpy.data.armatures[0].type
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'Armature' object has no attribute 'type'


WHAAAT?!??

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

Postby CoDEmanX » Thu Feb 14, 2013 5:10 pm

don't confuse objects of type armature with armature datablocks!

Code: Select all

ob = bpy.context.object
print(ob.type) # e.g. 'ARMATURE'
print(ob.data) # bpy.data.armatures[...]


matrix_world is a property of objects, not armatures!

if you want to iterate over all armatures in current scene, use:

Code: Select all

for ob in bpy.context.scene.objects:
    if ob.type != 'ARMATURE':
        continue
    # use armature object below


if you want ALL armature objects, you can do

Code: Select all

for ob in bpy.data.objects:
    if ob.type != 'ARMATURE':
        continue
    # use armature object below
I'm sitting, waiting, wishing, building Blender in superstition...

caibbor
Posts: 10
Joined: Thu Sep 20, 2012 11:43 pm

Postby caibbor » Thu Feb 14, 2013 6:22 pm

cool. that works. however I've noticed something else strange about the armature datablock. len( bpy.data.objects['Armature'].bones ) is the total number of bones in the armature, although each bone has children that are also included in that set. the program I'm writing that imports from my format is designed to have a root bone with children bones, who also have children, etc. how do I get the root bone? it's not just bpy.data.objects['Armature'].data.bone[0] because if that gets deleted, the list just shuffles down.

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

Postby CoDEmanX » Fri Feb 15, 2013 1:29 pm

Blender allows unlimited root bones. What determines whether a bone is a root bone or not? Root bones don't have parent, so you can get all root bones like:

Code: Select all

[b for b in C.object.data.bones if not b.parent]


you may iterate over b and get all children via Bone.children_recursive

but you may want to do that yourself in case you wanna know the "level" of the children
I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Python”

Who is online

Users browsing this forum: No registered users and 2 guests