Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    How to obtain the path of a selected Asset in Asset Browser

    Cinema 4D SDK
    python 2023
    2
    3
    635
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E
      Easan
      last edited by

      I'm trying to script in Cinema 4D and I want to access the path of an Asset that's currently selected in the Asset Browser. I'm familiar with retrieving the path of an Asset, but I'm not quite sure how to identify or get the Asset that's actively selected by the user.
      Could anyone guide me on this, or share a code snippet that achieves this?

      Thanks for any help!

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @Easan
        last edited by ferdinand

        Hello @Easan,

        Thank you for reaching out to us. What you want to do is not possible, as there is no way to retrieve the selected asset(s) in "the" Asset Browser. For once because we tend to not expose UIs and their functionalities and because there can be many Asset Browser instances in Cinema 4D. And these instances do not share selection states, so the state is not stored in the asset itself (wich then loops back to the fact that we do not expose UIs).

        What you can do is:

        • Just use drag and drop. This is the preferred method. I just yesterday made a posting on how to manually handle asset drag drop events (at the example of a tree view, would work similarly in other cases where you have access to drag and drop data). But in most cases, you won't have to do all that, because things like text fields and filename fields have already built in asset drag and drop support. So, you can just add them to your UI and start using them.
        • Open your own Asset Browser popup to let the user select an asset, using maxon.AssetManagerInterface.OpenPopup. See the example at the end of this posting for details.

        Cheers,
        Ferdinand

        Code:

        """Demonstrates how to let a user select one or multiple assets with an asset browser popup.
        """
        
        import c4d
        import maxon
        
        doc: c4d.documents.BaseDocument  # The active document
        
        def OnAssetSelect(data: maxon.DragAndDropDataAssetArray) -> None:
            """Called by the maxon.AssetManagerInterface.OpenPopup call below once the user finalizes his
            or her selection.
            """
            asset: maxon.AssetDescriptionInterface
            url: maxon.Url
        
            # Iterate over each selected asset ...
            for asset, url, _ in data.GetAssetDescriptions():
                aid: maxon.Id = asset.GetId()
                repo: maxon.AssetRepositoryRef = asset.GetRepository()
                name: str = asset.GetMetaString(
                    maxon.OBJECT.BASE.NAME, maxon.Resource.GetCurrentLanguage(), "")
                
                # ... and print some metadata for it.
                print (f"{name = }, {aid = }, {url = }, {repo = }")
        
        
        def main() -> None:
            """
            """
            maxon.AssetManagerInterface.OpenPopup(
                flags=maxon.ASSETPOPUPOPTIONS.SHOWCATEGORYLIST,
                masterFilter=maxon.MASTERFILTER.MEDIA,
                filterString="",
                title="Select textures to load",
                inSelection=[],
                outSelection=OnAssetSelect)
        
        if __name__ == '__main__':
            main()
        

        MAXON SDK Specialist
        developers.maxon.net

        E 1 Reply Last reply Reply Quote 0
        • E
          Easan @ferdinand
          last edited by

          @ferdinand
          Thank you very much for your help, your code has been extremely useful to me.
          Cheers!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post