Different ordering of bones in Blender than in Python












0














My modelexporter mostly works, but I added an extra bone to a model, named the bone 'gun' and in my script this bone's index is 5 (connected to 'stump'). The vertices attached to this bone, has the index 10. I clicked around in the windows, and the Graph editor shows the gun-bone is the last, the 10th. Here's a pic side by side:
wrong order:
wrong order



How can I get the proper order in Python?










share|improve this question
























  • You're comparing action with armature, and they have pretty much nothing in common. You can read from action (e.g. bpy.data.actions[0].groups) if you have to, but even in that case I wouldn't assume it absolutely have to match what interface shows, as it can apply different sorting rules. Why is that important to you?
    – keltar
    Nov 23 at 8:10










  • My workflow is like this: I make the model, then the armature, parent them with automatic weights, then make some animation in the dope-sheet window, like walking, shooting etc. Then I export it with a python script to a text format, so my engine can use it. To make animation work, I need bone matrices and vertex with weights and bone-indices to know which bones infulences the said vertex. But I cannot get the proper bone ordering when I add a new bone after making the parenting and animation, during the customization of the model.
    – fpete
    Nov 23 at 17:24










  • I don't really get your problem, can you edit your pseudocode into the question? So far I don't see why order is important at all, as both bone and its vertex group share the same name. Can't you use any order, as long as it is the same in both mesh and armature? Relying only on action is probably not the best since some bones may have no animation assigned and hence they will not appear in action.
    – keltar
    Nov 23 at 18:51
















0














My modelexporter mostly works, but I added an extra bone to a model, named the bone 'gun' and in my script this bone's index is 5 (connected to 'stump'). The vertices attached to this bone, has the index 10. I clicked around in the windows, and the Graph editor shows the gun-bone is the last, the 10th. Here's a pic side by side:
wrong order:
wrong order



How can I get the proper order in Python?










share|improve this question
























  • You're comparing action with armature, and they have pretty much nothing in common. You can read from action (e.g. bpy.data.actions[0].groups) if you have to, but even in that case I wouldn't assume it absolutely have to match what interface shows, as it can apply different sorting rules. Why is that important to you?
    – keltar
    Nov 23 at 8:10










  • My workflow is like this: I make the model, then the armature, parent them with automatic weights, then make some animation in the dope-sheet window, like walking, shooting etc. Then I export it with a python script to a text format, so my engine can use it. To make animation work, I need bone matrices and vertex with weights and bone-indices to know which bones infulences the said vertex. But I cannot get the proper bone ordering when I add a new bone after making the parenting and animation, during the customization of the model.
    – fpete
    Nov 23 at 17:24










  • I don't really get your problem, can you edit your pseudocode into the question? So far I don't see why order is important at all, as both bone and its vertex group share the same name. Can't you use any order, as long as it is the same in both mesh and armature? Relying only on action is probably not the best since some bones may have no animation assigned and hence they will not appear in action.
    – keltar
    Nov 23 at 18:51














0












0








0







My modelexporter mostly works, but I added an extra bone to a model, named the bone 'gun' and in my script this bone's index is 5 (connected to 'stump'). The vertices attached to this bone, has the index 10. I clicked around in the windows, and the Graph editor shows the gun-bone is the last, the 10th. Here's a pic side by side:
wrong order:
wrong order



How can I get the proper order in Python?










share|improve this question















My modelexporter mostly works, but I added an extra bone to a model, named the bone 'gun' and in my script this bone's index is 5 (connected to 'stump'). The vertices attached to this bone, has the index 10. I clicked around in the windows, and the Graph editor shows the gun-bone is the last, the 10th. Here's a pic side by side:
wrong order:
wrong order



How can I get the proper order in Python?







python blender






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 3:30









Michael Butscher

4,24321321




4,24321321










asked Nov 23 at 3:08









fpete

82




