Hi Yue,
Using ChimeraX's Python interpreter directly skips over a lot of crucial initialization that the app does, such as what commands are available, what tools are available, what data formats can be opened/saved, etc.  So the bare interpreter is only usable in a fairly narrow set of situations, and running commands is not one of them.
I would instead recommend running your script from the shell prompt with "chimerax --script your-script.py --exit".  The script you supplied could be run as is, except you can just delete the line that creates a Session, since ChimeraX's current session will be supplied to the script in a global variable named "session".  Running your script will bring up the ChimeraX interface as it runs.  If you want to suppress that while still being able to create images you have to use the Linux version of ChimeraX and add "--nogui --offscreen" to the shell command arguments.  I don't know how feasible that will be if you are using Windows, but maybe you also have access to Linux.
Starting ChimeraX from the shell prompt is discussed here: https://www.cgl.ucsf.edu/chimerax/docs/user/startup.html

--Eric

Eric Pettersen
UCSF Computer Graphics Lab


On Jul 16, 2024, at 1:52 PM, Eric Pettersen via ChimeraX-users <chimerax-users@cgl.ucsf.edu> wrote:



Begin forwarded message:

From: chimerax-users-owner@cgl.ucsf.edu
From: Yue Bao <nkby@outlook.com>
Subject: Question about automating with ChimeraX in Visual Studio Code
Date: July 15, 2024 at 11:54:25 PM PDT


Hi, 

I am using ChimeraX to visualize a list of protein structures, including hiding/showing/coloring specific chains (sometimes amino acids specific). Because I have a long list, I wanted to get it done not with the ChimeraX console but with Visual Studio Code, since you provided python APIs. 

N.B. I can do run(session, 'open 6qzp') in the Qt console in ChimeraX, as session has been loaded. It is good, just not as efficient as running in VS code. 

However, in VS code it is not working. Here are my code and error messages. Is it because I didn't setup the standalone environment correctly? If possible, could you help me with a piece of sample code and the documentation?

best,
Yue
---

interpreter I used
/Applications/ChimeraX-1.7.1.app/Contents/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11

I have a script basically to open a PDB structure and get some visualizations done
from chimerax.core.session import Session
from chimerax.core.commands import run

def main():
   session = Session('chimerax')
   run(session, 'open 6qzp')
   run(session, 'hide atom')
   run(session, 'show cartoon')
   run(session, 'select #1') # select all proteins
   run(session, 'color #AAAAAA All')
   run(session, 'select #0')

main()

when I run it in VS code, it  gives me some error
    run(session, 'open 6qzp')  
  File
"/Applications/ChimeraX-1.7.1.app/Contents/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-
packages/chimerax/core/commands/run.py", line 49, in run  
    results = command.run(text, log=log, return_json=return_json)  
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
  File
"/Applications/ChimeraX-1.7.1.app/Contents/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-
packages/chimerax/core/commands/cli.py", line 2849, in run  
    _compute_available_commands(session)  
  File
"/Applications/ChimeraX-1.7.1.app/Contents/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-
packages/chimerax/core/commands/cli.py", line 2431, in
_compute_available_commands  
    ts.register_available_commands(session.logger)  
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
AttributeError: 'NoneType' object has no attribute
'register_available_commands'  
  

BUG: AttributeError: 'NoneType' object has no attribute
'register_available_commands'  
  
File
"/Applications/ChimeraX-1.7.1.app/Contents/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-
packages/chimerax/core/commands/cli.py", line 2431, in
_compute_available_commands  
ts.register_available_commands(session.logger)  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
  
 _See log for complete Python traceback._ 


_______________________________________________
ChimeraX-users mailing list -- chimerax-users@cgl.ucsf.edu
To unsubscribe send an email to chimerax-users-leave@cgl.ucsf.edu
Archives: https://mail.cgl.ucsf.edu/mailman/archives/list/chimerax-users@cgl.ucsf.edu/