【问题标题】:Custom export script for blender搅拌机的自定义导出脚本
【发布时间】:2016-05-24 08:59:15
【问题描述】:

我正在尝试编写一个自定义脚本来导出场景中的对象及其旋转中心。这是我的算法获得旋转中心的样子: 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 步,但我不知道如何将光标捕捉到对象并获取光标坐标。

【问题讨论】:

    标签: python-3.x blender


    【解决方案1】:

    当您从 blenders 文本编辑器运行脚本时,您会在尝试运行大多数运算符时遇到上下文错误,但无需使用运算符即可检索您想要的信息。

    如果你想要 3DCursor 的位置,你可以在scene.cursor_location找到它

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

    对象旋转存储为弧度,因此您可能需要import math 并使用math.degrees()

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

    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) )
    

    【讨论】:

    • 确实不需要捕捉到光标,因为对象的位置是指它的中心。谢谢你的时间:)!
    猜你喜欢
    • 2017-06-15
    • 2011-12-08
    • 2012-02-07
    • 1970-01-01
    • 2021-09-25
    • 2012-02-02
    • 1970-01-01
    • 2017-09-03
    • 2014-09-24
    相关资源
    最近更新 更多