Is there any way to export a Tree Objects to a file to use it as an template

ZZ.tang
ZZ.tang Member Posts: 23
10 Comments Name Dropper
**

Hello,

I have a question about the script in mechanical.

For example, I have a contact object in model A, which includes many settings. I want to reuse these settings as a contact template in some other models.
Is there any good way to export a tree object as a reusable file? Then I can import the file and modify the geometry scope to use it in a new model.

I tried to export the data like below, but the file not importable. It does not include and information.

import pickle
with open(r"A.pickle", 'wb') as f:
a=Tree.FirstActiveObject
pickle.dump(a, f)

Thanks!

Tagged:

Best Answers

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 357
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    FYI, there is a UI, built-in method to do this for contacts. You can RMB>Export and/or Import from a .xml file.

    If you want to make the logic more robust, I would suggest you use the mechanical recording function in the scripting console. All the UI actions of modifying the details window properties of a contact should be recorded. You can then re-use this python to set properties of other contact objects any way you want.

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    Thanks @Mike.Thompson I was not aware of this enhancement! Using script recording at 24.2:

    contact_region_35 = DataModel.GetObjectById(35)
    contact_region_35.SaveContactRegionSettings(r'D:\Temp\Contact Region.xml')
    
    contact_region_38 = DataModel.GetObjectById(38)
    contact_region_38.LoadContactRegionSettings(r'D:\Temp\Contact Region.xml')
    

Answers

  • Erik Kostson
    Erik Kostson Member, Moderator, Employee Posts: 275
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    Hi

    Another possible way is just to extract all of the contact properties (via mech script) and save them all to a text file.

    All the best

    Erik

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    @Mike.Thompson Do you have a tool for this?

  • ZZ.tang
    ZZ.tang Member Posts: 23
    10 Comments Name Dropper
    **

    Thanks, @Mike.Thompson & @Landon Mitchell Kanner. It works great for contact.

    Beside the contact, is there any good way to export the load and other object?
    Especially for the one has tabular data.

    I try to go through all the items in "Tree.FirstActiveObject.VisibleProperties", but it looks like there is no tabular data included.