nolanlabs

Viết script

Trên trang này
  1. Cài đặt
  2. Script đầu tiên
  3. Gọi bất kỳ op nào
  4. Theo việc bạn muốn làm
  5. Tạo media bằng code
  6. Làm việc từ shell
  7. Làm việc với bất kỳ dự án nào đang mở
  8. Chuyển chế độ và cập nhật
  9. Mỗi object có gì
  10. Script của agent

Dùng script khi một việc dễ làm hơn bằng chương trình: cắt theo từng beat, đặt hiệu ứng vào từng cú hit, đo trước khi cắt. Python client nolan và nolan-cli gọi được cùng những op như các nút bấm và agent, nên mọi thay đổi đều được kiểm tra, và một thay đổi bằng script là một bước trong lịch sử.

Cài đặt

Client chỉ cần Python 3.10 trở lên. OpenTimelineIO là tùy chọn, dùng cho to_otio(). Ứng dụng mang client trong phần tài nguyên của nó, trong thư mục tên python-client. Trên Mac, khi ứng dụng nằm trong Applications:

pip install /Applications/nolanlabs.app/Contents/Resources/python-client

Thêm [otio] vào cuối đường dẫn để có OpenTimelineIO, hoặc đặt thư mục đó vào PYTHONPATH.

Client tự tìm ứng dụng đang chạy. NOLAN_URL được ưu tiên nếu bạn đặt nó. Mọi request đều mang secret của dịch vụ: khi chạy cạnh ứng dụng, client và CLI tự đọc nó, nên không cần đặt gì. NOLAN_TOKEN chỉ định nó cho một dịch vụ chạy ở nơi khác.

Script đầu tiên

import nolan

s = nolan.connect()                         # NOLAN_URL, or http://127.0.0.1:8790
tl = s.timeline()                           # NOLAN_TIMELINE, or the first timeline
music = s.sources("Music")[0]["id"]
beats = s.sound(music, fps=tl.fps)["beats"]

with tl.transaction("Cut to the beat"):     # one step in the history; dropped if the block raises
    for clip, beat in zip(tl.track("V1").items, beats[4::4]):
        if clip.end > beat:
            clip.trim("tail", beat - clip.end, ripple=True)
    tl.track("V1").items[0].effect("grain", 0.3)

print(tl.summary()["mean_shot_s"])

Ngoài transaction, mỗi batch vào timeline thành một bước riêng. Bên trong with tl.transaction(label), các batch đi vào một transaction mà trang hiện giống như đề xuất của agent, và khi khối lệnh kết thúc thì nó được giữ thành một bước. Batch nào bị engine từ chối sẽ raise nolan.NolanError, với op_index chỉ ra lệnh nào, và không thay đổi gì.

Gọi bất kỳ op nào

Mỗi op là một hàm theo nhóm, các trường của nó là tham số, và docstring cho biết đơn vị và phạm vi của từng trường. Trường không tồn tại sẽ bị từ chối, kèm danh sách các trường có thật. session ở đây là thứ nolan.connect() trả về.

nolan.ops.clip.trim(clip="c4", edge="tail", delta=-6, tl=tl)
nolan.ops.text.add(at=24, len=48, text="NEXT LEVEL", look="impact", y=0.2, in_="slam", in_len=6, tl=tl)
beats = nolan.analyse.sound("Rocket")["beats"]          # nolan.analyse and nolan.track are nolan.ops.analyse and .track
tl.edit([{"op": "clip.dissolve", "clip": "c5", "frames": 12},
         {"op": "grade.primary", "clip": "c5", "temp": 0.15}], name="Warmer")
tl.do("timeline.marker_add", at=96, name="Drop")
session.op("deliver.render", preset="prores", path="/out/master.mov")

Trường nào trùng từ khóa Python thì thêm _ ở cuối khi làm tham số (in_, from_). Trong tl.edit và tl.do chúng giữ nguyên tên. nolan.ops.CATALOGUE có mọi op kèm các trường của nó, và nolan.ops.catalogue() hỏi trình dựng, gồm cả plug-in.

Theo việc bạn muốn làm

# Bring things in, and cut them
s.import_media(["/footage/a.mov", "/footage/b.mov"])
tl.insert("a", 0, length=48, overwrite=True)

# Grade with wheels and curves, and a LUT
clip.grade(lift=[0, 0, .02], gain=[.05, 0, -.03], curves={"m": [[0, 0], [.5, .55], [1, 1]]})
clip.grade(lut="film.cube", lut_mix=.6)  # in the project's LUTs folder, or a full path

# Read the frame from the originals, and check the numbers
print(tl.scopes(at=48)["luma"])

