@muqian/xiangsu-mcp-plus 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +146 -53
- package/editor-plugin/features/ai3d.cjs +444 -0
- package/editor-plugin/features/arvideo.cjs +318 -0
- package/editor-plugin/features/assets.cjs +69 -3
- package/editor-plugin/features/common.cjs +31 -1
- package/editor-plugin/features/community.cjs +523 -133
- package/editor-plugin/features/delivery.cjs +9 -1
- package/editor-plugin/features/discover.cjs +286 -0
- package/editor-plugin/features/makeup.cjs +128 -0
- package/editor-plugin/features/menu.cjs +2 -2
- package/editor-plugin/features/music.cjs +29 -10
- package/editor-plugin/features/square.cjs +2 -2
- package/editor-plugin/features/works.cjs +48 -13
- package/editor-plugin/library.cjs +20 -1
- package/package.json +4 -2
- package/scripts/install.mjs +7 -56
- package/src/bridge.mjs +2 -1
- package/src/frames.mjs +14 -0
- package/src/hotlists.mjs +74 -0
- package/src/images.mjs +16 -3
- package/src/inspect.mjs +121 -6
- package/src/longfetch.mjs +11 -0
- package/src/makeup.mjs +237 -60
- package/src/measure.mjs +129 -0
- package/src/official.mjs +26 -11
- package/src/outputs.mjs +10 -4
- package/src/plugininstall.mjs +112 -0
- package/src/projects.mjs +36 -5
- package/src/scenefile.mjs +25 -9
- package/src/server.mjs +970 -95
- package/src/works.mjs +12 -2
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// AI 3D (素材库 › AI 3D): 图生3D (an image → a static GLB mesh), 3D 动画 (an FBX with the standard humanoid
|
|
3
|
+
// skeleton + a reference video → that FBX animated with the video's motion) and 高精度模型重建 (an orbit
|
|
4
|
+
// video of a real object → a 3D Gaussian splat). Requests are the ones AI3DManager makes; results are
|
|
5
|
+
// downloaded to and imported from the folder the panel uses, so the panel finds them as well.
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const { execFileSync } = require('child_process');
|
|
9
|
+
const { M, editingScene, sceneObjectSet, addedObjectEditors, sceneObjectPath, projectResources, packageRootsAdded, recordImport } = require('./common.cjs');
|
|
10
|
+
|
|
11
|
+
// The panel's names for the three generation types (AI3DManager/type AIGenType).
|
|
12
|
+
const GEN_TYPE = { image: 'Image', animation: 'Animation', gs: 'Video' };
|
|
13
|
+
// The precision options of 图生3D (FaceNumSelection, 9.8.1).
|
|
14
|
+
const FACE_NUM = { low: 1000, medium: 10000, high: 50000 };
|
|
15
|
+
// The upload panels' own checks (9.8.1): extensions from the panels' texts, numbers from their code.
|
|
16
|
+
// The image panel measures images with the video probe, which reads 0 for pictures, so in the editor
|
|
17
|
+
// only the extension is really checked; the size and resolution here are the limits its text states.
|
|
18
|
+
const IMAGE_LIMIT = { exts: ['png', 'jpg', 'jpeg'], maxSide: 4000, maxMB: 10 };
|
|
19
|
+
const VIDEO_LIMIT = {
|
|
20
|
+
animation: { exts: ['mp4'], maxSide: 1280, maxMB: 20, maxSeconds: 30 },
|
|
21
|
+
gs: { exts: ['mp4', 'mov'], maxSide: 4000, maxMB: 100, maxSeconds: 120 },
|
|
22
|
+
};
|
|
23
|
+
// The panel refuses a fourth running task of the same type (3d_gen_max_limit).
|
|
24
|
+
const MAX_RUNNING = 3;
|
|
25
|
+
// Built-in reference models of 3D 动画 (BuiltinResource/AssetLibrary/<id>.fbx).
|
|
26
|
+
const PRESET_MODELS = {
|
|
27
|
+
whole: '3d_gen_preset_model_whole',
|
|
28
|
+
upper_body: '3d_gen_preset_model_upper_body',
|
|
29
|
+
lower_body: '3d_gen_preset_model_lower_body',
|
|
30
|
+
arm: '3d_gen_preset_model_arm',
|
|
31
|
+
};
|
|
32
|
+
// Bones the animation panel looks for in an uploaded model (case-insensitive, whole names). Missing ones
|
|
33
|
+
// only bring up a warning there; the model is still used.
|
|
34
|
+
const REQUIRED_BONES = [
|
|
35
|
+
'Hips', 'Spine', 'Spine1', 'Spine2', 'Neck', 'Head',
|
|
36
|
+
'LeftUpLeg', 'LeftLeg', 'LeftFoot', 'RightUpLeg', 'RightLeg', 'RightFoot',
|
|
37
|
+
'LeftShoulder', 'LeftArm', 'LeftForeArm', 'LeftHand', 'RightShoulder', 'RightArm', 'RightForeArm', 'RightHand',
|
|
38
|
+
];
|
|
39
|
+
const STATUS = { 1: 'running', 2: 'finished', 3: 'failed', 4: 'discarded' };
|
|
40
|
+
// How the editor tags and sizes each kind of result when importing it (AI3DManager.import*Model).
|
|
41
|
+
const IMPORT = {
|
|
42
|
+
image: { file: 'modelPath', idPrefix: 'AIAsset_3DMesh_', type: 'AIAsset_ImageTo3D' },
|
|
43
|
+
gs: { file: 'plyPath', idPrefix: 'AIAsset_3DGS_', type: 'AIAsset_VideoTo3DGS' },
|
|
44
|
+
animation: { file: 'resultFbxPath', idPrefix: 'AIAsset_Animation_', type: 'AIDanceToAnimation' },
|
|
45
|
+
};
|
|
46
|
+
// Picked, not measured: often enough to notice a finished task soon, rare enough not to hammer the API.
|
|
47
|
+
const POLL_MS = 3000;
|
|
48
|
+
// Same as the panel's TaskSucceed download.
|
|
49
|
+
const DOWNLOAD_TIMEOUT_MS = 30000;
|
|
50
|
+
// Measured: the history API answers 5062 「超出最大条数限制」 for a page of more than 20 (the panel asks for 5).
|
|
51
|
+
const HISTORY_PAGE = 20;
|
|
52
|
+
// Picked: how far back an import looks for a task's own record (queryAIGCResult does not return the
|
|
53
|
+
// input); 10 pages of 20 reach well past what anyone keeps importing from.
|
|
54
|
+
const HISTORY_PAGES = 10;
|
|
55
|
+
|
|
56
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
57
|
+
|
|
58
|
+
function parseJson(s) {
|
|
59
|
+
if (typeof s !== 'string' || !s) return {};
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(s);
|
|
62
|
+
} catch (_) {
|
|
63
|
+
return {};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function ext(file) {
|
|
68
|
+
return path.extname(file).slice(1).toLowerCase();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function checkFile(file, exts, what) {
|
|
72
|
+
if (typeof file !== 'string' || !path.isAbsolute(file)) throw Error(`${what}_PATH_NOT_ABSOLUTE: ${file}`);
|
|
73
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) throw Error(`${what}_NOT_FOUND: ${file}`);
|
|
74
|
+
if (!exts.includes(ext(file))) throw Error(`${what}_FORMAT: ${path.basename(file)}; supported: ${exts.join(', ')}`);
|
|
75
|
+
return file;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function checkKind(kind) {
|
|
79
|
+
if (!Object.prototype.hasOwnProperty.call(GEN_TYPE, kind)) throw Error(`INVALID_KIND: ${kind}; valid: ${Object.keys(GEN_TYPE).join(', ')}`);
|
|
80
|
+
return kind;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const iso = (sec) => (sec ? new Date(sec * 1000).toISOString() : null);
|
|
84
|
+
|
|
85
|
+
// What the record says about its own input, read the way the panel's 「重新编辑」 reads it.
|
|
86
|
+
function taskInput(kind, record) {
|
|
87
|
+
const params = parseJson(((record.inputNodeList || [])[0] || {}).params);
|
|
88
|
+
if (kind === 'animation') {
|
|
89
|
+
const req = params.req_json || {};
|
|
90
|
+
const preset = Object.keys(PRESET_MODELS).find((k) => PRESET_MODELS[k] === req.presetModel);
|
|
91
|
+
return preset ? { model: preset } : { model: req.localModel || null };
|
|
92
|
+
}
|
|
93
|
+
const req = (params.dyeh_custom_params && params.dyeh_custom_params.req_json) || {};
|
|
94
|
+
if (kind === 'image') {
|
|
95
|
+
const precision = Object.keys(FACE_NUM).find((k) => FACE_NUM[k] === req.faceNum);
|
|
96
|
+
return { precision: precision || null, face_num: req.faceNum ?? null };
|
|
97
|
+
}
|
|
98
|
+
return {};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function failureText(record) {
|
|
102
|
+
const out = (record.outputNodeList || [])[0] || {};
|
|
103
|
+
return (out.textList || [])[0] || null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const methods = {
|
|
107
|
+
ai3dRun(cmd, args) {
|
|
108
|
+
return (this.ai3dExec || execFileSync)(cmd, args, { encoding: 'utf8' });
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
ai3dConst() {
|
|
112
|
+
return this.mod(M.ai3dConst);
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
ai3dCheckImage(file) {
|
|
116
|
+
checkFile(file, IMAGE_LIMIT.exts, 'IMAGE');
|
|
117
|
+
const mb = fs.statSync(file).size / (1024 * 1024);
|
|
118
|
+
if (mb > IMAGE_LIMIT.maxMB) throw Error(`IMAGE_TOO_LARGE: ${mb.toFixed(1)} MB; the limit is ${IMAGE_LIMIT.maxMB} MB`);
|
|
119
|
+
const out = this.ai3dRun('sips', ['-g', 'pixelWidth', '-g', 'pixelHeight', file]);
|
|
120
|
+
const width = Number((/pixelWidth:\s*(\d+)/.exec(out) || [])[1]);
|
|
121
|
+
const height = Number((/pixelHeight:\s*(\d+)/.exec(out) || [])[1]);
|
|
122
|
+
if (!(width > 0 && height > 0)) throw Error(`NOT_AN_IMAGE: ${file}`);
|
|
123
|
+
if (width > IMAGE_LIMIT.maxSide || height > IMAGE_LIMIT.maxSide) throw Error(`IMAGE_RESOLUTION: ${width}×${height}; the limit is ${IMAGE_LIMIT.maxSide}×${IMAGE_LIMIT.maxSide}`);
|
|
124
|
+
return { width, height };
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
ai3dCheckVideo(file, kind) {
|
|
128
|
+
const limit = VIDEO_LIMIT[kind];
|
|
129
|
+
checkFile(file, limit.exts, 'VIDEO');
|
|
130
|
+
const meta = this.mod(M.preview).PreviewManager.getInstance().checkIsValidVideoFile(file);
|
|
131
|
+
if (!meta || !meta.valid) throw Error(`VIDEO_UNREADABLE: ${file}`);
|
|
132
|
+
// The probe gives the duration in microseconds and the size in MB.
|
|
133
|
+
const seconds = meta.duration / 1e6;
|
|
134
|
+
if (seconds > limit.maxSeconds) throw Error(`VIDEO_TOO_LONG: ${seconds.toFixed(1)} s; the limit is ${limit.maxSeconds} s`);
|
|
135
|
+
if (meta.width > limit.maxSide || meta.height > limit.maxSide) throw Error(`VIDEO_RESOLUTION: ${meta.width}×${meta.height}; the limit is ${limit.maxSide}×${limit.maxSide}`);
|
|
136
|
+
if (meta.size > limit.maxMB) throw Error(`VIDEO_TOO_LARGE: ${meta.size.toFixed(1)} MB; the limit is ${limit.maxMB} MB`);
|
|
137
|
+
return { width: meta.width, height: meta.height, seconds: Number(seconds.toFixed(2)) };
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
// The animation panel's model check: no skeleton or over the import limits refuses; missing bones warn.
|
|
141
|
+
async ai3dCheckRig(file) {
|
|
142
|
+
const EF = this.mod(M.ef);
|
|
143
|
+
const fbx = await EF.APJS.FbxLoaderAPJSWrapper.createFromPathAsync(file, 'zh_CN.UTF-8');
|
|
144
|
+
const triangles = fbx.getTriangleCount();
|
|
145
|
+
const bones = fbx.getMaxBoneCount();
|
|
146
|
+
if (!(triangles >= 0 && fbx.getVertexCount() >= 0 && bones > 0)) throw Error(`MODEL_NOT_RIGGED: ${path.basename(file)} has no skeleton`);
|
|
147
|
+
const limits = EF.ImportLimitationNumber;
|
|
148
|
+
if (bones > limits.BONE_NUM_LIMIT) throw Error(`MODEL_TOO_MANY_BONES: ${bones}; the limit is ${limits.BONE_NUM_LIMIT}`);
|
|
149
|
+
if (triangles > limits.SKIN_MESH_TRIANGLE_HARD_NUM_LIMIT) throw Error(`MODEL_TOO_MANY_TRIANGLES: ${triangles}; the limit is ${limits.SKIN_MESH_TRIANGLE_HARD_NUM_LIMIT}`);
|
|
150
|
+
const names = new Set(fbx.getAllNodeNames().map((n) => String(n).toLowerCase()));
|
|
151
|
+
return { bones, triangles, missing_bones: REQUIRED_BONES.filter((b) => !names.has(b.toLowerCase())) };
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
async ai3dRunningCheck(kind) {
|
|
155
|
+
const res = await this.mod(M.aiEffectApi).queryAITaskHistories({ cursor: 0, size: MAX_RUNNING, scene: this.ai3dConst().AIGen3DModelScene[GEN_TYPE[kind]], statusList: [1] });
|
|
156
|
+
if (!res || res.code !== 0) throw Error(`AI3D_HISTORY_FAILED: code=${res && res.code} message=${(res && res.message) || ''}`);
|
|
157
|
+
const running = ((res.data && res.data.taskRecords) || []).length;
|
|
158
|
+
if (running >= MAX_RUNNING) throw Error(`AI3D_TOO_MANY_RUNNING: ${running} ${kind} tasks are running; the editor allows ${MAX_RUNNING} at a time, wait for one to finish`);
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
async ai3dUpload(file, type) {
|
|
162
|
+
const api = this.mod(M.uploadApi);
|
|
163
|
+
const res = await api.uploadFileCommon({ filePath: file, fileType: api.FileType[type] });
|
|
164
|
+
if (!res || res.code !== 0) throw Error(`UPLOAD_FAILED: ${path.basename(file)} code=${res && res.code} message=${(res && res.message) || ''}`);
|
|
165
|
+
return res.data;
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
async ai3dUploadVideo(file) {
|
|
169
|
+
const api = this.mod(M.uploadApi);
|
|
170
|
+
const res = await api.uploadToSpace({ filePath: file, fileType: api.FileType.Video });
|
|
171
|
+
if (!res || res.code !== 0 || !res.data || res.data.res === false) throw Error(`UPLOAD_FAILED: ${path.basename(file)} code=${res && res.code} message=${(res && res.message) || ''}`);
|
|
172
|
+
return res.data.videoInfo;
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
async ai3dSubmit(kind, node, needRecord) {
|
|
176
|
+
const c = this.ai3dConst();
|
|
177
|
+
const type = GEN_TYPE[kind];
|
|
178
|
+
const res = await this.mod(M.aiEffectApi).submitAIGCRequest({
|
|
179
|
+
inputNodeList: [{ source: `loki_material://${c.AIGenModelIdMap[type]}`, ...node, aigc_env: this.mod(M.aigcUtils).getAIGCEnv() }],
|
|
180
|
+
scene: c.AIGen3DModelScene[type],
|
|
181
|
+
needRecord,
|
|
182
|
+
});
|
|
183
|
+
const b = res && res.body;
|
|
184
|
+
if (!b || b.code !== 0 || !b.data || !b.data.taskId) throw Error(`AI3D_SUBMIT_FAILED: code=${b && b.code} message=${(b && b.message) || ''}`);
|
|
185
|
+
return String(b.data.taskId);
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
async ai3dImageSubmit({ image_path, precision = 'medium' }) {
|
|
189
|
+
this.ensureCompatible('ai3d');
|
|
190
|
+
if (!Object.prototype.hasOwnProperty.call(FACE_NUM, precision)) throw Error(`INVALID_PRECISION: ${precision}; valid: ${Object.keys(FACE_NUM).join(', ')}`);
|
|
191
|
+
const size = this.ai3dCheckImage(image_path);
|
|
192
|
+
await this.ai3dRunningCheck('image');
|
|
193
|
+
const up = await this.ai3dUpload(image_path, 'Picture');
|
|
194
|
+
const params = { business_type: 'dyeh', dyeh_custom_params: { req_json: { faceNum: FACE_NUM[precision] } } };
|
|
195
|
+
const task_id = await this.ai3dSubmit('image', { params: JSON.stringify(params), uriList: [up.decryptedUri] }, true);
|
|
196
|
+
return { task_id, kind: 'image', precision, face_num: FACE_NUM[precision], image: size };
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
async ai3dAnimationSubmit({ video_path, model = 'whole' }) {
|
|
200
|
+
this.ensureCompatible('ai3d');
|
|
201
|
+
const video = this.ai3dCheckVideo(video_path, 'animation');
|
|
202
|
+
let modelPath;
|
|
203
|
+
let reqModel;
|
|
204
|
+
let rig = null;
|
|
205
|
+
if (Object.prototype.hasOwnProperty.call(PRESET_MODELS, model)) {
|
|
206
|
+
// The url() the panel resolves built-in files with; orionui's url() returns a relative path.
|
|
207
|
+
modelPath = this.mod(M.globalConfig).url(`./BuiltinResource/AssetLibrary/${PRESET_MODELS[model]}.fbx`);
|
|
208
|
+
if (!fs.existsSync(modelPath)) throw Error(`PRESET_MODEL_MISSING: ${modelPath}`);
|
|
209
|
+
reqModel = { presetModel: PRESET_MODELS[model], localModel: '' };
|
|
210
|
+
} else {
|
|
211
|
+
if (typeof model === 'string' && !path.isAbsolute(model)) throw Error(`INVALID_MODEL: ${model}; pass one of ${Object.keys(PRESET_MODELS).join(', ')} or the absolute path of an .fbx`);
|
|
212
|
+
modelPath = checkFile(model, ['fbx'], 'MODEL');
|
|
213
|
+
rig = await this.ai3dCheckRig(modelPath);
|
|
214
|
+
reqModel = { presetModel: '', localModel: path.basename(modelPath) };
|
|
215
|
+
}
|
|
216
|
+
await this.ai3dRunningCheck('animation');
|
|
217
|
+
const root = this.ai3dConst().AIGen3DFolder;
|
|
218
|
+
fs.mkdirSync(root, { recursive: true });
|
|
219
|
+
const dir = fs.mkdtempSync(path.join(root, 'xiangsu-mcp-ext-upload-'));
|
|
220
|
+
try {
|
|
221
|
+
const cover = path.join(dir, 'cover.png');
|
|
222
|
+
this.mod(M.preview).PreviewManager.getInstance().getFirstNonBlackFrame(video_path, cover);
|
|
223
|
+
if (!fs.existsSync(cover)) throw Error(`VIDEO_COVER_FAILED: no frame could be read from ${video_path}`);
|
|
224
|
+
const coverUp = await this.ai3dUpload(cover, 'Picture');
|
|
225
|
+
// The panel zips the model with its file name at the root of the archive.
|
|
226
|
+
const zip = path.join(dir, 'model.zip');
|
|
227
|
+
this.ai3dRun('zip', ['-j', '-q', zip, modelPath]);
|
|
228
|
+
const modelUp = await this.ai3dUpload(zip, 'File');
|
|
229
|
+
const info = await this.ai3dUploadVideo(video_path);
|
|
230
|
+
const params = { model: `tos://${modelUp.decryptedUri}`, video: info.vid, req_json: { videoCoverUri: coverUp.url, ...reqModel } };
|
|
231
|
+
const task_id = await this.ai3dSubmit('animation', { params: JSON.stringify(params) }, true);
|
|
232
|
+
const out = { task_id, kind: 'animation', model: Object.prototype.hasOwnProperty.call(PRESET_MODELS, model) ? model : modelPath, video };
|
|
233
|
+
if (rig) {
|
|
234
|
+
out.model_check = rig;
|
|
235
|
+
if (rig.missing_bones.length) out.warning = `模型缺少标准人形骨骼 ${rig.missing_bones.join('、')}(名字要完全一致,不能带 mixamorig_ 之类的前缀);编辑器在这种情况下也只是提醒,照样提交,生成结果可能不对`;
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
} finally {
|
|
239
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
|
|
243
|
+
// Step one of 高精度模型重建: the cloud finds the subject in the video; step two (ai3dGsSubmit) builds it.
|
|
244
|
+
async ai3dGsSegment({ video_path, wait_s = 120 }) {
|
|
245
|
+
this.ensureCompatible('ai3d');
|
|
246
|
+
this.ai3dCheckVideo(video_path, 'gs');
|
|
247
|
+
await this.ai3dRunningCheck('gs');
|
|
248
|
+
const info = await this.ai3dUploadVideo(video_path);
|
|
249
|
+
const req_json = { req_type: 1, vid: info.vid, prompt: '', duration: info.duration, width: info.width, height: info.height };
|
|
250
|
+
const params = { business_type: 'dyeh', dyeh_custom_params: { req_json } };
|
|
251
|
+
const seg_task_id = await this.ai3dSubmit('gs', { params: JSON.stringify(params), uriList: [''] }, false);
|
|
252
|
+
let res;
|
|
253
|
+
try {
|
|
254
|
+
// On timeout this cancels the task itself, as the panel's window does.
|
|
255
|
+
res = await this.mod(M.aiApiUtils).QueryTaskResult(seg_task_id, 1000, wait_s * 1000);
|
|
256
|
+
} catch (e) {
|
|
257
|
+
const why = /timeout/i.test(e.message)
|
|
258
|
+
? `; the editor stopped waiting before the cloud answered (wait_s ${wait_s}), which says nothing about the video: retry with a larger wait_s`
|
|
259
|
+
: '';
|
|
260
|
+
throw Error(`AI3D_SUBJECT_FAILED: ${e.message} (task ${seg_task_id})${why}`);
|
|
261
|
+
}
|
|
262
|
+
const node = (((res && res.result) || {}).output_node_list || [])[0] || {};
|
|
263
|
+
const subject_image_url = (node.image_url_list || [])[0];
|
|
264
|
+
const subject_uri = ((node.image_info_list || [])[0] || {}).uri;
|
|
265
|
+
if (!subject_image_url || !subject_uri) throw Error(`AI3D_SUBJECT_NOT_FOUND: the cloud found no subject in the video (task ${seg_task_id})`);
|
|
266
|
+
return { seg_task_id, subject_image_url, subject_uri, req_json };
|
|
267
|
+
},
|
|
268
|
+
|
|
269
|
+
async ai3dGsSubmit({ subject_uri, req_json }) {
|
|
270
|
+
this.ensureCompatible('ai3d');
|
|
271
|
+
if (typeof subject_uri !== 'string' || !subject_uri || !req_json || !req_json.vid) throw Error('INVALID_SUBJECT: pass what ai3d_gs_segment returned');
|
|
272
|
+
await this.ai3dRunningCheck('gs');
|
|
273
|
+
const params = { business_type: 'dyeh', dyeh_custom_params: { req_json: { ...req_json, req_type: 0 } } };
|
|
274
|
+
const task_id = await this.ai3dSubmit('gs', { params: JSON.stringify(params), uriList: [subject_uri] }, true);
|
|
275
|
+
return { task_id, kind: 'gs' };
|
|
276
|
+
},
|
|
277
|
+
|
|
278
|
+
ai3dLocal(task_id) {
|
|
279
|
+
const info = this.mod(M.ai3dUtils).getGen3DModelLocalInfoByTaskId(task_id);
|
|
280
|
+
const has = (p) => fs.existsSync(p);
|
|
281
|
+
return {
|
|
282
|
+
downloaded: has(info.unzipLocalPath),
|
|
283
|
+
preview_image_file: has(info.previewVideoCoverPath) ? info.previewVideoCoverPath : null,
|
|
284
|
+
preview_video_file: has(info.previewVideoPath) ? info.previewVideoPath : null,
|
|
285
|
+
};
|
|
286
|
+
},
|
|
287
|
+
|
|
288
|
+
async ai3dHistory(kind, cursor, size) {
|
|
289
|
+
const c = this.ai3dConst();
|
|
290
|
+
const res = await this.mod(M.aiEffectApi).queryAITaskHistories({ cursor, size, scene: c.AIGen3DModelScene[GEN_TYPE[kind]], statusList: c.AIGen3DModelQueryStatus });
|
|
291
|
+
if (!res || res.code !== 0) throw Error(`AI3D_HISTORY_FAILED: code=${res && res.code} message=${(res && res.message) || ''}`);
|
|
292
|
+
return res.data || {};
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
// The history record of one task, or null when it is not among the latest HISTORY_PAGES pages.
|
|
296
|
+
async ai3dRecord(kind, task_id) {
|
|
297
|
+
let cursor = 0;
|
|
298
|
+
for (let page = 0; page < HISTORY_PAGES; page++) {
|
|
299
|
+
const data = await this.ai3dHistory(kind, cursor, HISTORY_PAGE);
|
|
300
|
+
const hit = (data.taskRecords || []).find((t) => String(t.taskId) === task_id);
|
|
301
|
+
if (hit) return hit;
|
|
302
|
+
if (!data.hasMore) return null;
|
|
303
|
+
cursor = data.nextCursor;
|
|
304
|
+
}
|
|
305
|
+
return null;
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
async ai3dTasks({ kind, cursor = 0, count = HISTORY_PAGE }) {
|
|
309
|
+
this.ensureCompatible('ai3d');
|
|
310
|
+
checkKind(kind);
|
|
311
|
+
const data = await this.ai3dHistory(kind, cursor, count);
|
|
312
|
+
const items = (data.taskRecords || []).map((t) => ({
|
|
313
|
+
task_id: String(t.taskId),
|
|
314
|
+
status: STATUS[t.status] || String(t.status),
|
|
315
|
+
created_at: iso(t.createTime),
|
|
316
|
+
finished_at: t.status === 1 ? null : iso(t.finishTime),
|
|
317
|
+
...taskInput(kind, t),
|
|
318
|
+
...(t.status === 3 ? { failure: failureText(t) } : {}),
|
|
319
|
+
...this.ai3dLocal(String(t.taskId)),
|
|
320
|
+
}));
|
|
321
|
+
return { kind, items, next_cursor: data.nextCursor ?? null, has_more: !!data.hasMore };
|
|
322
|
+
},
|
|
323
|
+
|
|
324
|
+
async ai3dQuery(task_id) {
|
|
325
|
+
const res = await this.mod(M.aiEffectApi).queryAIGCResult({ taskId: task_id });
|
|
326
|
+
if (!res || res.code !== 0 || !res.data) throw Error(`AI3D_QUERY_FAILED: task ${task_id} code=${res && res.code} message=${(res && res.message) || ''}`);
|
|
327
|
+
return res.data;
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
// One task's state; with wait_s it keeps asking until the task stops running or the time is up.
|
|
331
|
+
async ai3dTask({ task_id, wait_s = 0 }) {
|
|
332
|
+
this.ensureCompatible('ai3d');
|
|
333
|
+
if (typeof task_id !== 'string' || !task_id) throw Error('INVALID_TASK_ID: task_id must be a non-empty string');
|
|
334
|
+
const poll = this.ai3dPollMs ?? POLL_MS;
|
|
335
|
+
const until = Date.now() + wait_s * 1000;
|
|
336
|
+
let data = await this.ai3dQuery(task_id);
|
|
337
|
+
while (data.status === 1 && Date.now() + poll <= until) {
|
|
338
|
+
await sleep(poll);
|
|
339
|
+
data = await this.ai3dQuery(task_id);
|
|
340
|
+
}
|
|
341
|
+
const out = { task_id, status: STATUS[data.status] || String(data.status) };
|
|
342
|
+
if (data.status === 3) out.failure = failureText(data);
|
|
343
|
+
if (data.status === 2) out.result_ready = !!((((data.outputNodeList || [])[0] || {}).objectInfoList || [])[0] || {}).url;
|
|
344
|
+
return { ...out, ...this.ai3dLocal(task_id) };
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
async ai3dImport({ task_id, kind }) {
|
|
348
|
+
this.ensureCompatible('ai3d');
|
|
349
|
+
checkKind(kind);
|
|
350
|
+
if (typeof task_id !== 'string' || !task_id) throw Error('INVALID_TASK_ID: task_id must be a non-empty string');
|
|
351
|
+
const EF = this.mod(M.ef);
|
|
352
|
+
const { sceneEditor, project } = editingScene(this);
|
|
353
|
+
const data = await this.ai3dQuery(task_id);
|
|
354
|
+
if (data.status !== 2) throw Error(`AI3D_NOT_FINISHED: task ${task_id} is ${STATUS[data.status] || data.status}`);
|
|
355
|
+
const url = ((((data.outputNodeList || [])[0] || {}).objectInfoList || [])[0] || {}).url;
|
|
356
|
+
if (!url) throw Error(`AI3D_NO_RESULT: task ${task_id} has no result file`);
|
|
357
|
+
const info = this.mod(M.ai3dUtils).getGen3DModelLocalInfoByTaskId(task_id);
|
|
358
|
+
// Absent until the account's first AI 3D result.
|
|
359
|
+
fs.mkdirSync(path.dirname(info.zipLocalPath), { recursive: true });
|
|
360
|
+
if (!fs.existsSync(info.zipLocalPath)) await this.mod(M.lokiUtils).downloadTemplateFile(url, info.zipLocalPath, DOWNLOAD_TIMEOUT_MS);
|
|
361
|
+
if (!fs.existsSync(info.unzipLocalPath)) this.ai3dRun('ditto', ['-x', '-k', info.zipLocalPath, info.unzipLocalPath]);
|
|
362
|
+
const spec = IMPORT[kind];
|
|
363
|
+
const result = info[spec.file];
|
|
364
|
+
if (!fs.existsSync(result)) throw Error(`AI3D_RESULT_MISSING: ${path.basename(result)} is not in task ${task_id}'s result; is it a ${kind} task?`);
|
|
365
|
+
const I18n = this.mod(M.orionui).I18n;
|
|
366
|
+
let file;
|
|
367
|
+
if (kind === 'image') file = path.join(info.unzipLocalPath, `${I18n.t('ai_3d_generation')}.glb`);
|
|
368
|
+
else if (kind === 'gs') file = path.join(info.unzipLocalPath, '视频生3DGS模型.ply');
|
|
369
|
+
else {
|
|
370
|
+
// The panel names the copy after the model the task was made with.
|
|
371
|
+
const record = await this.ai3dRecord('animation', task_id);
|
|
372
|
+
const { model } = taskInput('animation', record || {});
|
|
373
|
+
const name = Object.prototype.hasOwnProperty.call(PRESET_MODELS, model) ? `${I18n.t(PRESET_MODELS[model])}.fbx` : model || `${task_id}.fbx`;
|
|
374
|
+
file = path.join(info.unzipLocalPath, 'animationResult', name);
|
|
375
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
376
|
+
}
|
|
377
|
+
if (!fs.existsSync(file)) fs.copyFileSync(result, file);
|
|
378
|
+
const before = sceneObjectSet(sceneEditor);
|
|
379
|
+
const beforePaths = new Set(projectResources(project).map((r) => r.assetPath));
|
|
380
|
+
const RIH = this.mod(M.resImport).ResourceImportHandles;
|
|
381
|
+
let item;
|
|
382
|
+
try {
|
|
383
|
+
const pending = RIH.importExtAsset([file], false, undefined, true, { enablePopupWindow: false });
|
|
384
|
+
item = await new Promise((resolve) => RIH.registerLoadingCallbackByResItem(pending, (res, state) => resolve(state === 'loaded' ? res : null)));
|
|
385
|
+
} finally {
|
|
386
|
+
// The editor keeps the animation copy and removes the other two once imported.
|
|
387
|
+
if (kind !== 'animation') fs.rmSync(file, { force: true });
|
|
388
|
+
}
|
|
389
|
+
if (!item) throw Error(`AI3D_IMPORT_FAILED: the editor could not import ${path.basename(file)}`);
|
|
390
|
+
const tag = { id: `${spec.idPrefix}${task_id}`, styleTag: '', type: spec.type };
|
|
391
|
+
for (const r of [item, ...(item.subResourceItems || [])]) {
|
|
392
|
+
if (!r) continue;
|
|
393
|
+
r.assetPackageID = tag.id;
|
|
394
|
+
r.assetStudioData = { ...tag };
|
|
395
|
+
}
|
|
396
|
+
const created = EF.Extension.Scene.AssetSceneObjectCreationSystem.createSceneObjectFromAssetWithUndo({
|
|
397
|
+
sceneEditor,
|
|
398
|
+
resItems: [item],
|
|
399
|
+
onDoUndoRedo: () => EF.SceneView.SceneView.repaintAll(),
|
|
400
|
+
});
|
|
401
|
+
for (const so of created) {
|
|
402
|
+
if (kind === 'image') {
|
|
403
|
+
const scale = so.getChildren()[0];
|
|
404
|
+
scale.target.getComponent('Transform').localScale = new EF.APJS.Vector3f(15, 15, 15);
|
|
405
|
+
scale.name = I18n.t('ai_3d_generation_scale');
|
|
406
|
+
scale.getChildren()[0].name = I18n.t('ai_3d_generation_mesh');
|
|
407
|
+
} else if (kind === 'gs') {
|
|
408
|
+
so.target.getComponent('Transform').localScale = new EF.APJS.Vector3f(0.3, 0.3, 0.3);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const added = addedObjectEditors(before, sceneEditor);
|
|
412
|
+
if (!added.length) throw Error(`AI3D_IMPORT_NO_OBJECT: ${path.basename(file)} was imported but nothing was added to the scene`);
|
|
413
|
+
const packageFolders = packageRootsAdded(project, beforePaths);
|
|
414
|
+
recordImport(project, added, packageFolders);
|
|
415
|
+
return {
|
|
416
|
+
task_id,
|
|
417
|
+
kind,
|
|
418
|
+
added_objects: added.map((o) => ({ name: o.name, object_path: sceneObjectPath(o) })),
|
|
419
|
+
package_folders: packageFolders,
|
|
420
|
+
...this.ai3dLocal(task_id),
|
|
421
|
+
};
|
|
422
|
+
},
|
|
423
|
+
|
|
424
|
+
async ai3dCancel({ task_id }) {
|
|
425
|
+
this.ensureCompatible('ai3d');
|
|
426
|
+
if (typeof task_id !== 'string' || !task_id) throw Error('INVALID_TASK_ID: task_id must be a non-empty string');
|
|
427
|
+
const res = await this.mod(M.aiEffectApi).cancelAIGCRequest({ taskId: task_id });
|
|
428
|
+
if (!res || res.code !== 0) throw Error(`AI3D_CANCEL_FAILED: task ${task_id} code=${res && res.code} message=${(res && res.message) || ''}`);
|
|
429
|
+
return { task_id, cancelled: true };
|
|
430
|
+
},
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const METHODS = {
|
|
434
|
+
ai3d_image_submit: 'ai3dImageSubmit',
|
|
435
|
+
ai3d_animation_submit: 'ai3dAnimationSubmit',
|
|
436
|
+
ai3d_gs_segment: 'ai3dGsSegment',
|
|
437
|
+
ai3d_gs_submit: 'ai3dGsSubmit',
|
|
438
|
+
ai3d_tasks: 'ai3dTasks',
|
|
439
|
+
ai3d_task: 'ai3dTask',
|
|
440
|
+
ai3d_import: 'ai3dImport',
|
|
441
|
+
ai3d_cancel: 'ai3dCancel',
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
module.exports = { methods, METHODS, GEN_TYPE, FACE_NUM, IMAGE_LIMIT, VIDEO_LIMIT, MAX_RUNNING, PRESET_MODELS, REQUIRED_BONES, HISTORY_PAGE };
|