2015-05-24 04:51:16 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <system_error>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "CBlenderConnection.hpp"
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define DEFAULT_BLENDER_BIN "/Applications/Blender.app/Contents/MacOS/blender"
|
|
|
|
#elif _WIN32
|
|
|
|
#define DEFAULT_BLENDER_BIN "%ProgramFiles%\\Blender Foundation\\Blender\\blender.exe"
|
|
|
|
#else
|
|
|
|
#define DEFAULT_BLENDER_BIN "blender"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TEMP_SHELLSCRIPT "/home/jacko/hecl/blender/blendershell.py"
|
|
|
|
|
2015-05-24 22:19:28 +00:00
|
|
|
size_t CBlenderConnection::_readLine(char* buf, size_t bufSz)
|
2015-05-24 04:51:16 +00:00
|
|
|
{
|
|
|
|
size_t readBytes = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (readBytes >= bufSz)
|
2015-05-25 01:34:31 +00:00
|
|
|
{
|
2015-05-24 04:51:16 +00:00
|
|
|
throw std::length_error("Pipe buffer overrun");
|
2015-05-25 01:34:31 +00:00
|
|
|
*(buf-1) = '\0';
|
|
|
|
return bufSz - 1;
|
|
|
|
}
|
2015-05-24 04:51:16 +00:00
|
|
|
ssize_t ret = read(m_readpipe[0], buf, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
else if (ret == 1)
|
|
|
|
{
|
|
|
|
if (*buf == '\n')
|
|
|
|
{
|
|
|
|
*buf = '\0';
|
|
|
|
return readBytes;
|
|
|
|
}
|
|
|
|
++readBytes;
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*buf = '\0';
|
|
|
|
return readBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
throw std::error_code(errno, std::system_category());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-24 22:19:28 +00:00
|
|
|
size_t CBlenderConnection::_writeLine(const char* buf)
|
2015-05-24 04:51:16 +00:00
|
|
|
{
|
|
|
|
ssize_t ret, nlerr;
|
|
|
|
ret = write(m_writepipe[1], buf, strlen(buf));
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
nlerr = write(m_writepipe[1], "\n", 1);
|
|
|
|
if (nlerr < 0)
|
|
|
|
goto err;
|
|
|
|
return (size_t)ret;
|
|
|
|
err:
|
|
|
|
throw std::error_code(errno, std::system_category());
|
|
|
|
}
|
|
|
|
|
2015-05-24 22:19:28 +00:00
|
|
|
size_t CBlenderConnection::_readBuf(char* buf, size_t len)
|
2015-05-24 04:51:16 +00:00
|
|
|
{
|
|
|
|
ssize_t ret = read(m_readpipe[0], buf, len);
|
|
|
|
if (ret < 0)
|
|
|
|
throw std::error_code(errno, std::system_category());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-24 22:19:28 +00:00
|
|
|
size_t CBlenderConnection::_writeBuf(const char* buf, size_t len)
|
2015-05-24 04:51:16 +00:00
|
|
|
{
|
|
|
|
ssize_t ret = write(m_writepipe[1], buf, len);
|
|
|
|
if (ret < 0)
|
|
|
|
throw std::error_code(errno, std::system_category());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-24 22:19:28 +00:00
|
|
|
void CBlenderConnection::_closePipe()
|
2015-05-24 04:51:16 +00:00
|
|
|
{
|
|
|
|
close(m_readpipe[0]);
|
|
|
|
close(m_writepipe[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
CBlenderConnection::CBlenderConnection(bool silenceBlender)
|
|
|
|
{
|
|
|
|
/* Construct communication pipes */
|
|
|
|
pipe(m_readpipe);
|
|
|
|
pipe(m_writepipe);
|
|
|
|
|
|
|
|
/* User-specified blender path */
|
|
|
|
char* blenderBin = getenv("BLENDER_BIN");
|
|
|
|
|
|
|
|
/* Child process of blender */
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (!pid)
|
|
|
|
{
|
|
|
|
close(m_writepipe[1]);
|
|
|
|
close(m_readpipe[0]);
|
|
|
|
|
|
|
|
if (silenceBlender)
|
|
|
|
{
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
}
|
|
|
|
|
|
|
|
char errbuf[256];
|
|
|
|
char readfds[32];
|
|
|
|
snprintf(readfds, 32, "%d", m_writepipe[0]);
|
|
|
|
char writefds[32];
|
|
|
|
snprintf(writefds, 32, "%d", m_readpipe[1]);
|
|
|
|
|
2015-05-24 22:19:28 +00:00
|
|
|
/* Try user-specified blender first */
|
2015-05-24 04:51:16 +00:00
|
|
|
if (blenderBin)
|
|
|
|
{
|
2015-05-24 22:19:28 +00:00
|
|
|
execlp(blenderBin, blenderBin,
|
|
|
|
"--background", "-P", TEMP_SHELLSCRIPT,
|
2015-05-24 04:51:16 +00:00
|
|
|
"--", readfds, writefds, NULL);
|
|
|
|
if (errno != ENOENT)
|
|
|
|
{
|
|
|
|
snprintf(errbuf, 256, "NOLAUNCH %s\n", strerror(errno));
|
|
|
|
write(m_writepipe[1], errbuf, strlen(errbuf));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 22:19:28 +00:00
|
|
|
/* Otherwise default blender */
|
|
|
|
execlp(DEFAULT_BLENDER_BIN, DEFAULT_BLENDER_BIN,
|
|
|
|
"--background", "-P", TEMP_SHELLSCRIPT,
|
2015-05-24 04:51:16 +00:00
|
|
|
"--", readfds, writefds, NULL);
|
|
|
|
if (errno != ENOENT)
|
|
|
|
{
|
|
|
|
snprintf(errbuf, 256, "NOLAUNCH %s\n", strerror(errno));
|
|
|
|
write(m_writepipe[1], errbuf, strlen(errbuf));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unable to find blender */
|
|
|
|
write(m_writepipe[1], "NOBLENDER\n", 10);
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
close(m_writepipe[0]);
|
|
|
|
close(m_readpipe[1]);
|
|
|
|
m_blenderProc = pid;
|
|
|
|
|
|
|
|
/* Handle first response */
|
|
|
|
char lineBuf[256];
|
2015-05-24 22:19:28 +00:00
|
|
|
_readLine(lineBuf, sizeof(lineBuf));
|
2015-05-24 04:51:16 +00:00
|
|
|
if (!strcmp(lineBuf, "NOLAUNCH"))
|
|
|
|
{
|
2015-05-24 22:19:28 +00:00
|
|
|
_closePipe();
|
2015-05-24 04:51:16 +00:00
|
|
|
throw std::runtime_error("Unable to launch blender");
|
|
|
|
}
|
|
|
|
else if (!strcmp(lineBuf, "NOBLENDER"))
|
|
|
|
{
|
2015-05-24 22:19:28 +00:00
|
|
|
_closePipe();
|
2015-05-24 04:51:16 +00:00
|
|
|
if (blenderBin)
|
2015-05-24 22:19:28 +00:00
|
|
|
throw std::runtime_error("Unable to find blender at '" +
|
|
|
|
std::string(blenderBin) + "' or '" +
|
2015-05-24 04:51:16 +00:00
|
|
|
std::string(DEFAULT_BLENDER_BIN) + "'");
|
|
|
|
else
|
|
|
|
throw std::runtime_error("Unable to find blender at '" +
|
|
|
|
std::string(DEFAULT_BLENDER_BIN) + "'");
|
|
|
|
}
|
|
|
|
else if (!strcmp(lineBuf, "NOADDON"))
|
|
|
|
{
|
2015-05-24 22:19:28 +00:00
|
|
|
_closePipe();
|
2015-05-24 04:51:16 +00:00
|
|
|
throw std::runtime_error("HECL addon not installed within blender");
|
|
|
|
}
|
|
|
|
else if (strcmp(lineBuf, "READY"))
|
|
|
|
{
|
2015-05-24 22:19:28 +00:00
|
|
|
_closePipe();
|
|
|
|
throw std::runtime_error("read '" + std::string(lineBuf) +
|
|
|
|
"' from blender; expected 'READY'");
|
2015-05-24 04:51:16 +00:00
|
|
|
}
|
2015-05-24 22:19:28 +00:00
|
|
|
_writeLine("ACK");
|
2015-05-24 04:51:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CBlenderConnection::~CBlenderConnection()
|
|
|
|
{
|
2015-05-24 22:19:28 +00:00
|
|
|
_closePipe();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBlenderConnection::openBlend(const std::string& path)
|
|
|
|
{
|
|
|
|
_writeLine(("OPEN" + path).c_str());
|
|
|
|
char lineBuf[256];
|
|
|
|
_readLine(lineBuf, sizeof(lineBuf));
|
|
|
|
if (!strcmp(lineBuf, "FINISHED"))
|
|
|
|
{
|
|
|
|
m_loadedBlend = path;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-26 04:42:20 +00:00
|
|
|
bool CBlenderConnection::cookBlend(std::function<char*(uint32_t)> bufGetter,
|
2015-05-24 22:19:28 +00:00
|
|
|
const std::string& expectedType,
|
|
|
|
const std::string& platform,
|
|
|
|
bool bigEndian)
|
|
|
|
{
|
|
|
|
char lineBuf[256];
|
|
|
|
char reqLine[512];
|
|
|
|
snprintf(reqLine, 512, "COOK %s %c", platform.c_str(), bigEndian?'>':'<');
|
|
|
|
_writeLine(reqLine);
|
|
|
|
_readLine(lineBuf, sizeof(lineBuf));
|
|
|
|
if (strcmp(expectedType.c_str(), lineBuf))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("expected '" + m_loadedBlend +
|
|
|
|
"' to contain " + expectedType +
|
|
|
|
" not " + lineBuf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_writeLine("ACK");
|
|
|
|
|
|
|
|
for (_readLine(lineBuf, sizeof(lineBuf));
|
|
|
|
!strcmp("BUF", lineBuf);
|
|
|
|
_readLine(lineBuf, sizeof(lineBuf)))
|
|
|
|
{
|
|
|
|
uint32_t sz;
|
2015-05-26 04:42:20 +00:00
|
|
|
_readBuf((char*)&sz, 4);
|
|
|
|
char* buf = bufGetter(sz);
|
2015-05-24 22:19:28 +00:00
|
|
|
_readBuf(buf, sz);
|
|
|
|
}
|
|
|
|
if (!strcmp("SUCCESS", lineBuf))
|
|
|
|
return true;
|
|
|
|
else if (!strcmp("EXCEPTION", lineBuf))
|
|
|
|
throw std::runtime_error("blender script exception");
|
|
|
|
|
|
|
|
return false;
|
2015-05-24 04:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBlenderConnection::quitBlender()
|
|
|
|
{
|
2015-05-24 22:19:28 +00:00
|
|
|
_writeLine("QUIT");
|
2015-05-24 04:51:16 +00:00
|
|
|
char lineBuf[256];
|
2015-05-24 22:19:28 +00:00
|
|
|
_readLine(lineBuf, sizeof(lineBuf));
|
2015-05-24 04:51:16 +00:00
|
|
|
}
|