Wednesday, September 30, 2015

Python wrapper for hscript

Houdini has a nice little function in Python in the hou namespace called "hscript". You can pass in a string for an hscript command. It passes back out a tuple with stdout and stderr strings. This is dandy, but it would be nice to call hscript commands with (in essence) Python syntax. So I made a little class that does just that.
class HscriptWrapper(object):
    commands = hou.hscript('help')[0]
    commands = frozenset(commands.split())

    def __getattr__(self, attr):
        if attr not in self.commands:
            raise AttributeError('No hscript command by name "%s"' % attr)
        # cache for future calls
        self.__dict__[attr] = rattr = self._command_factory(attr)
        return rattr

    def _command_factory(self, attr):
        def cmd(*args, **kwargs):
            fmt = '{cmd} {flags} {args}'
            hsargs = ' '.join(args)
            hsflags = []
            for k, v in kwargs.items():
                if isinstance(v, bool) and v is True:
                    hsflags.append('-' + k)
                    hsflags.append('-{0} {1}'.format(k, v))
            hsflags = ' '.join(hsflags)
            hscmd = fmt.format(cmd=attr, flags=hsflags, args=hsargs)
            return hou.hscript(hscmd)
        cmd.__name__ = attr
        cmd.__doc__ = hou.hscript('help {}'.format(attr))[0]
        return cmd
To use it, create an instance of the HscriptWrapper class, then access hscript commands as attributes of the instance. The returned attribute is a callable function that takes positional and keyword arguments. This Python snippet:
hswrap = HscriptWrapper()
hswrap.opfind('cam1', p='/')
Is equivalent to this hscript command:
opfind -p / cam1

Any positional arguments are passed through as positional arguments to hscript. Keyword arguments are passed through as option flags with an argument. If the value of a keyword argument is a boolean, then the flag doesn't get created with an argument and only gets  created if the value is 'True'. Thus, it is safe to write "hscmd(f=False)" and the 'f' flag will not be included, whereas "hscmd(f=True)" will include the flag with no trailing argument.

More complex hscript voodoo won't work with this setup, but for simply calling an hscript command that has no python equivalent and getting its output, this does the trick nicely.

It doesn't do any safe escaping of arguments yet. For instance, if you have a file path with a space in it, this will be detected as two arguments instead of one argument with an embedded space. That is next on the list.

The above code is placed in the public domain and is provided as is, without express or implied warranty of any kind, to the extent allowable by law. Use it however you like. There is no requirement to attribute the code to me (although any mention would be appreciated).

Thursday, February 14, 2013

Houdini Rigging

I am generally pretty sad with the number of free tutorials about Houdini rigging. Thus, I have started a series of them on youtube. They aren't polished or even really planned out before hand. Really, they are little more than just stream of consciousness, but they have information, which is the important part. If you are interested in certain topics relating to Houdini rigging, post a comment and let me know what you want me to cover next.

Thursday, January 3, 2013

Creating an ISO image from a CD/DVD on Unix

Hey, it has been a while since I last posted anything. I apologize to some of you since this post is going to be rather techie/geekie. It is more as a reminder to myself about how to do this than anything else.

Earlier today, I was furiously trying to find a utility on Linux to take a CD/DVD and create an ISO image from it. I was coming up dry and was feeling a bit frustrated until I stumbled onto this website. Once I read it I felt like a complete moron. Why did I feel so stupid? One of the great things about Linux, BSD, Mac OS X and pretty much every other OS descended from Unix is that everything is a file. Literally everything. And I had forgotten that disc drives (including optical drives) fall well within the category of "everything".

Thus the way to create an ISO image from a CD/DVD is as simple as running the following from the terminal:

    cat /dev/cdrom > /home/user/path/to/image.iso

That's it. Simply redirect the output of CD/DVD device into a file. Easy, right?