2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
#include "CCL_api.h"
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "blender_sync.h"
|
|
|
|
#include "blender_session.h"
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
#include "util_foreach.h"
|
2012-11-03 14:32:35 +00:00
|
|
|
#include "util_md5.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_opengl.h"
|
|
|
|
#include "util_path.h"
|
|
|
|
|
2012-11-03 14:32:35 +00:00
|
|
|
#ifdef WITH_OSL
|
|
|
|
#include "osl.h"
|
|
|
|
|
|
|
|
#include <OSL/oslquery.h>
|
|
|
|
#include <OSL/oslconfig.h>
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2014-07-01 15:43:37 +06:00
|
|
|
static void *pylong_as_voidptr_typesafe(PyObject *object)
|
|
|
|
{
|
|
|
|
if(object == Py_None)
|
|
|
|
return NULL;
|
|
|
|
return PyLong_AsVoidPtr(object);
|
|
|
|
}
|
|
|
|
|
2014-02-12 23:13:45 +01:00
|
|
|
void python_thread_state_save(void **python_thread_state)
|
2014-02-12 21:49:34 +01:00
|
|
|
{
|
2014-02-12 23:13:45 +01:00
|
|
|
*python_thread_state = (void*)PyEval_SaveThread();
|
2014-02-12 21:49:34 +01:00
|
|
|
}
|
|
|
|
|
2014-02-12 23:13:45 +01:00
|
|
|
void python_thread_state_restore(void **python_thread_state)
|
2014-02-12 21:49:34 +01:00
|
|
|
{
|
2014-02-12 23:13:45 +01:00
|
|
|
PyEval_RestoreThread((PyThreadState*)*python_thread_state);
|
|
|
|
*python_thread_state = NULL;
|
2014-02-12 21:49:34 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 14:53:49 +02:00
|
|
|
static const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
/* bug [#31856] oddly enough, Python3.2 --> 3.3 on Windows will throw an
|
|
|
|
* exception here this needs to be fixed in python:
|
|
|
|
* see: bugs.python.org/issue15859 */
|
|
|
|
if(!PyUnicode_Check(py_str)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if((*coerce = PyUnicode_EncodeFSDefault(py_str))) {
|
|
|
|
return PyBytes_AS_STRING(*coerce);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
static PyObject *init_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
2014-10-14 14:53:49 +02:00
|
|
|
PyObject *path, *user_path;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2014-10-14 14:53:49 +02:00
|
|
|
if(!PyArg_ParseTuple(args, "OO", &path, &user_path)) {
|
2011-04-27 11:58:34 +00:00
|
|
|
return NULL;
|
2014-10-14 14:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *path_coerce = NULL, *user_path_coerce = NULL;
|
|
|
|
path_init(PyC_UnicodeAsByte(path, &path_coerce),
|
|
|
|
PyC_UnicodeAsByte(user_path, &user_path_coerce));
|
|
|
|
Py_XDECREF(path_coerce);
|
|
|
|
Py_XDECREF(user_path_coerce);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *create_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
2012-01-09 16:58:01 +00:00
|
|
|
PyObject *pyengine, *pyuserpref, *pydata, *pyscene, *pyregion, *pyv3d, *pyrv3d;
|
2013-01-28 17:37:51 +00:00
|
|
|
int preview_osl;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-01-30 17:04:51 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "OOOOOOOi", &pyengine, &pyuserpref, &pydata, &pyscene, &pyregion, &pyv3d, &pyrv3d, &preview_osl))
|
2011-04-27 11:58:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* RNA */
|
|
|
|
PointerRNA engineptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_pointer_create(NULL, &RNA_RenderEngine, (void*)PyLong_AsVoidPtr(pyengine), &engineptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::RenderEngine engine(engineptr);
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
PointerRNA userprefptr;
|
2013-06-11 12:52:02 +00:00
|
|
|
RNA_pointer_create(NULL, &RNA_UserPreferences, (void*)PyLong_AsVoidPtr(pyuserpref), &userprefptr);
|
2012-01-09 16:58:01 +00:00
|
|
|
BL::UserPreferences userpref(userprefptr);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
PointerRNA dataptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pydata), &dataptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::BlendData data(dataptr);
|
|
|
|
|
|
|
|
PointerRNA sceneptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::Scene scene(sceneptr);
|
|
|
|
|
|
|
|
PointerRNA regionptr;
|
2014-07-01 15:43:37 +06:00
|
|
|
RNA_id_pointer_create((ID*)pylong_as_voidptr_typesafe(pyregion), ®ionptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::Region region(regionptr);
|
|
|
|
|
|
|
|
PointerRNA v3dptr;
|
2014-07-01 15:43:37 +06:00
|
|
|
RNA_id_pointer_create((ID*)pylong_as_voidptr_typesafe(pyv3d), &v3dptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::SpaceView3D v3d(v3dptr);
|
|
|
|
|
|
|
|
PointerRNA rv3dptr;
|
2014-07-01 15:43:37 +06:00
|
|
|
RNA_id_pointer_create((ID*)pylong_as_voidptr_typesafe(pyrv3d), &rv3dptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::RegionView3D rv3d(rv3dptr);
|
|
|
|
|
|
|
|
/* create session */
|
|
|
|
BlenderSession *session;
|
|
|
|
|
|
|
|
if(rv3d) {
|
2013-01-28 17:37:51 +00:00
|
|
|
/* interactive viewport session */
|
2011-04-27 11:58:34 +00:00
|
|
|
int width = region.width();
|
|
|
|
int height = region.height();
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
session = new BlenderSession(engine, userpref, data, scene, v3d, rv3d, width, height);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-01-28 17:37:51 +00:00
|
|
|
/* override some settings for preview */
|
|
|
|
if(engine.is_preview()) {
|
|
|
|
PointerRNA cscene = RNA_pointer_get(&sceneptr, "cycles");
|
|
|
|
|
|
|
|
RNA_boolean_set(&cscene, "shading_system", preview_osl);
|
|
|
|
RNA_boolean_set(&cscene, "use_progressive_refine", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* offline session or preview render */
|
2012-01-09 16:58:01 +00:00
|
|
|
session = new BlenderSession(engine, userpref, data, scene);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2014-02-12 23:13:45 +01:00
|
|
|
python_thread_state_save(&session->python_thread_state);
|
|
|
|
|
|
|
|
session->create();
|
|
|
|
|
|
|
|
python_thread_state_restore(&session->python_thread_state);
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
return PyLong_FromVoidPtr(session);
|
|
|
|
}
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
static PyObject *free_func(PyObject *self, PyObject *value)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-12-24 02:47:13 +00:00
|
|
|
delete (BlenderSession*)PyLong_AsVoidPtr(value);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
static PyObject *render_func(PyObject *self, PyObject *value)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-12-24 02:47:13 +00:00
|
|
|
BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
|
2014-02-12 23:13:45 +01:00
|
|
|
|
|
|
|
python_thread_state_save(&session->python_thread_state);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
session->render();
|
|
|
|
|
2014-02-12 23:13:45 +01:00
|
|
|
python_thread_state_restore(&session->python_thread_state);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2014-01-02 19:05:07 -02:00
|
|
|
/* pixel_array and result passed as pointers */
|
|
|
|
static PyObject *bake_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *pysession, *pyobject;
|
|
|
|
PyObject *pypixel_array, *pyresult;
|
|
|
|
const char *pass_type;
|
|
|
|
int num_pixels, depth;
|
|
|
|
|
|
|
|
if(!PyArg_ParseTuple(args, "OOsOiiO", &pysession, &pyobject, &pass_type, &pypixel_array, &num_pixels, &depth, &pyresult))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
|
|
|
|
|
|
|
|
PointerRNA objectptr;
|
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyobject), &objectptr);
|
|
|
|
BL::Object b_object(objectptr);
|
|
|
|
|
|
|
|
void *b_result = PyLong_AsVoidPtr(pyresult);
|
|
|
|
|
|
|
|
PointerRNA bakepixelptr;
|
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pypixel_array), &bakepixelptr);
|
|
|
|
BL::BakePixel b_bake_pixel(bakepixelptr);
|
|
|
|
|
2014-05-22 20:18:45 -03:00
|
|
|
python_thread_state_save(&session->python_thread_state);
|
|
|
|
|
2014-07-28 14:29:03 -03:00
|
|
|
session->bake(b_object, pass_type, b_bake_pixel, (size_t)num_pixels, depth, (float *)b_result);
|
2014-01-02 19:05:07 -02:00
|
|
|
|
2014-05-22 20:18:45 -03:00
|
|
|
python_thread_state_restore(&session->python_thread_state);
|
2014-01-02 19:05:07 -02:00
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
static PyObject *draw_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
2011-05-05 13:51:33 +00:00
|
|
|
PyObject *pysession, *pyv3d, *pyrv3d;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-05-05 13:51:33 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "OOO", &pysession, &pyv3d, &pyrv3d))
|
2011-04-27 11:58:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
2011-05-05 13:51:33 +00:00
|
|
|
BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-05-05 13:51:33 +00:00
|
|
|
if(PyLong_AsVoidPtr(pyrv3d)) {
|
2011-04-27 11:58:34 +00:00
|
|
|
/* 3d view drawing */
|
|
|
|
int viewport[4];
|
|
|
|
glGetIntegerv(GL_VIEWPORT, viewport);
|
|
|
|
|
2011-08-23 12:20:11 +00:00
|
|
|
session->draw(viewport[2], viewport[3]);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:46:53 +00:00
|
|
|
static PyObject *reset_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *pysession, *pydata, *pyscene;
|
|
|
|
|
|
|
|
if(!PyArg_ParseTuple(args, "OOO", &pysession, &pydata, &pyscene))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
|
|
|
|
|
|
|
|
PointerRNA dataptr;
|
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pydata), &dataptr);
|
|
|
|
BL::BlendData b_data(dataptr);
|
|
|
|
|
|
|
|
PointerRNA sceneptr;
|
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
|
|
|
|
BL::Scene b_scene(sceneptr);
|
|
|
|
|
2014-02-12 23:13:45 +01:00
|
|
|
python_thread_state_save(&session->python_thread_state);
|
2012-11-09 08:46:53 +00:00
|
|
|
|
|
|
|
session->reset_session(b_data, b_scene);
|
|
|
|
|
2014-02-12 23:13:45 +01:00
|
|
|
python_thread_state_restore(&session->python_thread_state);
|
2012-11-09 08:46:53 +00:00
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
static PyObject *sync_func(PyObject *self, PyObject *value)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-12-24 02:47:13 +00:00
|
|
|
BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
|
2014-02-12 23:13:45 +01:00
|
|
|
|
|
|
|
python_thread_state_save(&session->python_thread_state);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
session->synchronize();
|
|
|
|
|
2014-02-12 23:13:45 +01:00
|
|
|
python_thread_state_restore(&session->python_thread_state);
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *available_devices_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
2012-01-09 16:58:01 +00:00
|
|
|
vector<DeviceInfo>& devices = Device::available_devices();
|
|
|
|
PyObject *ret = PyTuple_New(devices.size());
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
for(size_t i = 0; i < devices.size(); i++) {
|
|
|
|
DeviceInfo& device = devices[i];
|
|
|
|
PyTuple_SET_ITEM(ret, i, PyUnicode_FromString(device.description.c_str()));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-03 14:32:35 +00:00
|
|
|
#ifdef WITH_OSL
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2012-11-03 14:32:35 +00:00
|
|
|
static PyObject *osl_update_node_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *pynodegroup, *pynode;
|
|
|
|
const char *filepath = NULL;
|
|
|
|
|
|
|
|
if(!PyArg_ParseTuple(args, "OOs", &pynodegroup, &pynode, &filepath))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* RNA */
|
|
|
|
PointerRNA nodeptr;
|
|
|
|
RNA_pointer_create((ID*)PyLong_AsVoidPtr(pynodegroup), &RNA_ShaderNodeScript, (void*)PyLong_AsVoidPtr(pynode), &nodeptr);
|
|
|
|
BL::ShaderNodeScript b_node(nodeptr);
|
|
|
|
|
|
|
|
/* update bytecode hash */
|
|
|
|
string bytecode = b_node.bytecode();
|
|
|
|
|
|
|
|
if(!bytecode.empty()) {
|
|
|
|
MD5Hash md5;
|
|
|
|
md5.append((const uint8_t*)bytecode.c_str(), bytecode.size());
|
|
|
|
b_node.bytecode_hash(md5.get_hex().c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
b_node.bytecode_hash("");
|
|
|
|
|
|
|
|
/* query from file path */
|
|
|
|
OSL::OSLQuery query;
|
|
|
|
|
|
|
|
if(!OSLShaderManager::osl_query(query, filepath))
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
|
|
|
|
/* add new sockets from parameters */
|
|
|
|
set<void*> used_sockets;
|
|
|
|
|
|
|
|
for(int i = 0; i < query.nparams(); i++) {
|
|
|
|
const OSL::OSLQuery::Parameter *param = query.getparam(i);
|
|
|
|
|
|
|
|
/* skip unsupported types */
|
|
|
|
if(param->varlenarray || param->isstruct || param->type.arraylen > 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* determine socket type */
|
2013-03-18 16:34:57 +00:00
|
|
|
std::string socket_type;
|
|
|
|
BL::NodeSocket::type_enum data_type = BL::NodeSocket::type_VALUE;
|
|
|
|
float4 default_float4 = make_float4(0.0f, 0.0f, 0.0f, 1.0f);
|
2012-11-03 14:32:35 +00:00
|
|
|
float default_float = 0.0f;
|
|
|
|
int default_int = 0;
|
2012-11-06 21:36:44 +00:00
|
|
|
std::string default_string = "";
|
2012-11-03 14:32:35 +00:00
|
|
|
|
|
|
|
if(param->isclosure) {
|
2013-03-18 16:34:57 +00:00
|
|
|
socket_type = "NodeSocketShader";
|
2013-05-01 13:28:44 +00:00
|
|
|
data_type = BL::NodeSocket::type_SHADER;
|
2012-11-03 14:32:35 +00:00
|
|
|
}
|
|
|
|
else if(param->type.vecsemantics == TypeDesc::COLOR) {
|
2013-03-18 16:34:57 +00:00
|
|
|
socket_type = "NodeSocketColor";
|
|
|
|
data_type = BL::NodeSocket::type_RGBA;
|
2012-11-03 14:32:35 +00:00
|
|
|
|
|
|
|
if(param->validdefault) {
|
|
|
|
default_float4[0] = param->fdefault[0];
|
|
|
|
default_float4[1] = param->fdefault[1];
|
|
|
|
default_float4[2] = param->fdefault[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(param->type.vecsemantics == TypeDesc::POINT ||
|
|
|
|
param->type.vecsemantics == TypeDesc::VECTOR ||
|
2014-05-05 02:19:08 +10:00
|
|
|
param->type.vecsemantics == TypeDesc::NORMAL)
|
|
|
|
{
|
2013-03-18 16:34:57 +00:00
|
|
|
socket_type = "NodeSocketVector";
|
|
|
|
data_type = BL::NodeSocket::type_VECTOR;
|
2012-11-03 14:32:35 +00:00
|
|
|
|
|
|
|
if(param->validdefault) {
|
|
|
|
default_float4[0] = param->fdefault[0];
|
|
|
|
default_float4[1] = param->fdefault[1];
|
|
|
|
default_float4[2] = param->fdefault[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(param->type.aggregate == TypeDesc::SCALAR) {
|
|
|
|
if(param->type.basetype == TypeDesc::INT) {
|
2013-03-18 16:34:57 +00:00
|
|
|
socket_type = "NodeSocketInt";
|
|
|
|
data_type = BL::NodeSocket::type_INT;
|
2012-11-03 14:32:35 +00:00
|
|
|
if(param->validdefault)
|
|
|
|
default_int = param->idefault[0];
|
|
|
|
}
|
|
|
|
else if(param->type.basetype == TypeDesc::FLOAT) {
|
2013-03-18 16:34:57 +00:00
|
|
|
socket_type = "NodeSocketFloat";
|
|
|
|
data_type = BL::NodeSocket::type_VALUE;
|
2012-11-03 14:32:35 +00:00
|
|
|
if(param->validdefault)
|
|
|
|
default_float = param->fdefault[0];
|
|
|
|
}
|
2012-11-06 21:36:44 +00:00
|
|
|
else if(param->type.basetype == TypeDesc::STRING) {
|
2013-03-18 16:34:57 +00:00
|
|
|
socket_type = "NodeSocketString";
|
|
|
|
data_type = BL::NodeSocket::type_STRING;
|
2012-11-06 21:36:44 +00:00
|
|
|
if(param->validdefault)
|
|
|
|
default_string = param->sdefault[0];
|
|
|
|
}
|
2012-11-09 21:27:05 +00:00
|
|
|
else
|
|
|
|
continue;
|
2012-11-03 14:32:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* find socket socket */
|
2013-03-18 16:34:57 +00:00
|
|
|
BL::NodeSocket b_sock(PointerRNA_NULL);
|
|
|
|
if (param->isoutput) {
|
2014-08-05 23:49:42 +02:00
|
|
|
b_sock = b_node.outputs[param->name.string()];
|
2013-03-18 16:34:57 +00:00
|
|
|
/* remove if type no longer matches */
|
|
|
|
if(b_sock && b_sock.bl_idname() != socket_type) {
|
|
|
|
b_node.outputs.remove(b_sock);
|
|
|
|
b_sock = BL::NodeSocket(PointerRNA_NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2014-08-05 23:49:42 +02:00
|
|
|
b_sock = b_node.inputs[param->name.string()];
|
2013-03-18 16:34:57 +00:00
|
|
|
/* remove if type no longer matches */
|
|
|
|
if(b_sock && b_sock.bl_idname() != socket_type) {
|
|
|
|
b_node.inputs.remove(b_sock);
|
|
|
|
b_sock = BL::NodeSocket(PointerRNA_NULL);
|
|
|
|
}
|
2012-11-03 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 20:47:58 +00:00
|
|
|
if(!b_sock) {
|
|
|
|
/* create new socket */
|
|
|
|
if(param->isoutput)
|
|
|
|
b_sock = b_node.outputs.create(socket_type.c_str(), param->name.c_str(), param->name.c_str());
|
|
|
|
else
|
|
|
|
b_sock = b_node.inputs.create(socket_type.c_str(), param->name.c_str(), param->name.c_str());
|
|
|
|
|
|
|
|
/* set default value */
|
2013-03-18 16:34:57 +00:00
|
|
|
if(data_type == BL::NodeSocket::type_VALUE) {
|
|
|
|
set_float(b_sock.ptr, "default_value", default_float);
|
2012-11-03 14:32:35 +00:00
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
else if(data_type == BL::NodeSocket::type_INT) {
|
|
|
|
set_int(b_sock.ptr, "default_value", default_int);
|
2012-11-03 14:32:35 +00:00
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
else if(data_type == BL::NodeSocket::type_RGBA) {
|
|
|
|
set_float4(b_sock.ptr, "default_value", default_float4);
|
2012-11-03 14:32:35 +00:00
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
else if(data_type == BL::NodeSocket::type_VECTOR) {
|
|
|
|
set_float3(b_sock.ptr, "default_value", float4_to_float3(default_float4));
|
2012-11-03 14:32:35 +00:00
|
|
|
}
|
2013-03-18 16:34:57 +00:00
|
|
|
else if(data_type == BL::NodeSocket::type_STRING) {
|
|
|
|
set_string(b_sock.ptr, "default_value", default_string);
|
2012-11-06 21:36:44 +00:00
|
|
|
}
|
2012-11-03 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
used_sockets.insert(b_sock.ptr.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove unused parameters */
|
|
|
|
bool removed;
|
|
|
|
|
|
|
|
do {
|
|
|
|
BL::Node::inputs_iterator b_input;
|
|
|
|
BL::Node::outputs_iterator b_output;
|
|
|
|
|
|
|
|
removed = false;
|
|
|
|
|
|
|
|
for (b_node.inputs.begin(b_input); b_input != b_node.inputs.end(); ++b_input) {
|
|
|
|
if(used_sockets.find(b_input->ptr.data) == used_sockets.end()) {
|
2013-03-18 16:34:57 +00:00
|
|
|
b_node.inputs.remove(*b_input);
|
2012-11-03 14:32:35 +00:00
|
|
|
removed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (b_node.outputs.begin(b_output); b_output != b_node.outputs.end(); ++b_output) {
|
|
|
|
if(used_sockets.find(b_output->ptr.data) == used_sockets.end()) {
|
2013-03-18 16:34:57 +00:00
|
|
|
b_node.outputs.remove(*b_output);
|
2012-11-03 14:32:35 +00:00
|
|
|
removed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(removed);
|
|
|
|
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *osl_compile_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
const char *inputfile = NULL, *outputfile = NULL;
|
|
|
|
|
|
|
|
if(!PyArg_ParseTuple(args, "ss", &inputfile, &outputfile))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* return */
|
|
|
|
if(!OSLShaderManager::osl_compile(inputfile, outputfile))
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
static PyMethodDef methods[] = {
|
|
|
|
{"init", init_func, METH_VARARGS, ""},
|
|
|
|
{"create", create_func, METH_VARARGS, ""},
|
2011-12-24 02:47:13 +00:00
|
|
|
{"free", free_func, METH_O, ""},
|
|
|
|
{"render", render_func, METH_O, ""},
|
2014-01-02 19:05:07 -02:00
|
|
|
{"bake", bake_func, METH_VARARGS, ""},
|
2011-04-27 11:58:34 +00:00
|
|
|
{"draw", draw_func, METH_VARARGS, ""},
|
2011-12-24 02:47:13 +00:00
|
|
|
{"sync", sync_func, METH_O, ""},
|
2012-11-09 08:46:53 +00:00
|
|
|
{"reset", reset_func, METH_VARARGS, ""},
|
2012-11-03 14:32:35 +00:00
|
|
|
#ifdef WITH_OSL
|
|
|
|
{"osl_update_node", osl_update_node_func, METH_VARARGS, ""},
|
|
|
|
{"osl_compile", osl_compile_func, METH_VARARGS, ""},
|
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
{"available_devices", available_devices_func, METH_NOARGS, ""},
|
2011-05-31 16:19:02 +00:00
|
|
|
{NULL, NULL, 0, NULL},
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct PyModuleDef module = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
2011-12-24 02:47:13 +00:00
|
|
|
"_cycles",
|
2011-11-07 17:13:32 +00:00
|
|
|
"Blender cycles render integration",
|
2011-04-27 11:58:34 +00:00
|
|
|
-1,
|
2011-05-31 16:19:02 +00:00
|
|
|
methods,
|
|
|
|
NULL, NULL, NULL, NULL
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
2012-09-16 00:26:36 +00:00
|
|
|
static CCLDeviceInfo *compute_device_list(DeviceType type)
|
2012-01-09 16:58:01 +00:00
|
|
|
{
|
|
|
|
/* device list stored static */
|
|
|
|
static ccl::vector<CCLDeviceInfo> device_list;
|
|
|
|
static ccl::DeviceType device_type = DEVICE_NONE;
|
|
|
|
|
|
|
|
/* create device list if it's not already done */
|
|
|
|
if(type != device_type) {
|
|
|
|
ccl::vector<DeviceInfo>& devices = ccl::Device::available_devices();
|
|
|
|
|
|
|
|
device_type = type;
|
|
|
|
device_list.clear();
|
|
|
|
|
|
|
|
/* add devices */
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
foreach(DeviceInfo& info, devices) {
|
|
|
|
if(info.type == type ||
|
2012-06-09 18:56:12 +00:00
|
|
|
(info.type == DEVICE_MULTI && info.multi_devices[0].type == type))
|
|
|
|
{
|
2012-11-10 08:37:02 +00:00
|
|
|
CCLDeviceInfo cinfo;
|
|
|
|
|
|
|
|
strncpy(cinfo.identifier, info.id.c_str(), sizeof(cinfo.identifier));
|
|
|
|
cinfo.identifier[info.id.length()] = '\0';
|
|
|
|
|
|
|
|
strncpy(cinfo.name, info.description.c_str(), sizeof(cinfo.name));
|
|
|
|
cinfo.name[info.description.length()] = '\0';
|
|
|
|
|
|
|
|
cinfo.value = i++;
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
device_list.push_back(cinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* null terminate */
|
|
|
|
if(!device_list.empty()) {
|
2012-11-10 08:37:02 +00:00
|
|
|
CCLDeviceInfo cinfo = {"", "", 0};
|
2012-01-09 16:58:01 +00:00
|
|
|
device_list.push_back(cinfo);
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
return (device_list.empty())? NULL: &device_list[0];
|
|
|
|
}
|
2011-08-16 16:15:34 +00:00
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
void *CCL_python_module_init()
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-06-09 18:56:12 +00:00
|
|
|
PyObject *mod = PyModule_Create(&ccl::module);
|
2011-12-24 02:47:13 +00:00
|
|
|
|
|
|
|
#ifdef WITH_OSL
|
2013-12-08 15:03:17 +06:00
|
|
|
/* TODO(sergey): This gives us library we've been linking against.
|
|
|
|
* In theory with dynamic OSL library it might not be
|
|
|
|
* accurate, but there's nothing in OSL API which we
|
2014-03-01 01:21:25 +01:00
|
|
|
* might use to get version in runtime.
|
2013-12-08 15:03:17 +06:00
|
|
|
*/
|
|
|
|
int curversion = OSL_LIBRARY_VERSION_CODE;
|
2011-12-24 02:47:13 +00:00
|
|
|
PyModule_AddObject(mod, "with_osl", Py_True);
|
|
|
|
Py_INCREF(Py_True);
|
2013-12-08 15:03:17 +06:00
|
|
|
PyModule_AddObject(mod, "osl_version",
|
|
|
|
Py_BuildValue("(iii)",
|
|
|
|
curversion / 10000, (curversion / 100) % 100, curversion % 100));
|
|
|
|
PyModule_AddObject(mod, "osl_version_string",
|
|
|
|
PyUnicode_FromFormat("%2d, %2d, %2d",
|
|
|
|
curversion / 10000, (curversion / 100) % 100, curversion % 100));
|
2011-12-24 02:47:13 +00:00
|
|
|
#else
|
|
|
|
PyModule_AddObject(mod, "with_osl", Py_False);
|
|
|
|
Py_INCREF(Py_False);
|
2013-12-08 15:19:21 +06:00
|
|
|
PyModule_AddStringConstant(mod, "osl_version", "unknown");
|
|
|
|
PyModule_AddStringConstant(mod, "osl_version_string", "unknown");
|
2011-12-24 02:47:13 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-07 02:29:53 +01:00
|
|
|
#ifdef WITH_NETWORK
|
|
|
|
PyModule_AddObject(mod, "with_network", Py_True);
|
|
|
|
Py_INCREF(Py_True);
|
|
|
|
#else /* WITH_NETWORK */
|
|
|
|
PyModule_AddObject(mod, "with_network", Py_False);
|
|
|
|
Py_INCREF(Py_False);
|
|
|
|
#endif /* WITH_NETWORK */
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
return (void*)mod;
|
|
|
|
}
|
|
|
|
|
2013-12-07 02:29:53 +01:00
|
|
|
CCLDeviceInfo *CCL_compute_device_list(int device_type)
|
2012-01-09 16:58:01 +00:00
|
|
|
{
|
2013-12-07 02:29:53 +01:00
|
|
|
ccl::DeviceType type;
|
|
|
|
switch(device_type) {
|
|
|
|
case 0:
|
|
|
|
type = ccl::DEVICE_CUDA;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
type = ccl::DEVICE_OPENCL;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
type = ccl::DEVICE_NETWORK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
type = ccl::DEVICE_NONE;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-09 16:58:01 +00:00
|
|
|
return ccl::compute_device_list(type);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|