Maya python open another instance of the attribute editor - python

In Maya 2018, using Python, how can you check if the attribute editor is open, and if it is not, open it. Also, can you open multiple instances of the attribute editor, preferably showing the attributes of different nodes?

I advise you to turn on "echo all command" if your looking to some code.
Opening the Attribute Editor will give you in echo :
attributeEditorVisibilityStateChange(`workspaceControl -q -visible AttributeEditor`, "");
In this command you can already guess that :
`workspaceControl -q -visible AttributeEditor`
it is the part to query the visibility of the attribute editor, in python a simple translation :
cmds.workspaceControl('AttributeEditor', q=1, visible=1)
Then you have this mel function :
attributeEditorVisibilityStateChange
In Mel you can use this command to find where the procedure belong :
whatIs attributeEditorVisibilityStateChange;
// Result: Mel procedure found in: D:\maya_path\scripts\startup\initAttributeEditor.mel //
Opening the file and reading the first proc, you find already : showAttributeEditor
This function is commented as obsolet and advise to use : ToggleAttributeEditor
Making a quick whatIs, i find out it was a runtime command (so it should be use straight away):
cmds.ToggleAttributeEditor()
You should have your answer for opening and check if the atrribute editor still exists, if the command is not the one you want because you want maybe some docking ability, there is lots more MEL to read using whatIs; and the second proc in the file.
And now that I've explained you the method to find python command, I think you can use the same technique to create a function for the "copy tab" of the attribute editor !
If you find it is to annoying (maya has sometimes lots of nested code, and it can be tidious), you can use :
import maya.mel
mel.eval('attributeEditorVisibilityStateChange(`workspaceControl -q -visible AttributeEditor`, "");')
it will execute mel code inside python. you can use python format to insert arguments...etc as it must be evaluated as a string.

Related

How do I call a function in vs code using python?

I'll want to know how to call a function in vs code. I read the answer to similar questions, but they don't work:
def userInput(n):
return n*n
userInput(5)
And appends nothing
def Input(n):
return n*n
And in the terminal:
from file import *
from: can't read /var/mail/file
Can somebody help me?
You are doing everything correctly in the first picture. In order to call a function in python on vs code you first have to define the function, which you did by typing def userInput(n):. If you want to see the result of your function, you should not use return, you should use print instead. Return is a keyword- so when your computer reaches the return keyword it attempts to send that value from one point in your code to another. If you want to see the result of your code, typing print (n) would work better.
Your code should look like this:
def userInput(n):
print (n * n)
userInput(5)
The code would print the result 25
Your terminal is your general way to access your operating system, so you have to tell it that you want it to interpret your Python code first.
If you want to run the file you're typing in, you have to first know the location of that file. When you type ls in your terminal, does the name of your Python file show up? If not, hover over the tab in VSCode (it's close to the top of the editor) and see what path appears. Then in your terminal type cd (short for "change directory") and then the path that you saw, minus the <your filename here>.py bit. Type ls again, and you should see your Python file. Now you can type python <your filename here>.py to run it (provided you have Python installed).
You could also run the IDLE by just typing python in your terminal. This will allow you to write your code line-by-line and immediately evaluate it, but it's easier to write in VSCode and then run it with the method I described before.

Using subprocess in Python, I get different results when in Python cmd and Python IDLE

I hope the title makes sense. To give specifics:
I am using csvtotable (https://github.com/vividvilla/csvtotable) to generate HTML tables from CSVs. I have installed via pip and am able to run a command line command:
csvtotable test1743.csv test1743.html
to generate a HTML page. All good so far.
I wanted to do this from within a Python script I had already written so I heard that subprocess was the way to do this. I looked up how to do it and understood that it can be done using the following:
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)
So I tested this via the command line first by doing
python
from the command line and then running
import subprocess
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)
Success! It worked. Fantastic.
However, when I try to do this from IDLE, it just returns a 1. I have checked the directory thinking that maybe the csv was missing from there, but it still doesn't work.
Am I misunderstanding how subprocess works?
Solved by finding a way to call the function without subprocess. I think the issue may have related to default arguments not being set when it is executed through python and hence why below I have had to specify so many arguments.
Code:
from csvtotable import convert
content = convert.convert("C:\\Users\\admin\\Google Drive\\test1743.csv",delimiter=",",quotechar='"',display_length=-1,overwrite=False,serve=False,pagination=True,virtual_scroll=1000, no_header=False, export=True, export_options=["copy","csv","json","print"])
convert.save("C:\\Users\\admin\\Google Drive\\test1743.html",content)
Note that the argument names had to be changed where they had a - in the name. I just changed any instance e.g. display-length to display_length in convert.py

