Hello, everyone.
I've found a topic that is already leading me in the right direction:
https://www.blender.org/forum/viewtopic.php?t=28466However, this script only writes what vertex group contains what amount of vertices.
What I would like to do is write out the vertex indices that are used within that vertex group to a file.
Currently, I cannot think of a good way to go about doing this. Does anyone have any suggestions?
Any help is greatly appreciated.
Replies
Keep in mind, I'm using 2.79. I'm not sure if that means anything, though.
for v in ob.data.vertices:
for g in v.groups:
vertexGroupsExport[vertexGroupsCollection[g.group].name].append((v.index, g.weight))
# Write 'vertexGroupsExport' to a file.
</code><code><code>vertexGroupsCollection = ob.vertex_groups<br>
- {'Bone': [0, 1, 2, 3, 8, 9, 10, 11], 'Bone.001': [4, 5, 6, 7]}
It would look something like that:- Bone: 0, 1, 2, 3, 8, 9, 10, 11
- Bone.001: 4, 5, 6, 7
How would I go about doing that? Thanks in advance.- >>> stringOutput = '\n'.join('%s: %s' % (key, repr(value)) for key, value in dictionary.items())<br>>>> print(stringOutput) # Or write to file.<br>Bone.001: [4, 5, 6, 7]<br>Bone: [0, 1, 2, 3, 8, 9, 10, 11]
The problem is if you want to parse this string later. This is why using JSON saves you time, for every json.dumps() you can use a json.loads() on the contents of the text file, instead of using the several string operations that you'd need.I know this is getting older. After having looked at the Blender Python API, and some other sources on the internet, I have managed to come up with this method of getting the correct vertices from the correct bones - all as integers. Here's the code if anyone wants to use it. I know it's not the nicest looking thing, but it works like it's supposed to.
<code># Line below uses "dictionary comprehension" to create a dictionary.<br>
myVertexGroups = {i: [ ] for i in range(len(mesh.vertex_groups))}for v in mesh.data.vertices:
for g in v.groups:
myVertexGroups[g.group].append(v)
for groupIndex, groupVertices in myVertexGroups.items():
print('Bone %i:\n\t%s\n' % (groupIndex, groupVertices))mesh = bpy.context.active_object<br>
Thank you! Most of my models are low polygon - generally 550 triangles or less. So, I don't think my code is too bad. I'm probably very wrong, though. But, I'll definitely keep this dictionary method in mind. I'm beginning to see why you do it this way. Not only does it take up less code - but it looks like it's becoming easier to reference.