Tuesday, June 30, 2009

Playing .wav files on Windows, Mac, Linux

Playing .wav files in the current directory.

Windows


import glob, winsound, random
wavfiles = glob.glob("*.wav")
wavfile = random.choice(wavfiles)
while(True):
raw_input()
wavfile = random.choice(wavfiles)
winsound.PlaySound(wavfile, winsound.SND_FILENAME)

Mac (got the idea here)

from AppKit import NSSound
from time import sleep
import glob, random
wavfiles = glob.glob("*.wav")
while(True):
raw_input()
wavfile = random.choice(wavfiles)
s = NSSound.alloc()
s.initWithContentsOfFile_byReference_(wavfile, True)
s.play()
while s.isPlaying():
sleep(0.1)
s.stop()

Linux (needs play command)

import os, glob, random
wavfiles = glob.glob("*.wav")
wavfile = random.choice(wavfiles)
while(True):
raw_input()
wavfile = random.choice(wavfiles)
os.system("play -q " + wavfile)

Monday, June 29, 2009

Damien Walters Showreel 2009

Hey look at this!

pydoc look up command (Emacs)

Just add the following stuff in your .emacs.
and type Ctrl-h f at point where you want to look up pydoc.















;;----pydoc lookup----
(defun hohe2-lookup-pydoc ()
(interactive)
(let ((curpoint (point)) (prepoint) (postpoint) (cmd))
(save-excursion
(beginning-of-line)
(setq prepoint (buffer-substring (point) curpoint)))
(save-excursion
(end-of-line)
(setq postpoint (buffer-substring (point) curpoint)))
(if (string-match "[_a-z][_\\.0-9a-z]*$" prepoint)
(setq cmd (substring prepoint (match-beginning 0) (match-end 0))))
(if (string-match "^[_0-9a-z]*" postpoint)
(setq cmd (concat cmd (substring postpoint (match-beginning 0) (match-end 0)))))
(if (string= cmd "") nil
(let ((max-mini-window-height 0))
(shell-command (concat "pydoc " cmd))))))