Verifying the existance of a file or folder using subprocess library in python

I can check the presence of a file or folder using OS library very easily.
The following two links have described that
directoryExistance fileExistance
I am attempting to use the subprocess library to do the same
and, I tried a couple of approaches already
1- status = subprocess.call(['test','-e',<path>]), which is always returning 1, no matter what I pass in path.
2- Using getstatusoutput,
/bin/sh: 1: : Permission denied
status, result = subprocess.getstatusoutput([<path>])
print(status)
print(result)
which is working fine because status variable returns 126 if the file/folder exist and 127 when the file/folder doesn't exist. Also the result variable contains message but the "result" variable contains the message : Permission denied
But the second solution looks like a hack to me. Is their a better way, of doing this ?
The test command is a shell builtin, and on many platforms doesn't exist as an independent command you can run.
If you use shell=True to use the shell to run this command, you should pass in a single string, not a list of tokens.
status = subprocess.call("test -e '{}'".format(path), shell=True)
This will produce a malformed command if path contains any single quotes; try path.replace("'", r"\'") if you want to be completely correct and robust, or use one of the existing quoting functions to properly escape any shell metacharacters in the command you pass in.
The subprocess library now offers a function run() which is slightly less unwieldy than the old legacy call() function; if backwards compatibility is not important, you should probably switch to that... or, as several commenters have already implored you, not use subprocess for this task when portable, lightweight native Python solutions are available.
As pointed in the comments section
status = subprocess.call(['test','-e',<path>])
can be made to work with a shell expansion if we use "shell=True"
Although using os.path might be much more efficient anyways.

vscode extension: how to create a language specific command

I wrote a simple extension for vscode, which registers a command, namely comandName.
In my package.json file I have:
"activationEvents": [
"onLanguage:python",
"onCommand:commandName"
]
I meant to execute this command only for Python. To my knowledge, however, the activationEvents in my package.json means that the command would be activated when a Python file is opened or the command is executed. But the command can be executed within any language. I searched the documentation but found no way to execute the command for certain languages.
Is there any way to achieve this target?
I'm afraid this is impossible for now. However you can work around this to avoid any side effects on other files.
If you bind this command to some key combination or to context menu you can use when clause to limit the command to specific types of files.
It will still allow to execute the command from command palette though. To work around this you can just ignore it when being in file other than Python:
vsc.commands.registerTextEditorCommand('testCommand', editor => {
if (editor.document.languageId !== 'python') {
return
}
// command logic
}))
Notice I used registerTextEditorCommand instead of regular command. The difference is that this one requires user to be in text editor context to work which is what you probabl

Parsing python's default error syntax to Visual Studio's error window syntax

Ive been using MSVC2010 for some time now combined with Python Tools 1.0
I'm currently working on a python project and my solution looks like this:
Solution
- Pyproj
- EmptyTestRunner
The pyproj contains a MainTest.py file that runs all my tests,
the EmptyTestRunner is just an empty project with a "Post-Build" action that runs the MainTest.py (thus running all my tests written in PyUnit on my python code each time i "re-build")
My problem is that running the python file, it produces normal python output - which because i'm running it as a post-build event, refers it to the MSVC output window.
MSVC2010 doesn't recognize this output as a "compile / linker" error (for example when an exception is raised in my python code which isn't caught) so it doesn't add it to the error window.
Is there a tool that already does this?
Is there a flag passed to the python.exe that can do it?
I've googled the subject throghouly but found nothing resembling it.
I'm about to just simply write a python script that will re-format the output of my pyproj to the msvc format.
thx in advance for any help!
I've done this with Visual Studio 9 (2008), so I don't know if the trick works for 2010, but anyway: the linking between the output window and the error manager in VS is automagical. All it expects is a line of the form:
path\to\file\name(lineno) : error : text of the error
e.g.
c:\dev\myfile.ext(17) : error : 'foo' is not a valid configuration directive
if your script somehow reads a configuration file.
Then when you double click this line, it opens the file at the specified line. The error message ('text of the error' here) also appears in the Error list window.
All other lines of your script's output, if they do not have this format, are not affected, so you may very well throw a lengthy explanation after the magical line - just as the C++ compiler does for template errors.
Whether you choose to modify your script to translate Python errors to a VS-compatible format or use a wrapper script is your decision - I'd favor the first one since you can catch all errors in the __main__ function, and VS-print them (given that you can still print the full Python trace just afterwards).

Categories