Skip to content

Commit 513b42e

Browse files
author
Roberto De Ioris
committed
added kaiju_builder.py example
1 parent 76cd2d4 commit 513b42e

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

examples/kaiju_builder.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import unreal_engine as ue
2+
from unreal_engine.classes import PyFbxFactory, TextureFactory, Material, Actor
3+
from unreal_engine import FVector, FRotator
4+
from unreal_engine.enums import EFBXImportType
5+
import os.path
6+
import time
7+
8+
# in which content folder to store the new assets
9+
kaiju_destination = '/Game/Kaiju'
10+
# the path from where new assets are loaded
11+
kaiju_source = os.path.expanduser('~/Desktop')
12+
13+
# an fbx skeletal mesh
14+
kaiju_mesh = 'slicer_tpose.fbx'
15+
# textures (from substance designer)
16+
kaiju_textures = ['slicer_BaseColor.tga', 'slicer_Emissive.tga', 'slicer_Normal.tga', 'slicer_OcclusionRoughnessMetallic.tga']
17+
# an fbx skeletal animation
18+
kaiju_animation = 'slicer_walking.fbx'
19+
20+
# start configuring the mesh importer
21+
factory = PyFbxFactory()
22+
factory.ImportUI.bCreatePhysicsAsset = False
23+
factory.ImportUI.bImportMaterials = False
24+
factory.ImportUI.bImportTextures = False
25+
factory.ImportUI.bImportAnimations = False
26+
# scale the mesh
27+
factory.ImportUI.SkeletalMeshImportData.ImportUniformScale = 0.1;
28+
29+
# import the mesh
30+
mesh = factory.factory_import_object(os.path.join(kaiju_source, kaiju_mesh), kaiju_destination)
31+
32+
33+
# start configurint the texture importer
34+
texture_factory = TextureFactory()
35+
36+
imported_textures = []
37+
38+
for texture in kaiju_textures:
39+
imported_texture = texture_factory.factory_import_object(os.path.join(kaiju_source, texture), kaiju_destination)
40+
imported_textures.append(imported_texture)
41+
42+
43+
mat = Material()
44+
#mat.set_name('kaiju_Material_' + str(time.time()))
45+
mat.save_package(kaiju_destination + '/kaiju_Material')
46+
47+
from unreal_engine.classes import MaterialExpressionTextureSample, Texture2D, MaterialExpressionConstant3Vector, MaterialExpressionSine, MaterialExpressionMultiply, MaterialExpressionTime, Skeleton, Character
48+
from unreal_engine.structs import ColorMaterialInput, ScalarMaterialInput, ExpressionInput, VectorMaterialInput, SingleAnimationPlayData
49+
from unreal_engine.enums import EMaterialSamplerType, EAnimationMode
50+
51+
# inform the editor you want to modify the new material
52+
mat.modify()
53+
54+
# build the material graph
55+
sample_base_color = MaterialExpressionTextureSample('', mat)
56+
sample_base_color.Texture = imported_textures[0]
57+
sample_base_color.MaterialExpressionEditorX = -400
58+
sample_base_color.MaterialExpressionEditorY = 0
59+
60+
sample_emissive = MaterialExpressionTextureSample('', mat)
61+
sample_emissive.Texture = imported_textures[1]
62+
sample_emissive.MaterialExpressionEditorX = -600
63+
sample_emissive.MaterialExpressionEditorY = 0
64+
65+
sample_normal = MaterialExpressionTextureSample('', mat)
66+
sample_normal.Texture = imported_textures[2]
67+
sample_normal.SamplerType = EMaterialSamplerType.SAMPLERTYPE_Normal
68+
sample_normal.MaterialExpressionEditorX = -400
69+
sample_normal.MaterialExpressionEditorY = 400
70+
71+
sample_orm = MaterialExpressionTextureSample('', mat)
72+
sample_orm.Texture = imported_textures[3]
73+
sample_orm.MaterialExpressionEditorX = -600
74+
sample_orm.MaterialExpressionEditorY = 400
75+
76+
sine = MaterialExpressionSine('', mat)
77+
sine.MaterialExpressionEditorX = -1000
78+
sine.MaterialExpressionEditorY = 0
79+
80+
time = MaterialExpressionTime('', mat)
81+
time.MaterialExpressionEditorX = -1200
82+
time.MaterialExpressionEditorY = 0
83+
84+
multiply = MaterialExpressionMultiply('', mat)
85+
multiply.MaterialExpressionEditorX = -800
86+
multiply.MaterialExpressionEditorY = 0
87+
88+
mat.Expressions = [sample_base_color, sample_emissive, sample_normal, sample_orm, time, sine, multiply]
89+
90+
sine.Input = ExpressionInput(Expression=time)
91+
multiply.A = ExpressionInput(Expression=sample_emissive)
92+
multiply.B = ExpressionInput(Expression=sine)
93+
94+
mat.BaseColor = ColorMaterialInput(Expression=sample_base_color)
95+
mat.EmissiveColor = ColorMaterialInput(Expression=multiply)
96+
mat.Normal = VectorMaterialInput(Expression=sample_normal)
97+
mat.Roughness = ScalarMaterialInput(Expression=sample_orm, Mask=1, MaskG=1)
98+
mat.Metallic = ScalarMaterialInput(Expression=sample_orm, Mask=1, MaskB=1)
99+
mat.AmbientOcclusion = ScalarMaterialInput(Expression=sample_orm, Mask=1, MaskR=1)
100+
101+
# run material compilatiom
102+
mat.post_edit_change()
103+
# re-save it
104+
mat.save_package()
105+
106+
107+
# import the animation
108+
109+
anim_factory = PyFbxFactory()
110+
anim_factory.ImportUI.Skeleton = mesh.Skeleton
111+
anim_factory.ImportUI.bImportMesh = False
112+
anim_factory.ImportUI.bImportMaterials = False
113+
anim_factory.ImportUI.bImportTextures = False
114+
anim_factory.ImportUI.AnimSequenceImportData.ImportUniformScale = 0.1;
115+
116+
animation = anim_factory.factory_import_object(os.path.join(kaiju_source, kaiju_animation), kaiju_destination)
117+
118+
new_blueprint = ue.create_blueprint(Character, kaiju_destination + '/Kaiju_BP')
119+
120+
new_blueprint.GeneratedClass.get_cdo().Mesh.SkeletalMesh = mesh
121+
new_blueprint.GeneratedClass.get_cdo().Mesh.RelativeLocation = FVector(0, 0, -140)
122+
new_blueprint.GeneratedClass.get_cdo().Mesh.RelativeRotation = FRotator(0, 0, -90)
123+
new_blueprint.GeneratedClass.get_cdo().CapsuleComponent.CapsuleHalfHeight = 150
124+
new_blueprint.GeneratedClass.get_cdo().CapsuleComponent.CapsuleRadius = 50
125+
126+
new_blueprint.GeneratedClass.get_cdo().Mesh.OverrideMaterials = [None, mat]
127+
128+
new_blueprint.GeneratedClass.get_cdo().Mesh.AnimationMode = EAnimationMode.AnimationSingleNode
129+
new_blueprint.GeneratedClass.get_cdo().Mesh.AnimationData = SingleAnimationPlayData(AnimToPlay=animation)
130+
131+
# move forward
132+
tick = new_blueprint.UberGraphPages[0].graph_add_node_event(Actor, 'ReceiveTick')
133+
add_movement_input = new_blueprint.UberGraphPages[0].graph_add_node_call_function(Character.AddMovementInput)
134+
add_movement_input.node_find_pin('WorldDirection').default_value = '1,0,0'
135+
tick.node_find_pin('then').make_link_to(add_movement_input.node_find_pin('execute'))
136+
137+
ue.compile_blueprint(new_blueprint)
138+
139+
world = ue.get_editor_world()
140+
new_actor = world.actor_spawn(new_blueprint.GeneratedClass, FVector(0, 0, 150))
141+
142+
ue.editor_save_all()

0 commit comments

Comments
 (0)