(add-hook 'python-mode-hook
(lambda ()
(local-set-key (kbd "C-h f") 'hohe2-lookup-pydoc)))

I had been thinking that Python is a toy and the rest of the programming languages are all tools, but I realized Emacs Lisp is a toy too. I'm sure I'm reinventing a wheel. Don't worry!

Saturday, June 27, 2009

Emacs To Maya for Python Scripts

I modified the original Emacs To Maya by slavomir.kaslev.












Feb. 13, 2011.
Sending a command through file is a spec, not cutting corners. Due to Maya command port limitation we need to make a command small enough to have it within one packet (I don't know if the limitation still exisits in Maya 2011, anyone knows it?).

Feb. 6, 2011.
When I posted this entry two years ago, I emailed Slavomir and he replied
I am very happy that you find etom.el usefull. Etom.el is free

software, so you can do whatever you want with it to suit your needs.
so you can feel free to use this patch. Still please keep in mind that this is GPL licenced.

Jan. 23, 2011.
If you get error regarding replace-in-string, it's probably due to missing function on your emacs. you can find a replace-in-string implementation in find-files.el module, made by Robert Fenk.

And on Windows Vista,
commandPort -eo -n ":2222";
doesn't work. Execute
commandPort -eo -n "127.0.0.1:2222";

Tnanks Hajime!


This is not an authorized version by the original author/maintainer, and I only tested on Linux (Fedora 11, Maya 8.5, GNU Emacs 22.3.1)
last modified: Jul. 27, 2012

;;; etom.el --- Emacs to Maya communication

;; Copyright (C) 2007 Slavomir Kaslev

;; Author: Slavomir Kaslev <slavomir.kaslev@gmail.com>
;; Maintainer: Slavomir Kaslev <slavomir.kaslev@gmail.com>
;; Created: 17 Jun 2007
;; Version: etom.el 0.02 dated 07/07/03 at 16:45:51
;; Keywords: emacs, maya, mel

;; Modified the original script by Slavomir Kaslev to send Python scripts
;; Modified By: Koichi Tamura <hohehohe2@gmail.com>
;; Date: 28 Jun 2009

;; This file is NOT part of Emacs.
;;
;; 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 2
;; 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, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301, USA.

;; Commentary:

;; This package is used for communication between emacs and Maya. For
;; example you can use it together with Shuji Narazaki's mel-mode to
;; send pieces of mel code to Maya and get the results back in emacs.

;; To use this, insert in your ~/.emacs file:
;; (add-hook
;;  'mel-mode-hook
;;  (lambda ()
;;    (require 'etom)
;;    (setq etom-default-host "localhost")
;;    (setq etom-default-port 2222)
;;    (local-set-key (kbd "C-c C-r") 'etom-send-region)
;;    (local-set-key (kbd "C-c C-c") 'etom-send-buffer)
;;    (local-set-key (kbd "C-c C-l") 'etom-send-buffer)
;;    (local-set-key (kbd "C-c C-z") 'etom-show-buffer)))
;;
;; For Python
;;
;; Add the following script after the above one.
;; (add-hook
;;  'python-mode-hook
;;  (lambda ()
;;    (require 'etom)
;;    (setq etom-default-host "localhost")
;;    (setq etom-default-port 2222)
;;    (local-set-key (kbd "C-c C-r") 'etom-send-region-py)
;;    (local-set-key (kbd "C-c C-c") 'etom-send-buffer-py)
;;    (local-set-key (kbd "C-c C-l") 'etom-send-buffer-py)
;;    (local-set-key (kbd "C-c C-z") 'etom-show-buffer)))


;;; Code:

(require 'comint)

(defcustom etom-default-host "localhost"
"Default name of the host on which Maya is running."
:type 'string
:group 'etom)

(defcustom etom-default-port 2222
"Default port number to connect to Maya."
:type 'integer
:group 'etom)

(defcustom etom-always-show t
"Non-nil means display etom-buffer after sending a command."
:type 'boolean
:group 'etom)

(defcustom etom-prompt-regexp "^\0$"
"Regexp which matches the Maya's prompt."
:type 'regexp
:group 'etom)

(defvar etom-buffer nil
"Buffer used for communication with Maya.")

(defun etom-show-buffer ()
"Make sure `etom-buffer' is being displayed."
(interactive)
(if (not (etom-connected))
(etom-connect))
(display-buffer etom-buffer))

(defun etom-hide-buffer ()
"Delete all windows that display `etom-buffer'."
(interactive)
(delete-windows-on etom-buffer))

(defun etom-connect ()
"Connect to Maya."
(interactive)
(setq comint-prompt-regexp etom-prompt-regexp)
(setq etom-buffer (make-comint "Maya" (cons etom-default-host etom-default-port)))
(set-process-query-on-exit-flag (get-buffer-process etom-buffer) nil)
(if etom-always-show
(etom-show-buffer))
(comint-simple-send (get-buffer-process etom-buffer)
                (concat
                 "python(\""
                 "def etom_pyexec(fname):\\n"
                 "  f = open(fname)\\n"
                 "  try:\\n"
                 "    c = f.read().replace('\\\\r\\\\n', '\\\\n')\\n"
                 "    try:\\n"
                 "      return str(eval(c))\\n"
                 "    except:\\n"
                 "      exec c in globals(), globals()\\n"
                 "  finally:\\n"
                 "    f.close()"
                 "\")")))

(defun etom-disconnect ()
"Disconnect from Maya and kill etom-buffer."
(interactive)
(if etom-buffer
(kill-buffer etom-buffer)))

(defun etom-connected ()
"Return non-nil if there is connection to Maya."
(interactive)
(comint-check-proc etom-buffer))

(defun etom-prompt-line ()
(save-excursion
(forward-line 0)
(looking-at comint-prompt-regexp)))

(defun etom-wait-for-prompt (last-prompt)
(let ((prompt-found nil))
(while (not prompt-found)
(accept-process-output (get-buffer-process (current-buffer)))
(goto-char (point-max))
(setq prompt-found (and (etom-prompt-line) (not (= (count-lines (point-min) (point-max)) last-prompt)))))))

(defun etom-send-current-line ()
"Send current line to Maya."
(interactive)
(let ((start (save-excursion (beginning-of-line) (point)))
(end (save-excursion (end-of-line) (point))))
(etom-send-region start end)))

(defun etom-send-current-line-py ()
"Send current line to Maya as Python."
(interactive)
(let ((start (save-excursion (beginning-of-line) (point)))
(end (save-excursion (end-of-line) (point))))
(etom-send-region-py start end)))

(defun etom-send-region (start end &optional aspython)
"Send region to Maya."
(interactive "r")
(if (not (etom-connected))
(etom-connect))
(if etom-always-show
(etom-show-buffer))
(let (
(tempfile (make-temp-file "etom-"))
(formatstr (if aspython "python(\"etom_pyexec('%s')\")" "source \"%s\";")))
(write-region start end tempfile)
;; send source(tempfile)
(with-current-buffer etom-buffer
(let ((last-prompt (count-lines (point-min) (point-max))))
(goto-char (point-max))
(comint-simple-send (get-buffer-process (current-buffer))
                   (format formatstr
                           (replace-in-string tempfile "\\\\" "\\\\\\\\" )))
(etom-wait-for-prompt last-prompt)
(delete-file tempfile)))))

(defun etom-send-region-py (start end)
"Send region to Maya as Python."
(interactive "r")
(etom-send-region start end t))

(defun etom-send-region-2 (start end)
"Send region to Maya."
(interactive "r")
(if (not (etom-connected))
(etom-connect))
(if etom-always-show
(etom-show-buffer))
(comint-simple-send (get-buffer-process etom-buffer)
             (buffer-substring start end)))

(defun etom-send-buffer ()
"Send whole buffer to Maya."
(interactive)
(etom-send-region (point-min) (point-max)))

(defun etom-send-buffer-py ()
"Send whole buffer to Maya as Python."
(interactive)
(etom-send-region-py (point-min) (point-max)))

(provide 'etom)

;;; etom.el ends here

Friday, June 26, 2009

Emacs To Maya

I started using Emacs To Maya.
Now modifying it to send Python scripts (Half done, thinking about how to get the value printed in the command history).

Ah, I added the following script to .emacs before


(add-hook
'mel-mode-hook ...

to avoid

Symbol's function definition is void: replace-in-string

error.

(if (not (fboundp 'replace-in-string))
(defun replace-in-string (string regexp replacement &optional literal)
"Replace regex in string with replacement"
(replace-regexp-in-string regexp replacement string t literal)))

(got it here)
For those who has seen the same error.

Recursive directory search configuration for anything.el

Continued from the previous entry.
If the selected document is html (".html" or ".htm" case insensitive) it opens the document with w3m in a new frame, otherwise it just opens it normally as a text file in the current frame.


hohe2-anything-find.el


(require 'w3m)
(require 'anything-config)


;;;; Find


(defun hohe2-register-c-source-find (config-symbol title-name find-dir min-required-chars)
"Register a new find configuration"
(set config-symbol
(list
`(name . ,title-name)
`(candidates . (lambda ()
(apply 'start-process "find-process" nil
(list
"find"
,find-dir
"-iname"
(concat "*" anything-pattern "*"))
)))
'(type . findfile)
`(requires-pattern . ,min-required-chars)
'(delayed))
)
)


(defun hohe2-anything-html-open-method (c)
(select-window (frame-selected-window (make-frame)))
(w3m-browse-url (concat "file://" c)))

(defun hohe2-anything-default-open-method (c)
(let ((ext (file-name-extension c)))
(if ext
(if (member (downcase ext) '("html" "htm"))
(hohe2-anything-html-open-method c)
(find-file c)))))

(add-to-list 'anything-type-attributes
'(findfile (action
("Default" . hohe2-anything-default-open-method)
("Browse other window" . (lambda (c)
(hohe2-anything-html-open-method c)))
("Browse" . (lambda (c) (w3m-browse-url (concat "file://" c))))
("Browse with Firefox" . (lambda (c) (shell-command (concat "firefox file://" c))))
("Open as text" . find-file)
)))



(provide 'hohe2-anything-find)

anything.el for Maya document (continued)


For those who uses emacs for Maya programming :)

Internally it uses "find". It is not limited to Maya documents. you can search for documents in any directory (and its sub directories recursively).

Usage:
Type META-; (I made a shortcut that searches only Maya API documents)
See the movie in another blog entry.

Installation:
1) If you haven't installed anything.el install it.

2) Install w3m web browser for Emacs. I just executed


sudo yum install w3m-el

3) Add hohe2-anything-find.el in any directory.

4) Add this line to .emacs, (unless the directory path has been added already)

(add-to-list 'load-path "path/to/directory")
5) Add the following script to your .emacs file. (Change the paths to the document directories).

;;Additional anything config for Maya.
(hohe2-register-c-source-find
'hohe2-anything-c-source-find-mayaapi "Maya API" "/usr/autodesk/maya/docs/Maya8.5/en_US/API" 2)

(hohe2-register-c-source-find
'hohe2-anything-c-source-find-mayamel "Maya MEL" "/usr/autodesk/maya/docs/Maya8.5/en_US/Commands" 2)

(hohe2-register-c-source-find
'hohe2-anything-c-source-find-mayapython "Maya Python" "/usr/autodesk/maya/docs/Maya8.5/en_US/CommandsPython" 2)

(hohe2-register-c-source-find
'hohe2-anything-c-source-find-mayanode "Maya Node" "/usr/autodesk/maya/docs/Maya8.5/en_US/Nodes" 2)

(setq hohe2-anything-alternative-sources (list hohe2-anything-c-source-find-mayaapi
hohe2-anything-c-source-find-mayamel
hohe2-anything-c-source-find-mayapython
hohe2-anything-c-source-find-mayanode
))

(defun hohe2-anything-alternative ()
(interactive)
(anything hohe2-anything-alternative-sources);;)
)
(global-set-key (kbd "M-;") 'hohe2-anything-alternative)
6) Restart Emacs.

7) Enjoy programming if it works.

This is my development environment

2.6.29.4-167.fc11.i686.PAE #1 SMP Wed May 27 17:28:22 EDT 2009 i686 i686 i386 GNU/Linux
Gnome 2.26.2
GNU Emacs 22.3.1

No guarantee. Use it at your own risk.

hohe2-anything-find.el

(require 'w3m)
(require 'anything-config)


;;;; Find


(defun hohe2-register-c-source-find (config-symbol title-name find-dir min-required-chars)
"Register a new find configuration"
(set config-symbol
(list
`(name . ,title-name)
`(candidates . (lambda ()
(apply 'start-process "find-process" nil
(list
"find"
,find-dir
"-iname"
(concat "*" anything-pattern "*"))
)))
'(type . findfile)
`(requires-pattern . ,min-required-chars)
'(delayed))
)
)


(add-to-list 'anything-type-attributes
'(findfile (action
("Browse other window" . (lambda (c)
(select-window (frame-selected-window (make-frame)))
(w3m-browse-url (concat "file://" c))))
("Browse" . (lambda (c) (w3m-browse-url (concat "file://" c))))
("Browse with Firefox" . (lambda (c) (shell-command (concat "firefox file://" c))))
("Open as text" . find-file)
)))



(provide 'hohe2-anything-find)

Currently it assumes every document is HTML. I will modify the script so that it looks at the file extension to know how to open the file soon(er or later). If you modify part of the above code like this it opens the file as a text by default.
June 28 added. Done.

(add-to-list 'anything-type-attributes
'(findfile (action
("Open as text" . find-file)
("Browse other window" . (lambda (c)
(select-window (frame-selected-window (make-frame)))
(w3m-browse-url (concat "file://" c))))
("Browse" . (lambda (c) (w3m-browse-url (concat "file://" c))))
("Browse with Firefox" . (lambda (c) (shell-command (concat "firefox file://" c))))
)))

Thursday, June 25, 2009

anything.el for Maya document (under construction)

Currently making an anything.el configuration for Maya document.

Wednesday, June 24, 2009

Complementary Afterimage

You can see an example of complementary afterimage here (Stare at the black dot in the center of the image for 10 sec and move the mouse pointer over the image). This is a very good one.

You can also find a lot of examples on youtube like this (keep looking at the center of the image)


I was curious if I can "add" those fake colors just like others (e.g. red and green makes yellow).
I made a simple test. I looked at cyan with my left eye and and magenta with my right for 10 seconds. Interestingly enough, I could see a yellow band in between two colors (red and green) when I looked at gray with my both eyes. Now I'm trying to make another test that makes clearer result which proves it, since the band was not easy to see and my preconception may have had it visible (and using left eye and right eye separately is not good for this test).

Monday, June 22, 2009

Sketch Furniture

Drawing a furniture in the air with a mocap marker attached finger and send the data to a 3d forming machine. Awesome!

Thursday, June 18, 2009

One way to control shader parameters

The basic idea is making a shader with which you can control the surface's reflection characteristics by editing its BRDF in the GUI. Simple BRDF has two parameters. By mapping the two parameters to the angle components theta, phi of a polar coordinate, and mapping the reflectance ratio (so not BRDF, strictly speaking. I just used the term BRDF for explanation) to the r components of the polar coordinate, you can make a 3D shape that represents a BRDF (not BRDF strictly ...)












I made a test shader looooong time ago. The shader is assigned to the sphere. By changing the shape of the cobra like surface you can you can control the reflection characteristics of the test shader. It's a just a test so I shot rays from the origin to get r (length from the origin to the hit point) in side the surface shader, still you can edit the reflection characteristics interactively and animate them if you like. The test shader uses surface color too. It'll be better to be able to store the shape data to the file so that we can separate reflection editing and using it.






















The usage is limited of course. It is rather a simple reflection, it can control only parameters which are mapped to the surface but it's more intuitive, the user doesn't need to write a code.

Monday, June 15, 2009

My software development profile

Professional experience
- Group based crowd simulator development.
    - From concept design making to artist support
    - system API design/implementation
    - Maya nodes for crowd control (mocap placement, Python expression node for crowd control etc.)
    - Maya hardware for preview
- 3D reconstruction from images (interactive still image interpolation)
- Procedural animation software
- Tool development for artists

And these are my private works (links to the blog entries)
- Maya node to map texture color/normal to arbitrary per particle attribute
- Maya command to generate and control curves
- Shake Command Window
- boost.python patch
- Visual programming framework (prototype)
- Shake and Maya functional integration

Sunday, June 14, 2009

How Will Balloons Look That Lift Your House Up?

There's an interesting article about Disney-Pixar's new movie Up.
How Many Balloons Will Lift Your House Up?

It says,
According to Pixar, Carl's house in the film attains liftoff with about 20,000 balloons. But for the sake of realism, director Pete Docter and his crew wanted to know how many balloons it would take in real life to make the scenario plausible, which came in at 20 million to 30 million.

I just wondered "what if there are 20 million balloons?", and tested it.
Each balloon is r=10cm sphere and the house is 15m x 7.5m x 7.5m.
(Click for bigger image)












Hmmm, I guess just 20000 balloons might possibly look better...