1
$\begingroup$

For a workflow tool I made a panel in which it displays each separate library that is linked in my file. For each library I want to add some buttons that does stuff to the library in question.

So in this example I have my panel which shows for example 2 libraries and for each one it creates a "reload" button.

Now my question is.. How do I make it so the reload button only reloads the library I want to reload? I would probably have to give the button an id or something so it knows for which library it was created...

my code so far:

import bpy

libs = bpy.data.libraries

class Reloadlib(bpy.types.Operator):
    """Reloads the current Library"""
    bl_idname = "my.reload_lib"
    bl_label = "Reload"

    def execute(self, context):
        libs[?????].reload()
        self.report({'INFO'}, 'Reloading ' + libs[?????].name)
        return {'FINISHED'}

class LibPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Scene Libraries"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        layout.label(text="Libraries:")

        for i in libs:
            layout.label(text= i.name.replace(".blend",""))
            row = layout.row()
            sub = row.row()
            sub.operator("my.reload_xref", icon = "FILE_REFRESH")

def register():
    bpy.utils.register_class(Reloadlib)
    bpy.utils.register_class(LibPanel)

def unregister():
    bpy.utils.unregister_class(Reloadlib)
    bpy.utils.unregister_class(LibPanel)

if __name__ == "__main__":
    register()

Where the question marks are is where I should be able to make the difference between the first [0] or second [1] library in the libs[] array.

In future I would also like to ad an 'open' and an 'unlink' button for each library so I can make changes to the other file. But this works by the same principle as the reload button.

$\endgroup$
3
  • $\begingroup$ Is reload_xref meant to be reload_lib? $\endgroup$ Nov 27, 2019 at 17:32
  • $\begingroup$ Yes it was meant to be reload_lib I first started to make the script referring to the libraries as xrefs (from my autodesk background) I was just changing the xrefs to libraries because it was probably just better to simply use blenders' naming :) $\endgroup$
    – Benjamin
    Nov 29, 2019 at 7:34
  • $\begingroup$ The solution works as a charm though! Thank you very much!!! $\endgroup$
    – Benjamin
    Nov 29, 2019 at 7:35

1 Answer 1

2
$\begingroup$

I'm presuming that reload_xref is meant to be reload_lib.

In your Reloadlib class you can create a StringProperty to pass the name of the library into:

class Reloadlib(bpy.types.Operator):
    """Reloads the current Library"""
    bl_idname = "my.reload_lib"
    bl_label = "Reload"

    #2.8
    library_name : bpy.props.StringProperty()
    #2.79
    #library_name = bpy.props.StringProperty()

Then in execute you can access this local property with self.library_name:

    def execute(self, context):
        libs[self.library_name].reload()
        self.report({'INFO'}, 'Reloading ' + libs[self.library_name].name)
        return {'FINISHED'}

Then, when you display the operator you can pass in the name:

for i in libs:
    layout.label(text= i.name.replace(".blend",""))
    row = layout.row()
    sub = row.row()
    operator = sub.operator("my.reload_xref", icon = "FILE_REFRESH")
    operator.library_name = i.name

(To be clear, I haven't tested the specific functionality of reloading libraries, but this is how you pass data generally.)

Full code:

import bpy

libs = bpy.data.libraries

class Reloadlib(bpy.types.Operator):
    """Reloads the current Library"""
    bl_idname = "my.reload_lib"
    bl_label = "Reload"

    #2.8
    library_name : bpy.props.StringProperty()
    #2.79
    #library_name = bpy.props.StringProperty()

    def execute(self, context):
        libs[self.library_name].reload()
        self.report({'INFO'}, 'Reloading ' + libs[self.library_name].name)
        return {'FINISHED'}

class LibPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Scene Libraries"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        layout.label(text="Libraries:")

        for i in libs:
            layout.label(text= i.name.replace(".blend",""))
            row = layout.row()
            sub = row.row()
            operator = sub.operator("my.reload_lib", icon = "FILE_REFRESH")
            operator.library_name = i.name

def register():
    bpy.utils.register_class(Reloadlib)
    bpy.utils.register_class(LibPanel)

def unregister():
    bpy.utils.unregister_class(Reloadlib)
    bpy.utils.unregister_class(LibPanel)

if __name__ == "__main__":
    register()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.