95 lines
3.6 KiB
Python
95 lines
3.6 KiB
Python
# tools/fixtures/_fbx_blender: run by tools/fixtures/fbx.py inside Blender (blender -b --python <this> -- <outdir>): builds
|
|
# test scenes (textured cube with two materials, vertex colours and a child; a skinned, animated cylinder with a
|
|
# shape key; a camera and three lights) and exports each to binary FBX 7.4.
|
|
import bpy, sys, os, math
|
|
out = sys.argv[sys.argv.index("--") + 1]
|
|
def reset():
|
|
bpy.ops.wm.read_factory_settings(use_empty=True)
|
|
def export(name, **kw):
|
|
bpy.ops.export_scene.fbx(filepath=os.path.join(out, name), **kw)
|
|
|
|
# 1. textured cube: material, uv, vertex colours, packed image
|
|
reset()
|
|
bpy.ops.mesh.primitive_cube_add(size=2, location=(1, 2, 3), rotation=(0.3, 0.2, 0.1))
|
|
cube = bpy.context.object
|
|
cube.name = "Cube"
|
|
mesh = cube.data
|
|
mesh.color_attributes.new("Col", "BYTE_COLOR", "CORNER")
|
|
for i, d in enumerate(mesh.color_attributes["Col"].data):
|
|
d.color = ((i % 3) / 2, (i % 5) / 4, (i % 7) / 6, 1)
|
|
img = bpy.data.images.new("checker", 8, 8)
|
|
img.pixels = [((x + y) % 2) * 1.0 if c < 3 else 1.0 for y in range(8) for x in range(8) for c in range(4)]
|
|
img.filepath_raw = os.path.join(out, "checker.png")
|
|
img.file_format = "PNG"
|
|
img.save()
|
|
img.pack()
|
|
mat = bpy.data.materials.new("Red")
|
|
mat.use_nodes = True
|
|
bsdf = mat.node_tree.nodes["Principled BSDF"]
|
|
bsdf.inputs["Base Color"].default_value = (0.8, 0.1, 0.1, 1)
|
|
bsdf.inputs["Roughness"].default_value = 0.4
|
|
tex = mat.node_tree.nodes.new("ShaderNodeTexImage")
|
|
tex.image = img
|
|
mat.node_tree.links.new(tex.outputs["Color"], bsdf.inputs["Base Color"])
|
|
mesh.materials.append(mat)
|
|
mat2 = bpy.data.materials.new("Blue")
|
|
mat2.diffuse_color = (0.1, 0.1, 0.9, 1)
|
|
mesh.materials.append(mat2)
|
|
for i, p in enumerate(mesh.polygons):
|
|
p.material_index = i % 2
|
|
bpy.ops.mesh.primitive_uv_sphere_add(segments=12, ring_count=6, location=(-3, 0, 0))
|
|
bpy.context.object.name = "Sphere"
|
|
bpy.context.object.parent = cube
|
|
export("blender_cube.fbx", path_mode="COPY", embed_textures=True)
|
|
|
|
# 2. skinned, animated mesh with shape keys
|
|
reset()
|
|
bpy.ops.object.armature_add(location=(0, 0, 0))
|
|
arm = bpy.context.object
|
|
arm.name = "Rig"
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
b0 = arm.data.edit_bones[0]
|
|
b0.name = "root"
|
|
b0.head, b0.tail = (0, 0, 0), (0, 0, 1)
|
|
b1 = arm.data.edit_bones.new("tip")
|
|
b1.head, b1.tail, b1.parent = (0, 0, 1), (0, 0, 2), b0
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
bpy.ops.mesh.primitive_cylinder_add(vertices=8, depth=2, radius=0.3, location=(0, 0, 1))
|
|
cyl = bpy.context.object
|
|
cyl.name = "Body"
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
bpy.ops.mesh.subdivide(number_cuts=3)
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
cyl.shape_key_add(name="Basis")
|
|
sk = cyl.shape_key_add(name="Fat")
|
|
for v in sk.data:
|
|
v.co.x *= 1.5
|
|
v.co.y *= 1.5
|
|
sk.value = 0.25
|
|
cyl.select_set(True)
|
|
arm.select_set(True)
|
|
bpy.context.view_layer.objects.active = arm
|
|
bpy.ops.object.parent_set(type="ARMATURE_AUTO")
|
|
bpy.context.scene.frame_start, bpy.context.scene.frame_end = 1, 20
|
|
bpy.ops.object.mode_set(mode="POSE")
|
|
pb = arm.pose.bones["tip"]
|
|
pb.rotation_mode = "XYZ"
|
|
for f, a in ((1, 0), (10, 0.8), (20, 0)):
|
|
pb.rotation_euler = (a, 0, 0)
|
|
pb.keyframe_insert("rotation_euler", frame=f)
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
for f, v in ((1, 0), (20, 1)):
|
|
sk.value = v
|
|
sk.keyframe_insert("value", frame=f)
|
|
export("blender_skin.fbx", add_leaf_bones=False, bake_anim=True)
|
|
|
|
# 3. camera and lights
|
|
reset()
|
|
bpy.ops.object.camera_add(location=(0, -5, 1), rotation=(math.pi / 2, 0, 0))
|
|
bpy.context.object.data.lens = 35
|
|
for kind, loc in (("POINT", (1, 0, 3)), ("SUN", (0, 0, 5)), ("SPOT", (2, 2, 2))):
|
|
bpy.ops.object.light_add(type=kind, location=loc)
|
|
bpy.context.object.data.energy = 10
|
|
bpy.ops.mesh.primitive_plane_add(size=4)
|
|
export("blender_camlight.fbx")
|