Python 3.x 搅拌机的自定义导出脚本

Python 3.x 搅拌机的自定义导出脚本,python-3.x,blender,Python 3.x,Blender,我正在尝试编写一个自定义脚本,以导出场景中具有旋转中心的对象。下面是我的算法得到旋转中心的样子: 1-使用对象名称选择对象,然后调用 2-将光标捕捉到对象(中心) 3-获取鼠标坐标 4-写入鼠标坐标 import bpy sce = bpy.context.scene ob_list = sce.objects path = 'C:\\Users\\bestc\\Dropbox\\NetBeansProjects\\MoonlightWanderer\\res\\Character\\pla

我正在尝试编写一个自定义脚本,以导出场景中具有旋转中心的对象。下面是我的算法得到旋转中心的样子: 1-使用对象名称选择对象,然后调用 2-将光标捕捉到对象(中心) 3-获取鼠标坐标 4-写入鼠标坐标

import bpy

sce = bpy.context.scene
ob_list = sce.objects

path = 'C:\\Users\\bestc\\Dropbox\\NetBeansProjects\\MoonlightWanderer\\res\\Character\\player.dat'

# Now copy the coordinate of the mouse as center of rotation
try:
    outfile = open(path, 'w')
    for ob in ob_list:
        if ob.name != "Camera" and ob.name != "Lamp":
            ob.select = True
            bpy.ops.view3d.snap_cursor_to_selected()

            mouseX, mouseY, mouseZ = bpy.ops.view3d.cursor_location
            # write object name, coords, center of rotation and rotation followed by a newline
            outfile.write( "%s\n" % (ob.name))

            x, y, z = ob.location # unpack the ob.loc tuple for printing
            x2, y2, z2 = ob.rotation_euler # unpack the ob.rot tuple for printing
            outfile.write( "%f %f %f %f %f\n" % (y, z, mouseY, mouseZ, y2) )

    #outfile.close()

except Exception as e:
    print ("Oh no! something went wrong:", e)

else:
    if outfile: outfile.close()
    print("done writing")`enter code here`

显然,问题出在第2步和第3步,但我不知道如何将光标捕捉到对象并获取光标坐标。

当您从blenders文本编辑器运行脚本时,当您尝试运行大多数运算符时,会出现上下文错误,但您需要的信息可以在不使用运算符的情况下检索

如果需要3DCursor位置,可以在中找到它

如果已将光标捕捉到对象,则光标位置将等于对象位置,即,因为捕捉光标将使光标给出相同的值,您可以仅使用对象位置,而不必将光标捕捉到对象位置。代码实际要做的(如果它工作的话)是将光标捕捉到选定的对象,这会将光标定位在所有选定对象之间的中间点。在对象中循环时,您正在选择每个对象,但没有取消选择它们,因此每次迭代都会向选择中添加另一个对象,每次都会将光标偏移到不同的位置,但如果在开始时取消选择所有对象,则只会将光标偏移到第一个对象的位置

对象旋转存储为弧度,因此您可能希望
导入数学
并使用

此外,由于对象可以有任何名称,您应该发现测试对象类型以选择要导出的对象更准确

for ob in ob_list:
    if ob.type != "CAMERA" and ob.type != "LAMP":

        mouseX, mouseY, mouseZ = sce.cursor_location
        # write object name, coords, center of rotation and rotation followed by a newline
        outfile.write( "%s\n" % (ob.name))

        x, y, z = ob.location # unpack the ob.loc tuple for printing
        x2, y2, z2 = ob.rotation_euler # unpack the ob.rot tuple for printing
        outfile.write( "%f %f %f %f %f\n" % (y, z, mouseY, mouseZ, y2) )

实际上,不需要捕捉到光标,因为对象的位置指的是它的中心。谢谢你抽出时间:)!