Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abc_object.cc « intern « alembic « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b169988096617e51c28fa12b300ad54afb4917d (plain)
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
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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.
 *
 * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "abc_object.h"

#include "abc_util.h"

extern "C" {
#include "DNA_cachefile_types.h"
#include "DNA_constraint_types.h"
#include "DNA_modifier_types.h"
#include "DNA_object_types.h"
#include "DNA_space_types.h"  /* for FILE_MAX */

#include "BKE_constraint.h"
#include "BKE_idprop.h"
#include "BKE_library.h"
#include "BKE_modifier.h"
#include "BKE_object.h"

#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_string.h"
}

using Alembic::AbcGeom::IObject;
using Alembic::AbcGeom::IXform;
using Alembic::AbcGeom::IXformSchema;

using Alembic::AbcGeom::OCompoundProperty;
using Alembic::AbcGeom::ODoubleArrayProperty;
using Alembic::AbcGeom::ODoubleProperty;
using Alembic::AbcGeom::OFloatArrayProperty;
using Alembic::AbcGeom::OFloatProperty;
using Alembic::AbcGeom::OInt32ArrayProperty;
using Alembic::AbcGeom::OInt32Property;
using Alembic::AbcGeom::OStringArrayProperty;
using Alembic::AbcGeom::OStringProperty;

/* ************************************************************************** */

AbcObjectWriter::AbcObjectWriter(Scene *scene,
                                 Object *ob,
                                 uint32_t time_sampling,
                                 ExportSettings &settings,
                                 AbcObjectWriter *parent)
    : m_object(ob)
    , m_settings(settings)
    , m_scene(scene)
    , m_time_sampling(time_sampling)
    , m_first_frame(true)
{
	m_name = get_id_name(m_object) + "Shape";

	if (parent) {
		parent->addChild(this);
	}
}

AbcObjectWriter::~AbcObjectWriter()
{}

void AbcObjectWriter::addChild(AbcObjectWriter *child)
{
	m_children.push_back(child);
}

Imath::Box3d AbcObjectWriter::bounds()
{
	BoundBox *bb = BKE_object_boundbox_get(this->m_object);

	if (!bb) {
		if (this->m_object->type != OB_CAMERA) {
			ABC_LOG(m_settings.logger) << "Bounding box is null!\n";
		}

		return Imath::Box3d();
	}

	/* Convert Z-up to Y-up. This also changes which vector goes into which min/max property. */
	this->m_bounds.min.x = bb->vec[0][0];
	this->m_bounds.min.y = bb->vec[0][2];
	this->m_bounds.min.z = -bb->vec[6][1];

	this->m_bounds.max.x = bb->vec[6][0];
	this->m_bounds.max.y = bb->vec[6][2];
	this->m_bounds.max.z = -bb->vec[0][1];

	return this->m_bounds;
}

void AbcObjectWriter::write()
{
	do_write();
	m_first_frame = false;
}

/* ************************************************************************** */

AbcObjectReader::AbcObjectReader(const IObject &object, ImportSettings &settings)
    : m_name("")
    , m_object_name("")
    , m_data_name("")
    , m_object(NULL)
    , m_iobject(object)
    , m_settings(&settings)
    , m_min_time(std::numeric_limits<chrono_t>::max())
    , m_max_time(std::numeric_limits<chrono_t>::min())
    , m_refcount(0)
    , parent_reader(NULL)
{
	m_name = object.getFullName();
	std::vector<std::string> parts;
	split(m_name, '/', parts);

	if (parts.size() >= 2) {
		m_object_name = parts[parts.size() - 2];
		m_data_name = parts[parts.size() - 1];
	}
	else {
		m_object_name = m_data_name = parts[parts.size() - 1];
	}
}

AbcObjectReader::~AbcObjectReader()
{}

const IObject &AbcObjectReader::iobject() const
{
	return m_iobject;
}

Object *AbcObjectReader::object() const
{
	return m_object;
}

void AbcObjectReader::object(Object *ob)
{
	m_object = ob;
}

static Imath::M44d blend_matrices(const Imath::M44d &m0, const Imath::M44d &m1, const float weight)
{
	float mat0[4][4], mat1[4][4], ret[4][4];

	/* Cannot use Imath::M44d::getValue() since this returns a pointer to
	 * doubles and interp_m4_m4m4 expects pointers to floats. So need to convert
	 * the matrices manually.
	 */

	for (int i = 0; i < 4; ++i) {
		for (int j = 0; j < 4; ++j) {
			mat0[i][j] = static_cast<float>(m0[i][j]);
		}
	}

	for (int i = 0; i < 4; ++i) {
		for (int j = 0; j < 4; ++j) {
			mat1[i][j] = static_cast<float>(m1[i][j]);
		}
	}

	interp_m4_m4m4(ret, mat0, mat1, weight);

	Imath::M44d m;

	for (int i = 0; i < 4; ++i) {
		for (int j = 0; j < 4; ++j) {
			m[i][j] = ret[i][j];
		}
	}

	return m;
}

Imath::M44d get_matrix(const IXformSchema &schema, const float time)
{
	Alembic::AbcGeom::index_t i0, i1;
	Alembic::AbcGeom::XformSample s0, s1;

	const float weight = get_weight_and_index(time,
	                                          schema.getTimeSampling(),
	                                          schema.getNumSamples(),
	                                          i0,
	                                          i1);

	schema.get(s0, Alembic::AbcGeom::ISampleSelector(i0));

	if (i0 != i1) {
		schema.get(s1, Alembic::AbcGeom::ISampleSelector(i1));
		return blend_matrices(s0.getMatrix(), s1.getMatrix(), weight);
	}

	return s0.getMatrix();
}

