python - 多排序 Python

标签 python sorting blender

我正在尝试用 Python 为 Blender 编写自定义导出器。但是,我在弄清楚如何使用 Python 的 native 排序实现以我需要的方式对顶点进行排序时遇到了一些问题。

基本上,我需要以可以形成这样的网格的方式导出顶点,假设 V 是 double 。

V V V V V
V V V V V
V V V V V
V V V V V

我正在编写的“引擎”会根据坐标在上面显示的顶点图中的位置自动检索坐标的 X 和 Z 值。 V 代表 Y 坐标。所以这个 5 x 5 的顶点贴图将创建一个 4 x 4 面的网格。

但是,在 Blender 中,顶点似乎是针对网格的每个面单独排序的,而不是像我这样的格式。因此,为了导出它,我需要先按深度(Blender 中的 Y 坐标)然后按宽度(Blender 中的 X 坐标)对坐标进行排序。它必须表现得像 SQL 查询,其中给定的第一个参数优先于第二个参数。

这是我的代码目前的样子

#!BPY

"""
Name: 'VaV Export'
Blender: 269
Group: 'Export'
Tooltip: 'Export to VaV file format'
"""
import Blender
import bpy

class Coordinate:
    def __init__(self, posX, posY):
        self.posX = posX
        self.posY = posY

    def sortX(self, other):
        if posX < other.posX:
            return 1

    def sortY(self, other):
        if posY < other.posY:
            return 1



def write(filename):
    out = open(filename, "w")
    terraindata = bpy.data.meshes["Terrain"]
    vertices = terraindata.vertices
    vertices = sorted(vertices, key = lambda x: x.co.y, reverse = True)
    vertices = sorted(vertices, key = lambda x: x.co.x, reverse = False)
    print(vertices)

Blender.Window.FileSelector(write, "Export")

第一次调用 sorted() 成功地根据深度 (Y) 对其进行排序。然而,第二次调用 sorted 打乱了顺序(如预期的那样)。我怎样才能修改它以允许两种类型发生而不会出现第二种情况?

提前感谢您抽出时间!

最佳答案

改变

vertices = sorted(vertices, key = lambda x: x.co.y, reverse = True)

vertices = sorted(vertices, key = lambda x: (x.co.y, x.co.x), reverse = True)

...应该就可以了。

关于python - 多排序 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053746/

相关文章:

android - openGLES 骨骼动画解决方案的方法

c++ - Boost.Python dll 在编译时被跳过

java - 尝试按值对链接的 HashMap <Character, Integer> 进行排序

Java选择排序修改

iphone - 带数字的字符串排序 last objective c

c++ - OBJ 中只有第一个对象有法线

java - 如何将UV纹理从 blender 加载到opengl android java

python - scikit learn 模型的嵌套并行性

python - 如何根据条件替换字符串中的单词?

python - pandas,lambda 内的比较