搅拌机的自定义导出脚本

问题描述:

我正在尝试编写一个自定义脚本来导出场景中的对象与它们的旋转中心。下面是我的算法看起来像得到旋转中心:使用其名称 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步,但我不知道如何将光标捕捉到对象并获取光标坐标。

当您从搅拌器文本编辑器运行脚本时,当您尝试运行大多数操作员时,将会出现上下文错误,但是您需要的信息可以在不使用操作员的情况下进行检索。

如果你想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)) 
+0

确实没有必要捕捉到光标,因为对象的位置指的是它的中心。谢谢你的时间 :) ! –