Blender 导出插件和 bl_idname
Posted
技术标签:
【中文标题】Blender 导出插件和 bl_idname【英文标题】:Blender export addon and bl_idname 【发布时间】:2014-11-11 12:35:54 【问题描述】:我正在尝试用 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 和 Python 都是新手,所以我可能完全错了。 每当我尝试启用插件时都会收到错误消息:AttributeError: "module' object has no attribute 'register'。 【参考方案1】:bl_idname
定义用于访问 blender 中的运算符的名称。使用“object.ExportS3D”,您的导入运算符可用作bpy.ops.object.ExportS3D
,但 bl_idname 需要小写,因此请使用“object.exports3d”
为了让您的进口商开始工作,将 register_module(__name__)
更改为 register_class(ExportS3D)
与取消注册相同。
您需要将return 'FINISHED'
添加到execute()
,这样您就可以去掉所有的“;”你有。
【讨论】:
以上是关于Blender 导出插件和 bl_idname的主要内容,如果未能解决你的问题,请参考以下文章
Blender/UE4骨骼转换插件:Uefy v1.2.1 For UE4 BL 2.8+使用教程
Three.js 的导出 Blender 模型中的材料不起作用
使用python在blender中注销并删除插件不会从菜单中删除该项目