Skip to content

Instantly share code, notes, and snippets.

@ABelliqueux
Created July 30, 2021 08:10
Show Gist options
  • Save ABelliqueux/751c49e48c2fb75198b835084809864b to your computer and use it in GitHub Desktop.
Save ABelliqueux/751c49e48c2fb75198b835084809864b to your computer and use it in GitHub Desktop.
Blender RSD 279-280 equivalent
>>> obj = C.object
>>> print(obj)
<bpy_struct, Object("Cube")>
>>> mesh = obj.to_mesh()
>>> print(mesh)
<bpy_struct, Mesh("Cube")>
>>> mesh_uvs = mesh.uv_layers.active
>>> print(mesh_uvs)
<bpy_struct, MeshUVLoopLayer("UVMap")>
>>> mesh_uvs = mesh_uvs.data
>>> print(mesh_uvs)
<bpy_collection[24], MeshUVLoopLayer.data> # loop through groups of 4 to get 4 uv coords
>>> mesh.calc_loop_triangles()
>>> print(mesh.loop_triangles)
<bpy_collection[12], MeshLoopTriangles>
>>> for i,p in enumerate(mesh.loop_triangles):
... print(i)
... print(p)
...
# 12 triangles = 6 quads/faces
0
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddee8>
1
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddef8>
2
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf08>
3
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf18>
4
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf28>
5
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf38>
6
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf48>
7
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf58>
8
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf68>
9
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf78>
10
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf88>
11
<bpy_struct, MeshLoopTriangle at 0x7feb0e1ddf98>
# colors
>>> mesh_cols = mesh.vertex_colors.active.data
>>> print(mesh_cols)
<bpy_collection[24], MeshLoopColorLayer.data> # 1 layer = 1 color - loop through 4 items for RGBA
for i,p in enumerate(mesh_cols):
col = mesh_cols[i]
print(col)
<bpy_struct, MeshLoopColor at 0x7fdb43201578>
<bpy_struct, MeshLoopColor at 0x7fdb4320157c>
<bpy_struct, MeshLoopColor at 0x7fdb43201580>
<bpy_struct, MeshLoopColor at 0x7fdb43201584>
<bpy_struct, MeshLoopColor at 0x7fdb43201588>
<bpy_struct, MeshLoopColor at 0x7fdb4320158c>
<bpy_struct, MeshLoopColor at 0x7fdb43201590>
<bpy_struct, MeshLoopColor at 0x7fdb43201594>
<bpy_struct, MeshLoopColor at 0x7fdb43201598>
<bpy_struct, MeshLoopColor at 0x7fdb4320159c>
<bpy_struct, MeshLoopColor at 0x7fdb432015a0>
<bpy_struct, MeshLoopColor at 0x7fdb432015a4>
<bpy_struct, MeshLoopColor at 0x7fdb432015a8>
<bpy_struct, MeshLoopColor at 0x7fdb432015ac>
<bpy_struct, MeshLoopColor at 0x7fdb432015b0>
<bpy_struct, MeshLoopColor at 0x7fdb432015b4>
<bpy_struct, MeshLoopColor at 0x7fdb432015b8>
<bpy_struct, MeshLoopColor at 0x7fdb432015bc>
<bpy_struct, MeshLoopColor at 0x7fdb432015c0>
<bpy_struct, MeshLoopColor at 0x7fdb432015c4>
<bpy_struct, MeshLoopColor at 0x7fdb432015c8>
<bpy_struct, MeshLoopColor at 0x7fdb432015cc>
<bpy_struct, MeshLoopColor at 0x7fdb432015d0>
<bpy_struct, MeshLoopColor at 0x7fdb432015d4>
colRGBA = []
>>> for i,p in enumerate(mesh_cols):
... col = mesh_cols[i]
... col = col.color[0], col.color[1], col.color[2], col.color[3]
... colRGBA.append(col)
...
>>> print(colRGBA)
[(0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.48627451062202454, 0.4627451002597809, 1.0, 1.0),
(0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0),
(0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0),
(0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0),
(0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.48627451062202454, 0.4627451002597809, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.48627451062202454, 0.4627451002597809, 1.0, 1.0),
(0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0), (0.48627451062202454, 0.4627451002597809, 1.0, 1.0), (0.46666666865348816, 0.4431372582912445, 1.0, 1.0)]
UVs =[]
for i,p in enumerate(mesh_uvs):
uv = mesh_uvs[i].uv
UVs.append(uv)
[Vector((0.6666666865348816, 8.940696005765858e-08)), Vector((1.0, 3.973642748178463e-08)), Vector((1.0, 0.3333333730697632)),Vector((0.6666666865348816, 0.3333333432674408)),
Vector((0.0, 0.6666668057441711)), Vector((6.953874986947994e-08, 0.33333343267440796)), Vector((0.3333333134651184, 0.3333335220813751)), Vector((0.333333283662796, 0.6666667461395264)),
Vector((0.6666666865348816, 0.333333283662796)), Vector((0.33333340287208557, 0.3333333432674408)), Vector((0.3333333134651184, 0.0)), Vector((0.6666666269302368, 1.9868213740892315e-08)),
Vector((2.9802320611338473e-08, 0.33333343267440796)), Vector((0.0, 1.291433875394432e-07)), Vector((0.33333322405815125, 0.0)), Vector((0.3333333134651184, 0.333333283662796)),
Vector((0.6666666269302368, 0.33333343267440796)), Vector((0.9999999403953552, 0.33333343267440796)), Vector((0.9999999403953552, 0.6666666865348816)), Vector((0.6666666269302368, 0.6666667461395264)),
Vector((0.3333333134651184, 0.33333343267440796)), Vector((0.666666567325592, 0.33333343267440796)), Vector((0.6666666269302368, 0.6666666865348816)), Vector((0.3333333432674408, 0.6666668057441711))]
# UV Textures
>>> for uv in mesh_uvs:
... print(uv)
...
<bpy_struct, MeshUVLoop at 0x7fdb422ac0c8>
<bpy_struct, MeshUVLoop at 0x7fdb422ac0d4>
<bpy_struct, MeshUVLoop at 0x7fdb422ac0e0>
<bpy_struct, MeshUVLoop at 0x7fdb422ac0ec>
<bpy_struct, MeshUVLoop at 0x7fdb422ac0f8>
<bpy_struct, MeshUVLoop at 0x7fdb422ac104>
<bpy_struct, MeshUVLoop at 0x7fdb422ac110>
<bpy_struct, MeshUVLoop at 0x7fdb422ac11c>
<bpy_struct, MeshUVLoop at 0x7fdb422ac128>
<bpy_struct, MeshUVLoop at 0x7fdb422ac134>
<bpy_struct, MeshUVLoop at 0x7fdb422ac140>
<bpy_struct, MeshUVLoop at 0x7fdb422ac14c>
<bpy_struct, MeshUVLoop at 0x7fdb422ac158>
<bpy_struct, MeshUVLoop at 0x7fdb422ac164>
<bpy_struct, MeshUVLoop at 0x7fdb422ac170>
<bpy_struct, MeshUVLoop at 0x7fdb422ac17c>
<bpy_struct, MeshUVLoop at 0x7fdb422ac188>
<bpy_struct, MeshUVLoop at 0x7fdb422ac194>
<bpy_struct, MeshUVLoop at 0x7fdb422ac1a0>
<bpy_struct, MeshUVLoop at 0x7fdb422ac1ac>
<bpy_struct, MeshUVLoop at 0x7fdb422ac1b8>
<bpy_struct, MeshUVLoop at 0x7fdb422ac1c4>
<bpy_struct, MeshUVLoop at 0x7fdb422ac1d0>
<bpy_struct, MeshUVLoop at 0x7fdb422ac1dc>
# texture names
>>> import os
>>> mesh_materials = obj.material_slots
>>> for tex in mesh_materials:
... texFileName = os.path.splitext(tex.material.node_tree.nodes["Image Texture"].image.name)[0]
... print(texFileName)
...
cubetex
wings
# texture sizes
TexSizes = []
for tex in mesh_materials:
tex_w = tex.material.node_tree.nodes["Image Texture"].image.size[0]-0.85
tex_h = tex.material.node_tree.nodes["Image Texture"].image.size[1]-0.85
TexSizes.append(tex_w, tex_h)
print(TexSizes)
[1024, 1024, 64, 32]
# vertices
>>> for i,p in enumerate(mesh.loop_triangles):
... for j,c in enumerate(p.vertices):
... print(j)
... print(c)
...
0
0
1
1
2
2
0
0
1
2
2
3
0
4
1
7
2
6
0
4
1
6
2
5
0
0
1
4
2
5
0
0
1
5
2
1
0
1
1
5
2
6
0
1
1
6
2
2
0
2
1
6
2
7
0
2
1
7
2
3
0
4
1
0
2
3
0
4
1
3
2
7
for i,p in enumerate(mesh.loop_triangles):
uv = (mesh_uvs[i].uv,
mesh_uvs[i].uv,
mesh_uvs[i].uv
)
tex_w = mesh_uvs[i].id_data.materials[0].node_tree.nodes['Image Texture'].image.size[0]-0.85
tex_h = mesh_uvs[i].id_data.materials[0].node_tree.nodes['Image Texture'].image.size[1]-0.85
for j,c in enumerate(p.vertices):
print(round(tex_w*uv[j].x))
print(round(tex_h-(tex_h*uv[j].y)))
682
1023
682
1023
682
1023
1023
1023
1023
1023
1023
1023
1023
682
1023
682
1023
682
682
682
682
682
682
682
0
341
0
341
0
341
0
682
0
682
0
682
341
682
341
682
341
682
341
341
341
341
341
341
682
682
682
682
682
682
341
682
341
682
341
682
341
1023
341
1023
341
1023
682
1023
682
1023
682
1023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment