python - Blender 3D 附加脚本加载失败

标签 python api blender bpy

标题

我制作了附加脚本。

但是在 blender UI 中加载失败。

错误消息是“_RestrictContext”对象没有属性“scene”。

但是这个脚本在文本编辑器的 blender 中运行得很好。

为什么不加载这个附加组件?

bl_info = {
    "name": "Add Cube",
    "author": "jsh",
    "version": (1, 0),
    "blender": (2, 68, 0),
    "location": "View3D > Tool Shelf > Text make",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "http://togetherall.infomaster.co.kr",
    "category": "Object"}



import bpy
from bpy.props import *

#
#    Store properties in the active scene
#
def initSceneProperties(scn):
    bpy.types.Scene.MyInt = IntProperty(
        name = "Integer", 
        description = "Enter an integer")
    scn['MyInt'] = 17

    bpy.types.Scene.MyFloat = FloatProperty(
        name = "Float", 
        description = "Enter a float",
        default = 33.33,
        min = -100,
        max = 100)

    bpy.types.Scene.MyBool = BoolProperty(
        name = "Boolean", 
        description = "True or False?")
    scn['MyBool'] = True

    bpy.types.Scene.MyEnum = EnumProperty(
        items = [('Eine', 'Un', 'One'), 
                 ('Zwei', 'Deux', 'Two'),
                 ('Drei', 'Trois', 'Three')],
        name = "Ziffer")
    scn['MyEnum'] = 2

    bpy.types.Scene.MyString = StringProperty(
        name = "String2")
    scn['MyString'] = "Lorem ipsum dolor sit amet"
    return

initSceneProperties(bpy.context.scene)

#
#    Menu in UI region
#
class UIPanel(bpy.types.Panel):
    bl_label = "Make Text"
    bl_space_type = "VIEW_3D"
    #bl_region_type = "UI"
    bl_region_type = "TOOL_PROPS"


    def draw(self, context):
        layout = self.layout
        scn = context.scene
        layout.prop(scn, 'MyInt', icon='BLENDER', toggle=True)
        layout.prop(scn, 'MyFloat')
        layout.prop(scn, 'MyBool')
        layout.prop(scn, 'MyEnum')
        layout.prop(scn, 'MyString')
        layout.operator("idname_must.be_all_lowercase_and_contain_one_dot")

#
#    The button prints the values of the properites in the console.
#

class OBJECT_OT_PrintPropsButton(bpy.types.Operator):
    bl_idname = "idname_must.be_all_lowercase_and_contain_one_dot"
    bl_label = "make"

    def execute(self, context):
        bpy.ops.mesh.primitive_cube_add()       
        return{'FINISHED'}    

def printProp(label, key, scn):
    try:
        val = scn[key]
    except:
        val = 'Undefined'
    #print("%s %s" % (key, val))
    return val



def register():
    bpy.utils.register_class(UIPanel)
    bpy.utils.register_class(OBJECT_OT_PrintPropsButton)

def unregister():
    bpy.utils.unregister_class(UIPanel)
    bpy.utils.unregister_class(OBJECT_OT_PrintPropsButton)



if __name__ == "__main__":
    register()

最佳答案

Blender 使用所谓的 RestrictContext在插件的注册/取消注册阶段。这意味着,您无法对上下文执行某些操作,因为内容可能尚未准备好。

就您而言,您正在全局模块范围内执行 initSceneProperties(bpy.context.scene) ,这意味着它将在加载该模块后立即执行。例如,将该初始化代码移动到运算符的 execute() 方法中,并在首次运行运算符时或任何其他有意义的地方执行此操作(尽可能晚,必要时尽早)。

请参阅 RestrictContext 上的文档有关如何执行此操作的示例。

关于python - Blender 3D 附加脚本加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20302017/

相关文章:

python - 树莓派相机透明图像叠加

python - 如何从多行字符串中提取特定信息

c++ - sockaddr、sockaddr_in 和 sockaddr_in6 有什么区别?

api - 语音识别API

java - Jmonkey 空指针异常

python - 按字母顺序对数据框排序

json - 允许对象在 swagger 中具有 oneOf 某些类型

python - Blender Python API 添加属性到对象

python - 使用python启动并行ssh渲染作业

python - 在 Cloudformation 中使用时无法从 S3 下载引导文件