" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.
UseVimball
finish
plugin/vimya.vim [[[1
243
"""
"
" Coypright 2009, 2013-2014 Stefan Goebel.
"
" This program is free software: you can redistribute it and/or modify it under
" the terms of the GNU General Public License as published by the Free Software
" Foundation, either version 3 of the License, or (at your option) any later
" version.
"
" This program is distributed in the hope that it will be useful, but WITHOUT
" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
" FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
" details.
"
" You should have received a copy of the GNU General Public License along with
" this program. If not, see .
"
"""
"""
"
" Vimya 0.4 - Execute buffer contents as MEL or Python scripts in Autodesk Maya
"
" Help is available in doc/vimya.txt or from within Vim with :help vimya.
"
"""
if exists ('g:loadedVimya') || &cp || ! has ('python')
finish
endif
let g:loadedVimya = '0.4'
"""
" Configuration variables:
"""
if ! exists ('g:vimyaPort')
let g:vimyaPort = 12345
endif
if ! exists ('g:vimyaHost')
let g:vimyaHost = '127.0.0.1'
endif
if ! exists ('g:vimyaDefaultFiletype')
let g:vimyaDefaultFiletype = 'python'
endif
if ! exists ('g:vimyaShowLog')
let g:vimyaShowLog = 1
endif
if ! exists ('g:vimyaTempDir')
let g:vimyaTempDir = ''
endif
if ! exists ('g:vimyaTailCommand')
let g:vimyaTailCommand = 'TabTail'
endif
"""
" Mappings:
"""
if ! hasmapto ('sendBufferToMaya')
nnoremap sm :py sendBufferToMaya ()
vnoremap sm :py sendBufferToMaya ()
nnoremap sb :py sendBufferToMaya (True)
vnoremap sb :py sendBufferToMaya (True)
endif
"""
" Main stuff (most of it is Python):
"""
let g:vimyaUseTail = 0
if exists ('g:Tail_Loaded')
let vimyaUseTail = 1
endif
autocmd VimLeavePre * py __vimyaRemoveLog ()
python << EOP
import os
import socket
import tempfile
import vim
logPath = ''
setLog = 0
tempFiles = []
# __vimyaRemoveLog ():
#
# If a logfile was written, delete it. Automatically executed when leaving Vim.
# Also, if some of the temporary file are left on disk, we delete them.
def __vimyaRemoveLog ():
global logPath
if logPath != '':
__vimyaCloseLog ()
try:
os.unlink (logPath)
except:
pass
for file in tempFiles:
try:
os.unlink (file)
except:
pass
# __vimyaCloseLog ():
#
# Issues a `cmdFileOutput -closeAll` command to close the log file.
def __vimyaCloseLog ():
host = vim.eval ('g:vimyaHost')
port = int (vim.eval ('g:vimyaPort'))
try:
connection = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
connection.settimeout (5)
connection.connect ((host, port))
connection.send ("cmdFileOutput -ca;\n")
connection.close ()
except:
return __vimyaErrorMsg ('Could not close the log file.')
return True
# errorMsg (message = ):
#
# Print the error message given by with the appropriate highlighting.
# Returns always False.
def __vimyaErrorMsg (message):
vim.command ('echohl ErrorMsg')
vim.command ("echo \"%s\"" % message )
vim.command ('echohl None')
return False
# sendBufferToMaya (forceBuffer = False, userCmd = None):
#
# If userCmd is not specified, saves the buffer (or a part of it) to a temp
# file and instructs Maya to source this file. In visual mode only the selected
# lines are used, else the complete buffer. In visual mode, forceBuffer may be
# set to True to force executing the complete buffer. If selection starts (or
# ends) in the middle of a line, the complete line is included! If userCmd is
# specified, this command will be written to the file sourced by Maya, and the
# buffer content will be ignored. Returns False if an error occured, else True.
def sendBufferToMaya (forceBuffer = False, userCmd = None):
global logPath, setLog, tempFiles
type = vim.eval ('&g:ft')
defaultType = vim.eval ('g:vimyaDefaultFiletype')
host = vim.eval ('g:vimyaHost')
tempDir = vim.eval ('g:vimyaTempDir')
tailCommand = vim.eval ('g:vimyaTailCommand')
port = int (vim.eval ('g:vimyaPort'))
tail = int (vim.eval ('g:vimyaUseTail'))
showLog = int (vim.eval ('g:vimyaShowLog'))
if tempDir:
tempfile.tempdir = tempDir
else:
tempfile.tempdir = None
if type != '' and type != 'python' and type != 'mel':
return __vimyaErrorMsg (
"Error: Supported filetypes: 'python', 'mel', None."
)
if logPath == '' and tail == 1 and showLog == 1:
(logHandle, logPath) = tempfile.mkstemp (
suffix = '.log', prefix = 'vimya.', text = 1
)
setLog = 1
(tmpHandle, tmpPath) = tempfile.mkstemp (
suffix = '.py', prefix = 'vimya.', text = 1
)
tempFiles.append (tmpPath)
if userCmd:
os.write (tmpHandle, "%s\n" % userCmd)
else:
vStart = vim.current.buffer.mark ('<')
if (vStart is None) or (forceBuffer):
for line in vim.current.buffer:
os.write (tmpHandle, "%s\n" % line)
else:
vEnd = vim.current.buffer.mark ('>')
for line in vim.current.buffer [vStart [0] - 1 : vEnd [0]]:
os.write (tmpHandle, "%s\n" % line)
os.close (tmpHandle)
try:
connection = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
connection.settimeout (5)
except:
return __vimyaErrorMsg ('Could not create socket.')
try:
connection.connect ((host, port))
except:
return __vimyaErrorMsg ('Could not connect to the command port.')
try:
if setLog == 1:
connection.send (
"cmdFileOutput -o \"%s\";\n" % logPath.replace ('\\', '/')
)
vim.command ("%s %s" % (tailCommand, logPath))
setLog = 0
connection.send ("commandEcho -state on -lineNumbers on;\n")
if type == 'python' or (type == '' and defaultType == 'python'):
connection.send (
"python (\"execfile ('%s')\");\n" % \
tmpPath.replace ('\\', '/')
)
elif (type == 'mel' or (type == '' and defaultType == 'mel')):
connection.send ("source \"%s\";\n" % tmpPath.replace ('\\', '/'))
connection.send ("commandEcho -state off -lineNumbers off;\n")
connection.send (
"sysFile -delete \"%s\";\n" % tmpPath.replace ('\\', '/')
)
except:
return __vimyaErrorMsg ('Could not send the commands to Maya.')
try:
connection.close ()
except:
return __vimyaErrorMsg ('Could not close socket.')
return True
EOP
" vim: set et si nofoldenable ft=python sts=4 sw=4 tw=79 ts=4 fenc=utf8 :
doc/vimya.txt [[[1
257
*vimya.txt* Vimya - Send buffer contents to Autodesk Maya
=======================================================================
Contents *vimya*
1. Overview |vimya-overview|
2. Requirements |vimya-requirements|
3. Installation |vimya-installation|
4. Configuration |vimya-configuration|
5. Usage |vimya-usage|
6. Additional Information |vimya-info|
7. Changelog |vimya-changelog|
8. License |vimya-license|
=======================================================================
1. Overview *vimya-overview*
Vimya can be used to execute the contents of a Vim buffer, the
current visual selection, or a single command in Autodesk Maya. The
plugin will save the command(s) to be executed in a temporary file
and tell Maya via the command port to source this file. Both MEL and
Python scripts are supported, Maya's log can optionally be opened in
a separate preview window or tab in Vim.
=======================================================================
2. Requirements *vimya-requirements*
Python support is required for this plugin to run, check with
>
vim --version
<
Additionally, you may install the 'Tail Bundle' plugin, download it
from
>
http://www.vim.org/scripts/script.php?script_id=1714
<
and install it before using Vimya to view the output of Maya (i.e.
the stuff printed in the script editor) in Vim. You do not need to
install it if you do not need this feature.
=======================================================================
3. Installation *vimya-installation*
When using the vimball (recommended, but requires the 'Vimball'
plugin), all you need to do is open the file and source it:
>
vim vimya-0.4.vba
:so %
<
When using the archive: Extract the contents and copy the files in
the 'doc' and 'plugin' subfolders to the appropriate folders in your
runtime path. Usually this is '~/.vim/' (see |'runtime-path'| and
|add-global-plugin| for details). You may need to create the folders
'doc' and 'plugin' if they do not exist.
When all files are in place, start Vim and run the |:helptags|
command to update the tags index.
See |vimya-configuration| and |vimya-usage| for details on how to
setup and use this plugin.
You also need to open a command port in Maya. To open port 12345
you may copy the following command to your userSetup.mel file:
>
if (! `commandPort -q ":12345"`) commandPort -n ":12345";
<
Refer to the Maya documentation for more details.
Note that only INET sockets are supported at the moment, this may be
A SECURITY RISK!
=======================================================================
4. Configuration *vimya-configuration*
This plugin uses the following configuration variables, they can be
set in your |.vimrc| file:
g:vimyaHost - string (default '127.0.0.1')
Specifies the address of the host to connect to.
g:vimyaPort - number (default 12345)
The port number Maya is listening on for connections. See the
short note above on |vimya-installation| and the Maya manual
for details.
g:vimyaDefaultFiletype - string (default 'python')
Vimya needs to know if the code that should be executed by Maya
is MEL or Python code. Usually this is determined by the
current filetype. If the filetype is not set, e.g. when a new
file is created, this default value is used. It must be set to
either 'mel' or 'python'.
g:vimyaShowLog - number (default 1)
If you have the 'Tail Bundle' plugin installed and you do not
want the Maya script editor log to be opened, set this variable
to 0.
g:vimyaTailCommand - string (default 'TabTail')
The Tail Bundle command used to open the log (if enabled, see
above). The default is 'TabTail' to open the log in a new tab.
Set it to 'Tail' if a non-tabbed preview window should be used.
g:vimyaTempDir - string (default '')
You may set this to a directory name to change the location of
the temporary files. If it is an empty string (the default),
the system's default temporary path will be used, as determined
by Python's 'tempfile' library. See there for details.
Example setting in your |.vimrc| file:
>
let vimyaPort=54321
<
The plugin will create the following mappings unless you configure
your own mappings:
nnoremap sm :py sendBufferToMaya ()
vnoremap sm :py sendBufferToMaya ()
nnoremap sb :py sendBufferToMaya (True)
vnoremap sb :py sendBufferToMaya (True)
See |vimya-usage| below for details on these function.
=======================================================================
5. Usage *vimya-usage*
The Vimya plugin defines the following Python function for public
use:
>
sendBufferToMaya (forceBuffer = False, userCmd = None)
<
If you call this function with
>
:py sendBufferToMaya ()
<
the code to be executed by Maya will be saved to a temporary file,
and Vimya will send the appropriate commands to Maya to source this
file.
The current filetype - |ft| - must be set to either 'mel' or
'python', or not set at all ('none'). If it is not set, the variable
'g:vimyaDefaultFileType' is used to determine the correct commands
to send to Maya. Other filetypes will cause an error message and no
further actions to be performed.
In visual mode, only the selected lines will be used unless you use
the
>
:py sendBufferToMaya (True)
<
command, in that case the complete current buffer will be used. The
complete buffer is also used when not in visual mode. Important: If
a selection starts or ends in the middle of a line, this complete
line will be executed!
If you explicitely specify a command, i.e.
>
:py sendBufferToMaya (userCmd = "")
<
the buffer content (and visual selection) is ignored, and this
command is used instead. Obviously, the forceBuffer parameter is
also ignored in this case. Note that the comamnd will still be
written to the temporary file first, and then this file will be
sourced.
It is suggested to map the commands you need to keyboard shortcuts,
the default mappings are shown above. If you set up your own
mappings, no default mappings will be created. See |key-mapping| for
details. (Note: If you create one mapping for this function, you
need to create the other mappings even if you want them to be the
defaults, else they will not be available!)
If the 'Tail Bundle' plugin is installed, the output of Maya will be
written to a temporary log file and this file will be opened in a
new preview tab (or window, see the 'g:vimyaTailCommand' option) in
Vim, unless this behaviour is disabled by setting the variable
'g:vimyaShowLog' to 0. See |:TabTail| or |:Tail| if installed.
All temporary files are deleted automatically. The files sourced by
Maya will be deleted by Maya itself: after the command to source the
file the
>
sysFile -delete [temporary file]
<
MEL command is sent. The temporary logfile will be deleted when Vim
is closed, do not delete it manually before you close Vim. The
location of the temporary files depends on your system, unless the
option 'g:vimyaTempDir' is set to override the default. See the
Python documentation on 'tempfile.mkstemp' for details.
Note: Since version 0.2 the plugin keeps track of all temporary
files and tries to delete them when leaving Vim, since deleting
with Maya's own commands did not always work. Additionally, when
leaving Vim the command
>
cmdFileOutput -closeAll
<
is sent to Maya, which will close all open log files, before the
plugin tries to delete the log file.
The timeout for the communication with Maya is set to 5 seconds,
which should be enough for connections to the localhost or to
machines in the LAN, and there is currently no option to change
this. If you need a higher value, modify the plugin's source code.
You may file a bug report in this case, maybe I will add an option.
=======================================================================
6. Additional Information *vimya-info*
See Vimya's page in the scripts section of the official Vim homepage
for more information, including links to bug tracker etc.:
>
http://www.vim.org/scripts/script.php?script_id=2626
<
Feel free to contact the author for any questions regarding this
plugin, for bug reports, suggestions etc.
=======================================================================
7. Changelog *vimya-changelog*
2014/02/11 * added the vimyaTempDir and vimyaTailCommand options,
thanks to Claude Ronin
* minor updates to the documentation
* changed version number to 0.4
2013/01/29 * added userCmd parameter to sendBufferToMaya(), thanks
to Gary Fixler for the patch
* changed version number to 0.3
2009/12/27 * bug fixed: backslash characters in paths are now
replaced by slashes, should now work on Windows
* temporary file deletion should be more reliable now
2009/05/06 * first public version
=======================================================================
8. License *vimya-license*
Copyright 2009, 2013-2014 Stefan Goebel.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see .