/
pyluxcore.cpp
1726 lines (1449 loc) · 65.2 KB
/
pyluxcore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright 1998-2018 by authors (see AUTHORS.txt) *
* *
* This file is part of LuxCoreRender. *
* *
* 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 *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* 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. *
***************************************************************************/
#ifdef WIN32
// Windows seems to require this #define otherwise VisualC++ looks for
// Boost Python DLL symbols.
// Do not use for Unix(s), it makes some symbol local.
#define BOOST_PYTHON_STATIC_LIB
#endif
#define BOOST_PYTHON_MAX_ARITY 18
#include <memory>
#include <boost/foreach.hpp>
#include <boost/python.hpp>
#include <boost/python/call.hpp>
#include <Python.h>
#include "luxrays/luxrays.h"
#include "luxcore/luxcore.h"
#include "luxcore/luxcoreimpl.h"
#include "luxcore/pyluxcore/pyluxcoreforblender.h"
using namespace std;
using namespace luxcore;
using namespace boost::python;
namespace luxcore {
//------------------------------------------------------------------------------
// Utility functions
//------------------------------------------------------------------------------
// From https://stackoverflow.com/questions/35957073/boost-python-returning-tuple-containing-custom-types
template <typename T> boost::python::object TransferToPython(T *t) {
// Transfer ownership to a smart pointer, allowing for proper cleanup
// incase Boost.Python throws.
auto_ptr<T> ptr(t);
// Use the manage_new_object generator to transfer ownership to Python.
typename boost::python::manage_new_object::apply<T *>::type converter;
// Transfer ownership to the Python handler and release ownership
// from C++.
boost::python::handle<> handle(converter(*ptr));
ptr.release();
return boost::python::object(handle);
}
template<typename T> void GetArray(const boost::python::object &obj, vector<T> &a,
const u_int width = 1, const u_int stride = 0) {
a.clear();
if (!obj.is_none()) {
//----------------------------------------------------------------------
// Try if it is a list
//----------------------------------------------------------------------
extract<boost::python::list> arrayListExtract(obj);
if (arrayListExtract.check()) {
const boost::python::list &arrayList = arrayListExtract();
const boost::python::ssize_t size = len(arrayList);
if (size % (width + stride) != 0)
throw runtime_error("Wrong data size in GetArray() method: " + size);
if (stride == 0) {
a.reserve(size);
// A fast path for stride = 0
for (u_int i = 0; i < size; ++i)
a.push_back(extract<T>(arrayList[i]));
} else {
a.reserve((size / (width + stride))* width);
for (u_int i = 0; i < size; i += width + stride) {
for (u_int j = 0; j < width; ++j)
a.push_back(extract<T>(arrayList[i + j]));
}
}
} else
//----------------------------------------------------------------------
// Try if it is a buffer
//----------------------------------------------------------------------
if (PyObject_CheckBuffer(obj.ptr())) {
Py_buffer view;
if (!PyObject_GetBuffer(obj.ptr(), &view, PyBUF_SIMPLE)) {
size_t size = view.len / sizeof(T);
if (size % (width + stride) != 0)
throw runtime_error("Wrong data size in GetArray() method: " + size);
T *buffer = (T *)view.buf;
if (stride == 0) {
a.resize(size);
// A fast path for stride = 0
copy(buffer, buffer + size, a.begin());
} else {
a.reserve((size / (width + stride))* width);
for (u_int i = 0; i < size; i += width + stride) {
for (u_int j = 0; j < width; ++j)
a.push_back(buffer[i + j]);
}
}
PyBuffer_Release(&view);
} else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Unable to get a data view in GetArray() method: " + objType);
}
}
//----------------------------------------------------------------------
// Unsupported type
//----------------------------------------------------------------------
else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type for the list of values of method GetArray(): " + objType);
}
} else
throw runtime_error("None object in GetArray()");
}
static void GetMatrix4x4(const boost::python::object &obj, float mat[16]) {
if (!obj.is_none()) {
extract<boost::python::list> matListExtract(obj);
if (matListExtract.check()) {
const boost::python::list &matList = matListExtract();
const boost::python::ssize_t size = len(matList);
if (size != 16) {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong number of elements for the list of values of method GetMatrix4x4(): " + objType);
}
for (u_int i = 0; i < 16; ++i)
mat[i] = extract<float>(matList[i]);
} else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type for the list of values of method GetMatrix4x4(): " + objType);
}
} else
throw runtime_error("None transformation in GetMatrix4x4()");
}
//------------------------------------------------------------------------------
// Module functions
//------------------------------------------------------------------------------
static boost::mutex luxCoreInitMutex;
static boost::python::object luxCoreLogHandler;
#if (PY_VERSION_HEX >= 0x03040000)
static void PythonDebugHandler(const char *msg) {
// PyGILState_Check() is available since Python 3.4
if (PyGILState_Check())
luxCoreLogHandler(string(msg));
else {
// The following code is supposed to work ... but it doesn't (it never
// returns). So I'm just avoiding to call Python without the GIL and
// I silently discard the message.
//PyGILState_STATE state = PyGILState_Ensure();
//luxCoreLogHandler(string(msg));
//PyGILState_Release(state);
}
}
#else
static int PyGILState_Check2(void) {
PyThreadState *tstate = _PyThreadState_Current;
return tstate && (tstate == PyGILState_GetThisThreadState());
}
static void PythonDebugHandler(const char *msg) {
if (PyGILState_Check2())
luxCoreLogHandler(string(msg));
}
#endif
static void LuxCore_Init() {
boost::unique_lock<boost::mutex> lock(luxCoreInitMutex);
Init();
}
static void LuxCore_InitDefaultHandler(boost::python::object &logHandler) {
boost::unique_lock<boost::mutex> lock(luxCoreInitMutex);
// I wonder if I should increase the reference count for Python
luxCoreLogHandler = logHandler;
Init(&PythonDebugHandler);
}
static void LuxCore_SetLogHandler(boost::python::object &logHandler) {
luxCoreLogHandler = logHandler;
if (logHandler.is_none())
SetLogHandler(NULL);
}
static const char *LuxCoreVersion() {
static const char *luxCoreVersion = LUXCORE_VERSION_MAJOR "." LUXCORE_VERSION_MINOR;
return luxCoreVersion;
}
static boost::python::list GetOpenCLDeviceList() {
luxrays::Context ctx;
vector<luxrays::DeviceDescription *> deviceDescriptions = ctx.GetAvailableDeviceDescriptions();
// Select only OpenCL devices
luxrays::DeviceDescription::Filter(luxrays::DEVICE_TYPE_OPENCL_ALL, deviceDescriptions);
// Add all device information to the list
boost::python::list l;
for (size_t i = 0; i < deviceDescriptions.size(); ++i) {
luxrays::DeviceDescription *desc = deviceDescriptions[i];
l.append(boost::python::make_tuple(
desc->GetName(),
luxrays::DeviceDescription::GetDeviceType(desc->GetType()),
desc->GetComputeUnits(),
desc->GetNativeVectorWidthFloat(),
desc->GetMaxMemory(),
desc->GetMaxMemoryAllocSize()));
}
return l;
}
//------------------------------------------------------------------------------
// Glue for Property class
//------------------------------------------------------------------------------
static boost::python::list Property_GetBlobByIndex(luxrays::Property *prop, const u_int i) {
const luxrays::Blob &blob = prop->Get<const luxrays::Blob &>(i);
const char *data = blob.GetData();
const size_t size = blob.GetSize();
boost::python::list l;
for (size_t i = 0; i < size; ++i)
l.append((int)data[i]);
return l;
}
static boost::python::list Property_Get(luxrays::Property *prop) {
boost::python::list l;
for (u_int i = 0; i < prop->GetSize(); ++i) {
const luxrays::PropertyValue::DataType dataType = prop->GetValueType(i);
switch (dataType) {
case luxrays::PropertyValue::BOOL_VAL:
l.append(prop->Get<bool>(i));
break;
case luxrays::PropertyValue::INT_VAL:
l.append(prop->Get<int>(i));
break;
case luxrays::PropertyValue::DOUBLE_VAL:
l.append(prop->Get<double>(i));
break;
case luxrays::PropertyValue::STRING_VAL:
l.append(prop->Get<string>(i));
break;
case luxrays::PropertyValue::BLOB_VAL:
l.append(Property_GetBlobByIndex(prop, i));
break;
default:
throw runtime_error("Unsupported data type in list extraction of a Property: " + prop->GetName());
}
}
return l;
}
static boost::python::list Property_GetBools(luxrays::Property *prop) {
boost::python::list l;
for (u_int i = 0; i < prop->GetSize(); ++i)
l.append(prop->Get<bool>(i));
return l;
}
static boost::python::list Property_GetInts(luxrays::Property *prop) {
boost::python::list l;
for (u_int i = 0; i < prop->GetSize(); ++i)
l.append(prop->Get<int>(i));
return l;
}
static boost::python::list Property_GetFloats(luxrays::Property *prop) {
boost::python::list l;
for (u_int i = 0; i < prop->GetSize(); ++i)
l.append(prop->Get<double>(i));
return l;
}
static boost::python::list Property_GetStrings(luxrays::Property *prop) {
boost::python::list l;
for (u_int i = 0; i < prop->GetSize(); ++i)
l.append(prop->Get<string>(i));
return l;
}
static boost::python::list Property_GetBlobs(luxrays::Property *prop) {
boost::python::list l;
for (u_int i = 0; i < prop->GetSize(); ++i)
l.append(Property_GetBlobByIndex(prop, i));
return l;
}
static bool Property_GetBool(luxrays::Property *prop) {
return prop->Get<bool>(0);
}
static int Property_GetInt(luxrays::Property *prop) {
return prop->Get<int>(0);
}
static unsigned long long Property_GetUnsignedLongLong(luxrays::Property *prop) {
return prop->Get<unsigned long long>(0);
}
static double Property_GetFloat(luxrays::Property *prop) {
return prop->Get<double>(0);
}
static string Property_GetString(luxrays::Property *prop) {
return prop->Get<string>(0);
}
static boost::python::list Property_GetBlob(luxrays::Property *prop) {
return Property_GetBlobByIndex(prop, 0);
}
static luxrays::Property &Property_Add(luxrays::Property *prop, const boost::python::list &l) {
const boost::python::ssize_t size = len(l);
for (boost::python::ssize_t i = 0; i < size; ++i) {
const string objType = extract<string>((l[i].attr("__class__")).attr("__name__"));
const boost::python::object obj = l[i];
if (objType == "bool") {
const bool v = extract<bool>(obj);
prop->Add(v);
} else if (objType == "int") {
const int v = extract<int>(obj);
prop->Add(v);
} else if (objType == "float") {
const double v = extract<double>(obj);
prop->Add(v);
} else if (objType == "str") {
const string v = extract<string>(obj);
prop->Add(v);
} else if (objType == "list") {
const boost::python::list ol = extract<boost::python::list>(obj);
const boost::python::ssize_t os = len(ol);
vector<char> data(os);
for (boost::python::ssize_t i = 0; i < os; ++i)
data[i] = extract<int>(ol[i]);
prop->Add(luxrays::Blob(&data[0], os));
} else if (PyObject_CheckBuffer(obj.ptr())) {
Py_buffer view;
if (!PyObject_GetBuffer(obj.ptr(), &view, PyBUF_SIMPLE)) {
const char *buffer = (char *)view.buf;
const size_t size = (size_t)view.len;
luxrays::Blob blob(buffer, size);
prop->Add(blob);
PyBuffer_Release(&view);
} else
throw runtime_error("Unable to get a data view in Property.Add() method: " + objType);
} else
throw runtime_error("Unsupported data type included in Property.Add() method list: " + objType);
}
return *prop;
}
static luxrays::Property &Property_AddAllBool(luxrays::Property *prop,
const boost::python::object &obj) {
vector<bool> v;
GetArray<bool>(obj, v);
for (auto e : v)
prop->Add<bool>(e);
return *prop;
}
static luxrays::Property &Property_AddAllInt(luxrays::Property *prop,
const boost::python::object &obj) {
vector<int> v;
GetArray<int>(obj, v);
for (auto e : v)
prop->Add<int>(e);
return *prop;
}
static luxrays::Property &Property_AddAllUnsignedLongLong(luxrays::Property *prop,
const boost::python::object &obj) {
vector<unsigned long long> v;
GetArray<unsigned long long>(obj, v);
for (auto e : v)
prop->Add<unsigned long long>(e);
return *prop;
}
static luxrays::Property &Property_AddAllFloat(luxrays::Property *prop,
const boost::python::object &obj) {
vector<float> v;
GetArray<float>(obj, v);
for (auto e : v)
prop->Add<float>(e);
return *prop;
}
static luxrays::Property &Property_AddAllBoolStride(luxrays::Property *prop,
const boost::python::object &obj, const u_int width, const u_int stride) {
vector<bool> v;
GetArray<bool>(obj, v, width, stride);
for (auto e : v)
prop->Add<bool>(e);
return *prop;
}
static luxrays::Property &Property_AddAllIntStride(luxrays::Property *prop,
const boost::python::object &obj, const u_int width, const u_int stride) {
vector<int> v;
GetArray<int>(obj, v, width, stride);
for (auto e : v)
prop->Add<int>(e);
return *prop;
}
static luxrays::Property &Property_AddAllUnsignedLongLongStride(luxrays::Property *prop,
const boost::python::object &obj, const u_int width, const u_int stride) {
vector<unsigned long long> v;
GetArray<unsigned long long>(obj, v, width, stride);
for (auto e : v)
prop->Add<unsigned long long>(e);
return *prop;
}
static luxrays::Property &Property_AddAllFloatStride(luxrays::Property *prop,
const boost::python::object &obj, const u_int width, const u_int stride) {
vector<float> v;
GetArray<float>(obj, v, width, stride);
for (auto e : v)
prop->Add<float>(e);
return *prop;
}
static luxrays::Property &Property_Set(luxrays::Property *prop, const u_int i,
const boost::python::object &obj) {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
if (objType == "bool") {
const bool v = extract<bool>(obj);
prop->Set(i, v);
} else if (objType == "int") {
const int v = extract<int>(obj);
prop->Set(i, v);
} else if (objType == "float") {
const double v = extract<double>(obj);
prop->Set(i, v);
} else if (objType == "str") {
const string v = extract<string>(obj);
prop->Set(i, v);
} else if (objType == "list") {
const boost::python::list ol = extract<boost::python::list>(obj);
const boost::python::ssize_t os = len(ol);
vector<char> data(os);
for (boost::python::ssize_t i = 0; i < os; ++i)
data[i] = extract<int>(ol[i]);
prop->Set(i, luxrays::Blob(&data[0], os));
} else if (PyObject_CheckBuffer(obj.ptr())) {
Py_buffer view;
if (!PyObject_GetBuffer(obj.ptr(), &view, PyBUF_SIMPLE)) {
const char *buffer = (char *)view.buf;
const size_t size = (size_t)view.len;
luxrays::Blob blob(buffer, size);
prop->Set(i, blob);
PyBuffer_Release(&view);
} else
throw runtime_error("Unable to get a data view in Property.Add() method: " + objType);
} else
throw runtime_error("Unsupported data type used for Property.Set() method: " + objType);
return *prop;
}
static luxrays::Property &Property_Set(luxrays::Property *prop, const boost::python::list &l) {
const boost::python::ssize_t size = len(l);
for (boost::python::ssize_t i = 0; i < size; ++i) {
const boost::python::object obj = l[i];
Property_Set(prop, i, obj);
}
return *prop;
}
static luxrays::Property *Property_InitWithList(const str &name, const boost::python::list &l) {
luxrays::Property *prop = new luxrays::Property(extract<string>(name));
Property_Add(prop, l);
return prop;
}
//------------------------------------------------------------------------------
// Glue for Properties class
//------------------------------------------------------------------------------
static boost::python::list Properties_GetAllNamesRE(luxrays::Properties *props, const string &pattern) {
boost::python::list l;
const vector<string> &keys = props->GetAllNamesRE(pattern);
BOOST_FOREACH(const string &key, keys) {
l.append(key);
}
return l;
}
static boost::python::list Properties_GetAllNames1(luxrays::Properties *props) {
boost::python::list l;
const vector<string> &keys = props->GetAllNames();
BOOST_FOREACH(const string &key, keys) {
l.append(key);
}
return l;
}
static boost::python::list Properties_GetAllNames2(luxrays::Properties *props, const string &prefix) {
boost::python::list l;
const vector<string> keys = props->GetAllNames(prefix);
BOOST_FOREACH(const string &key, keys) {
l.append(key);
}
return l;
}
static boost::python::list Properties_GetAllUniqueSubNames(luxrays::Properties *props, const string &prefix) {
boost::python::list l;
const vector<string> keys = props->GetAllUniqueSubNames(prefix);
BOOST_FOREACH(const string &key, keys) {
l.append(key);
}
return l;
}
static luxrays::Property Properties_GetWithDefaultValues(luxrays::Properties *props,
const string &name, const boost::python::list &l) {
luxrays::PropertyValues values;
const boost::python::ssize_t size = len(l);
for (boost::python::ssize_t i = 0; i < size; ++i) {
const string objType = extract<string>((l[i].attr("__class__")).attr("__name__"));
if (objType == "bool") {
const bool v = extract<bool>(l[i]);
values.push_back(v);
} else if (objType == "int") {
const int v = extract<int>(l[i]);
values.push_back(v);
} else if (objType == "float") {
const double v = extract<double>(l[i]);
values.push_back(v);
} else if (objType == "str") {
const string v = extract<string>(l[i]);
values.push_back(v);
} else
throw runtime_error("Unsupported data type included in Properties Get with default method: " + objType);
}
return luxrays::Property(name, values);
}
void Properties_DeleteAll(luxrays::Properties *props, const boost::python::list &l) {
const boost::python::ssize_t size = len(l);
for (boost::python::ssize_t i = 0; i < size; ++i) {
const string objType = extract<string>((l[i].attr("__class__")).attr("__name__"));
if (objType == "str")
props->Delete(extract<string>(l[i]));
else
throw runtime_error("Unsupported data type included in Properties.DeleteAll() list: " + objType);
}
}
//------------------------------------------------------------------------------
// Glue for Film class
//------------------------------------------------------------------------------
// Blender bgl.Buffer definition
typedef struct {
PyObject_VAR_HEAD
PyObject *parent;
int type; /* GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT */
int ndimensions;
int *dimensions;
union {
char *asbyte;
short *asshort;
int *asint;
float *asfloat;
double *asdouble;
void *asvoid;
} buf;
} BGLBuffer;
static void Film_GetOutputFloat1(luxcore::detail::FilmImpl *film, const Film::FilmOutputType type,
boost::python::object &obj, const u_int index) {
const size_t outputSize = film->GetOutputSize(type) * sizeof(float);
if (PyObject_CheckBuffer(obj.ptr())) {
Py_buffer view;
if (!PyObject_GetBuffer(obj.ptr(), &view, PyBUF_SIMPLE)) {
if ((size_t)view.len >= outputSize) {
if(!film->HasOutput(type)) {
const string errorMsg = "Film Output not available: " + luxrays::ToString(type);
PyBuffer_Release(&view);
throw runtime_error(errorMsg);
}
float *buffer = (float *)view.buf;
film->GetOutput<float>(type, buffer, index);
PyBuffer_Release(&view);
} else {
const string errorMsg = "Not enough space in the buffer of Film.GetOutputFloat() method: " +
luxrays::ToString(view.len) + " instead of " + luxrays::ToString(outputSize);
PyBuffer_Release(&view);
throw runtime_error(errorMsg);
}
} else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Unable to get a data view in Film.GetOutputFloat() method: " + objType);
}
} else {
const PyObject *pyObj = obj.ptr();
const PyTypeObject *pyTypeObj = Py_TYPE(pyObj);
// Check if it is a Blender bgl.Buffer object
if (strcmp(pyTypeObj->tp_name, "bgl.Buffer") == 0) {
// This is a special path for optimizing Blender preview
const BGLBuffer *bglBuffer = (BGLBuffer *)pyObj;
// A safety check for buffer type and size
// 0x1406 is the value of GL_FLOAT
if (bglBuffer->type == 0x1406) {
if (bglBuffer->ndimensions == 1) {
if (bglBuffer->dimensions[0] * sizeof(float) >= outputSize) {
if(!film->HasOutput(type)) {
throw runtime_error("Film Output not available: " + luxrays::ToString(type));
}
film->GetOutput<float>(type, bglBuffer->buf.asfloat, index);
} else
throw runtime_error("Not enough space in the Blender bgl.Buffer of Film.GetOutputFloat() method: " +
luxrays::ToString(bglBuffer->dimensions[0] * sizeof(float)) + " instead of " + luxrays::ToString(outputSize));
} else
throw runtime_error("A Blender bgl.Buffer has the wrong dimension in Film.GetOutputFloat(): " + luxrays::ToString(bglBuffer->ndimensions));
} else
throw runtime_error("A Blender bgl.Buffer has the wrong type in Film.GetOutputFloat(): " + luxrays::ToString(bglBuffer->type));
} else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Unsupported data type in Film.GetOutputFloat(): " + objType);
}
}
}
static void Film_GetOutputFloat2(luxcore::detail::FilmImpl *film, const Film::FilmOutputType type,
boost::python::object &obj) {
Film_GetOutputFloat1(film, type, obj, 0);
}
static void Film_GetOutputUInt1(luxcore::detail::FilmImpl *film, const Film::FilmOutputType type,
boost::python::object &obj, const u_int index) {
if (PyObject_CheckBuffer(obj.ptr())) {
Py_buffer view;
if (!PyObject_GetBuffer(obj.ptr(), &view, PyBUF_SIMPLE)) {
if ((size_t)view.len >= film->GetOutputSize(type) * sizeof(u_int)) {
if(!film->HasOutput(type)) {
const string errorMsg = "Film Output not available: " + luxrays::ToString(type);
PyBuffer_Release(&view);
throw runtime_error(errorMsg);
}
u_int *buffer = (u_int *)view.buf;
film->GetOutput<unsigned int>(type, buffer, index);
PyBuffer_Release(&view);
} else {
const string errorMsg = "Not enough space in the buffer of Film.GetOutputUInt() method: " +
luxrays::ToString(view.len) + " instead of " + luxrays::ToString(film->GetOutputSize(type) * sizeof(u_int));
PyBuffer_Release(&view);
throw runtime_error(errorMsg);
}
} else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Unable to get a data view in Film.GetOutputUInt() method: " + objType);
}
} else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Unsupported data type in Film.GetOutputUInt() method: " + objType);
}
}
static void Film_GetOutputUInt2(luxcore::detail::FilmImpl *film, const Film::FilmOutputType type,
boost::python::object &obj) {
Film_GetOutputUInt1(film, type, obj, 0);
}
static void Film_AddFilm1(luxcore::detail::FilmImpl *film, luxcore::detail::FilmImpl *srcFilm) {
film->AddFilm(*srcFilm);
}
static void Film_AddFilm2(luxcore::detail::FilmImpl *film, luxcore::detail::FilmImpl *srcFilm,
const u_int srcOffsetX, const u_int srcOffsetY,
const u_int srcWidth, const u_int srcHeight,
const u_int dstOffsetX, const u_int dstOffsetY) {
film->AddFilm(*srcFilm, srcOffsetX, srcOffsetY, srcWidth, srcHeight, dstOffsetX, dstOffsetY);
}
static float Film_GetFilmY1(luxcore::detail::FilmImpl *film) {
return film->GetFilmY();
}
static float Film_GetFilmY2(luxcore::detail::FilmImpl *film, const u_int imagePipelineIndex) {
return film->GetFilmY(imagePipelineIndex);
}
//------------------------------------------------------------------------------
// Glue for Camera class
//------------------------------------------------------------------------------
static void Camera_Translate(luxcore::detail::CameraImpl *camera, const boost::python::tuple t) {
camera->Translate(extract<float>(t[0]), extract<float>(t[1]), extract<float>(t[2]));
}
static void Camera_Rotate(luxcore::detail::CameraImpl *camera, const float angle, const boost::python::tuple axis) {
camera->Rotate(angle, extract<float>(axis[0]), extract<float>(axis[1]), extract<float>(axis[2]));
}
//------------------------------------------------------------------------------
// Glue for Scene class
//------------------------------------------------------------------------------
static luxcore::detail::CameraImpl &Scene_GetCamera(luxcore::detail::SceneImpl *scene) {
return (luxcore::detail::CameraImpl &)scene->GetCamera();
}
static void Scene_DefineImageMap(luxcore::detail::SceneImpl *scene, const string &imgMapName,
boost::python::object &obj, const float gamma,
const u_int channels, const u_int width, const u_int height) {
if (PyObject_CheckBuffer(obj.ptr())) {
Py_buffer view;
if (!PyObject_GetBuffer(obj.ptr(), &view, PyBUF_SIMPLE)) {
if ((size_t)view.len >= width * height * channels * sizeof(float)) {
float *buffer = (float *)view.buf;
scene->DefineImageMap(imgMapName, buffer, gamma, channels,
width, height, Scene::DEFAULT, Scene::REPEAT);
PyBuffer_Release(&view);
} else {
const string errorMsg = "Not enough space in the buffer of Scene.DefineImageMap() method: " +
luxrays::ToString(view.len) + " instead of " + luxrays::ToString(width * height * channels * sizeof(float));
PyBuffer_Release(&view);
throw runtime_error(errorMsg);
}
} else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Unable to get a data view in Scene.DefineImageMap() method: " + objType);
}
} else {
const string objType = extract<string>((obj.attr("__class__")).attr("__name__"));
throw runtime_error("Unsupported data type Scene.DefineImageMap() method: " + objType);
}
}
static void Scene_DefineMesh1(luxcore::detail::SceneImpl *scene, const string &meshName,
const boost::python::object &p, const boost::python::object &vi,
const boost::python::object &n, const boost::python::object &uv,
const boost::python::object &cols, const boost::python::object &alphas,
const boost::python::object &transformation) {
// NOTE: I would like to use boost::scoped_array but
// some guy has decided that boost::scoped_array must not have
// a release() method for some ideological reason...
// Translate all vertices
long plyNbVerts;
luxrays::Point *points = NULL;
extract<boost::python::list> getPList(p);
if (getPList.check()) {
const boost::python::list &l = getPList();
const boost::python::ssize_t size = len(l);
plyNbVerts = size;
points = (luxrays::Point *)luxcore::detail::SceneImpl::AllocVerticesBuffer(size);
for (boost::python::ssize_t i = 0; i < size; ++i) {
extract<boost::python::tuple> getTuple(l[i]);
if (getTuple.check()) {
const boost::python::tuple &t = getTuple();
points[i] = luxrays::Point(extract<float>(t[0]), extract<float>(t[1]), extract<float>(t[2]));
} else {
const string objType = extract<string>((l[i].attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type in the list of vertices of method Scene.DefineMesh() at position " + luxrays::ToString(i) +": " + objType);
}
}
} else {
const string objType = extract<string>((p.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type for the list of vertices of method Scene.DefineMesh(): " + objType);
}
// Translate all triangles
long plyNbTris;
luxrays::Triangle *tris = NULL;
extract<boost::python::list> getVIList(vi);
if (getVIList.check()) {
const boost::python::list &l = getVIList();
const boost::python::ssize_t size = len(l);
plyNbTris = size;
tris = (luxrays::Triangle *)luxcore::detail::SceneImpl::AllocTrianglesBuffer(size);
for (boost::python::ssize_t i = 0; i < size; ++i) {
extract<boost::python::tuple> getTuple(l[i]);
if (getTuple.check()) {
const boost::python::tuple &t = getTuple();
tris[i] = luxrays::Triangle(extract<u_int>(t[0]), extract<u_int>(t[1]), extract<u_int>(t[2]));
} else {
const string objType = extract<string>((l[i].attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type in the list of triangles of method Scene.DefineMesh() at position " + luxrays::ToString(i) +": " + objType);
}
}
} else {
const string objType = extract<string>((vi.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type for the list of triangles of method Scene.DefineMesh(): " + objType);
}
// Translate all normals
luxrays::Normal *normals = NULL;
if (!n.is_none()) {
extract<boost::python::list> getNList(n);
if (getNList.check()) {
const boost::python::list &l = getNList();
const boost::python::ssize_t size = len(l);
normals = new luxrays::Normal[size];
for (boost::python::ssize_t i = 0; i < size; ++i) {
extract<boost::python::tuple> getTuple(l[i]);
if (getTuple.check()) {
const boost::python::tuple &t = getTuple();
normals[i] = luxrays::Normal(extract<float>(t[0]), extract<float>(t[1]), extract<float>(t[2]));
} else {
const string objType = extract<string>((l[i].attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type in the list of triangles of method Scene.DefineMesh() at position " + luxrays::ToString(i) +": " + objType);
}
}
} else {
const string objType = extract<string>((n.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type for the list of triangles of method Scene.DefineMesh(): " + objType);
}
}
// Translate all UVs
luxrays::UV *uvs = NULL;
if (!uv.is_none()) {
extract<boost::python::list> getUVList(uv);
if (getUVList.check()) {
const boost::python::list &l = getUVList();
const boost::python::ssize_t size = len(l);
uvs = new luxrays::UV[size];
for (boost::python::ssize_t i = 0; i < size; ++i) {
extract<boost::python::tuple> getTuple(l[i]);
if (getTuple.check()) {
const boost::python::tuple &t = getTuple();
uvs[i] = luxrays::UV(extract<float>(t[0]), extract<float>(t[1]));
} else {
const string objType = extract<string>((l[i].attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type in the list of UVs of method Scene.DefineMesh() at position " + luxrays::ToString(i) +": " + objType);
}
}
} else {
const string objType = extract<string>((uv.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type for the list of UVs of method Scene.DefineMesh(): " + objType);
}
}
// Translate all colors
luxrays::Spectrum *colors = NULL;
if (!cols.is_none()) {
extract<boost::python::list> getColList(cols);
if (getColList.check()) {
const boost::python::list &l = getColList();
const boost::python::ssize_t size = len(l);
colors = new luxrays::Spectrum[size];
for (boost::python::ssize_t i = 0; i < size; ++i) {
extract<boost::python::tuple> getTuple(l[i]);
if (getTuple.check()) {
const boost::python::tuple &t = getTuple();
colors[i] = luxrays::Spectrum(extract<float>(t[0]), extract<float>(t[1]), extract<float>(t[2]));
} else {
const string objType = extract<string>((l[i].attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type in the list of colors of method Scene.DefineMesh() at position " + luxrays::ToString(i) +": " + objType);
}
}
} else {
const string objType = extract<string>((cols.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type for the list of colors of method Scene.DefineMesh(): " + objType);
}
}
// Translate all alphas
float *as = NULL;
if (!alphas.is_none()) {
extract<boost::python::list> getAlphaList(alphas);
if (getAlphaList.check()) {
const boost::python::list &l = getAlphaList();
const boost::python::ssize_t size = len(l);
as = new float[size];
for (boost::python::ssize_t i = 0; i < size; ++i)
as[i] = extract<float>(l[i]);
} else {
const string objType = extract<string>((alphas.attr("__class__")).attr("__name__"));
throw runtime_error("Wrong data type for the list of alphas of method Scene.DefineMesh(): " + objType);
}
}
luxrays::ExtTriangleMesh *mesh = new luxrays::ExtTriangleMesh(plyNbVerts, plyNbTris, points, tris, normals, uvs, colors, as);
// Apply the transformation if required
if (!transformation.is_none()) {
float mat[16];
GetMatrix4x4(transformation, mat);
mesh->ApplyTransform(luxrays::Transform(luxrays::Matrix4x4(mat).Transpose()));
}
mesh->SetName(meshName);
scene->DefineMesh(mesh);
}
static void Scene_DefineMesh2(luxcore::detail::SceneImpl *scene, const string &meshName,
const boost::python::object &p, const boost::python::object &vi,
const boost::python::object &n, const boost::python::object &uv,
const boost::python::object &cols, const boost::python::object &alphas) {