dazscript-framework 1.0.34 → 1.0.36
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/package.json +1 -1
- package/src/helpers/figure-helper.ts +23 -0
- package/src/helpers/material-helper.ts +43 -0
- package/src/helpers/obj-file-helper.ts +112 -0
- package/src/helpers/primitive-helper.ts +67 -0
- package/src/helpers/scene-helper.ts +35 -0
- package/src/helpers/scene-node-helper.ts +1 -0
- package/src/helpers/string-helper.ts +4 -0
package/package.json
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const resolveFigureNode = (node: DzNode | null): DzSkeleton | null => {
|
|
2
|
+
if (!node) return null
|
|
3
|
+
if (node.inherits('DzSkeleton')) return node as DzSkeleton
|
|
4
|
+
return node.getSkeleton?.() ?? null
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const getFigureResolutionLevel = (figure: DzNode): number | null => {
|
|
8
|
+
const property = getFigureResolutionProperty(figure)
|
|
9
|
+
return property ? property.getValue() : null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const setFigureResolutionLevel = (figure: DzNode, resolution: number): boolean => {
|
|
13
|
+
const property = getFigureResolutionProperty(figure)
|
|
14
|
+
if (!property) return false
|
|
15
|
+
|
|
16
|
+
property.setValue(resolution)
|
|
17
|
+
return true
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const getFigureResolutionProperty = (figure: DzNode): DzEnumProperty | null => {
|
|
21
|
+
const shape = figure.getObject()?.getCurrentShape()
|
|
22
|
+
return shape?.findProperty('lodlevel') as DzEnumProperty | null
|
|
23
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type SimpleMaterialOptions = {
|
|
2
|
+
color: Color
|
|
3
|
+
opacity: number
|
|
4
|
+
name?: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const applySingleMaterial = (node: DzNode, options: SimpleMaterialOptions): boolean => {
|
|
8
|
+
const shape = node.getObject()?.getCurrentShape()
|
|
9
|
+
if (!shape) return false
|
|
10
|
+
|
|
11
|
+
if (shape.getNumMaterials() < 1) {
|
|
12
|
+
shape.createMaterial(options.name ?? 'Material')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let applied = false
|
|
16
|
+
for (let i = 0; i < shape.getNumMaterials(); i++) {
|
|
17
|
+
const material = shape.getMaterial(i)
|
|
18
|
+
if (!material) continue
|
|
19
|
+
applyMaterialValues(material, options)
|
|
20
|
+
applied = true
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return applied
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const applyMaterialValues = (material: DzMaterial, options: SimpleMaterialOptions): void => {
|
|
27
|
+
const anyMaterial = material as any
|
|
28
|
+
if (options.name && typeof anyMaterial.setName === 'function') {
|
|
29
|
+
anyMaterial.setName(options.name)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
material.setDiffuseColor(options.color)
|
|
33
|
+
material.setBaseOpacity(clampOpacity(options.opacity))
|
|
34
|
+
|
|
35
|
+
if (typeof anyMaterial.setOpacity === 'function') {
|
|
36
|
+
anyMaterial.setOpacity(clampOpacity(options.opacity))
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const clampOpacity = (opacity: number): number => {
|
|
41
|
+
if (!isFinite(opacity) || isNaN(opacity)) return 0.25
|
|
42
|
+
return Math.max(0, Math.min(1, opacity))
|
|
43
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
export type SilentObjExportOptions = {
|
|
2
|
+
selectedOnly?: boolean
|
|
3
|
+
selectedRootsOnly?: boolean
|
|
4
|
+
includeParented?: boolean
|
|
5
|
+
writeUvs?: boolean
|
|
6
|
+
writeNormals?: boolean
|
|
7
|
+
writeObjects?: boolean
|
|
8
|
+
writeGroups?: boolean
|
|
9
|
+
writeMaterials?: boolean
|
|
10
|
+
writeMaterialLibrary?: boolean
|
|
11
|
+
groupByGeometry?: boolean
|
|
12
|
+
groupByNodes?: boolean
|
|
13
|
+
groupBySurfaces?: boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type ObjFileResult = {
|
|
17
|
+
ok: boolean
|
|
18
|
+
path: string
|
|
19
|
+
result: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const createSilentObjExportSettings = (options: SilentObjExportOptions = {}): DzFileIOSettings => {
|
|
23
|
+
const settings = createObjAxisSettings()
|
|
24
|
+
settings.setBoolValue('IgnoreInvisible', false)
|
|
25
|
+
settings.setBoolValue('WeldSeams', false)
|
|
26
|
+
settings.setBoolValue('RemoveUnusedVerts', false)
|
|
27
|
+
settings.setBoolValue('WriteVT', options.writeUvs ?? true)
|
|
28
|
+
settings.setBoolValue('WriteVN', options.writeNormals ?? false)
|
|
29
|
+
settings.setBoolValue('WriteO', options.writeObjects ?? true)
|
|
30
|
+
settings.setBoolValue('WriteG', options.writeGroups ?? true)
|
|
31
|
+
settings.setBoolValue('GroupGeom', options.groupByGeometry ?? true)
|
|
32
|
+
settings.setBoolValue('GroupNodes', options.groupByNodes ?? false)
|
|
33
|
+
settings.setBoolValue('GroupSurfaces', options.groupBySurfaces ?? true)
|
|
34
|
+
settings.setBoolValue('GroupSingle', false)
|
|
35
|
+
settings.setBoolValue('WriteUsemtl', options.writeMaterials ?? true)
|
|
36
|
+
settings.setBoolValue('WriteMtllib', options.writeMaterialLibrary ?? false)
|
|
37
|
+
settings.setBoolValue('CollectMaps', false)
|
|
38
|
+
settings.setBoolValue('ConvertMaps', false)
|
|
39
|
+
settings.setBoolValue('SelectedOnly', options.selectedOnly ?? true)
|
|
40
|
+
settings.setBoolValue('SelectedRootsOnly', options.selectedRootsOnly ?? true)
|
|
41
|
+
settings.setBoolValue('PrimaryRootOnly', false)
|
|
42
|
+
settings.setBoolValue('IncludeParented', options.includeParented ?? false)
|
|
43
|
+
settings.setBoolValue('TriangulateNgons', false)
|
|
44
|
+
settings.setBoolValue('CollapseUVTiles', false)
|
|
45
|
+
settings.setBoolValue('ShowIndividualSettings', true)
|
|
46
|
+
settings.setIntValue('FloatPrecision', 6)
|
|
47
|
+
settings.setIntValue('RunSilent', 1)
|
|
48
|
+
return settings
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const createMinimalSilentObjExportSettings = (): DzFileIOSettings =>
|
|
52
|
+
createSilentObjExportSettings({
|
|
53
|
+
writeUvs: false,
|
|
54
|
+
writeNormals: false,
|
|
55
|
+
writeObjects: false,
|
|
56
|
+
writeGroups: false,
|
|
57
|
+
writeMaterials: false,
|
|
58
|
+
writeMaterialLibrary: false,
|
|
59
|
+
groupByGeometry: false,
|
|
60
|
+
groupByNodes: false,
|
|
61
|
+
groupBySurfaces: false,
|
|
62
|
+
selectedOnly: true,
|
|
63
|
+
selectedRootsOnly: true,
|
|
64
|
+
includeParented: false
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
export const createSilentObjImportSettings = (): DzFileIOSettings => {
|
|
68
|
+
const settings = createObjAxisSettings()
|
|
69
|
+
settings.setBoolValue('IncludeVT', false)
|
|
70
|
+
settings.setBoolValue('IncludeG', false)
|
|
71
|
+
settings.setBoolValue('IncludeUsemtl', false)
|
|
72
|
+
settings.setBoolValue('IncludeMtllib', false)
|
|
73
|
+
settings.setBoolValue('ShowIndividualSettings', true)
|
|
74
|
+
settings.setIntValue('RunSilent', 1)
|
|
75
|
+
return settings
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const exportObjSilent = (path: string, settings: DzFileIOSettings = createMinimalSilentObjExportSettings()): ObjFileResult => {
|
|
79
|
+
const exporter = (App.getExportMgr() as any).findExporterByClassName('DzObjExporter')
|
|
80
|
+
if (!exporter) return { ok: false, path, result: 'DzObjExporter not found' }
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
const result = exporter.writeFile(path, settings)
|
|
84
|
+
return { ok: true, path, result: String(result) }
|
|
85
|
+
} finally {
|
|
86
|
+
exporter.deleteLater?.()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const importObjSilent = (path: string, settings: DzFileIOSettings = createSilentObjImportSettings()): ObjFileResult => {
|
|
91
|
+
const importer = (App.getImportMgr() as any).findImporterByClassName('DzObjImporter')
|
|
92
|
+
if (!importer) return { ok: false, path, result: 'DzObjImporter not found' }
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const result = importer.readFile(path, settings)
|
|
96
|
+
return { ok: true, path, result: String(result) }
|
|
97
|
+
} finally {
|
|
98
|
+
importer.deleteLater?.()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const createObjAxisSettings = (): DzFileIOSettings => {
|
|
103
|
+
const settings = new DzFileIOSettings()
|
|
104
|
+
settings.setFloatValue('Scale', 243.84)
|
|
105
|
+
settings.setStringValue('LatAxis', 'X')
|
|
106
|
+
settings.setStringValue('VertAxis', 'Y')
|
|
107
|
+
settings.setStringValue('DepthAxis', 'Z')
|
|
108
|
+
settings.setBoolValue('InvertLat', false)
|
|
109
|
+
settings.setBoolValue('InvertVert', false)
|
|
110
|
+
settings.setBoolValue('InvertDepth', false)
|
|
111
|
+
return settings
|
|
112
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export type PrimitiveKind = 'Sphere' | 'Cube' | 'Cone' | 'Plane' | 'Torus' | 'Cylinder'
|
|
2
|
+
|
|
3
|
+
export type CreatePrimitiveOptions = {
|
|
4
|
+
kind: PrimitiveKind
|
|
5
|
+
name?: string
|
|
6
|
+
label?: string
|
|
7
|
+
size?: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type CreatePrimitiveResult = {
|
|
11
|
+
ok: boolean
|
|
12
|
+
node: DzNode | null
|
|
13
|
+
message: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const Geometry: DzGeometryUtil | undefined
|
|
17
|
+
|
|
18
|
+
export const createPrimitive = (options: CreatePrimitiveOptions): CreatePrimitiveResult => {
|
|
19
|
+
const geometryUtil = getGeometryUtil()
|
|
20
|
+
if (!geometryUtil) {
|
|
21
|
+
return { ok: false, node: null, message: 'Global Geometry utility is not available' }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const primitiveType = getPrimitiveType(geometryUtil, options.kind)
|
|
25
|
+
if (primitiveType === null) {
|
|
26
|
+
return { ok: false, node: null, message: `Unsupported primitive kind: ${options.kind}` }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const settings = new DzSettings()
|
|
30
|
+
geometryUtil.getDefaultPrimitiveOptions(primitiveType, settings)
|
|
31
|
+
|
|
32
|
+
if (typeof options.size === 'number' && isFinite(options.size) && options.size > 0) {
|
|
33
|
+
settings.setFloatValue('size', options.size)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const node = geometryUtil.createPrimitive(settings)
|
|
37
|
+
if (!node) {
|
|
38
|
+
return { ok: false, node: null, message: `DzGeometryUtil did not create a ${options.kind}` }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (options.name) node.setName(options.name)
|
|
42
|
+
if (options.label) node.setLabel(options.label)
|
|
43
|
+
if (!isNodeInScene(node)) Scene.addNode(node)
|
|
44
|
+
|
|
45
|
+
return { ok: true, node, message: '' }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const getGeometryUtil = (): DzGeometryUtil | null =>
|
|
49
|
+
typeof Geometry !== 'undefined' ? Geometry : null
|
|
50
|
+
|
|
51
|
+
const getPrimitiveType = (geometryUtil: DzGeometryUtil, kind: PrimitiveKind): number | null => {
|
|
52
|
+
const primitiveTypes = geometryUtil as any
|
|
53
|
+
if (kind === 'Sphere') return primitiveTypes.Sphere
|
|
54
|
+
if (kind === 'Cube') return primitiveTypes.Cube
|
|
55
|
+
if (kind === 'Cone') return primitiveTypes.Cone
|
|
56
|
+
if (kind === 'Plane') return primitiveTypes.Plane
|
|
57
|
+
if (kind === 'Torus') return primitiveTypes.Torus
|
|
58
|
+
if (kind === 'Cylinder') return primitiveTypes.Cylinder
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const isNodeInScene = (node: DzNode): boolean => {
|
|
63
|
+
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
64
|
+
if (Scene.getNode(i) === node) return true
|
|
65
|
+
}
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
@@ -2,6 +2,7 @@ import { scene } from '@dsf/core/global'
|
|
|
2
2
|
import { contains, distinct, group } from './array-helper'
|
|
3
3
|
import { getRoot, isFigure } from './node-helper'
|
|
4
4
|
import { getPaneNodeEditor, getParametersPane, getParametersPaneNodeEditor } from './pane-helper'
|
|
5
|
+
import { startsWith } from './string-helper'
|
|
5
6
|
|
|
6
7
|
export const getSelectedOf = <T>(typeName: string): DzNode[] => {
|
|
7
8
|
let nodes: DzNode[] = []
|
|
@@ -143,3 +144,37 @@ export const timeToFrame = (time: DzTime): number => {
|
|
|
143
144
|
export const frameToTime = (frame: number): DzTime => {
|
|
144
145
|
return new DzTime(scene.getTimeStep().valueOf() * frame)
|
|
145
146
|
}
|
|
147
|
+
|
|
148
|
+
export const collectSceneNodeIds = (): number[] => {
|
|
149
|
+
const ids: number[] = []
|
|
150
|
+
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
151
|
+
ids.push(Scene.getNode(i).elementID)
|
|
152
|
+
}
|
|
153
|
+
return ids
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export const getNodesAddedAfter = (beforeIds: number[]): DzNode[] => {
|
|
157
|
+
const nodes: DzNode[] = []
|
|
158
|
+
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
159
|
+
const node = Scene.getNode(i)
|
|
160
|
+
if (beforeIds.indexOf(node.elementID) < 0) nodes.push(node)
|
|
161
|
+
}
|
|
162
|
+
return nodes
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export const removeNodesByNameOrLabelPrefix = (prefix: string): number => {
|
|
166
|
+
const nodes: DzNode[] = []
|
|
167
|
+
|
|
168
|
+
for (let i = 0; i < Scene.getNumNodes(); i++) {
|
|
169
|
+
const node = Scene.getNode(i)
|
|
170
|
+
if (startsWith(String(node.getName()), prefix) || startsWith(String(node.getLabel()), prefix)) {
|
|
171
|
+
nodes.push(node)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
176
|
+
Scene.removeNode(nodes[i])
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return nodes.length
|
|
180
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { collectSceneNodeIds, getNodesAddedAfter, removeNodesByNameOrLabelPrefix } from './scene-helper'
|
|
@@ -14,6 +14,10 @@ export const contains = (source: string, search: string | string[]): boolean =>
|
|
|
14
14
|
return source.indexOf(search) >= 0
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
export const startsWith = (source: string, search: string): boolean => {
|
|
18
|
+
return source.substr(0, search.length) === search
|
|
19
|
+
}
|
|
20
|
+
|
|
17
21
|
export const remove = (source: string, search: string): string => {
|
|
18
22
|
return source.replace(search, "")
|
|
19
23
|
}
|