Skip to content

Commit d7c3f7d

Browse files
author
Roberto De Ioris
authored
Update WritingAColladaFactoryWithPython.md
1 parent f918beb commit d7c3f7d

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

‎tutorials/WritingAColladaFactoryWithPython.md‎

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Note that whenever you re-run the scripts, the internal Unreal Engine reflection
4040

4141
as you can see from the screenshot, the address of the UClass as well as its python UObject counterpart remains the same, while the `__init__` call is updated. Remember that whenever you create a new UClass from python, the `__init__` method is automatically called by the ClassDefaultObject (this information is useful only if you are into unreal engine internals, otherwise you can simply ignore it ;).
4242

43+
Remember: only functions starting with an uppercase letter will be exposed to unreal, the others will be usable only from python.
44+
4345
## Parse .dae files with pycollada
4446

4547
The collada format is based on XML, so we can easily parse it from python, but instead of doing the hard work by hand, we can use the amazing pycollada module (https://github.com/pycollada/pycollada). Just pip install it and update your class to import and use it:
@@ -120,6 +122,26 @@ class ColladaFactory(PyFactory):
120122
self.Formats = ['dae;Collada']
121123
# set the UClass this UFactory will generate
122124
self.SupportedClass = StaticMesh
125+
ue.log(dir(self))
126+
127+
def FixMeshData(self):
128+
# move from collada system (y on top) to ue4 one (z on top, forward decreases over viewer)
129+
for i in range(0, len(self.vertices), 3):
130+
xv, yv, zv = self.vertices[i], self.vertices[i+1], self.vertices[i+2]
131+
# invert forward
132+
self.vertices[i] = zv * -1
133+
self.vertices[i+1] = xv
134+
self.vertices[i+2] = yv
135+
xn, yn, zn = self.normals[i], self.normals[i+1], self.normals[i+2]
136+
# invert forward
137+
self.normals[i] = zn * -1
138+
self.normals[i+1] = xn
139+
self.normals[i+2] = yn
140+
141+
# fix uvs from 0 on bottom to 0 on top
142+
for i, uv in enumerate(uvs):
143+
if i % 2 != 0:
144+
uvs[i] = 1 - uv
123145

124146
def PyFactoryCreateFile(self, uclass: Class, parent: Object, name: str, filename: str) -> Object:
125147
# load the collada file
@@ -133,37 +155,22 @@ class ColladaFactory(PyFactory):
133155

134156
# extract vertices, uvs and normals from the da file (numpy.ravel will flatten the arrays to simple array of floats)
135157
triset = dae.geometries[0].primitives[0]
136-
vertices = numpy.ravel(triset.vertex[triset.vertex_index])
158+
self.vertices = numpy.ravel(triset.vertex[triset.vertex_index])
137159
# take the first uv channel (there could be multiple channels, like the one for lightmapping)
138-
uvs = numpy.ravel(triset.texcoordset[0][triset.texcoord_indexset[0]])
139-
normals = numpy.ravel(triset.normal[triset.normal_index])
160+
self.uvs = numpy.ravel(triset.texcoordset[0][triset.texcoord_indexset[0]])
161+
self.normals = numpy.ravel(triset.normal[triset.normal_index])
140162

141-
# move from collada system (y on top) to ue4 one (z on top, forward decreases over viewer)
142-
for i in range(0, len(vertices), 3):
143-
xv, yv, zv = vertices[i], vertices[i+1], vertices[i+2]
144-
# invert forward
145-
vertices[i] = zv * -1
146-
vertices[i+1] = xv
147-
vertices[i+2] = yv
148-
xn, yn, zn = normals[i], normals[i+1], normals[i+2]
149-
# invert forward
150-
normals[i] = zn * -1
151-
normals[i+1] = xn
152-
normals[i+2] = yn
163+
# fix mesh data
164+
self.FixMeshData()
153165

154-
# fix uvs from 0 on bottom to 0 on top
155-
for i, uv in enumerate(uvs):
156-
if i % 2 != 0:
157-
uvs[i] = 1 - uv
158-
159166
# create a new mesh, FRawMesh is an ptopmized wrapper exposed by the python plugin. read: no reflection involved
160167
mesh = FRawMesh()
161168
# assign vertices
162-
mesh.set_vertex_positions(vertices)
169+
mesh.set_vertex_positions(self.vertices)
163170
# uvs are required
164-
mesh.set_wedge_tex_coords(uvs)
171+
mesh.set_wedge_tex_coords(self.uvs)
165172
# normals are optionals
166-
mesh.set_wedge_tangent_z(normals)
173+
mesh.set_wedge_tangent_z(self.normals)
167174

168175
# assign indices (not optimized, just return the list of triangles * 3...)
169176
mesh.set_wedge_indices(numpy.arange(0, len(triset) * 3))

0 commit comments

Comments
 (0)