我有以下代码可以使用blender启用文件浏览器:
import bpy
import os
from bpy.props import StringProperty
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator
sel = ''
class OpenBrowser(bpy.types.Operator):
bl_idname = "open.file"
bl_label = "Select Excel File"
bli_description = "Simulation output excel file"
filter_glob: StringProperty(default = '*.xls;*.xlsx',options = {'HIDDEN'})
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
#somewhere to remember the address of the file
def execute(self, context):
global sel
sel = self.filepath
#self.selected_file = self.filepath
#display = "filepath= "+self.filepath
#print(display) #Prints to console
#Window>>>Toggle systen console
return {'FINISHED'}
def invoke(self, context, event): # See comments at end [1]
context.window_manager.fileselect_add(self)
global sel
sel = self.filepath
#Open browser, take reference to 'self'
#read the path to selected file,
#put path in declared string type data structure self.filepath
return {'RUNNING_MODAL'}
# Tells Blender to hang on for the slow user input
bpy.utils.register_class(OpenBrowser)
#Tell Blender this exists and should be used
# [1] In this invoke(self, context, event) is being triggered by the below command
#but in your script you create a button or menu item. When it is clicked
# Blender runs invoke() automatically.
#execute(self,context) prints self.filepath as proof it works.. I hope.
bpy.ops.open.file('INVOKE_DEFAULT')
print(sel)
我面临的问题是,我已经声明了一个全局变量sel
,我希望在运行代码时将从用户选择的文件路径保存到该变量。但是,当我运行脚本时,我看到sel没有改变,它与初始化时一样。有人能帮助我如何从类中访问self.filepath
变量吗?我做错了什么
如果我理解正确,您希望存储该值以备将来使用。 我不确定为什么在您的情况下“sel”甚至不更新,但我认为更正确的方法是使用这样的property:
属性可以添加到所有ID类型,但对于“全局”值,通常使用^us。虽然一个项目中可以有多个场景,但它们将具有单独的值。搅拌机关闭时存储属性值
如果您正在制作一个插件,您还可以将您的值存储在Addon Preferences中。此值对于所有blender项目都相同
相关问题 更多 >
编程相关推荐