# Mask, track and move the mask with what it follows
clip.set(masks=[{"shape": "ellipse", "x": .5, "y": .4, "w": .3, "h": .5, "feather": .05, "invert": True}])
path = s.track(clip.src, clip.source_in, clip.source_in + clip.length, x=.5, y=.4, w=.3, h=.5, mode="move")
clip.track_apply(path, target="mask:0")

# Words on an arc, letter by letter
tl.title("STUNT SHOW", 0, 72, path=[[.2, .6], [.5, .4], [.8, .6]], by="char",
         per=[{"prop": "opacity", "stagger": 2, "keys": [{"f": 0, "v": 0}, {"f": 8, "v": 1}]}])

# Sound
clip.set(afx=[{"kind": "highpass", "freq": 80}, {"kind": "reverb", "size": .4, "mix": .15}], pan=-.2)

# Cameras
angles = s.sync(["cam_a", "cam_b"])
tl.multicam(angles, at=0, length=240)

# Deliver anything
tl.render("/out/master.mov", preset="prores", profile="hq", audio="pcm24", loudness=-16)
tl.render("/out/frames", sequence="exr", alpha=True)
tl.render("/out/hdr.mov", codec="prores", profile="hq", hdr="pq")   # cache=True does nothing for HDR or EXR
tl.export("fcpxml", "/out/cut.fcpxml")

Mỗi method gửi một batch rồi đọc lại timeline, nên các object thể hiện timeline đúng như sau thay đổi. Clip bị cắt làm đôi giữ id của nó ở phần bên trái.

Tạo media bằng code

Một chương trình có thể tự tạo thứ nó đặt lên timeline. Mỗi lệnh dưới đây ghi file vào Media/Generated của dự án, nhập nó và đặt nó chồng lên những gì đang có, không dời gì cả, thành một bước trong lịch sử. Hoàn tác sẽ gỡ clip đi, còn nguồn vẫn nằm trong kho media.

import numpy as np
from PIL import Image, ImageDraw

# A picture: an array or a Pillow image, for 5 seconds at the end of the track unless you say
picture = Image.new("RGBA", (tl.width, tl.height), (0, 0, 0, 0))
ImageDraw.Draw(picture).ellipse((200, 100, 600, 500), fill=(255, 80, 0, 255))
tl.add_image(picture, at=0, length=48, track="V2", name="Disc")

# Moving pictures: a generator is sent as it makes them, so length costs no memory
def noise():
    rng = np.random.default_rng(7)
    for i in range(96):
        yield rng.random((tl.height, tl.width, 3)) * (i / 96)
tl.add_frames(noise(), fps=tl.fps, at=48, name="Noise")

# Sound: float from -1 to 1, or int16; one channel or (samples, channels)
t = np.arange(96000) / 48000
tl.add_audio(np.sin(2 * np.pi * 110 * t) * 0.3, rate=48000, at=0, track="A2", name="Hum")
Lệnh gọi Nhận gì Thành gì
tl.add_image(image, at, length, track, name) Một mảng (H, W), (H, W, 1), (H, W, 3) hoặc (H, W, 4), kiểu uint8, uint16 hoặc float từ 0 tới 1, hoặc một ảnh Pillow Một file PNG, 16-bit nếu từ uint16 hoặc float
tl.add_frames(frames, fps, at, length, track, name) Một generator hay list các thứ trên, hoặc một mảng (T, H, W[, C]) ProRes 422 HQ, hoặc 4444 nếu có alpha
tl.add_audio(samples, rate, at, length, track, name) Một mảng (N,) hoặc (N, channels), float hoặc int16, hoặc một generator trả về từng khối Một file WAV 24-bit

Mảng và ảnh là của chương trình bạn, nên NumPy và Pillow bạn tự cài để dùng các lệnh này.

Làm việc từ shell

nolan-cli [--url URL] [--project NAME|FOLDER|ID] [--timeline ID] COMMAND

projects | open PATH [--force] | close [NAME] [--stop]    the projects open at once
runs [--state S] | ask PROMPT [--thread T] [--wait]       the agent of --project (or the one the app shows)
answer RUN OPTION [--note N] | cancel RUN
host status | host restart|stop [--when idle|now] | host cancel

