How can I pass the wb namespace from a main wb journal file to a submodule?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 182
100 Comments 25 Answers 25 Likes First Anniversary
✭✭✭✭

If I create a wbjn file which accesses other python files, I am able to import modules like os, sys etc. but the commands like GetProjectDirectory() are no longer accesible to me, within this module.

How do I get acces to the functions and methods of workbench from sublevel python scripts?

Best Answer

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 182
    100 Comments 25 Answers 25 Likes First Anniversary
    ✭✭✭✭
    Answer ✓

    Here are the steps to achive this.

    1. Create a function called Initialize in the .py file which is being imported as shown below. This takes global name space as an input coming from the main wbjn file.
    def Initialize(gb):
        print(gb["GetProjectDirectory"].__call__()) 
    
    1. Try passing globals() to the module after import as shown below in the main wbjn file as shown below.
    import mypythonmodule as pymod
    pymod.Initialize(globals())
    

    In this way we can get access to functions and methods also in the sublevel python scripts.

    Some more discussion can be found in the below thread.
    https://discuss.ansys.com/discussion/2172/how-to-create-and-use-python-modules-files-in-mechanical