82












  • You're comparing action with armature, and they have pretty much nothing in common. You can read from action (e.g. bpy.data.actions[0].groups) if you have to, but even in that case I wouldn't assume it absolutely have to match what interface shows, as it can apply different sorting rules. Why is that important to you?
    – keltar
    Nov 23 at 8:10










  • My workflow is like this: I make the model, then the armature, parent them with automatic weights, then make some animation in the dope-sheet window, like walking, shooting etc. Then I export it with a python script to a text format, so my engine can use it. To make animation work, I need bone matrices and vertex with weights and bone-indices to know which bones infulences the said vertex. But I cannot get the proper bone ordering when I add a new bone after making the parenting and animation, during the customization of the model.
    – fpete
    Nov 23 at 17:24










  • I don't really get your problem, can you edit your pseudocode into the question? So far I don't see why order is important at all, as both bone and its vertex group share the same name. Can't you use any order, as long as it is the same in both mesh and armature? Relying only on action is probably not the best since some bones may have no animation assigned and hence they will not appear in action.
    – keltar
    Nov 23 at 18:51


















  • You're comparing action with armature, and they have pretty much nothing in common. You can read from action (e.g. bpy.data.actions[0].groups) if you have to, but even in that case I wouldn't assume it absolutely have to match what interface shows, as it can apply different sorting rules. Why is that important to you?
    – keltar
    Nov 23 at 8:10










  • My workflow is like this: I make the model, then the armature, parent them with automatic weights, then make some animation in the dope-sheet window, like walking, shooting etc. Then I export it with a python script to a text format, so my engine can use it. To make animation work, I need bone matrices and vertex with weights and bone-indices to know which bones infulences the said vertex. But I cannot get the proper bone ordering when I add a new bone after making the parenting and animation, during the customization of the model.
    – fpete
    Nov 23 at 17:24










  • I don't really get your problem, can you edit your pseudocode into the question? So far I don't see why order is important at all, as both bone and its vertex group share the same name. Can't you use any order, as long as it is the same in both mesh and armature? Relying only on action is probably not the best since some bones may have no animation assigned and hence they will not appear in action.
    – keltar
    Nov 23 at 18:51
















You're comparing action with armature, and they have pretty much nothing in common. You can read from action (e.g. bpy.data.actions[0].groups) if you have to, but even in that case I wouldn't assume it absolutely have to match what interface shows, as it can apply different sorting rules. Why is that important to you?
– keltar
Nov 23 at 8:10




You're comparing action with armature, and they have pretty much nothing in common. You can read from action (e.g. bpy.data.actions[0].groups) if you have to, but even in that case I wouldn't assume it absolutely have to match what interface shows, as it can apply different sorting rules. Why is that important to you?
– keltar
Nov 23 at 8:10












My workflow is like this: I make the model, then the armature, parent them with automatic weights, then make some animation in the dope-sheet window, like walking, shooting etc. Then I export it with a python script to a text format, so my engine can use it. To make animation work, I need bone matrices and vertex with weights and bone-indices to know which bones infulences the said vertex. But I cannot get the proper bone ordering when I add a new bone after making the parenting and animation, during the customization of the model.
– fpete
Nov 23 at 17:24




My workflow is like this: I make the model, then the armature, parent them with automatic weights, then make some animation in the dope-sheet window, like walking, shooting etc. Then I export it with a python script to a text format, so my engine can use it. To make animation work, I need bone matrices and vertex with weights and bone-indices to know which bones infulences the said vertex. But I cannot get the proper bone ordering when I add a new bone after making the parenting and animation, during the customization of the model.
– fpete
Nov 23 at 17:24












I don't really get your problem, can you edit your pseudocode into the question? So far I don't see why order is important at all, as both bone and its vertex group share the same name. Can't you use any order, as long as it is the same in both mesh and armature? Relying only on action is probably not the best since some bones may have no animation assigned and hence they will not appear in action.
– keltar
Nov 23 at 18:51




I don't really get your problem, can you edit your pseudocode into the question? So far I don't see why order is important at all, as both bone and its vertex group share the same name. Can't you use any order, as long as it is the same in both mesh and armature? Relying only on action is probably not the best since some bones may have no animation assigned and hence they will not appear in action.
– keltar
Nov 23 at 18:51












1 Answer
1






active

oldest

votes


















0