DerivedMesh *AbcObjectReader::read_derivedmesh(DerivedMesh *dm,
                                               const Alembic::Abc::ISampleSelector &UNUSED(sample_sel),
                                               int UNUSED(read_flag),
                                               const char **UNUSED(err_str))
{
	return dm;
}

void AbcObjectReader::setupObjectTransform(const float time)
{
	bool is_constant = false;

	this->read_matrix(m_object->obmat, time, m_settings->scale, is_constant);
	invert_m4_m4(m_object->imat, m_object->obmat);

	BKE_object_apply_mat4(m_object, m_object->obmat, false,  false);

	if (!is_constant) {
		bConstraint *con = BKE_constraint_add_for_object(m_object, NULL, CONSTRAINT_TYPE_TRANSFORM_CACHE);
		bTransformCacheConstraint *data = static_cast<bTransformCacheConstraint *>(con->data);
		BLI_strncpy(data->object_path, m_iobject.getFullName().c_str(), FILE_MAX);

		data->cache_file = m_settings->cache_file;
		id_us_plus(&data->cache_file->id);

		data->reader = reinterpret_cast<CacheReader *>(this);
		this->incref();
	}
}

Alembic::AbcGeom::IXform AbcObjectReader::xform()
{
	/* Check that we have an empty object (locator, bone head/tail...).  */
	if (IXform::matches(m_iobject.getMetaData())) {
		return IXform(m_iobject, Alembic::AbcGeom::kWrapExisting);
	}

	/* Check that we have an object with actual data, in which case the
	 * parent Alembic object should contain the transform. */
	IObject abc_parent = m_iobject.getParent();

	/* The archive's top object can be recognised by not having a parent. */
	if (abc_parent.getParent()
	        && IXform::matches(abc_parent.getMetaData())) {
		return IXform(abc_parent, Alembic::AbcGeom::kWrapExisting);
	}

	/* Should not happen. */
	std::cerr << "AbcObjectReader::xform(): "
	          << "unable to find IXform for Alembic object '"
	          << m_iobject.getFullName() << "'\n";
	BLI_assert(false);

	return IXform();
}

void AbcObjectReader::read_matrix(float r_mat[4][4], const float time,
                                  const float scale, bool &is_constant)
{
	IXform ixform = xform();
	if (!ixform) {
		return;
	}

	const IXformSchema & schema(ixform.getSchema());
	if (!schema.valid()) {
		std::cerr << "Alembic object " << ixform.getFullName()
		          << " has an invalid schema." << std::endl;
		return;
	}

	bool has_alembic_parent;
	IObject ixform_parent = ixform.getParent();
	if (!ixform_parent.getParent()) {
		/* The archive top object certainly is not a transform itself, so handle
		 * it as "no parent". */
		has_alembic_parent = false;
	}
	else {
		has_alembic_parent = ixform_parent && schema.getInheritsXforms();

		if (has_alembic_parent && m_object->parent == NULL) {
			/* TODO Sybren: This happened in some files. I think I solved it,
			 * but I'll leave this check in here anyway until we've tested it
			 * more thoroughly. Better than crashing on a null parent anyway. */
			std::cerr << "Alembic object " << m_iobject.getFullName()
			          << " with transform " << ixform.getFullName()
			          << " has an Alembic parent but no parent Blender object."
			          << std::endl;
			has_alembic_parent = false;
		}
	}

	const Imath::M44d matrix = get_matrix(schema, time);
	convert_matrix(matrix, m_object, r_mat);

	if (has_alembic_parent) {
		/* In this case, the matrix in Alembic is in local coordinates, so
		 * convert to world matrix. To prevent us from reading and accumulating
		 * all parent matrices in the Alembic file, we assume that the Blender
		 * parent object is already updated for the current timekey, and use its
		 * world matrix. */
		BLI_assert(m_object->parent);
		mul_m4_m4m4(r_mat, m_object->parent->obmat, r_mat);
	}
	else {
		/* Only apply scaling to root objects, parenting will propagate it. */
		float scale_mat[4][4];
		scale_m4_fl(scale_mat, scale);
		scale_mat[3][3] = scale; /* scale translations too */
		mul_m4_m4m4(r_mat, r_mat, scale_mat);
	}

	is_constant = schema.isConstant();
}

void AbcObjectReader::addCacheModifier()
{
	ModifierData *md = modifier_new(eModifierType_MeshSequenceCache);
	BLI_addtail(&m_object->modifiers, md);

	MeshSeqCacheModifierData *mcmd = reinterpret_cast<MeshSeqCacheModifierData *>(md);

	mcmd->cache_file = m_settings->cache_file;
	id_us_plus(&mcmd->cache_file->id);

	BLI_strncpy(mcmd->object_path, m_iobject.getFullName().c_str(), FILE_MAX);

	mcmd->reader = reinterpret_cast<CacheReader *>(this);
	this->incref();
}

chrono_t AbcObjectReader::minTime() const
{
	return m_min_time;
}

chrono_t AbcObjectReader::maxTime() const
{
	return m_max_time;
}

int AbcObjectReader::refcount() const
{
	return m_refcount;
}

void AbcObjectReader::incref()
{
	++m_refcount;
}

void AbcObjectReader::decref()
{
	--m_refcount;
	BLI_assert(m_refcount >= 0);
}