Skip to content

Commit dbbb99d

Browse files
author
Roberto De Ioris
committed
added set_material_by_name()
1 parent 706916c commit dbbb99d

3 files changed

Lines changed: 52 additions & 38 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
10041004

10051005
// Material
10061006
{ "set_material", (PyCFunction)py_ue_set_material, METH_VARARGS, "" },
1007+
{ "set_material_by_name", (PyCFunction)py_ue_set_material_by_name, METH_VARARGS, "" },
10071008
{ "set_material_scalar_parameter", (PyCFunction)py_ue_set_material_scalar_parameter, METH_VARARGS, "" },
10081009
{ "set_material_vector_parameter", (PyCFunction)py_ue_set_material_vector_parameter, METH_VARARGS, "" },
10091010
{ "set_material_texture_parameter", (PyCFunction)py_ue_set_material_texture_parameter, METH_VARARGS, "" },

‎Source/UnrealEnginePython/Private/UObject/UEPyMaterial.cpp‎

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,56 @@
1515
#include "Components/PrimitiveComponent.h"
1616
#include "Engine/StaticMesh.h"
1717

18+
PyObject *py_ue_set_material_by_name(ue_PyUObject *self, PyObject * args)
19+
{
20+
21+
ue_py_check(self);
22+
23+
char *slot_name;
24+
PyObject *py_mat;
25+
if (!PyArg_ParseTuple(args, "sO:set_material_by_name", &slot_name, &py_mat))
26+
{
27+
return nullptr;
28+
}
29+
30+
UPrimitiveComponent *primitive = ue_py_check_type<UPrimitiveComponent>(self);
31+
if (!primitive)
32+
return PyErr_Format(PyExc_Exception, "uobject is not a UPrimitiveComponent");
33+
34+
UMaterialInterface *material = ue_py_check_type<UMaterialInterface>(py_mat);
35+
if (!material)
36+
return PyErr_Format(PyExc_Exception, "argument is not a UMaterialInterface");
37+
38+
primitive->SetMaterialByName(FName(UTF8_TO_TCHAR(slot_name)), material);
39+
40+
Py_RETURN_NONE;
41+
}
42+
43+
PyObject *py_ue_set_material(ue_PyUObject *self, PyObject * args)
44+
{
45+
46+
ue_py_check(self);
47+
48+
int slot;
49+
PyObject *py_mat;
50+
if (!PyArg_ParseTuple(args, "iO:set_material", &slot, &py_mat))
51+
{
52+
return nullptr;
53+
}
54+
55+
UPrimitiveComponent *primitive = ue_py_check_type<UPrimitiveComponent>(self);
56+
if (!primitive)
57+
return PyErr_Format(PyExc_Exception, "uobject is not a UPrimitiveComponent");
58+
59+
UMaterialInterface *material = ue_py_check_type<UMaterialInterface>(py_mat);
60+
if (!material)
61+
return PyErr_Format(PyExc_Exception, "argument is not a UMaterialInterface");
62+
63+
primitive->SetMaterial(slot, material);
64+
65+
Py_RETURN_NONE;
66+
}
67+
1868
PyObject *py_ue_set_material_scalar_parameter(ue_PyUObject *self, PyObject * args)
1969
{
2070

@@ -308,44 +358,6 @@ PyObject *py_ue_create_material_instance_dynamic(ue_PyUObject *self, PyObject *
308358
Py_RETURN_UOBJECT(material_dynamic);
309359
}
310360

311-
PyObject *py_ue_set_material(ue_PyUObject *self, PyObject * args)
312-
{
313-
314-
ue_py_check(self);
315-
316-
int index;
317-
PyObject *py_material = nullptr;
318-
319-
if (!PyArg_ParseTuple(args, "iO:set_material", &index, &py_material))
320-
{
321-
return NULL;
322-
}
323-
324-
if (!self->ue_object->IsA<UPrimitiveComponent>())
325-
{
326-
return PyErr_Format(PyExc_Exception, "uobject is not a UPrimitiveComponent");
327-
}
328-
329-
UPrimitiveComponent *component = (UPrimitiveComponent *)self->ue_object;
330-
331-
if (!ue_is_pyuobject(py_material))
332-
{
333-
return PyErr_Format(PyExc_Exception, "argument is not a UObject");
334-
}
335-
336-
ue_PyUObject *py_obj = (ue_PyUObject *)py_material;
337-
338-
if (!py_obj->ue_object->IsA<UMaterialInterface>())
339-
{
340-
return PyErr_Format(PyExc_Exception, "uobject is not a UMaterialInterface");
341-
}
342-
343-
UMaterialInterface *material_interface = (UMaterialInterface *)py_obj->ue_object;
344-
345-
component->SetMaterial(index, material_interface);
346-
347-
Py_RETURN_NONE;
348-
}
349361

350362

351363
#if WITH_EDITOR

‎Source/UnrealEnginePython/Private/UObject/UEPyMaterial.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PyObject *py_ue_create_material_instance_dynamic(ue_PyUObject *, PyObject *);
1717

1818
PyObject *py_ue_set_material(ue_PyUObject *, PyObject *);
1919

20+
PyObject *py_ue_set_material_by_name(ue_PyUObject *, PyObject *);
2021

2122
#if WITH_EDITOR
2223
PyObject *py_ue_set_material_parent(ue_PyUObject *, PyObject *);

0 commit comments

Comments
 (0)