File: ModelInterface.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (191 lines) | stat: -rw-r--r-- 5,906 bytes parent folder | download | duplicates (2)
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
#include "ModelInterface.h"

#include <pybind11/pybind11.h>
#include "imodelsurface.h"
#include "modelskin.h"

namespace script
{

int ScriptModelSurface::getNumVertices() const
{
	return _surface.getNumVertices();
}

int ScriptModelSurface::getNumTriangles() const
{
	return _surface.getNumTriangles();
}

const MeshVertex& ScriptModelSurface::getVertex(int vertexIndex) const
{
	return _surface.getVertex(vertexIndex);
}

model::ModelPolygon ScriptModelSurface::getPolygon(int polygonIndex) const
{
	return _surface.getPolygon(polygonIndex);
}

std::string ScriptModelSurface::getDefaultMaterial() const
{
	return _surface.getDefaultMaterial();
}

std::string ScriptModelSurface::getActiveMaterial() const
{
	return _surface.getActiveMaterial();
}

// ----------- ScriptModelNode -----------

// Constructor, checks if the passed node is actually an entity
ScriptModelNode::ScriptModelNode(const scene::INodePtr& node) :
	ScriptSceneNode((node != NULL && Node_isModel(node)) ? node : scene::INodePtr())
{}

std::string ScriptModelNode::getFilename()
{
	model::ModelNodePtr modelNode = Node_getModel(*this);
	if (modelNode == NULL) return "";

	return modelNode->getIModel().getFilename();
}

std::string ScriptModelNode::getModelPath()
{
	model::ModelNodePtr modelNode = Node_getModel(*this);
	if (modelNode == NULL) return "";

	return modelNode->getIModel().getModelPath();
}

int ScriptModelNode::getSurfaceCount()
{
	model::ModelNodePtr modelNode = Node_getModel(*this);
	if (modelNode == NULL) return -1;

	return modelNode->getIModel().getSurfaceCount();
}

int ScriptModelNode::getVertexCount()
{
	model::ModelNodePtr modelNode = Node_getModel(*this);
	if (modelNode == NULL) return -1;

	return modelNode->getIModel().getVertexCount();
}

int ScriptModelNode::getPolyCount()
{
	model::ModelNodePtr modelNode = Node_getModel(*this);
	if (modelNode == NULL) return -1;

	return modelNode->getIModel().getPolyCount();
}

ScriptModelSurface ScriptModelNode::getSurface(int surfaceNum)
{
	model::ModelNodePtr modelNode = Node_getModel(*this);
	if (modelNode == NULL) throw std::runtime_error("Empty model node.");

	return ScriptModelSurface(modelNode->getIModel().getSurface(surfaceNum));
}

model::StringList ScriptModelNode::getActiveMaterials()
{
	model::ModelNodePtr modelNode = Node_getModel(*this);
	if (modelNode == NULL) return model::StringList();

	// Get the list of default shaders from this model, this is without any skins applied
	model::StringList materials = modelNode->getIModel().getActiveMaterials();

	// Check if the model is a skinned one, so let's check for active skins
	auto skinnedModel = std::dynamic_pointer_cast<SkinnedModel>(modelNode);

	if (skinnedModel)
	{
		// This is a skinned model, get the surface remap
		std::string curSkin = skinnedModel->getSkin();

		auto skin = GlobalModelSkinCache().findSkin(curSkin);

        if (skin)
        {
            for (auto& material : materials)
            {
                std::string remap = skin->getRemap(material);

                if (remap.empty()) continue;

                // Remapping found, use this material instead of the default material
                material = remap;
            }
        }
	}

	return materials;
}

// Checks if the given SceneNode structure is a ModelNode
bool ScriptModelNode::isModel(const ScriptSceneNode& node) {
	return Node_isModel(node);
}

// "Cast" service for Python, returns a ScriptModelNode.
// The returned node is non-NULL if the cast succeeded
ScriptModelNode ScriptModelNode::getModel(const ScriptSceneNode& node) {
	// Try to cast the node onto a model
	model::ModelNodePtr modelNode = Node_getModel(node);

	// Construct a modelNode (contained node is NULL if not a model)
	return ScriptModelNode(modelNode != NULL
                           ? node
                           : ScriptSceneNode(scene::INodePtr()));
}

void ModelInterface::registerInterface(py::module& scope, py::dict& globals)
{
	py::class_<MeshVertex> vertex(scope, "MeshVertex");

	vertex.def_readwrite("texcoord", &MeshVertex::texcoord);
	vertex.def_readwrite("normal", &MeshVertex::normal);
	vertex.def_readwrite("vertex", &MeshVertex::vertex);
	vertex.def_readwrite("tangent", &MeshVertex::tangent);
	vertex.def_readwrite("bitangent", &MeshVertex::bitangent);
	vertex.def_readwrite("colour", &MeshVertex::colour);

    // Register the old name as alias to MeshVertex
    scope.add_object("ArbitraryMeshVertex", vertex);

	py::class_<model::ModelPolygon> poly(scope, "ModelPolygon");

	poly.def_readonly("a", &model::ModelPolygon::a);
	poly.def_readonly("b", &model::ModelPolygon::b);
	poly.def_readonly("c", &model::ModelPolygon::c);

	// Add the ModelSurface interface
	py::class_<ScriptModelSurface> surface(scope, "ModelSurface");

	surface.def(py::init<const model::IModelSurface&>());
	surface.def("getNumVertices", &ScriptModelSurface::getNumVertices);
	surface.def("getNumTriangles", &ScriptModelSurface::getNumTriangles);
	surface.def("getVertex", &ScriptModelSurface::getVertex, py::return_value_policy::reference);
	surface.def("getPolygon", &ScriptModelSurface::getPolygon);
	surface.def("getDefaultMaterial", &ScriptModelSurface::getDefaultMaterial);
	surface.def("getActiveMaterial", &ScriptModelSurface::getActiveMaterial);

	// Add the ModelNode interface
	py::class_<ScriptModelNode, ScriptSceneNode> modelNode(scope, "ModelNode");

	modelNode.def(py::init<const scene::INodePtr&>());
	modelNode.def("getFilename", &ScriptModelNode::getFilename);
	modelNode.def("getModelPath", &ScriptModelNode::getModelPath);
	modelNode.def("getSurfaceCount", &ScriptModelNode::getSurfaceCount);
	modelNode.def("getVertexCount", &ScriptModelNode::getVertexCount);
	modelNode.def("getPolyCount", &ScriptModelNode::getPolyCount);
	modelNode.def("getActiveMaterials", &ScriptModelNode::getActiveMaterials);
	modelNode.def("getSurface", &ScriptModelNode::getSurface);
}

} // namespace script