1030 lines
43 KiB
Python
1030 lines
43 KiB
Python
# tools/fixtures/_blend_make: run by tools/fixtures/blend.py inside Blender 2.79 .. 5.x (blender -b --python <this> --
|
|
# <outdir>): builds the test scenes with whatever API the running version has and saves them as .blend files:
|
|
# scene_<ver>.blend collections, transforms (euler / quaternion / delta), parenting with a parent inverse,
|
|
# meshes (quads, an n-gon, smooth / flat), two UV maps, colour attributes, materials (Principled
|
|
# + packed and external images, a legacy one), cameras, lights, modifiers
|
|
# rig_<ver>.blend armature with rolls, a skinned mesh (vertex groups, 5 influences on some vertices), shape
|
|
# keys, actions on bones / objects / shape keys (bezier, linear, constant), a bone parent
|
|
# scene_<ver>_z.blend scene_<ver> saved compressed (gzip before 3.0, zstd after)
|
|
# compat_<ver>.blend 2.7x only: the "legacy mesh format" save (MFace tessellated faces)
|
|
# ctl_<ver>.blend a control rig: deform / control bones, layers + bone groups (before 4.0) or bone
|
|
# collections and colours (4.x), custom shapes (a hidden widget in the scene, one outside it),
|
|
# pose-bone constraints (IK, copy rotation / location / transforms, damped track, limit
|
|
# rotation, child of, stretch to), lock and IK flags; shape keys with slider ranges, mute,
|
|
# relative keys, a vertex group, and a shape-key action; an unassigned action driven by an
|
|
# action constraint; transform / armature / floor / limit distance / track to / locked
|
|
# track constraints; bone inheritance and b-bone settings; a saved pose basis; drivers
|
|
# (every variable type, keys, modifiers) on the rig, its data and the shape keys; custom
|
|
# properties on pose bones, bones and objects; vertex, vertex-triangle and lattice parents,
|
|
# an object parented to a bone
|
|
# mat_<ver>.blend material graphs: images through Mix / MixRGB, Hue-Saturation, Bright-Contrast, Gamma,
|
|
# Invert, RGB Curves, Color Ramp, reroutes and node groups; roughness images linked directly
|
|
# or through a ramp; vertex colours; bump; unresolved links; a UDIM image with two tiles
|
|
# (2.82+, the tiles as udim.1001.png / udim.1002.png next to the file)
|
|
import bpy, sys, os, math
|
|
|
|
out = sys.argv[sys.argv.index("--") + 1]
|
|
V = bpy.app.version
|
|
TAG = "%d%d" % (V[0], V[1])
|
|
OLD = V < (2, 80)
|
|
|
|
|
|
def reset():
|
|
bpy.ops.wm.read_factory_settings(use_empty=True) if not OLD else bpy.ops.wm.read_factory_settings()
|
|
if OLD:
|
|
for o in list(bpy.data.objects):
|
|
bpy.data.objects.remove(o, do_unlink=True)
|
|
sc = bpy.context.scene
|
|
sc.render.fps = 24
|
|
if OLD:
|
|
sc.render.engine = "CYCLES"
|
|
sc.frame_start, sc.frame_end = 1, 20
|
|
|
|
|
|
def link(obj, coll=None):
|
|
sc = bpy.context.scene
|
|
if OLD:
|
|
sc.objects.link(obj)
|
|
else:
|
|
(coll or sc.collection).objects.link(obj)
|
|
return obj
|
|
|
|
|
|
def mesh(name, verts, faces, smooth=False):
|
|
me = bpy.data.meshes.new(name)
|
|
me.from_pydata(verts, [], faces)
|
|
me.update()
|
|
for p in me.polygons:
|
|
p.use_smooth = smooth
|
|
return me
|
|
|
|
|
|
def uv(me, name, f):
|
|
if OLD:
|
|
me.uv_textures.new(name)
|
|
layer = me.uv_layers[name]
|
|
else:
|
|
layer = me.uv_layers.new(name=name)
|
|
for i, d in enumerate(layer.data):
|
|
d.uv = f(i)
|
|
|
|
|
|
def byte_colors(me, name):
|
|
if V >= (3, 2):
|
|
a = me.color_attributes.new(name, "BYTE_COLOR", "CORNER")
|
|
data = a.data
|
|
else:
|
|
data = me.vertex_colors.new(name=name).data
|
|
for i, d in enumerate(data):
|
|
c = ((i % 3) / 2, (i % 5) / 4, (i % 7) / 6)
|
|
d.color = c + (1.0,) if len(d.color) == 4 else c
|
|
|
|
|
|
def image(name, w, h, f, pack=True, path=None):
|
|
img = bpy.data.images.new(name, w, h)
|
|
px = []
|
|
for y in range(h):
|
|
for x in range(w):
|
|
px.extend(f(x, y))
|
|
img.pixels = px
|
|
if path:
|
|
img.filepath_raw = path
|
|
img.file_format = "PNG"
|
|
img.save()
|
|
img.filepath = "//" + os.path.basename(path)
|
|
if pack:
|
|
if OLD:
|
|
img.pack(as_png=True)
|
|
else:
|
|
img.pack()
|
|
return img
|
|
|
|
|
|
def socket(node, *names):
|
|
for n in names:
|
|
if n in node.inputs:
|
|
return node.inputs[n]
|
|
return None
|
|
|
|
|
|
def principled(mat, base, metallic, rough, emission=None, strength=1.0, alpha=1.0):
|
|
mat.use_nodes = True
|
|
nt = mat.node_tree
|
|
bsdf = None
|
|
for n in nt.nodes:
|
|
if n.bl_idname == "ShaderNodeBsdfPrincipled":
|
|
bsdf = n
|
|
if bsdf is None:
|
|
for n in list(nt.nodes):
|
|
if n.bl_idname != "ShaderNodeOutputMaterial":
|
|
nt.nodes.remove(n)
|
|
bsdf = nt.nodes.new("ShaderNodeBsdfPrincipled")
|
|
outs = [n for n in nt.nodes if n.bl_idname == "ShaderNodeOutputMaterial"]
|
|
outn = outs[0] if outs else nt.nodes.new("ShaderNodeOutputMaterial")
|
|
nt.links.new(bsdf.outputs[0], outn.inputs["Surface"])
|
|
socket(bsdf, "Base Color").default_value = base
|
|
socket(bsdf, "Metallic").default_value = metallic
|
|
socket(bsdf, "Roughness").default_value = rough
|
|
e = socket(bsdf, "Emission Color", "Emission")
|
|
if emission and e:
|
|
e.default_value = emission + (1.0,)
|
|
s = socket(bsdf, "Emission Strength")
|
|
if s:
|
|
s.default_value = strength
|
|
a = socket(bsdf, "Alpha")
|
|
if a:
|
|
a.default_value = alpha
|
|
return bsdf
|
|
|
|
|
|
def tex(mat, img, to, normal=False):
|
|
nt = mat.node_tree
|
|
t = nt.nodes.new("ShaderNodeTexImage")
|
|
t.image = img
|
|
if normal:
|
|
if not OLD:
|
|
img.colorspace_settings.name = "Non-Color"
|
|
nm = nt.nodes.new("ShaderNodeNormalMap")
|
|
nm.inputs["Strength"].default_value = 0.7
|
|
nt.links.new(t.outputs["Color"], nm.inputs["Color"])
|
|
nt.links.new(nm.outputs["Normal"], to)
|
|
else:
|
|
nt.links.new(t.outputs["Color"], to)
|
|
|
|
|
|
def camera(name, ortho=False):
|
|
cam = bpy.data.cameras.new(name)
|
|
if ortho:
|
|
cam.type = "ORTHO"
|
|
cam.ortho_scale = 7.0
|
|
else:
|
|
cam.lens = 35.0
|
|
cam.sensor_width = 36.0
|
|
cam.clip_start, cam.clip_end = 0.1, 100.0
|
|
return cam
|
|
|
|
|
|
def light(name, kind):
|
|
data = (bpy.data.lamps if OLD else bpy.data.lights).new(name, kind)
|
|
data.color = (1.0, 0.5, 0.25)
|
|
data.energy = {"POINT": 1000.0, "SUN": 3.0, "SPOT": 500.0, "AREA": 200.0}[kind]
|
|
if kind == "SPOT":
|
|
data.spot_size = 0.8
|
|
data.spot_blend = 0.2
|
|
return data
|
|
|
|
|
|
def save(path, compress=False, compat=False):
|
|
kw = {"filepath": path, "compress": compress}
|
|
if compat:
|
|
kw["use_mesh_compat"] = True
|
|
bpy.ops.wm.save_as_mainfile(**kw)
|
|
|
|
|
|
def update():
|
|
if OLD:
|
|
bpy.context.scene.update()
|
|
else:
|
|
bpy.context.view_layer.update()
|
|
|
|
|
|
def scene_file():
|
|
reset()
|
|
sc = bpy.context.scene
|
|
props = inner = None
|
|
if not OLD:
|
|
props = bpy.data.collections.new("Props")
|
|
sc.collection.children.link(props)
|
|
inner = bpy.data.collections.new("Inner")
|
|
props.children.link(inner)
|
|
cube_v = [(-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1)]
|
|
cube_f = [(0, 3, 2, 1), (4, 5, 6, 7), (0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]
|
|
me = mesh("BoxMesh", cube_v, cube_f)
|
|
uv(me, "UVMap", lambda i: ((i % 4) / 4 + 0.1, (i // 4) / 6))
|
|
uv(me, "Second", lambda i: (1 - (i % 3) / 3, (i % 2) * 0.5))
|
|
byte_colors(me, "Col")
|
|
if V >= (3, 2):
|
|
pc = me.color_attributes.new("PCol", "FLOAT_COLOR", "POINT")
|
|
for i, d in enumerate(pc.data):
|
|
d.color = (i / 8, 1 - i / 8, 0.5 + i / 16, 1.0)
|
|
checker = image("checker", 8, 8, lambda x, y: [float((x + y) % 2), 0.5, 0.25, 1.0])
|
|
bumps = image("bumps", 4, 4, lambda x, y: [0.5 + x / 8, 0.5 + y / 8, 1.0, 1.0])
|
|
outside = image("outside", 2, 2, lambda x, y: [1.0, 0.0, x * 1.0, 1.0], pack=False,
|
|
path=os.path.join(out, "outside.png"))
|
|
red = bpy.data.materials.new("Red")
|
|
bsdf = principled(red, (0.8, 0.1, 0.1, 1.0), 0.3, 0.4, (0.2, 0.3, 0.4), 2.0, 0.9)
|
|
tex(red, checker, socket(bsdf, "Base Color"))
|
|
tex(red, bumps, socket(bsdf, "Normal"), normal=True)
|
|
blue = bpy.data.materials.new("Blue")
|
|
blue.diffuse_color = (0.1, 0.2, 0.9) if OLD else (0.1, 0.2, 0.9, 1.0)
|
|
if not OLD:
|
|
blue.metallic = 0.25
|
|
blue.roughness = 0.6
|
|
if V >= (5, 0):
|
|
blue.use_nodes = False
|
|
me.materials.append(red)
|
|
me.materials.append(blue)
|
|
for i, p in enumerate(me.polygons):
|
|
p.material_index = i % 2
|
|
box = link(bpy.data.objects.new("Box", me), props)
|
|
box.location = (1, 2, 3)
|
|
box.rotation_euler = (0.3, 0.2, 0.1)
|
|
box.scale = (1, 2, 0.5)
|
|
box.modifiers.new("Subdiv", "SUBSURF")
|
|
# triangle, quad, convex pentagon, concave hexagon
|
|
sv = [(0, 0, 0), (1, 0, 0), (0, 1, 0), (2, 0, 0), (3, 0, 0), (3, 1, 0), (2, 1, 0),
|
|
(4, 0, 0), (5, 0, 0), (5.5, 0.8, 0), (4.5, 1.5, 0), (3.6, 0.8, 0),
|
|
(6, 0, 0), (8, 0, 0), (8, 2, 0), (7, 0.5, 0), (6, 2, 0), (6.5, 1, 0.2)]
|
|
sf = [(0, 1, 2), (3, 4, 5, 6), (7, 8, 9, 10, 11), (12, 13, 14, 15, 16, 17)]
|
|
shape_me = mesh("ShapeMesh", sv, sf, smooth=True)
|
|
glow = bpy.data.materials.new("Glow")
|
|
gb = principled(glow, (0.2, 0.9, 0.2, 1.0), 0.0, 0.8, (1.0, 0.8, 0.1), 5.0)
|
|
tex(glow, outside, socket(gb, "Emission Color", "Emission") or socket(gb, "Subsurface Color"))
|
|
shape_me.materials.append(glow)
|
|
shape = link(bpy.data.objects.new("Shape", shape_me), props)
|
|
shape.location = (-4, 0, 1)
|
|
shape.modifiers.new("Mirror", "MIRROR")
|
|
ball_me = mesh("BallMesh", [(math.cos(a * math.pi / 3) * 0.5, math.sin(a * math.pi / 3) * 0.5, z)
|
|
for z in (-0.4, 0.4) for a in range(6)],
|
|
[(0, 1, 7, 6), (1, 2, 8, 7), (2, 3, 9, 8), (3, 4, 10, 9), (4, 5, 11, 10), (5, 0, 6, 11),
|
|
(5, 4, 3, 2, 1, 0), (6, 7, 8, 9, 10, 11)], smooth=True)
|
|
ball = link(bpy.data.objects.new("Ball", ball_me), inner)
|
|
ball.location = (-3, 1, 0.5)
|
|
ball.rotation_euler = (0, 0, 0.7)
|
|
update()
|
|
ball.parent = box
|
|
ball.matrix_parent_inverse = box.matrix_world.inverted()
|
|
pivot = link(bpy.data.objects.new("Pivot", None), props)
|
|
pivot.location = (0, -3, 1)
|
|
pivot.rotation_mode = "QUATERNION"
|
|
q = (0.9, 0.1, 0.3, 0.2)
|
|
n = math.sqrt(sum(c * c for c in q))
|
|
pivot.rotation_quaternion = tuple(c / n for c in q)
|
|
leaf_me = mesh("LeafMesh", [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)], [(0, 1, 2, 3)])
|
|
leaf_me.materials.append(glow)
|
|
leaf = link(bpy.data.objects.new("Leaf", leaf_me), props)
|
|
leaf.parent = pivot
|
|
leaf.location = (0.5, 0.25, 0)
|
|
leaf.rotation_mode = "ZXY"
|
|
leaf.rotation_euler = (0.4, -0.3, 1.1)
|
|
leaf.scale = (1.5, 1, 1)
|
|
leaf.delta_location = (0, 0, 0.5)
|
|
leaf.delta_rotation_euler = (0, 0.2, 0)
|
|
leaf.delta_scale = (1, 1, 2)
|
|
# smooth grid with custom normals
|
|
gv = [(x, y, 0.1 * x * y) for y in range(3) for x in range(3)]
|
|
gf = [(0, 1, 4, 3), (1, 2, 5, 4), (3, 4, 7, 6), (4, 5, 8, 7)]
|
|
grid_me = mesh("BentMesh", gv, gf, smooth=True)
|
|
if V < (4, 1):
|
|
grid_me.use_auto_smooth = True
|
|
grid_me.normals_split_custom_set_from_vertices([(0.3 * (i % 3 - 1), 0.2 * (i // 3 - 1), 1.0) for i in range(9)])
|
|
bent = link(bpy.data.objects.new("Bent", grid_me), props)
|
|
bent.location = (0, 4, 0)
|
|
cp = link(bpy.data.objects.new("CamP", camera("CamP")))
|
|
cp.location = (7, -7, 5)
|
|
cp.rotation_euler = (1.1, 0, 0.8)
|
|
co = link(bpy.data.objects.new("CamO", camera("CamO", True)))
|
|
co.location = (0, 0, 10)
|
|
for kind in ("POINT", "SUN", "SPOT", "AREA"):
|
|
lo = link(bpy.data.objects.new("L" + kind.lower(), light("L" + kind.lower(), kind)))
|
|
lo.location = ({"POINT": 4, "SUN": 0, "SPOT": -4, "AREA": 2}[kind], 1, 6)
|
|
lo.rotation_euler = (0.5, 0.1, 0)
|
|
sc.frame_set(1)
|
|
update()
|
|
save(os.path.join(out, "scene_%s.blend" % TAG))
|
|
save(os.path.join(out, "scene_%s_z.blend" % TAG), compress=True)
|
|
|
|
|
|
def rig_file():
|
|
reset()
|
|
sc = bpy.context.scene
|
|
arm_data = bpy.data.armatures.new("RigData")
|
|
rig = link(bpy.data.objects.new("Rig", arm_data))
|
|
rig.location = (0, 0, 0.5)
|
|
rig.rotation_euler = (0, 0, 0.2)
|
|
if OLD:
|
|
sc.objects.active = rig
|
|
else:
|
|
bpy.context.view_layer.objects.active = rig
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
eb = arm_data.edit_bones
|
|
root = eb.new("root")
|
|
root.head, root.tail, root.roll = (0, 0, 0), (0, 0, 1), 0.0
|
|
mid = eb.new("mid")
|
|
mid.head, mid.tail, mid.roll, mid.parent, mid.use_connect = (0, 0, 1), (0, 0.5, 2), 0.3, root, True
|
|
tip = eb.new("tip")
|
|
tip.head, tip.tail, tip.roll, tip.parent, tip.use_connect = (0, 0.5, 2), (0.3, 0.5, 2.8), -0.5, mid, True
|
|
aux = eb.new("aux")
|
|
aux.head, aux.tail, aux.roll, aux.parent = (0.5, 0, 0.5), (1, 0, 0.5), 0.0, root
|
|
nodef = eb.new("nodef")
|
|
nodef.head, nodef.tail, nodef.parent, nodef.use_deform = (-0.5, 0, 0.5), (-1, 0, 0.7), root, False
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
ring = [(math.cos(a * math.pi / 4) * 0.3, math.sin(a * math.pi / 4) * 0.3) for a in range(8)]
|
|
bv = [(x, y + (0.25 if z == 2 else 0), z) for z in (0, 1, 2) for x, y in ring]
|
|
bf = [(r * 8 + a, r * 8 + (a + 1) % 8, (r + 1) * 8 + (a + 1) % 8, (r + 1) * 8 + a) for r in range(2) for a in range(8)]
|
|
body_me = mesh("BodyMesh", bv, bf, smooth=True)
|
|
body = link(bpy.data.objects.new("Body", body_me))
|
|
body.parent = rig
|
|
body.location = (0, 0, 0)
|
|
mod = body.modifiers.new("Armature", "ARMATURE")
|
|
mod.object = rig
|
|
groups = {n: body.vertex_groups.new(name=n) for n in ("root", "mid", "tip", "aux", "nodef", "extra")}
|
|
for i in range(24):
|
|
z = i // 8
|
|
groups["root"].add([i], max(0.0, 1.0 - z * 0.6), "REPLACE")
|
|
groups["mid"].add([i], 0.3 + 0.2 * (z == 1), "REPLACE")
|
|
if z == 2:
|
|
groups["tip"].add([i], 0.9, "REPLACE")
|
|
if i % 3 == 0:
|
|
groups["aux"].add([i], 0.05 + 0.01 * i, "REPLACE")
|
|
groups["nodef"].add([i], 0.7, "REPLACE")
|
|
groups["extra"].add([i], 0.15, "REPLACE")
|
|
body.shape_key_add(name="Basis")
|
|
fat = body.shape_key_add(name="Fat")
|
|
for i, p in enumerate(fat.data):
|
|
p.co = (p.co[0] * 1.5, p.co[1] * 1.5, p.co[2])
|
|
thin = body.shape_key_add(name="Thin")
|
|
for i, p in enumerate(thin.data):
|
|
p.co = (p.co[0] * 0.5, p.co[1] * 0.5, p.co[2] + 0.1)
|
|
thin.value = 0.25
|
|
key = body_me.shape_keys
|
|
for f, v in ((1, 0.0), (10, 1.0), (20, 0.2)):
|
|
fat.value = v
|
|
fat.keyframe_insert("value", frame=f)
|
|
hat_me = mesh("HatMesh", [(-0.2, -0.2, 0), (0.2, -0.2, 0), (0, 0.2, 0), (0, 0, 0.3)], [(0, 1, 2), (0, 1, 3), (1, 2, 3), (2, 0, 3)])
|
|
hat = link(bpy.data.objects.new("Hat", hat_me))
|
|
hat.parent = rig
|
|
hat.parent_type = "BONE"
|
|
hat.parent_bone = "tip"
|
|
hat.location = (0.1, 0.2, 0.0)
|
|
pose = rig.pose.bones
|
|
pose["aux"].rotation_mode = "XYZ"
|
|
keys = ((1, (0, 0, 0), (1, 0, 0, 0), (1, 1, 1), (0, 0, 0)),
|
|
(10, (0.2, 0.1, 0), (0.92, 0.3, 0.2, 0.1), (1.2, 1, 1), (0.4, 0, 0.3)),
|
|
(20, (0, 0.3, 0.2), (0.8, -0.2, 0.5, 0.2), (0.8, 1.1, 1), (-0.3, 0.5, 0)))
|
|
for f, loc, quat, scl, eul in keys:
|
|
pose["root"].location = loc
|
|
pose["root"].keyframe_insert("location", frame=f)
|
|
n = math.sqrt(sum(c * c for c in quat))
|
|
pose["mid"].rotation_quaternion = tuple(c / n for c in quat)
|
|
pose["mid"].keyframe_insert("rotation_quaternion", frame=f)
|
|
pose["tip"].scale = scl
|
|
pose["tip"].keyframe_insert("scale", frame=f)
|
|
pose["aux"].rotation_euler = eul
|
|
pose["aux"].keyframe_insert("rotation_euler", frame=f)
|
|
mover = link(bpy.data.objects.new("Mover", mesh("MoverMesh", [(0, 0, 0), (1, 0, 0), (0, 1, 0)], [(0, 1, 2)])))
|
|
for f, loc, rot, scl in ((1, (3, 0, 0), (0, 0, 0), (1, 1, 1)), (8, (3, 2, 1), (0.5, 0, 1), (2, 1, 1)),
|
|
(20, (5, 2, 0), (0, 1, -1), (1, 1, 3))):
|
|
mover.location, mover.rotation_euler, mover.scale = loc, rot, scl
|
|
mover.keyframe_insert("location", frame=f)
|
|
mover.keyframe_insert("rotation_euler", frame=f)
|
|
mover.keyframe_insert("scale", frame=f)
|
|
# easing interpolations (Penner) with explicit and automatic easing
|
|
easer = link(bpy.data.objects.new("Easer", None))
|
|
eases = (("SINE", "EASE_IN_OUT"), ("BOUNCE", "AUTO"), ("SINE", "EASE_OUT"), ("BACK", "EASE_IN"),
|
|
("EXPO", "AUTO"), ("CIRC", "EASE_OUT"), ("QUART", "EASE_IN_OUT"), ("EXPO", "EASE_OUT"), ("EXPO", "EASE_IN_OUT"), ("CUBIC", "EASE_IN"))
|
|
for k in range(len(eases) + 1):
|
|
easer.location = (k % 2 * 2.0, k * 0.5, 0)
|
|
easer.keyframe_insert("location", frame=1 + k * 2.5)
|
|
for fc in (easer.animation_data.action.fcurves if hasattr(easer.animation_data.action, "fcurves") else
|
|
[c for l in easer.animation_data.action.layers for s in l.strips for b in s.channelbags for c in b.fcurves]):
|
|
for kp, (ipo, easing) in zip(fc.keyframe_points, eases):
|
|
kp.interpolation = ipo
|
|
kp.easing = easing
|
|
rig_action = rig.animation_data.action
|
|
mover_action = mover.animation_data.action
|
|
for act, rules in ((rig_action, {"location": "LINEAR", "scale": "CONSTANT"}),
|
|
(mover_action, {"rotation_euler": "LINEAR", "scale": "CONSTANT"})):
|
|
curves = act.fcurves if hasattr(act, "fcurves") else [c for l in act.layers for s in l.strips for b in s.channelbags for c in b.fcurves]
|
|
for fc in curves:
|
|
for k, ipo in rules.items():
|
|
if fc.data_path.endswith(k):
|
|
for kp in fc.keyframe_points:
|
|
kp.interpolation = ipo
|
|
if fc.data_path == "location" and fc.array_index == 2:
|
|
fc.extrapolation = "LINEAR"
|
|
sc.frame_set(1)
|
|
update()
|
|
save(os.path.join(out, "rig_%s.blend" % TAG))
|
|
|
|
|
|
def setp(obj, attr, value):
|
|
if hasattr(obj, attr):
|
|
try:
|
|
setattr(obj, attr, value)
|
|
except (TypeError, ValueError, AttributeError):
|
|
pass
|
|
|
|
|
|
def ctl_file():
|
|
reset()
|
|
sc = bpy.context.scene
|
|
tgt = link(bpy.data.objects.new("Tgt", None))
|
|
tgt.location = (1, 2, 3)
|
|
tgt.rotation_euler = (0.2, 0.4, 0.1)
|
|
cv = [(math.cos(a * math.pi / 4), math.sin(a * math.pi / 4), 0) for a in range(8)]
|
|
circle = bpy.data.meshes.new("WGT_circle")
|
|
circle.from_pydata(cv, [(a, (a + 1) % 8) for a in range(8)], [])
|
|
circle.update()
|
|
wcircle = bpy.data.objects.new("WGT_circle", circle)
|
|
if OLD:
|
|
wcircle.use_fake_user = True
|
|
else:
|
|
wgts = bpy.data.collections.new("Widgets") # not linked to the scene
|
|
wgts.objects.link(wcircle)
|
|
box = bpy.data.meshes.new("WGT_box")
|
|
box.from_pydata([(x, y, z) for z in (0, 1) for y in (-0.5, 0.5) for x in (-0.5, 0.5)],
|
|
[(0, 1), (1, 3), (3, 2), (2, 0), (4, 5), (5, 7), (7, 6), (6, 4), (0, 4), (1, 5), (2, 6), (3, 7)], [])
|
|
box.update()
|
|
wbox = link(bpy.data.objects.new("WGT_box", box))
|
|
wbox.location = (0, 0, -3)
|
|
if OLD:
|
|
wbox.hide = True
|
|
else:
|
|
wbox.hide_viewport = True
|
|
wbox.hide_render = True
|
|
arm_data = bpy.data.armatures.new("CtlData")
|
|
rig = link(bpy.data.objects.new("Ctl", arm_data))
|
|
rig.location = (0.5, -1, 0.25)
|
|
rig.rotation_euler = (0.1, 0, 0.3)
|
|
rig.scale = (1, 1, 1.5)
|
|
if OLD:
|
|
sc.objects.active = rig
|
|
else:
|
|
bpy.context.view_layer.objects.active = rig
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
eb = arm_data.edit_bones
|
|
spec = ( # name, head, tail, roll, parent, connect, deform
|
|
("root", (0, 0, 0), (0, 1, 0), 0.0, None, False, False),
|
|
("spine", (0, 0, 0.2), (0, 0.1, 1.2), 0.4, "root", False, True),
|
|
("upper", (0, 0.1, 1.2), (0.8, 0.1, 1.3), -0.7, "spine", True, True),
|
|
("lower", (0.8, 0.1, 1.3), (1.6, 0.3, 1.1), 1.1, "upper", True, True),
|
|
("hand_ik", (1.6, 0.3, 1.1), (1.6, 0.8, 1.1), 0.0, "root", False, False),
|
|
("pole", (0.8, -1, 1.3), (0.8, -1.3, 1.3), 0.2, "root", False, False),
|
|
("limit", (-0.5, 0, 0.5), (-0.5, 0, 1.1), 2.5, "root", False, True),
|
|
)
|
|
for name, head, tail, roll, parent, connect, deform in spec:
|
|
b = eb.new(name)
|
|
b.head, b.tail, b.roll = head, tail, roll
|
|
if parent:
|
|
b.parent = eb[parent]
|
|
b.use_connect = connect
|
|
b.use_deform = deform
|
|
up, lo = eb["hand_ik"], eb["lower"] # b-bone settings on a bone that deforms nothing (skinning stays linear)
|
|
setp(eb["upper"], "inherit_scale", "ALIGNED")
|
|
setp(eb["upper"], "use_inherit_scale", False)
|
|
setp(lo, "use_inherit_rotation", False)
|
|
setp(lo, "use_local_location", False)
|
|
setp(eb["limit"], "use_relative_parent", True)
|
|
up.bbone_segments = 4
|
|
up.bbone_x, up.bbone_z = 0.05, 0.08
|
|
setp(up, "bbone_easein", 0.5)
|
|
setp(up, "bbone_easeout", 1.5)
|
|
setp(up, "bbone_rollin", 0.2)
|
|
setp(up, "bbone_curveinx", 0.1)
|
|
setp(up, "bbone_curveinz", -0.1)
|
|
setp(up, "bbone_curveoutx", 0.3)
|
|
setp(up, "bbone_scalein", (1.0, 1.0, 1.2))
|
|
setp(up, "bbone_handle_type_start", "ABSOLUTE")
|
|
setp(up, "bbone_custom_handle_start", eb["spine"])
|
|
setp(up, "bbone_handle_type_end", "TANGENT")
|
|
setp(up, "bbone_custom_handle_end", eb["pole"])
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
bones = arm_data.bones
|
|
bones["root"].show_wire = True
|
|
pose = rig.pose.bones
|
|
if V >= (4, 0):
|
|
ctrl = arm_data.collections.new("Ctrl")
|
|
try:
|
|
ik = arm_data.collections.new("IK", parent=ctrl)
|
|
except TypeError:
|
|
ik = arm_data.collections.new("IK")
|
|
deform = arm_data.collections.new("Deform")
|
|
mch = arm_data.collections.new("MCH")
|
|
for c in list(arm_data.collections):
|
|
for b in list(c.bones):
|
|
c.unassign(b)
|
|
for n in ("root", "hand_ik", "pole"):
|
|
ctrl.assign(bones[n])
|
|
for n in ("hand_ik", "pole"):
|
|
ik.assign(bones[n])
|
|
for n in ("spine", "upper", "lower", "limit"):
|
|
deform.assign(bones[n])
|
|
mch.assign(bones["limit"])
|
|
ik.is_visible = False
|
|
mch.is_visible = False
|
|
bones["root"].color.palette = "THEME04"
|
|
bones["pole"].color.palette = "CUSTOM"
|
|
bones["pole"].color.custom.normal = (1.0, 0.0, 0.0)
|
|
bones["pole"].color.custom.select = (0.0, 1.0, 0.0)
|
|
bones["pole"].color.custom.active = (0.0, 0.0, 1.0)
|
|
pose["spine"].color.palette = "THEME09"
|
|
else:
|
|
layers = {"root": (0,), "hand_ik": (0, 1), "pole": (0, 1), "spine": (16,), "upper": (16,), "lower": (16,), "limit": (16, 31)}
|
|
for n, ls in layers.items():
|
|
bones[n].layers = [i in ls for i in range(32)]
|
|
arm_data.layers = [i in (0, 16) for i in range(32)]
|
|
g1 = rig.pose.bone_groups.new(name="Ctrls")
|
|
g1.color_set = "THEME04"
|
|
g2 = rig.pose.bone_groups.new(name="Custom")
|
|
g2.color_set = "CUSTOM"
|
|
g2.colors.normal = (1.0, 0.0, 0.0)
|
|
g2.colors.select = (0.0, 1.0, 0.0)
|
|
g2.colors.active = (0.0, 0.0, 1.0)
|
|
pose["root"].bone_group = g1
|
|
pose["hand_ik"].bone_group = g1
|
|
pose["pole"].bone_group = g2
|
|
# custom shapes
|
|
pose["root"].custom_shape = wcircle
|
|
setp(pose["root"], "custom_shape_scale", 1.5)
|
|
setp(pose["root"], "custom_shape_scale_xyz", (1.5, 0.5, 2.0))
|
|
pose["spine"].custom_shape = wbox
|
|
pose["spine"].custom_shape_transform = pose["root"]
|
|
setp(pose["spine"], "custom_shape_translation", (0, 0.1, 0))
|
|
setp(pose["spine"], "custom_shape_rotation_euler", (0.1, 0.2, 0.3))
|
|
setp(pose["spine"], "use_custom_shape_bone_size", False)
|
|
pose["pole"].custom_shape = wcircle
|
|
# locks, rotation modes, IK settings
|
|
pose["root"].lock_location = (True, True, True)
|
|
pose["spine"].rotation_mode = "QUATERNION"
|
|
pose["spine"].lock_rotation = (True, False, True)
|
|
pose["spine"].lock_rotation_w = True
|
|
pose["spine"].lock_rotations_4d = True
|
|
pose["limit"].lock_scale = (False, True, False)
|
|
pose["upper"].ik_stretch = 0.1
|
|
pose["upper"].lock_ik_y = True
|
|
pose["upper"].use_ik_limit_x = True
|
|
pose["upper"].ik_min_x = -0.5
|
|
pose["upper"].ik_max_x = 0.7
|
|
pose["upper"].ik_stiffness_z = 0.25
|
|
pose["lower"].rotation_mode = "YZX"
|
|
pose["hand_ik"].rotation_mode = "AXIS_ANGLE"
|
|
# constraints
|
|
c = pose["spine"].constraints.new("COPY_ROTATION")
|
|
c.name = "CopyRot"
|
|
c.target = tgt
|
|
c.use_y = False
|
|
c.invert_x = True
|
|
setp(c, "mix_mode", "ADD")
|
|
c.owner_space, c.target_space = "LOCAL_WITH_PARENT", "LOCAL"
|
|
c.influence = 0.75
|
|
c = pose["upper"].constraints.new("COPY_LOCATION")
|
|
c.target, c.subtarget = rig, "root"
|
|
c.use_offset = True
|
|
c.use_z = False
|
|
c.invert_z = True
|
|
c.head_tail = 0.5
|
|
c.target_space = "POSE"
|
|
c = pose["upper"].constraints.new("LIMIT_LOCATION")
|
|
c.use_min_x, c.min_x = True, -0.25
|
|
c.use_max_z, c.max_z = True, 0.75
|
|
c.use_transform_limit = True
|
|
c = pose["upper"].constraints.new("COPY_SCALE")
|
|
c.target = tgt
|
|
c.use_y = False
|
|
c.use_offset = True
|
|
c = pose["lower"].constraints.new("IK")
|
|
c.target, c.subtarget = rig, "hand_ik"
|
|
c.pole_target, c.pole_subtarget = rig, "pole"
|
|
c.pole_angle = -1.2
|
|
c.chain_count = 2
|
|
c.use_tail = True
|
|
c.use_stretch = False
|
|
c.iterations = 300
|
|
c.weight = 0.6
|
|
c = pose["hand_ik"].constraints.new("COPY_TRANSFORMS")
|
|
c.target = tgt
|
|
setp(c, "mix_mode", "BEFORE")
|
|
c.influence = 0.3
|
|
c.mute = True
|
|
c = pose["pole"].constraints.new("DAMPED_TRACK")
|
|
c.target, c.subtarget = rig, "lower"
|
|
c.track_axis = "TRACK_NEGATIVE_Z"
|
|
c.head_tail = 0.25
|
|
c = pose["limit"].constraints.new("LIMIT_ROTATION")
|
|
c.use_limit_x, c.min_x, c.max_x = True, -0.3, 0.4
|
|
c.use_limit_z, c.min_z, c.max_z = True, -1.0, 1.0
|
|
c.owner_space = "LOCAL"
|
|
c = pose["limit"].constraints.new("CHILD_OF")
|
|
c.name = "Parent"
|
|
c.target = tgt
|
|
c.use_rotation_y = False
|
|
c.use_scale_x = False
|
|
from mathutils import Matrix
|
|
c.inverse_matrix = Matrix(((1, 0, 0, 0.5), (0, 0, -1, 0.25), (0, 1, 0, -1), (0, 0, 0, 1)))
|
|
c = pose["limit"].constraints.new("STRETCH_TO")
|
|
c.target = tgt
|
|
c.influence = 0.5
|
|
c.rest_length = 0.75
|
|
c.bulge = 1.5
|
|
c.volume = "VOLUME_X"
|
|
c.keep_axis = "PLANE_Z"
|
|
setp(c, "use_bulge_min", True)
|
|
setp(c, "bulge_min", 0.5)
|
|
setp(c, "use_bulge_max", True)
|
|
setp(c, "bulge_max", 2.0)
|
|
setp(c, "bulge_smooth", 0.3)
|
|
c = pose["limit"].constraints.new("TRANSFORM")
|
|
c.target = tgt
|
|
c.map_from, c.map_to = "ROTATION", "SCALE"
|
|
c.map_to_x_from, c.map_to_y_from, c.map_to_z_from = "Z", "X", "Y"
|
|
setp(c, "from_max_x_rot", 2.356194)
|
|
setp(c, "from_min_y_rot", -0.5)
|
|
setp(c, "to_max_y_scale", 0.65)
|
|
setp(c, "to_min_x_scale", 0.9)
|
|
setp(c, "mix_mode_scale", "MULTIPLY")
|
|
setp(c, "from_rotation_mode", "SWING_TWIST_Y")
|
|
setp(c, "to_euler_order", "ZXY")
|
|
c.use_motion_extrapolate = True
|
|
c = pose["limit"].constraints.new("FLOOR")
|
|
c.target = tgt
|
|
c.floor_location = "FLOOR_NEGATIVE_Y"
|
|
c.offset = 0.2
|
|
c.use_rotation = True
|
|
c = pose["pole"].constraints.new("TRACK_TO")
|
|
c.target, c.subtarget = rig, "upper"
|
|
c.track_axis, c.up_axis = "TRACK_NEGATIVE_Y", "UP_X"
|
|
c.use_target_z = True
|
|
c = pose["root"].constraints.new("LOCKED_TRACK")
|
|
c.target = tgt
|
|
c.track_axis, c.lock_axis = "TRACK_X", "LOCK_Z"
|
|
c = pose["root"].constraints.new("LIMIT_DISTANCE")
|
|
c.target = tgt
|
|
c.distance = 2.5
|
|
c.limit_mode = "LIMITDIST_OUTSIDE"
|
|
c = pose["upper"].constraints["Copy Scale"]
|
|
setp(c, "use_make_uniform", True)
|
|
setp(c, "use_add", True)
|
|
setp(c, "power", 0.5)
|
|
if V >= (2, 80):
|
|
c = pose["spine"].constraints.new("ARMATURE")
|
|
t = c.targets.new()
|
|
t.target, t.subtarget, t.weight = rig, "root", 0.75
|
|
c.use_deform_preserve_volume = True
|
|
c.use_bone_envelopes = True
|
|
c.use_current_location = True
|
|
# an action only the action constraint uses: keys on hand_ik / limit, then unassigned
|
|
for f, loc, eul in ((1, (0, 0, 0), (0, 0, 0)), (10, (0.5, 0.1, 0), (0.3, 0, -0.2)), (25, (0, 0.4, 0.2), (-0.4, 0.2, 0))):
|
|
pose["hand_ik"].location = loc
|
|
pose["hand_ik"].keyframe_insert("location", frame=f)
|
|
pose["limit"].rotation_euler = eul
|
|
pose["limit"].keyframe_insert("rotation_euler", frame=f)
|
|
act = rig.animation_data.action
|
|
act.name = "CtlAct"
|
|
act.use_fake_user = True
|
|
curves = act.fcurves if hasattr(act, "fcurves") and V < (5, 0) else [c for l in act.layers for st in l.strips for b in st.channelbags for c in b.fcurves]
|
|
for fc in curves:
|
|
if fc.data_path.endswith("location"):
|
|
for kp in fc.keyframe_points:
|
|
kp.interpolation = "LINEAR"
|
|
elif fc.array_index == 1:
|
|
fc.keyframe_points[1].interpolation = "QUAD"
|
|
fc.keyframe_points[1].easing = "EASE_OUT"
|
|
fc.extrapolation = "LINEAR"
|
|
rig.animation_data.action = None
|
|
pose["hand_ik"].location = (0, 0, 0)
|
|
pose["limit"].rotation_euler = (0, 0, 0)
|
|
c = pose["hand_ik"].constraints.new("ACTION")
|
|
c.target, c.subtarget = rig, "pole"
|
|
c.action = act
|
|
c.transform_channel = "LOCATION_Y"
|
|
c.target_space = "LOCAL"
|
|
c.frame_start, c.frame_end = 0, 40
|
|
c.min, c.max = -0.05, 0.05
|
|
setp(c, "mix_mode", "AFTER")
|
|
setp(c, "use_eval_time", False)
|
|
setp(c, "eval_time", 0.25)
|
|
c.influence = 0.5
|
|
# a saved pose basis
|
|
pose["spine"].location = (0.01, 0.02, 0.03) # no rotation above the hinge bone (not modelled by joint nodes)
|
|
pose["pole"].rotation_quaternion = (0.9, 0.1, 0.3, 0.2)
|
|
pose["lower"].rotation_euler = (0.1, -0.2, 0.3)
|
|
pose["hand_ik"].rotation_axis_angle = (0.4, 0, 0, 1)
|
|
pose["limit"].scale = (1, 1.25, 0.8)
|
|
# shape keys on a mesh parented to the rig
|
|
fv = [(x * 0.5, 0, z * 0.5) for z in range(3) for x in range(3)]
|
|
ff = [(0, 1, 4, 3), (1, 2, 5, 4), (3, 4, 7, 6), (4, 5, 8, 7)]
|
|
face = link(bpy.data.objects.new("Face", mesh("FaceMesh", fv, ff)))
|
|
face.parent = rig
|
|
face.location = (0, -0.5, 0)
|
|
face.modifiers.new("Armature", "ARMATURE").object = rig
|
|
vg = face.vertex_groups.new(name="upper")
|
|
vg.add([6, 7, 8], 1.0, "REPLACE")
|
|
vg.add([3, 4, 5], 0.4, "REPLACE")
|
|
face.vertex_groups.new(name="spine").add(list(range(6)), 1.0, "REPLACE")
|
|
face.shape_key_add(name="Basis")
|
|
smile = face.shape_key_add(name="Smile")
|
|
for i, p in enumerate(smile.data):
|
|
p.co = (p.co[0], p.co[1] + 0.1 * i, p.co[2])
|
|
smile.slider_min, smile.slider_max = -0.5, 2.0
|
|
smile.value = 1.5
|
|
blink = face.shape_key_add(name="Blink")
|
|
for i, p in enumerate(blink.data):
|
|
p.co = (p.co[0] + 0.05 * i, p.co[1] + 0.1 * i, p.co[2] - 0.02 * i)
|
|
blink.relative_key = smile
|
|
blink.mute = True
|
|
blink.vertex_group = "upper"
|
|
blink.value = 0.5
|
|
puff = face.shape_key_add(name="Puff")
|
|
for i, p in enumerate(puff.data):
|
|
p.co = (p.co[0] * 1.2, p.co[1] - 0.05, p.co[2])
|
|
puff.value = 0.3
|
|
for f, v in ((1, 0.0), (10, 2.0), (20, -0.5)):
|
|
smile.value = v
|
|
smile.keyframe_insert("value", frame=f)
|
|
smile.value = 1.5
|
|
# vertex / triangle / lattice / bone parents
|
|
from mathutils import Matrix
|
|
anchor = link(bpy.data.objects.new("Anchor", mesh("AnchorMesh", [(0, 0, 0), (1, 0.2, 0), (0.3, 1, 0.4), (0.5, 0.5, 1)], [(0, 1, 2), (0, 2, 3)])))
|
|
anchor.location, anchor.rotation_euler, anchor.scale = (2, -2, 1), (0.3, 0.2, 0.5), (1, 1.5, 1)
|
|
update()
|
|
vkid = link(bpy.data.objects.new("VertKid", None))
|
|
vkid.parent, vkid.parent_type = anchor, "VERTEX"
|
|
vkid.parent_vertices = (2, 0, 0)
|
|
vkid.location = (0.1, 0.2, 0.3)
|
|
vkid.matrix_parent_inverse = Matrix.Translation((0.5, 0, -0.25))
|
|
tkid = link(bpy.data.objects.new("TriKid", None))
|
|
tkid.parent, tkid.parent_type = anchor, "VERTEX_3"
|
|
tkid.parent_vertices = (1, 2, 3)
|
|
tkid.location, tkid.rotation_euler = (0.2, 0, 0.1), (0.1, 0.4, 0)
|
|
lat = link(bpy.data.objects.new("Lat", bpy.data.lattices.new("LatData")))
|
|
lat.location = (-2, 1, 0)
|
|
caged = link(bpy.data.objects.new("Caged", mesh("CagedMesh", [(0, 0, 0), (1, 0, 0), (0, 1, 0)], [(0, 1, 2)])))
|
|
caged.parent = lat
|
|
setp(caged, "parent_type", "LATTICE")
|
|
caged.location = (0.25, 0, 0)
|
|
caged.modifiers.new("Lattice", "LATTICE").object = lat
|
|
bkid = link(bpy.data.objects.new("BoneKid", None))
|
|
bkid.parent, bkid.parent_type, bkid.parent_bone = rig, "BONE", "lower"
|
|
bkid.location = (0.1, 0.05, 0)
|
|
face.modifiers["Armature"].use_multi_modifier = True
|
|
am = caged.modifiers.new("Unbound", "ARMATURE") # no object: deforms nothing, its settings are data
|
|
am.use_deform_preserve_volume, am.use_bone_envelopes, am.use_vertex_groups = True, True, False
|
|
am.vertex_group, am.invert_vertex_group = "spine", True
|
|
am.show_viewport = False
|
|
sub = wbox.modifiers.new("Smooth", "SUBSURF")
|
|
sub.show_render = False
|
|
# custom properties (ID properties)
|
|
props = pose["root"]
|
|
props["ik_arm"] = 1.0
|
|
props["count"] = 3
|
|
props["label"] = "left"
|
|
props["weights"] = [0.25, 0.5, 0.75]
|
|
props["flags"] = [1, 2, 3]
|
|
props["nested"] = {"inner": 2.5, "name": "deep"}
|
|
if V >= (3, 0):
|
|
props["on"] = True
|
|
bones["spine"]["bone_prop"] = 7
|
|
rig["rig_prop"] = 0.5
|
|
face["face_prop"] = "smile"
|
|
if V >= (4, 2):
|
|
pose["limit"].constraints["Limit Rotation"].use_legacy_behavior = True
|
|
# drivers (data only for the reader: on constraint settings and a muted shape key, so the checked pose is unchanged)
|
|
def drive(owner, path, kind, expression="", index=-1):
|
|
fc = owner.driver_add(path) if index < 0 else owner.driver_add(path, index)
|
|
fc.driver.type = kind
|
|
if expression:
|
|
fc.driver.expression = expression
|
|
return fc
|
|
fc = drive(rig, 'pose.bones["limit"].constraints["Stretch To"].bulge_smooth', "SCRIPTED", "max(0, (distance - 0.4 * s) / 2) + ik")
|
|
setp(fc.driver, "use_self", True)
|
|
v = fc.driver.variables.new()
|
|
v.name, v.type = "distance", "LOC_DIFF"
|
|
v.targets[0].id, v.targets[0].bone_target = rig, "hand_ik"
|
|
v.targets[1].id, v.targets[1].bone_target = tgt, ""
|
|
v.targets[1].transform_space = "LOCAL_SPACE"
|
|
v = fc.driver.variables.new()
|
|
v.name, v.type = "s", "TRANSFORMS"
|
|
v.targets[0].id, v.targets[0].bone_target = rig, "upper"
|
|
v.targets[0].transform_type, v.targets[0].transform_space = "SCALE_Y", "TRANSFORM_SPACE"
|
|
setp(v.targets[0], "rotation_mode", "SWING_TWIST_Y")
|
|
v = fc.driver.variables.new()
|
|
v.name, v.type = "ik", "SINGLE_PROP"
|
|
v.targets[0].id = rig
|
|
v.targets[0].data_path = 'pose.bones["root"]["ik_arm"]'
|
|
for m in list(fc.modifiers):
|
|
fc.modifiers.remove(m)
|
|
fc.keyframe_points.insert(0.0, 0.0)
|
|
fc.keyframe_points.insert(1.0, 1.0)
|
|
for k in fc.keyframe_points:
|
|
k.interpolation = "LINEAR"
|
|
fc.extrapolation = "LINEAR"
|
|
fc = drive(rig, 'pose.bones["upper"].constraints["Copy Scale"].influence', "SUM")
|
|
v = fc.driver.variables.new()
|
|
v.name, v.type = "angle", "ROTATION_DIFF"
|
|
v.targets[0].id, v.targets[0].bone_target = rig, "spine"
|
|
v.targets[1].id, v.targets[1].bone_target = rig, "upper"
|
|
gen = fc.modifiers[0] if len(fc.modifiers) else fc.modifiers.new("GENERATOR")
|
|
gen.mode = "POLYNOMIAL"
|
|
gen.poly_order = 2
|
|
gen.coefficients = (0.5, 2.0, -1.0)
|
|
gen.use_additive = True
|
|
gen.influence = 0.8
|
|
gen.use_influence = True
|
|
lim = fc.modifiers.new("LIMITS")
|
|
lim.use_max_y, lim.max_y = True, 2.0
|
|
lim.mute = True
|
|
st = fc.modifiers.new("STEPPED")
|
|
st.frame_step = 2.0
|
|
st.use_restricted_range = True
|
|
st.frame_start, st.frame_end = 1.0, 10.0
|
|
fc = drive(rig, 'pose.bones["pole"].constraints["Damped Track"].head_tail', "MIN")
|
|
v = fc.driver.variables.new()
|
|
v.name = "z"
|
|
v.targets[0].id = tgt
|
|
v.targets[0].data_path = "location.z"
|
|
for m in list(fc.modifiers):
|
|
fc.modifiers.remove(m)
|
|
fc.modifiers.new("CYCLES")
|
|
nz = fc.modifiers.new("NOISE")
|
|
nz.scale, nz.strength = 2.0, 0.5
|
|
if V >= (4, 0):
|
|
v = fc.driver.variables.new()
|
|
v.name, v.type = "frame", "CONTEXT_PROP"
|
|
v.targets[0].context_property = "ACTIVE_SCENE"
|
|
v.targets[0].data_path = "frame_current"
|
|
fc = drive(arm_data, "bones[\"pole\"].bbone_x", "MAX")
|
|
v = fc.driver.variables.new()
|
|
v.name = "w"
|
|
v.targets[0].id_type = "ARMATURE"
|
|
v.targets[0].id = arm_data
|
|
v.targets[0].data_path = "bones[\"upper\"].bbone_x"
|
|
fc = drive(face.data.shape_keys, 'key_blocks["Blink"].value', "AVERAGE")
|
|
v = fc.driver.variables.new()
|
|
v.name, v.type = "rot", "TRANSFORMS"
|
|
v.targets[0].id, v.targets[0].bone_target = rig, "lower"
|
|
v.targets[0].transform_type, v.targets[0].transform_space = "ROT_X", "LOCAL_SPACE"
|
|
setp(v.targets[0], "rotation_mode", "XZY")
|
|
sc.frame_set(1)
|
|
update()
|
|
save(os.path.join(out, "ctl_%s.blend" % TAG))
|
|
|
|
|
|
def mat_file():
|
|
reset()
|
|
old_mix = V < (3, 4)
|
|
diffuse = image("diffuse", 4, 4, lambda x, y: [x / 3, y / 3, 0.5, 1.0])
|
|
rough = image("rough", 4, 4, lambda x, y: [0.25 + x / 8] * 3 + [1.0])
|
|
bump = image("height", 4, 4, lambda x, y: [(x + y) / 6] * 3 + [1.0])
|
|
nrm = image("nrm", 4, 4, lambda x, y: [0.5, 0.5, 1.0, 1.0])
|
|
udim = None
|
|
if V >= (2, 82):
|
|
paths = []
|
|
for tile, col in ((1001, (1.0, 0.0, 0.0)), (1002, (0.0, 0.0, 1.0))):
|
|
p = os.path.join(out, "udim.%d.png" % tile)
|
|
t = bpy.data.images.new("udim_%d" % tile, 2, 2)
|
|
t.pixels = list(col + (1.0,)) * 4
|
|
t.filepath_raw = p
|
|
t.file_format = "PNG"
|
|
t.save()
|
|
bpy.data.images.remove(t)
|
|
paths.append(p)
|
|
udim = bpy.data.images.load(paths[0])
|
|
udim.name = "udim"
|
|
udim.source = "TILED"
|
|
if len(udim.tiles) < 2:
|
|
udim.tiles.new(1002)
|
|
udim.tiles[0].label = "front"
|
|
udim.filepath = "//" + os.path.basename(udim.filepath.replace("1001", "<UDIM>" if V >= (3, 0) else "1001"))
|
|
|
|
def material(name):
|
|
m = bpy.data.materials.new(name)
|
|
bsdf = principled(m, (0.8, 0.8, 0.8, 1.0), 0.0, 0.5)
|
|
return m, m.node_tree, bsdf
|
|
|
|
def tex(nt, img):
|
|
t = nt.nodes.new("ShaderNodeTexImage")
|
|
t.image = img
|
|
return t
|
|
|
|
def mix(nt, a, b, fac):
|
|
if old_mix:
|
|
n = nt.nodes.new("ShaderNodeMixRGB")
|
|
ia, ib, f, o = n.inputs["Color1"], n.inputs["Color2"], n.inputs["Fac"], n.outputs["Color"]
|
|
else:
|
|
n = nt.nodes.new("ShaderNodeMix")
|
|
n.data_type = "RGBA"
|
|
ia, ib, f, o = n.inputs[6], n.inputs[7], n.inputs[0], n.outputs[2]
|
|
if a:
|
|
nt.links.new(a, ia)
|
|
if b:
|
|
nt.links.new(b, ib)
|
|
if fac:
|
|
nt.links.new(fac, f)
|
|
return o
|
|
|
|
def vcol(nt):
|
|
if V >= (2, 81):
|
|
n = nt.nodes.new("ShaderNodeVertexColor")
|
|
n.layer_name = "Col"
|
|
else:
|
|
n = nt.nodes.new("ShaderNodeAttribute")
|
|
n.attribute_name = "Col"
|
|
return n.outputs["Color"]
|
|
|
|
def ramp(nt, src):
|
|
n = nt.nodes.new("ShaderNodeValToRGB")
|
|
nt.links.new(src, n.inputs["Fac"])
|
|
return n.outputs["Color"]
|
|
|
|
def reroute(nt, src):
|
|
n = nt.nodes.new("NodeReroute")
|
|
nt.links.new(src, n.inputs[0])
|
|
return n.outputs[0]
|
|
|
|
mats = []
|
|
# body: Mix(A = image, B = HueSat(image), Fac = ramp(vertex colour)); roughness image linked directly
|
|
m, nt, b = material("MatBody")
|
|
img = udim or diffuse
|
|
hs = nt.nodes.new("ShaderNodeHueSaturation")
|
|
nt.links.new(tex(nt, img).outputs["Color"], hs.inputs["Color"])
|
|
nt.links.new(mix(nt, tex(nt, img).outputs["Color"], hs.outputs["Color"], ramp(nt, vcol(nt))), b.inputs["Base Color"])
|
|
nt.links.new(tex(nt, rough).outputs["Color"], b.inputs["Roughness"])
|
|
mats.append(m)
|
|
# hands: Mix(A = image, Fac = ramp(vertex colour)), B unlinked
|
|
m, nt, b = material("MatHands")
|
|
nt.links.new(mix(nt, tex(nt, diffuse).outputs["Color"], None, ramp(nt, vcol(nt))), b.inputs["Base Color"])
|
|
nt.links.new(tex(nt, rough).outputs["Color"], b.inputs["Roughness"])
|
|
mats.append(m)
|
|
# hair: reroutes; roughness through a reroute and a ramp
|
|
m, nt, b = material("MatHair")
|
|
nt.links.new(reroute(nt, reroute(nt, tex(nt, diffuse).outputs["Color"])), b.inputs["Base Color"])
|
|
nt.links.new(reroute(nt, ramp(nt, tex(nt, rough).outputs["Color"])), b.inputs["Roughness"])
|
|
mats.append(m)
|
|
# eyebrows: vertex colour
|
|
m, nt, b = material("MatVcol")
|
|
nt.links.new(vcol(nt), b.inputs["Base Color"])
|
|
mats.append(m)
|
|
# jeans: image, normal map, roughness image linked directly, metallic through Separate RGB (packed)
|
|
m, nt, b = material("MatJeans")
|
|
nt.links.new(tex(nt, diffuse).outputs["Color"], b.inputs["Base Color"])
|
|
nm = nt.nodes.new("ShaderNodeNormalMap")
|
|
nt.links.new(tex(nt, nrm).outputs["Color"], nm.inputs["Color"])
|
|
nt.links.new(nm.outputs["Normal"], b.inputs["Normal"])
|
|
nt.links.new(tex(nt, rough).outputs["Color"], b.inputs["Roughness"])
|
|
mats.append(m)
|
|
# a chain of colour adjustments
|
|
m, nt, b = material("MatChain")
|
|
src = tex(nt, diffuse).outputs["Color"]
|
|
for idname in ("ShaderNodeRGBCurve", "ShaderNodeInvert", "ShaderNodeGamma", "ShaderNodeBrightContrast"):
|
|
n = nt.nodes.new(idname)
|
|
nt.links.new(src, n.inputs["Color"])
|
|
src = n.outputs["Color"]
|
|
nt.links.new(src, b.inputs["Base Color"])
|
|
mats.append(m)
|
|
# a node group: an image inside it, and a pass-through of an outside image
|
|
grp = bpy.data.node_groups.new("TexGroup", "ShaderNodeTree")
|
|
gi, go = grp.nodes.new("NodeGroupInput"), grp.nodes.new("NodeGroupOutput")
|
|
if hasattr(grp, "interface"):
|
|
grp.interface.new_socket("Tint", in_out="INPUT", socket_type="NodeSocketColor")
|
|
grp.interface.new_socket("Color", in_out="OUTPUT", socket_type="NodeSocketColor")
|
|
grp.interface.new_socket("Through", in_out="OUTPUT", socket_type="NodeSocketColor")
|
|
else:
|
|
grp.inputs.new("NodeSocketColor", "Tint")
|
|
grp.outputs.new("NodeSocketColor", "Color")
|
|
grp.outputs.new("NodeSocketColor", "Through")
|
|
inner = grp.nodes.new("ShaderNodeTexImage")
|
|
inner.image = rough
|
|
grp.links.new(inner.outputs["Color"], go.inputs[0])
|
|
grp.links.new(gi.outputs[0], go.inputs[1])
|
|
m, nt, b = material("MatGroup")
|
|
gn = nt.nodes.new("ShaderNodeGroup")
|
|
gn.node_tree = grp
|
|
nt.links.new(tex(nt, diffuse).outputs["Color"], gn.inputs[0])
|
|
nt.links.new(gn.outputs[1], b.inputs["Base Color"])
|
|
nt.links.new(gn.outputs[0], b.inputs["Roughness"])
|
|
mats.append(m)
|
|
# bump, and unresolved links (a noise texture, a math node)
|
|
m, nt, b = material("MatBump")
|
|
bp = nt.nodes.new("ShaderNodeBump")
|
|
bp.inputs["Strength"].default_value = 0.5
|
|
bp.inputs["Distance"].default_value = 0.2
|
|
nt.links.new(tex(nt, bump).outputs["Color"], bp.inputs["Height"])
|
|
nt.links.new(bp.outputs["Normal"], b.inputs["Normal"])
|
|
nz = nt.nodes.new("ShaderNodeTexNoise")
|
|
nt.links.new(nz.outputs["Color"], b.inputs["Base Color"])
|
|
b.inputs["Base Color"].default_value = (0.0, 0.0, 0.0, 1.0)
|
|
mt = nt.nodes.new("ShaderNodeMath")
|
|
nt.links.new(mt.outputs[0], b.inputs["Metallic"])
|
|
b.inputs["Metallic"].default_value = 0.9
|
|
mats.append(m)
|
|
for i, m in enumerate(mats):
|
|
me = mesh("Quad" + m.name, [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)], [(0, 1, 2, 3)])
|
|
uv(me, "UVMap", lambda k: ((k % 2) * 1.0, (k // 2) * 1.0))
|
|
byte_colors(me, "Col")
|
|
me.materials.append(m)
|
|
ob = link(bpy.data.objects.new("Quad" + m.name, me))
|
|
ob.location = (i * 1.5, 0, 0)
|
|
update()
|
|
save(os.path.join(out, "mat_%s.blend" % TAG))
|
|
|
|
|
|
scene_file()
|
|
rig_file()
|
|
ctl_file()
|
|
mat_file()
|
|
if OLD:
|
|
scene_file()
|
|
save(os.path.join(out, "compat_%s.blend" % TAG), compat=True)
|
|
print("MADE", TAG)
|