All collections in blender are of type bpy_prop_collection. This collection functions both like a dictionary and a list, allowing items to be referenced either by index or name. Like a dictionary you can't rely on the items being in a specific order, either by name or creation order.



As there is no guarantee that a new item added to the collection will get the highest index number, each export will need to be aligned with the index at the time of export and not trying to re-use indexes of previous exports.



You should find that enumerating the bones list will give the same index numbers that are used internally, you can get blenders internal index number of a bone by using the collections find()



for i, b in enumerate(rig.bones):
print(i, rig.bones.find(b.name), b.name)


0 0 spine
1 1 spine.001
2 2 spine.003
3 3 spine.004
4 4 spine.005
5 5 spine.006
6 6 shoulder.L
7 7 upper_arm.L
...


Bones move vertices that are assigned a weight in a vertex group with a name matching the bone. So the bone named "shoulder.L" will move vertices that have been assigned a weight to vertex group named "shoulder.L". Expanding on this answer you can access each vertices group weight.



grpid = obj.data.vertices[0].groups[0].group
bone_name = obj.vertex_groups[grpid].name
vert_bone_weight = obj.data.vertices[0].groups[0].weight





share|improve this answer





















  • Yes, I realized this after checking the exported file. Index dosen't matter, only names count. Luckily I export each pose's name and saw there is a mixup in the animation matrices order. (armature.pose.bones) Sorting pose matrices by the bones' names fixes the problem. Thank you for clarifying.
    – fpete
    Nov 26 at 18:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440205%2fdifferent-ordering-of-bones-in-blender-than-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














All collections in blender are of type bpy_prop_collection. This collection functions both like a dictionary and a list, allowing items to be referenced either by index or name. Like a dictionary you can't rely on the items being in a specific order, either by name or creation order.



As there is no guarantee that a new item added to the collection will get the highest index number, each export will need to be aligned with the index at the time of export and not trying to re-use indexes of previous exports.



You should find that enumerating the bones list will give the same index numbers that are used internally, you can get blenders internal index number of a bone by using the collections find()



for i, b in enumerate(rig.bones):
print(i, rig.bones.find(b.name), b.name)


0 0 spine
1 1 spine.001
2 2 spine.003
3 3 spine.004
4 4 spine.005
5 5 spine.006
6 6 shoulder.L
7 7 upper_arm.L
...


Bones move vertices that are assigned a weight in a vertex group with a name matching the bone. So the bone named "shoulder.L" will move vertices that have been assigned a weight to vertex group named "shoulder.L". Expanding on this answer you can access each vertices group weight.



grpid = obj.data.vertices[0].groups[0].group
bone_name = obj.vertex_groups[grpid].name
vert_bone_weight = obj.data.vertices[0].groups[0].weight





share|improve this answer





















  • Yes, I realized this after checking the exported file. Index dosen't matter, only names count. Luckily I export each pose's name and saw there is a mixup in the animation matrices order. (armature.pose.bones) Sorting pose matrices by the bones' names fixes the problem. Thank you for clarifying.
    – fpete
    Nov 26 at 18:00
















0














All collections in blender are of type bpy_prop_collection. This collection functions both like a dictionary and a list, allowing items to be referenced either by index or name. Like a dictionary you can't rely on the items being in a specific order, either by name or creation order.



As there is no guarantee that a new item added to the collection will get the highest index number, each export will need to be aligned with the index at the time of export and not trying to re-use indexes of previous exports.



You should find that enumerating the bones list will give the same index numbers that are used internally, you can get blenders internal index number of a bone by using the collections find()



for i, b in enumerate(rig.bones):
print(i, rig.bones.find(b.name), b.name)


0 0 spine
1 1 spine.001
2 2 spine.003
3 3 spine.004
4 4 spine.005
5 5 spine.006
6 6 shoulder.L
7 7 upper_arm.L
...


Bones move vertices that are assigned a weight in a vertex group with a name matching the bone. So the bone named "shoulder.L" will move vertices that have been assigned a weight to vertex group named "shoulder.L". Expanding on this answer you can access each vertices group weight.



grpid = obj.data.vertices[0].groups[0].group
bone_name = obj.vertex_groups[grpid].name
vert_bone_weight = obj.data.vertices[0].groups[0].weight