run [--label L] [--dry-run] script.py [-- args]    a script, in one transaction
summary | ops [WORDS] | lookup WORDS               read; ops lists every op with its fields
op NAME [FIELD=JSON ...] [--dry-run]               any op: op clip.trim clip=c4 edge=tail delta=-6
inspect WHAT [FIELD=JSON ...]                      the agent's inspect tool: inspect clips track=V1
look WHAT [FIELD=JSON ...] [--out DIR]             the agent's look tool; pictures saved to DIR, their paths printed
wait ID [--timeout S] | busy | background          wait for a script in the background (r1) or a render (j1); what still runs
apply '[{"op": "split", "at": 48}]' [--dry-run]    any engine commands
clip ID [FIELD=JSON ...]                           read or change a clip
render [--out F] [--preset P] [--from A] [--to B] [FIELD=JSON ...]    waits for it
export otio|fcpxml|edl|aaf [OUT] [--track V2]      import FILE
scopes [--at F] | cache [--from A --to B --wait]   sheet OUT.jpg [--count N]
fonts | font FILE | relink [--folder D | --src ID --path F]
add image|frames|audio [--name N] [--at F] [--length L] [--track V2]
    [--width W --height H --pix rgb24 --fps F | --rate R --channels C --format f32le] < RAW

run chạy script của bạn với nolan, session và tl đã sẵn sàng, bên trong một transaction: được giữ thành một bước khi chạy tốt, bị bỏ khi raise lỗi. --dry-run cho biết nó đã làm gì rồi bỏ đi. Script bạn chạy theo cách này là của bạn và chạy không có safe mode. python -m nolan là cùng lệnh đó.

Từ bất kỳ ngôn ngữ nào, nolan-cli add đọc dữ liệu thô từ một pipe:

my_renderer | nolan-cli add frames --width 1920 --height 1080 --pix rgb24 --fps 24 --track V2 --name Sim
my_synth | nolan-cli add audio --rate 48000 --channels 2 --format f32le --at 96

Làm việc với bất kỳ dự án nào đang mở

nolan-cli --project NAME làm việc trong dự án đó mà không chuyển cửa sổ của ứng dụng. nolan.project(name) trong Python cũng vậy:

import nolan
teaser = nolan.project("Launch Teaser")
teaser.timeline().marker(48, "From the script")         # edits that project, whatever the app's window shows
run = teaser.agent.ask("Tighten the second half")      # a Run: id, thread, status (running, paused, done, failed, cancelled), and what it waits on you for
run.wait()                                             # waits through a restart: a paused run carries on by itself
for r in nolan.runs(state="waiting"):                  # every open project's runs that wait for an answer
    print(r.project, r.title)
teaser.close(stop=False)                               # refused while its work goes, unless stop=True
nolan.host.status(); nolan.host.restart(when="idle")

Chuyển chế độ và cập nhật

s.mode là chế độ của ứng dụng, "lite" hoặc "pro", và s.set_mode("pro") chuyển chế độ. nolan-cli mode in ra chế độ hiện tại, và nolan-cli mode lite chuyển chế độ.

nolan-cli update          # or: update check, update install

Mỗi object có gì

Object Có gì
Session project, timelines, timeline(id), new_timeline, new_project, open, import_media, add_media, sources(q), sound, sfx, sync(srcs, by), motion, track, shake, subject, key_colour, relink, fonts(), import_font(path), import_timeline(path), set_proxy(size), set_agent(access, ask_me), style(), set_style(user, project), plugins(), save_generator(name, code, params), op(name, **fields), run_script(code)
Timeline Đọc: fps, width, height, length, markers, tracks, track(id), clips, clip(id), at(frame, track), summary(), checks(), frame(at, path), still(at, path), contact_sheet(path), scopes(at, pictures, hdr), cache(frm, to, wait), mix(), watch(). Thay đổi: edit(ops, name), do(name, **fields), apply(commands), op(command, **fields), dry_run(commands), transaction(label), undo(), insert, lay, add_image, add_frames, add_audio, split, title, marker, add_track, compound(frm, to), decompose(clip), multicam(angles, at). Xuất ra: render(path, preset, frm, to, **deliver), export(kind, path, track), otio_json(), to_otio()
Track id, kind, name, items, set(**fields)
Clip id, track, src, at, end, length, source_in, title, anim, fx, speed; trim, roll, slip, slide, move, split, delete, set, keyframe, effect, motion, transition, grade, track_matte, track_apply, stabilize

Script của agent

Agent cũng có client này. Script agent chạy làm việc trên một bản nháp, tức một bản sao của đề xuất. Khi script chạy tốt, mọi thứ vào đề xuất thành một batch. Khi script lỗi, không có gì được giữ lại, và media nó đã tạo cũng bị bỏ theo.

Script của agent chạy trong safe mode: giới hạn import, chỉ ghi được bên trong thư mục tạm của chính nó, không tạo tiến trình mới, và giới hạn thời gian, CPU và bộ nhớ. Trên Windows không có chế độ chỉ-đọc cho ổ đĩa và không có giới hạn bộ nhớ. Safe mode là lớp chắn lỗi sơ suất, không phải ranh giới bảo mật. Xem Giới hạn hiện có. Đề xuất từ script của chính bạn có Giữ và Bỏ. Xem Đề xuất.

Tài liệu

↑↓ Di chuyển↵ Mở