I am new to Maya scripting and I am asked to convert the below MEL to Python.
I've been searching for all the commands in Mel and everything seemed to be fine, except the two terms ## followObject_setup and followObjectExpr ## in the below script.
Can someone explain me, what is this.
Thanks in Advance
global proc newPointBlast_Follow()
{
/*
Function to get the point from selection and camera from view.
Attach the camera to point.
*/
global string $followingCamera[];
global string $object_to_Follow[];
undoInfo -swf off;
string $currentPanel = `getPanel -wf`;
string $type = `getPanel -typeOf $currentPanel`;
if ( $type == "modelPanel" )
{
$followingCamera[0] = `modelPanel -q -camera $currentPanel`;
}
$object_to_Follow = `ls -sl`;
if ( `size( $object_to_Follow )` == 0 )
{
confirmDialog -title "No Object selected" -message "Please select an Object or Vertex to Follow" -button "OK"; }
else
{
newPointBlast_unlockImagePlanes();
if ( `objExists followObject_setup` )
{
delete followObject_setup;
}
if ( `objExists followObjectExpr` )
{
delete followObjectExpr;
}
What is the meaning of the last if statements?
It is some variables that has been defined somewhere else.
In maya, if you type in MEL :
whatIs followObjectExpr
whatIs followObject_setup
if it return None, it is not a proc or something belonging to maya
otherwise if you cant to convert your commands roughly in python, you can use the command below to convert a mel string :
import pymel.tools.mel2py as mel2py
text = raw_input()
print(mel2py.mel2pyStr(text, pymelNamespace='pm'))
or you can use this to convert a mel file :
import pymel.tools.mel2py as mel2py
mel2py.mel2py('/path/maya20XX/scripts/others/doWrapArgList.mel',
'/path/myfolder/doWrapArgList/')
Related
How to pass a string as argument from Python to a Go Dll using ctypes:
Go-code:
package main
import "C"
import "fmt"
//export GetInt
func GetInt() int32 {
return 42
}
//export GetString
func GetString() {
fmt.Println("Foo")
}
//export PrintHello
func PrintHello(name string) {
// if name == "hello" { ... }
fmt.Printf("From DLL: Hello, %s!", name)
}
func main() {
// Need a main function to make CGO compile package as C shared library
}
Compiled on MacOs using: GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -buildmode=c-shared -o ./dist/perf_nlp.dll
Python code:
import ctypes
def getString():
nlp = ctypes.windll.LoadLibrary("H:/perf_nlp.dll")
dllFunc = nlp.GetString
dllFunc.restype = ctypes.c_char_p
return dllFunc()
def getInt():
nlp = ctypes.windll.LoadLibrary("H:/perf_nlp.dll")
dllFunc = nlp.GetInt
dllFunc.restype = int
return dllFunc()
def readString():
nlp = ctypes.windll.LoadLibrary("H:/perf_nlp.dll")
dllFunc = nlp.ReadString
dllFunc.argtypes = [ctypes.c_char_p]
dllFunc.restype = ctypes.c_char_p
return dllFunc(b'Foo')
print(getInt())
print(getString())
print(readString()). # Fails
Out:
42
Foo
None
unexpected fault address 0x871000
fatal error: fault
[signal 0xc0000005 code=0x0 addr=0x871000 pc=0x623e501f]
goroutine 17 [running, locked to thread]:
runtime.throw(0x6245b592, 0x5)
/Users/foobar/.goenv/versions/1.14.15/src/runtime/panic.go:1116 +0x64 fp=0x1242fda4 sp=0x1242fd90 pc=0x623be404
runtime.sigpanic()
/Users/foobar/.goenv/versions/1.14.15/src/runtime/signal_windows.go:249 +0x1ed fp=0x1242fdb8 sp=0x1242fda4 pc=0x623ceb8d
runtime.memmove(0x12500011, 0x800588, 0x32efe4)
/Users/foobar/.goenv/versions/1.14.15/src/runtime/memmove_386.s:89 +0x7f fp=0x1242fdbc sp=0x1242fdb8 pc=0x623e501f
fmt.(*buffer).writeString(...)
/Users/foobar/.goenv/versions/1.14.15/src/fmt/print.go:82
fmt.(*fmt).padString(0x124a60b0, 0x800588, 0x32efe4)
/Users/foobar/.goenv/versions/1.14.15/src/fmt/format.go:110 +0x6c fp=0x1242fdfc sp=0x1242fdbc pc=0x6241576c
fmt.(*fmt).fmtS(0x124a60b0, 0x800588, 0x32efe4)
/Users/foobar/.goenv/versions/1.14.15/src/fmt/format.go:359 +0x4d fp=0x1242fe14 sp=0x1242fdfc pc=0x6241664d
fmt.(*pp).fmtString(0x124a6090, 0x800588, 0x32efe4, 0x73)
/Users/foobar/.goenv/versions/1.14.15/src/fmt/print.go:450 +0x188 fp=0x1242fe38 sp=0x1242fe14 pc=0x62418f58
fmt.(*pp).printArg(0x124a6090, 0x62447c80, 0x1248c110, 0x73)
/Users/foobar/.goenv/versions/1.14.15/src/fmt/print.go:698 +0x776 fp=0x1242fe80 sp=0x1242fe38 pc=0x6241ad56
fmt.(*pp).doPrintf(0x124a6090, 0x6245e0a5, 0x14, 0x1242ff48, 0x1, 0x1)
/Users/foobar/.goenv/versions/1.14.15/src/fmt/print.go:1030 +0x12b fp=0x1242fef0 sp=0x1242fe80 pc=0x6241d81b
fmt.Fprintf(0x62476550, 0x1248c0d8, 0x6245e0a5, 0x14, 0x1242ff48, 0x1, 0x1, 0x623e2fe7, 0x0, 0x12488030)
/Users/foobar/.goenv/versions/1.14.15/src/fmt/print.go:204 +0x52 fp=0x1242ff20 sp=0x1242fef0 pc=0x62417bd2
fmt.Printf(...)
/Users/foobar/.goenv/versions/1.14.15/src/fmt/print.go:213
main.ReadString(...)
/Users/foobar/__projects__/heine/db_dll/perf_nlp.go:19
main._cgoexpwrap_c3579cea1e16_ReadString(0x800588, 0x32efe4)
_cgo_gotypes.go:71 +0x8d fp=0x1242ff54 sp=0x1242ff20 pc=0x6241e9bd
runtime.call16(0x0, 0x32ef1c, 0x32ef68, 0x8, 0x0)
/Users/foobar/.goenv/versions/1.14.15/src/runtime/asm_386.s:565 +0x30 fp=0x1242ff68 sp=0x1242ff54 pc=0x623e3020
runtime.cgocallbackg1(0x0)
/Users/foobar/.goenv/versions/1.14.15/src/runtime/cgocall.go:332 +0x149 fp=0x1242ffb0 sp=0x1242ff68 pc=0x62393f59
runtime.cgocallbackg(0x0)
/Users/foobar/.goenv/versions/1.14.15/src/runtime/cgocall.go:207 +0xb5 fp=0x1242ffe0 sp=0x1242ffb0 pc=0x62393d85
runtime.cgocallback_gofunc(0x0, 0x0, 0x0, 0x0)
/Users/foobar/.goenv/versions/1.14.15/src/runtime/asm_386.s:806 +0x7e fp=0x1242fff0 sp=0x1242ffe0 pc=0x623e419e
runtime.goexit()
/Users/foobar/.goenv/versions/1.14.15/src/runtime/asm_386.s:1337 +0x1 fp=0x1242fff4 sp=0x1242fff0 pc=0x623e4651
Working solution:
//export ReadString
func ReadString(name *C.char) *C.char {
res := "";
goName := C.GoString(name);
if goName == "Foo" {
res = "From DLL: Hello, Foo"
}else{
res = "From DLL: Hello!"
}
return C.CString(res)
}
Python:
def readString():
nlp = ctypes.windll.LoadLibrary("H:/perf_nlp.dll")
dllFunc = nlp.ReadString
dllFunc.argtypes = [ctypes.c_char_p]
dllFunc.restype = ctypes.c_char_p
return dllFunc(b'cFoo')
A Go string and a C string are entirely unrelated (except in that both are called strings, which is a lie for at least one of them).
Here Python is sending a C string because you've told it to, but Go expects a Go string, which has a completely diffrent layout so it blows up. And if it didn't blow up at the callsite it'd probably blow up when the GC tries to handle the string, which it can't, because it's not a Go string.
You want to look at the magical "C" pseudo-package: you need to take in a *C.char and copy that to a Go string using C.GoString before you can pass it to anything expecting a go String. Or something along those lines, my experience with cgo (especially calling into it) is limited to avoiding this as a bad idea.
Regardless you probably want to at the very least read the cgo documentation in full, FFI is tricky at the best of time, and FFI between two managed languages much more so.
I have classes defined in C++ code, and added to the python dictionary like this:
try
{
Py_Initialize();
}
catch (bp::error_already_set &)
{
parse_python_exception();
}
main_module = bp::import("__main__");
main_namespace = main_module.attr("__dict__");
bp::class_<Gamefunctions> gamefunctions_binding = bp::class_<Gamefunctions>("Gamefunctions");
m_localDictionary["Gamefunctions"] = gamefunctions_binding;
m_localDictionary["gamefunctions"] = bp::ptr(&gamefunctions);
void (Gamefunctions::*addlog_comp)(string, string, string, bool) = &Gamefunctions::addlog;
void (Gamefunctions::*addlog_simp)(string, bool) = &Gamefunctions::addlog;
gamefunctions_binding.def("addlog", addlog_comp);
gamefunctions_binding.def("addlog", addlog_simp);
Py_Initialize();
bp::object execute;
string testcode = "def calllog():\n";
testcode += " gamefunctions.addlog(\"logmessage\", True)\n";
testcode += "gamefunctions.addlog(\"logmessage\", True)\n";
testcode += "calllog()";
try
{
execute = exec((testcode).c_str(), main_namespace, m_localDictionary);
}
catch (bp::error_already_set &)
{
parse_python_exception();
}
And I tried to call it from a function in a python file like this:
def calllog():
gamefunctions.addlog("logmessage",True)//not found
gamefunctions.addlog("logmessage",True)//works fine
calllog()
The call from outside the "def" works perfectly, but when I call it from the "def" It gives me this error message:
<type 'exceptions.NameError'> global name 'gamefunctions' is not
defined
How can I "define" the 'gamefunction' class in the 'calllog()' function?
Sorry for my bad english and thank you for any advice you give.
After a long searching, I found this page here:
Boost Python - Global and Local dictionary
I already knew, there is something with the local and global scopes, but I approached the probmem from the wrong direction.
I tought, I need to link the inside classes somehow in the python function/class itself, but actually I need to add the C++ classes to the global dictionary too like this:
bp::class_<Gamefunctions> gamefunctions_binding = bp::class_<Gamefunctions>("Gamefunctions");
m_localDictionary["Gamefunctions"] = gamefunctions_binding;
m_localDictionary["gamefunctions"] = bp::ptr(&gamefunctions);
main_namespace["Gamefunctions"] = gamefunctions_binding;
main_namespace["gamefunctions"] = bp::ptr(&gamefunctions);
void (Gamefunctions::*addlog_comp)(string, string, string, bool) = &Gamefunctions::addlog;
void (Gamefunctions::*addlog_simp)(string, bool) = &Gamefunctions::addlog;
gamefunctions_binding.def("addlog", addlog_comp);
gamefunctions_binding.def("addlog", addlog_simp);
I am new to child_process and I want to experiment with its capabilities.
Is there any way to pass a function and arguments to a python file, using spawn (or exec or execFile is spawn cannot do it)?
I want to do something like (pseudocode)
spawnORexecORexefile (python path/to/python/file function [argument1, argument2] )
or maybe
spawnORexecORexefile (python path/to/python/file function(argument1, argument2) )
Please explain and maybe give an example, because I am a newbie
How I am doing it now?
in node
var process = spawn('python', [path/to/pythonFile.py, 50, 60] );
in pythonFile.py
import sys
import awesomeFile
hoodie = int(sys.argv[1])
shoe = int(sys.argv[2])
awesomeFile.doYourMagic().anotherThing(hoodie, shoe)
as you can see, from node, I send data to pythonFile and then to awesomeFile, to a specific function
I would like to "cut the middle man" and remove the pythonFile and send data from node directly to the awesomeFile file, to a specific function. This is why I am asking
Thanks
You can run a specific Python function from the command-line using the form python -c 'import foo; print foo.hello()' (from https://stackoverflow.com/a/3987113/5666087).
Place both the files below in the same directory, and run with node index.js. You should see the following output:
Hoodie: 10
Shoe: 15
awesomeFile.py
def myfunc(hoodie, shoe):
hoodie = int(hoodie)
shoe = int(shoe)
print("Hoodie:", hoodie)
print("Shoe:", shoe)
index.js
const { spawn } = require('child_process');
let hoodie = 10,
shoe = 15;
const result = spawn("python",
["-c", `import awesomeFile; awesomeFile.myfunc(${hoodie}, ${shoe})`])
result.stdout.pipe(process.stdout)
You can group all the relevant functions you want to call to class and make a dictionary out of a class that maps to the class function. You can directly call awesome.py without an intermediate index.py. You can extend the class AwesomeFile with your methods
The follow program will take user input -
which python file to run
which method to run
method arguments
Number of arguments mismatch
What if unknown methods are given
awesomeFile.py
import sys
class AwesomeFile:
def __init__(self):
pass
def doYourMagic(self):
return self
def anotherThing(self, hoodie, shoe):
print(int(hoodie))
print(int(shoe))
awesomeFile = AwesomeFile()
methods = {m:getattr(awesomeFile, m) for m in dir(AwesomeFile) if not m.startswith('__')}
def run():
method_name = sys.argv[1]
if method_name not in methods:
print(f"Unknown Method {method_name}")
return
methodArgCount = methods[method_name].__code__.co_argcount
if methodArgCount - 1 != len(sys.argv[2:]):
print(f"Method {method_name} takes {methodArgCount - 1} arguments but you have given {len(sys.argv[2:])}")
return
methods[method_name](*sys.argv[2:])
if __name__ == "__main__":
run()
index.js
Note** - You would to install prompt - npm i prompt
'use strict';
var prompt = require('prompt');
const { spawn } = require( 'child_process' );
var prompt_attributes = [
{
name: 'pythonFilePath'
},
{
name: 'cmdLineArgs'
}
];
prompt.start();
prompt.get(prompt_attributes, function (err, result) {
if (err) {
console.log(err);
return 1;
}else {
console.log('Command-line received data:');
var filePath = result.pythonFilePath;
var cmdLineArgs = result.cmdLineArgs;
var args = cmdLineArgs.split(" ");
args.unshift(filePath);
const pythonProgram = spawn( 'python' , args);
pythonProgram.stdout.on( 'data', data => {
console.log( `stdout:\n\n ${data}` );
} );
pythonProgram.stderr.on( 'data', data => {
console.log( `stderr: ${data.data}` );
} );
pythonProgram.on( 'close', code => {
console.log( `child process exited with code ${code}` );
} );
}
});
To run the program -
I/O:
Argument Mistmatch -
prompt: Python File Path. Give absolute or relative path: ../python_files/awesomeFile.py # python file can be any location in the system
prompt: Command Line Arguments. Format = func_name arguments_list (Ex addFunc 1 2): anotherThing 1
Command-line received data:
stdout:
Method anotherThing takes 2 arguments but you have given 1
child process exited with code 0
Function Not found
prompt: Python File Path. Give absolute or relative path: ../python_files/awesomeFile.py
prompt: Command Line Arguments. Format = func_name arguments_list (Ex addFunc 1 2): helloworld 1 2
Command-line received data:
stdout:
Unknown Method helloworld
child process exited with code 0
Success Case:
prompt: Python File Path. Give absolute or relative path: ../python_files/awesomeFile.py
prompt: Command Line Arguments. Format = func_name arguments_list (Ex addFunc 1 2): anotherThing 50 60
Command-line received data:
stdout:
50
60
child process exited with code 0
I have the following named pipe created in Windows Powershell.
# .NET 3.5 is required to use the System.IO.Pipes namespace
[reflection.Assembly]::LoadWithPartialName("system.core") | Out-Null
$pipeName = "pipename"
$pipeDir = [System.IO.Pipes.PipeDirection]::InOut
$pipe = New-Object system.IO.Pipes.NamedPipeServerStream( $pipeName, $pipeDir )
Now, what i need is some Python code snippet to read from the above named pipe created. Can Python do that ?
Thanks in advance !
Courtesy :http://jonathonreinhart.blogspot.com/2012/12/named-pipes-between-c-and-python.html
Here's the C# Code
using System;
using System.IO;
using System.IO.Pipes;
using System.Text;
class PipeServer
{
static void Main()
{
var server = new NamedPipeServerStream("NPtest");
Console.WriteLine("Waiting for connection...");
server.WaitForConnection();
Console.WriteLine("Connected.");
var br = new BinaryReader(server);
var bw = new BinaryWriter(server);
while (true)
{
try
{
var len = (int)br.ReadUInt32(); // Read string length
var str = new string(br.ReadChars(len)); // Read string
Console.WriteLine("Read: \"{0}\"", str);
//str = new string(str.Reverse().ToArray()); // Aravind's edit: since Reverse() is not working, might require some import. Felt it as irrelevant
var buf = Encoding.ASCII.GetBytes(str); // Get ASCII byte array
bw.Write((uint)buf.Length); // Write string length
bw.Write(buf); // Write string
Console.WriteLine("Wrote: \"{0}\"", str);
}
catch (EndOfStreamException)
{
break; // When client disconnects
}
}
}
}
And here's the Python code:
import time
import struct
f = open(r'\\.\pipe\NPtest', 'r+b', 0)
i = 1
while True:
s = 'Message[{0}]'.format(i)
i += 1
f.write(struct.pack('I', len(s)) + s) # Write str length and str
f.seek(0) # EDIT: This is also necessary
print 'Wrote:', s
n = struct.unpack('I', f.read(4))[0] # Read str length
s = f.read(n) # Read str
f.seek(0) # Important!!!
print 'Read:', s
time.sleep(2)
Convert the C# code into a .ps1 file.
The objective is to have two simple ways to source some code, say func.R, containing a function. Calling R CMD BATCH func.R initializes the function and evaluates is. Within a session, issuing source("func.R") simply initializes the function.
Any idea?
I think that the interactive() function might work.
This function returns TRUE when R is being used interactively and FALSE otherwise. So just use if (interactive())
i.e. the equivalent is
if (!interactive()) {
main()
}
Another option is:
#!/usr/bin/Rscript
# runs only when script is run by itself
if (sys.nframe() == 0){
# ... do main stuff
}
You could pass arguments into R, and if an argument is present run main(). More on arguments here: http://yangfeng.wordpress.com/2009/09/03/including-arguments-in-r-cmd-batch-mode/
It's a lot of work, but I finally got it (and posted at Rosetta Code).
This example exports a function called meaningOfLife. When the script is run by itself, it runs main. When imported by another R file, it does not run main.
#!/usr/bin/Rscript
meaningOfLife <- function() {
42
}
main <- function(program, args) {
cat("Main: The meaning of life is", meaningOfLife(), "\n")
}
getProgram <- function(args) {
sub("--file=", "", args[grep("--file=", args)])
}
args <- commandArgs(trailingOnly = FALSE)
program <- getProgram(args)
if (length(program) > 0 && length(grep("scriptedmain", program)) > 0) {
main(program, args)
q("no")
}
I asked a similar question, in an answer, Matthew Plourde suggested using getOption('run.main', default=TRUE) in the main script and then setting options(run.main=FALSE) before calling source(). This worked in my case.
Otherwise a simpler pattern when you have an R script creating a bunch of functions and you want to write a few lines at the end of the script to experiment with the use of a function: place these extra lines in an if(FALSE){} block.
This works fairly well for my use. If you have two files and want to source one with the other while only running a certain part of the file.
parent file: parent.R
print("Running Parent File")
`__name__` <- "__main__"
print(paste0("This file is : ", `__name__`))
`__src__` <- "__not.main__"
source("child.R")
rm(`__src__`)
child file: child.R
print("Running Child File")
`__name__` <- "__main__"
if (exists("__src__")){`__name__` <- `__src__`}
if (`__name__` == "__main__"){
print(paste0("This file is : ", `__name__`))
} else {
print(paste0("This file is : ", `__name__`))
}
Output when running Rscript parent.R
[1] "Running Parent File"
[1] "This file is : __main__"
[1] "Running Child File"
[1] "This file is : __not.main__"
Output when running Rscript child.R
[1] "Running Child File"
[1] "This file is : __main__"
A more robust method would be to write a custom source function where a list of arguments can be included.
source2 <- function(file, args = list()){
tryCatch(
expr = {
assign("__src__", "__not.main__", envir = globalenv())
assign("__src.args__", args, envir = globalenv())
source(file)
},
error = function(e){
message("File could not be sourced")
},
finally = {
rm(list = c("__src__", "__src.args__"), envir = globalenv());
assign("__name__", "__main__", envir = globalenv())
})
}
source2("child.R", args = list("list", "of", "arguments"))