share|improve this answer





















  • Yes, I realized this after checking the exported file. Index dosen't matter, only names count. Luckily I export each pose's name and saw there is a mixup in the animation matrices order. (armature.pose.bones) Sorting pose matrices by the bones' names fixes the problem. Thank you for clarifying.
    – fpete
    Nov 26 at 18:00














0












0








0






All collections in blender are of type bpy_prop_collection. This collection functions both like a dictionary and a list, allowing items to be referenced either by index or name. Like a dictionary you can't rely on the items being in a specific order, either by name or creation order.



As there is no guarantee that a new item added to the collection will get the highest index number, each export will need to be aligned with the index at the time of export and not trying to re-use indexes of previous exports.



You should find that enumerating the bones list will give the same index numbers that are used internally, you can get blenders internal index number of a bone by using the collections find()



for i, b in enumerate(rig.bones):
print(i, rig.bones.find(b.name), b.name)


0 0 spine
1 1 spine.001
2 2 spine.003
3 3 spine.004
4 4 spine.005
5 5 spine.006
6 6 shoulder.L
7 7 upper_arm.L
...


Bones move vertices that are assigned a weight in a vertex group with a name matching the bone. So the bone named "shoulder.L" will move vertices that have been assigned a weight to vertex group named "shoulder.L". Expanding on this answer you can access each vertices group weight.



grpid = obj.data.vertices[0].groups[0].group
bone_name = obj.vertex_groups[grpid].name
vert_bone_weight = obj.data.vertices[0].groups[0].weight





share|improve this answer












All collections in blender are of type bpy_prop_collection. This collection functions both like a dictionary and a list, allowing items to be referenced either by index or name. Like a dictionary you can't rely on the items being in a specific order, either by name or creation order.



As there is no guarantee that a new item added to the collection will get the highest index number, each export will need to be aligned with the index at the time of export and not trying to re-use indexes of previous exports.



You should find that enumerating the bones list will give the same index numbers that are used internally, you can get blenders internal index number of a bone by using the collections find()



for i, b in enumerate(rig.bones):
print(i, rig.bones.find(b.name), b.name)


0 0 spine
1 1 spine.001
2 2 spine.003
3 3 spine.004
4 4 spine.005
5 5 spine.006
6 6 shoulder.L
7 7 upper_arm.L
...


Bones move vertices that are assigned a weight in a vertex group with a name matching the bone. So the bone named "shoulder.L" will move vertices that have been assigned a weight to vertex group named "shoulder.L". Expanding on this answer you can access each vertices group weight.



grpid = obj.data.vertices[0].groups[0].group
bone_name = obj.vertex_groups[grpid].name
vert_bone_weight = obj.data.vertices[0].groups[0].weight






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 at 1:54









sambler

4,6611816




4,6611816












  • Yes, I realized this after checking the exported file. Index dosen't matter, only names count. Luckily I export each pose's name and saw there is a mixup in the animation matrices order. (armature.pose.bones) Sorting pose matrices by the bones' names fixes the problem. Thank you for clarifying.
    – fpete
    Nov 26 at 18:00


















  • Yes, I realized this after checking the exported file. Index dosen't matter, only names count. Luckily I export each pose's name and saw there is a mixup in the animation matrices order. (armature.pose.bones) Sorting pose matrices by the bones' names fixes the problem. Thank you for clarifying.
    – fpete
    Nov 26 at 18:00
















Yes, I realized this after checking the exported file. Index dosen't matter, only names count. Luckily I export each pose's name and saw there is a mixup in the animation matrices order. (armature.pose.bones) Sorting pose matrices by the bones' names fixes the problem. Thank you for clarifying.
– fpete
Nov 26 at 18:00




Yes, I realized this after checking the exported file. Index dosen't matter, only names count. Luckily I export each pose's name and saw there is a mixup in the animation matrices order. (armature.pose.bones) Sorting pose matrices by the bones' names fixes the problem. Thank you for clarifying.
– fpete
Nov 26 at 18:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440205%2fdifferent-ordering-of-bones-in-blender-than-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks