我正在尝试为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/