Python 101 - delete library

Hi all,

Flame user of 10+ yrs but new to the forum and it’s great what you’re doing here!

Total newbie with python scripts and I’m just starting with some real basics, like creating a bespoke set of libraries and subfolders in a fresh project. I think I’ve nailed it (thanks to @andymilkis and @fredwarren for Logik Live Ep #07)… apart from deleting the automatically-generated Default Library. Any pointers please?

Thanks

3 Likes

Welcome Franz! Glad you enjoyed Ep 7! It seems like forever ago…this Sunday is #65 :slight_smile:

Here’s how I solved it:

At the top of the script I defined the existing default library that is created with a new project:

default_library = ws.libraries[0]

Then at the end of my script I have this to remove the default library:

if “Default Library” in str(default_library.name):
flame.delete(default_library)

HTH!

Andy

1 Like

The default library could probably be at any index. I would either iterate over all libraries to find it or use the build in function from the flame module to find it.

Find by iterating over all libs:

for lib in flame.project.current_project.current_workspace.libraries:
    if lib.name.get_value() == "Default Library":
        flame.delete(lib)

Find by using the flame module’s function:

for lib in flame.find_by_name("Default Library"):
    # make sure we are really dealing with a lib and not any other item that is just called "Default Library"
    if isinstance(lib, flame.PyLibrary):
        flame.delete(lib)

Find by name reference:
https://help.autodesk.com/view/FLAME/2022/ENU/?guid=Flame_API_Python_API_autodesk_flame_python_api_html#-find_by_name

3 Likes

By using default_library.name.get_value() you will directly get a string instead of a flame.PyAttribute and don’t need to do a conversion. :slight_smile:

2 Likes

Thanks both. Out of the 3 options I could only get the iteration over all libs working, but that’s done it!
When I get more time I’ll work out how to make the other options work. It’s pretty heavy going when you’re this new to python.
Thanks again

1 Like

Let us know what errors you get and we can help. :slight_smile:

1 Like

Hi all,
I have a similar request, I’m hoping to get the library name that contains the sequence I’m exporting to use in the file path.
Tried all variations on current library but no joy.
Any ideas?

Hey Jason,

PySequences do have a parent attribute, but I think this will only help you if the selected clip is in the library:
https://help.autodesk.com/view/FLAME/2022/ENU/?guid=Flame_API_Python_API_autodesk_flame_python_api_html#PySequence

If the sequence is on your current desk and you want to find the library where it has also been saved you probably need to crawl all libraries to find the sequence. Have a look at this code snippet which shows you how to collect clips from libraries with an unknown level of subfolders:

Instead of just adding every clip (clips.append(clip)) you would add a check if the clip name equals your clip name.

Thanks for you help Claus,
The clip will always be in a library.
Had a look at the link and can return lots of sequence info but don’t see any way to get which library its saved in.

Did you try to use the parent attribute as mentioned before?

Select your sequence in the media panel, run this and you will see the libraries name printed:

for item in flame.media_panel.selected_entries:
    print item.parent.name.get_value()

Amazing, thanks so much

You’re welcome! :slight_smile:

Thank you!!

Hi! First of all , sorry for bump this.

Coming back to first question, I’m trying to use find_by_name method and I would need return the name of founded items. According with the succint help, it seems that this function returns a list. Taking the second example, I tried to print the list with something like:

for lib in flame.find_by_name("Default Library"):
    if isinstance(lib, flame.PyLibrary):
        print(lib[:])

but it does not work.

Also, I wonder if find_by_name method can use * or so, to perform basic searchs, to find, for example, all libraries started by “tmp”

This should work:

for lib in flame.find_by_name("Default Library"):
    if isinstance(lib, flame.PyLibrary):
        print(lib)

It was my first option. Using

print(lib)

Console returns:

<flame.PyLibrary object at 0x7f4477e66432>

Trying to figure out the kind of object returned by find_by_name, using

print(type(lib))

<class ´flame.PyLibrary´>

What are you trying to do?

As part of a script project, I’m using a temp library. I would like delete it after finishing the script. So I want to check if some temp libraries exist, and delete it, printing it in console with something like ´temp library deleted´ . And beyond, also check if temp 1 , temp 2 exist, and delete all of them.

Ah… you could do something like this:

for lib in flame.project.current_project.current_workspace.libraries:
    if lib.name in ['temp 1', 'temp 2']:
        flame.delete(lib)
        print('Temp library deleted')

Sorry, I did not explain well. I meant that I wanted to print on the screen the actual names of the libraries founded.

But your answer gave me the right and simply answer. The right object is lib.name.

So it would be:

for lib in flame.find_by_name("temp"):
    if isinstance(lib, flame.PyLibrary):
        print(lib.name, ' library deleted')

… and it works.

I know it doesn’t make sense if I am looking for a specific name. But I was thinking if I could find more variations of that name (temp 1 , temp2 , temp whatever … and so on) with some procedural way and display the precise information about what was founded and deleted.

I don’t think find_by_name supports things like ´’temp*’ . I suppose I could try an if statement like you propose but with some kind of regular expression to make it more procedural.

Needless to say, I’m a complete noob with this.

1 Like