python - Blender 导出插件和 bl_idname

标签 python export blender

我正在尝试用 Python 为 Blender 编写一个简单的导出器,我认为问题出在 bl_idname 上。我不确定该值应该如何格式化。

class ExportS3D(bpy.types.Operator, ExportHelper) :
    bl_idname       = "object.ExportS3D";
    bl_label        = "S3D Exporter";
    bl_options      = {'PRESET'};

    filename_ext    = ".S3D";

我做错的事情可能是完全错误的,所以这是我的代码:

bl_info = {
"name":         "S3D Exporter",
"author":       "M---",
"blender":      (2,7,1),
"version":      (0,0,1),
"location":     "File > Import-Export > S3D",
"description":  "Export S3D files",
"category":     "Import-Export"
}

import bpy
from bpy_extras.io_utils import ExportHelper
import time


class ExportS3D(bpy.types.Operator, ExportHelper) :
    bl_idname       = "object.ExportS3D";
    bl_label        = "S3D Exporter";
    bl_options      = {'PRESET'};

    filename_ext    = ".S3D";

    def execute(self, context):
        export()

#end ExportS3D

def menu_func(self, context):
    self.layout.operator(object.bl_idname, text="Stupid 3D(.S3D)");

def register():
    bpy.utils.register_module(__name__);
    bpy.types.INFO_MT_file_export.append(menu_func);

def unregister():
    bpy.utils.unregister_module(__name__);
    bpy.types.INFO_MT_file_export.remove(menu_func);


def export():

    print( '\n--------------------\n' )

    #--------------------------------------
    #Change to OBJECT mode

    #Do this to ensure any changes made in EDIT mode (like UV Unwrap) are committed
    bpy.ops.object.mode_set( mode='OBJECT' ) 

    #--------------------------------------
    #Get the active object and its data

    ob = bpy.context.active_object
    if ( ob.type != 'MESH' ):
        print( 'Error: please select a MESH object.\n' )
        return

    mesh = ob.data

    #Not sure if this is needed with this script???
    if not mesh.tessfaces and mesh.polygons:
        mesh.calc_tessface()

    #--------------------------------------
    #Comments

    comments = ''

    localtime   = time.localtime()
    timeString  = time.strftime( '%B %d, %Y %H:%M:%S', localtime )
    comments += ( '%s\n' % (timeString) )

    comments += ( '3D object created in Blender%s\n' % (bpy.app.version_string) )
    comments += ( 'Object name: %s\n' % (ob.name) )

    comments += ( 'Blender vertices count: %i\n' % ( len(mesh.vertices) ) )
    comments += ( 'Blender tessfaces count: %i\n' % ( len(mesh.tessfaces) ) )

    #--------------------------------------
    #UV Layer

    if ( mesh.uv_layers.active is not None ):
        uv_layer = mesh.uv_layers.active.data
    else:
        print( 'Error: the object needs to be unwrapped.\n' )
        return

    #--------------------------------------
    #Vertices and Indices

    vertices = 'Vertices\n';
    indices = 'Indices\n';
    image = 'Image Name\n';


    i = 0
    t = 0
    c = 0
    for poly in mesh.polygons:

        for loop_index in poly.loop_indices:
            v = mesh.vertices[mesh.loops[loop_index].vertex_index]

            #Right-handed coordinate systems (OpenGL convention) use: v.co.z and v.normal.z
            #Left-handed coordinate systems (DirectX convention) use: -1*v.co.z and -1*v.normal.z
            #OpenGL textures use: uv_layer[loop_index].uv[1]
            #DirectX textures use: 1-uv_layer[loop_index].uv[1]
            vertices += ( 
                '%f, %f, %f, %f, %f, %f, %f, %f\n' % \
                (
                    v.co.x, v.co.y, v.co.z, 
                    v.normal.x, v.normal.y, v.normal.z,
                    uv_layer[loop_index].uv[0], uv_layer[loop_index].uv[1],
                )
            )
            c += 1

        #OpenGL convention is counter-clockwise winding.
        #DirectX convention is clockwise winding.
        if ( len(poly.vertices) == 3 ):
            #clockwise winding:
            #indices += ( '%i, %i, %i, ' % ( i, i+2, i+1 ) )
            #counter-clockwise winding:
            indices += ( '%i, %i, %i, ' % ( i, i+1, i+2 ) )
            i += 3
            t += 1
        elif ( len(poly.vertices) == 4 ):
            #clockwise winding:
            #indices += ( '%i, %i, %i, ' % ( i, i+2, i+1 ) )
            #indices += ( '%i, %i, %i, ' % ( i, i+3, i+2 ) )
            #counter-clockwise winding:
            indices += ( '%i, %i, %i, ' % ( i, i+1, i+2 ) )
            indices += ( '%i, %i, %i, ' % ( i, i+2, i+3 ) )
            i += 4
            t += 2
        else:
            print( 'Error: faces with less than 3 or more than 4 vertices found.\n' )
            return

    image += '%s' % (bpy.context.object.data.uv_textures.active.data[0].image.name)

    #Remove indices last comma and space
    indices = indices[:-2]

    comments += ( 'Exported vertices: %i\n' % ( i ) )
    comments += ( 'Exported triangles: %i\n' % ( t ) )
    comments += ( 'Exported indices: %i\n\n' % ( t * 3 ) )

    comments += 'Format\nVertex: px, py, pz, nx, ny, nz, u, v\nIndices: i'

    #--------------------------------------
    #Write File

    filenameSuffix = time.strftime( '%Y%d%m%H%M%S', localtime )
    #File path
    filenameFull = ( 'c:/Users/1043468/Desktop/%s.%s.S3D' % ( ob.name, filenameSuffix ) )
    out_file = open( filenameFull, 'wt' )
    out_file.write( '%s\n\n%s\n%s\n%s\n' % ( comments, vertices, indices, image) )
    out_file.close()
    print( '%s\n\nCompleted: %s\n' %(comments, filenameFull) )

if __name__ == "__main__":
    register()

文件名是S3D_Eporter.py,它放置在Blender/2.7.1/scripts/addons/中

最佳答案

bl_idname 定义用于访问 blender 内操作符的名称。使用“object.ExportS3D”,您的导入运算符可用作 bpy.ops.object.ExportS3D,但 bl_idname 需要小写,因此请使用“object.exports3d”

要让您的导入器开始工作,请将 register_module(__name__) 更改为 register_class(ExportS3D) 与取消注册相同。

您需要将 return {'FINISHED'} 添加到 execute() 中,这样就可以去掉所有的 ';'你有。

关于python - Blender 导出插件和 bl_idname,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25891166/

相关文章:

Python 错误 : len() of unsized object while using statsmodels with one row of data

python - 调试 Python 内存碎片

php - 避免使用 PHP 为文件 XLS 中继服务请求 Url 太长

python - blender :在球体周围走动

java - 只有环境光才能在 jME 中可靠工作

python - 如何在Python中将一个文件夹中的多个RGB图像转换为灰度图像

python - 如何在 itertools.accumulate 中定义自己的 func 参数?

macos - Homebrew 导出设置

java - 导入(合并)-导出java db数据库的最佳方法

json - ThreeJS - 未定义的 JSONLoader "Cannot read property ' 制服