Home Technical Talk

Blender (Python) 2.79: Getting Triangle And Its Properties?

polycounter lvl 6
Offline / Send Message
MrQuetch polycounter lvl 6
Hello, everyone. So, I can get the vertex positions, colors, and triangle indices of a model just fine. But, they do not have an overall relationship.
I need to be able to reference a triangle, and get the vertex positions and colors found within the three vertices in that triangle.
Although it's not needed at the moment, I would also like to know how to get the texture coordinates for future reference.
How can I go about doing this? Any help will be greatly appreciated. Thank you!

Replies

  • MrQuetch
    Offline / Send Message
    MrQuetch polycounter lvl 6
    So, I managed to find this: https://docs.blender.org/api/blender_python_api_2_77_1/bpy.types.Mesh.html#bpy.types.Mesh.loops
    So far, it's a great start, and I've already got the vertex colors and texture coordinates working.
    Apparently, I can't reference a vertex position from its index in the polygon.
    The documentation on that URL even says so.
    So, how can I go about doing this part?
  • MrQuetch
    Offline / Send Message
    MrQuetch polycounter lvl 6
    Nevermind! My script finally works! Basically, since I could only reference vertex indices of a triangle, I referenced vertices by loops using the vertex indices. It makes a lot of sense now that I think about it. I just had to think it through. The only problem now is that my models are lopsided. But, that should be fixed with rotation. Thank you everyone!
  • MrQuetch
    Offline / Send Message
    MrQuetch polycounter lvl 6
    It's fine if nobody gets back to me on this... But, does anyone know how to multiply the vertex coordinate by an object's rotation? My models still export lopsided. I've tried tons of different things with no luck. The model shows facing up-right on the Z-axis in Blender, but it's rotated 90 degrees - facing down in my actual game. Not sure how to go about doing this.
  • RN
    Offline / Send Message
    RN sublime tool
    A vertex coordinate is a mathutils.Vector object. You can rotate a Vector object by multiplying it by either a mathutils.Matrix or mathutils.Quaternion, in this order:
    rotatedVertex1 = myQuaternion * vertex.co # Using a Quaternion, or...<br>rotatedVertex2 = myMatrix * vertex.co # ...using a Matrix.<br>
    This will rotate the vertex around its origin.
    Edited for clarity: if the vertex coordinates are relative to the mesh object pivot ("local space") then they'll be rotated around that point. If you want to rotate them around the scene origin or some other arbitrary point, then you'll have to translate the vertices by the offset vector between that arbitrary point and the mesh object pivot.

    Using a mathutils.Quaternion is usually easier to create, check the examples: https://docs.blender.org/api/2.79/mathutils.html#mathutils.Quaternion.

    The .rotation attribute of a scene object is a Quaternion value, for example.
  • MrQuetch
    Offline / Send Message
    MrQuetch polycounter lvl 6
    RN said:
    A vertex coordinate is a mathutils.Vector object. You can rotate a Vector object by multiplying it by either a mathutils.Matrix or mathutils.Quaternion, in this order:
    rotatedVertex1 = myQuaternion * vertex.co # Using a Quaternion, or...<br>rotatedVertex2 = myMatrix * vertex.co # ...using a Matrix.<br>
    This will rotate the vertex around its origin.
    Edited for clarity: if the vertex coordinates are relative to the mesh object pivot ("local space") then they'll be rotated around that point. If you want to rotate them around the scene origin or some other arbitrary point, then you'll have to translate the vertices by the offset vector between that arbitrary point and the mesh object pivot.

    Using a mathutils.Quaternion is usually easier to create, check the examples: https://docs.blender.org/api/2.79/mathutils.html#mathutils.Quaternion.

    The .rotation attribute of a scene object is a Quaternion value, for example.
    Sorry for the late reply!

    Thank you so much, RN! This means a lot to me.
Sign In or Register to comment.