Skip to content

Commit 7f00057

Browse files
author
Roberto De Ioris
committed
added set_material and create_material_instance_dynamic
1 parent eefd917 commit 7f00057

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

Source/UnrealEnginePython/Private/UEPyMaterial.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,73 @@ PyObject *py_ue_get_material_texture_parameter(ue_PyUObject *self, PyObject * ar
219219
return (PyObject *)ret;
220220
}
221221

222+
PyObject *py_ue_create_material_instance_dynamic(ue_PyUObject *self, PyObject * args) {
223+
224+
ue_py_check(self);
225+
226+
PyObject *py_material = nullptr;
227+
228+
if (!PyArg_ParseTuple(args, "O:create_material_instance_dynamic", &py_material)) {
229+
return NULL;
230+
}
231+
232+
if (!ue_is_pyuobject(py_material)) {
233+
return PyErr_Format(PyExc_Exception, "argument is not a UObject");
234+
}
235+
236+
ue_PyUObject *py_obj = (ue_PyUObject *)py_material;
237+
238+
if (!py_obj->ue_object->IsA<UMaterialInstanceConstant>()) {
239+
return PyErr_Format(PyExc_Exception, "uobject is not a UMaterialInstanceConstant");
240+
}
241+
242+
UMaterialInstanceConstant *material_instance = (UMaterialInstanceConstant *)py_obj->ue_object;
243+
244+
UMaterialInstanceDynamic *material_dynamic = UMaterialInstanceDynamic::Create(material_instance, self->ue_object);
245+
246+
ue_PyUObject *ret = ue_get_python_wrapper(material_dynamic);
247+
if (!ret)
248+
return PyErr_Format(PyExc_Exception, "PyUObject is in invalid state");
249+
Py_INCREF(ret);
250+
return (PyObject *)ret;
251+
252+
}
253+
254+
PyObject *py_ue_set_material(ue_PyUObject *self, PyObject * args) {
255+
256+
ue_py_check(self);
257+
258+
int index;
259+
PyObject *py_material = nullptr;
260+
261+
if (!PyArg_ParseTuple(args, "iO:set_material", &index, &py_material)) {
262+
return NULL;
263+
}
264+
265+
if (!self->ue_object->IsA<UPrimitiveComponent>()) {
266+
return PyErr_Format(PyExc_Exception, "uobject is not a UPrimitiveComponent");
267+
}
268+
269+
UPrimitiveComponent *component = (UPrimitiveComponent *)self->ue_object;
270+
271+
if (!ue_is_pyuobject(py_material)) {
272+
return PyErr_Format(PyExc_Exception, "argument is not a UObject");
273+
}
274+
275+
ue_PyUObject *py_obj = (ue_PyUObject *)py_material;
276+
277+
if (!py_obj->ue_object->IsA<UMaterialInterface>()) {
278+
return PyErr_Format(PyExc_Exception, "uobject is not a UMaterialInterface");
279+
}
280+
281+
UMaterialInterface *material_interface = (UMaterialInterface *)py_obj->ue_object;
282+
283+
component->SetMaterial(index, material_interface);
284+
285+
Py_INCREF(Py_None);
286+
return Py_None;
287+
}
288+
222289

223290
#if WITH_EDITOR
224291
PyObject *py_ue_set_material_parent(ue_PyUObject *self, PyObject * args) {

Source/UnrealEnginePython/Private/UEPyMaterial.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ PyObject *py_ue_get_material_scalar_parameter(ue_PyUObject *, PyObject *);
1212
PyObject *py_ue_get_material_vector_parameter(ue_PyUObject *, PyObject *);
1313
PyObject *py_ue_get_material_texture_parameter(ue_PyUObject *, PyObject *);
1414

15+
PyObject *py_ue_create_material_instance_dynamic(ue_PyUObject *, PyObject *);
16+
17+
PyObject *py_ue_set_material(ue_PyUObject *, PyObject *);
18+
1519

1620
#if WITH_EDITOR
1721
PyObject *py_ue_set_material_parent(ue_PyUObject *, PyObject *);

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,14 @@ static PyMethodDef ue_PyUObject_methods[] = {
470470
{ "sequencer_add_master_track", (PyCFunction)py_ue_sequencer_add_master_track, METH_VARARGS, "" },
471471

472472
// Material
473-
473+
{ "set_material", (PyCFunction)py_ue_set_material, METH_VARARGS, "" },
474474
{ "set_material_scalar_parameter", (PyCFunction)py_ue_set_material_scalar_parameter, METH_VARARGS, "" },
475475
{ "set_material_vector_parameter", (PyCFunction)py_ue_set_material_vector_parameter, METH_VARARGS, "" },
476476
{ "set_material_texture_parameter", (PyCFunction)py_ue_set_material_texture_parameter, METH_VARARGS, "" },
477477
{ "get_material_scalar_parameter", (PyCFunction)py_ue_get_material_scalar_parameter, METH_VARARGS, "" },
478478
{ "get_material_vector_parameter", (PyCFunction)py_ue_get_material_vector_parameter, METH_VARARGS, "" },
479479
{ "get_material_texture_parameter", (PyCFunction)py_ue_get_material_texture_parameter, METH_VARARGS, "" },
480+
{ "create_material_instance_dynamic", (PyCFunction)py_ue_create_material_instance_dynamic, METH_VARARGS, "" },
480481
#if WITH_EDITOR
481482
{ "set_material_parent", (PyCFunction)py_ue_set_material_parent, METH_VARARGS, "" },
482483
{ "static_mesh_set_collision_for_lod", (PyCFunction)py_ue_static_mesh_set_collision_for_lod, METH_VARARGS, "" },

0 commit comments

Comments
 (0)