dazscript-framework 1.0.33 → 1.0.35

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dazscript-framework",
3
- "version": "1.0.33",
3
+ "version": "1.0.35",
4
4
  "author": "Freddy Diaz",
5
5
  "license": "MPL-2.0",
6
6
  "description": "",
@@ -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,37 @@
1
+ export const collectSceneNodeIds = (): number[] => {
2
+ const ids: number[] = []
3
+ for (let i = 0; i < Scene.getNumNodes(); i++) {
4
+ ids.push(Scene.getNode(i).elementID)
5
+ }
6
+ return ids
7
+ }
8
+
9
+ export const getNodesAddedAfter = (beforeIds: number[]): DzNode[] => {
10
+ const nodes: DzNode[] = []
11
+ for (let i = 0; i < Scene.getNumNodes(); i++) {
12
+ const node = Scene.getNode(i)
13
+ if (beforeIds.indexOf(node.elementID) < 0) nodes.push(node)
14
+ }
15
+ return nodes
16
+ }
17
+
18
+ export const removeNodesByNameOrLabelPrefix = (prefix: string): number => {
19
+ const nodes: DzNode[] = []
20
+
21
+ for (let i = 0; i < Scene.getNumNodes(); i++) {
22
+ const node = Scene.getNode(i)
23
+ if (startsWith(String(node.getName()), prefix) || startsWith(String(node.getLabel()), prefix)) {
24
+ nodes.push(node)
25
+ }
26
+ }
27
+
28
+ for (let i = 0; i < nodes.length; i++) {
29
+ Scene.removeNode(nodes[i])
30
+ nodes[i].deleteLater?.()
31
+ }
32
+
33
+ return nodes.length
34
+ }
35
+
36
+ const startsWith = (value: string, prefix: string): boolean =>
37
+ value.substr(0, prefix.length) === prefix
@@ -0,0 +1,81 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { getMirrorNameCandidate, getMirrorNodeMatch, getMirrorNodes } from './skeleton-helper'
3
+
4
+ type TestNode = {
5
+ name: string
6
+ getName: () => string
7
+ }
8
+
9
+ const node = (name: string): TestNode => ({
10
+ name,
11
+ getName: () => name
12
+ })
13
+
14
+ const figure = (names: string[]) => ({
15
+ findNodeChild: (name: string) => names.indexOf(name) >= 0 ? node(name) : null
16
+ })
17
+
18
+ describe('skeleton mirror helper', () => {
19
+ it('resolves Genesis-style compact prefixes', () => {
20
+ expect(getMirrorNameCandidate('rThighBend')).toMatchObject({
21
+ side: 'right',
22
+ convention: 'compact-prefix',
23
+ sourceToken: 'r',
24
+ mirrorToken: 'l',
25
+ mirrorName: 'lThighBend'
26
+ })
27
+
28
+ expect(getMirrorNameCandidate('lHand')).toMatchObject({
29
+ side: 'left',
30
+ mirrorName: 'rHand'
31
+ })
32
+ })
33
+
34
+ it('resolves underscore prefix and suffix names', () => {
35
+ expect(getMirrorNameCandidate('right_hand')).toMatchObject({
36
+ side: 'right',
37
+ convention: 'underscore-prefix',
38
+ mirrorName: 'left_hand'
39
+ })
40
+
41
+ expect(getMirrorNameCandidate('hand_R')).toMatchObject({
42
+ side: 'right',
43
+ convention: 'underscore-suffix',
44
+ mirrorName: 'hand_L'
45
+ })
46
+ })
47
+
48
+ it('resolves word prefix and suffix names', () => {
49
+ expect(getMirrorNameCandidate('RightHand')).toMatchObject({
50
+ side: 'right',
51
+ convention: 'word-prefix',
52
+ mirrorName: 'LeftHand'
53
+ })
54
+
55
+ expect(getMirrorNameCandidate('HandLeft')).toMatchObject({
56
+ side: 'left',
57
+ convention: 'word-suffix',
58
+ mirrorName: 'HandRight'
59
+ })
60
+ })
61
+
62
+ it('does not classify center bones as mirrored', () => {
63
+ expect(getMirrorNameCandidate('hip')).toBeNull()
64
+ expect(getMirrorNameCandidate('abdomenLower')).toBeNull()
65
+ })
66
+
67
+ it('returns rich match metadata while keeping getMirrorNodes compatible', () => {
68
+ const testFigure = figure(['lThighBend', 'hip'])
69
+ const source = node('rThighBend')
70
+ const center = node('hip')
71
+
72
+ const match = getMirrorNodeMatch(testFigure as unknown as DzSkeleton, source as unknown as DzNode)
73
+
74
+ expect(match.side).toBe('right')
75
+ expect(match.mirror?.getName()).toBe('lThighBend')
76
+ expect(getMirrorNodes(testFigure as unknown as DzSkeleton, [
77
+ source as unknown as DzNode,
78
+ center as unknown as DzNode
79
+ ]).map(item => item.getName())).toEqual(['lThighBend'])
80
+ })
81
+ })
@@ -10,18 +10,143 @@ export const getModifiers = (figure: DzSkeleton): DzModifier[] => {
10
10
  return modifiers
11
11
  }
12
12
 
13
+ export type MirrorSide = 'left' | 'right'
14
+
15
+ export type MirrorNameConvention =
16
+ | 'compact-prefix'
17
+ | 'underscore-prefix'
18
+ | 'underscore-suffix'
19
+ | 'word-prefix'
20
+ | 'word-suffix'
21
+
22
+ export type MirrorNodeMatch = {
23
+ source: DzNode
24
+ mirror: DzNode | null
25
+ side: MirrorSide | null
26
+ convention: MirrorNameConvention | null
27
+ sourceToken: string | null
28
+ mirrorToken: string | null
29
+ mirrorName: string | null
30
+ }
31
+
32
+ type MirrorNameCandidate = Omit<MirrorNodeMatch, 'source' | 'mirror'>
33
+
34
+ const preserveCase = (source: string, replacement: string): string => {
35
+ if (source.toUpperCase() === source) return replacement.toUpperCase()
36
+ if (source.toLowerCase() === source) return replacement.toLowerCase()
37
+ if (source.length > 0 && source[0].toUpperCase() === source[0]) {
38
+ return replacement[0].toUpperCase() + replacement.substring(1).toLowerCase()
39
+ }
40
+
41
+ return replacement
42
+ }
43
+
44
+ export const getMirrorNameCandidate = (name: string): MirrorNameCandidate | null => {
45
+ const compactPrefix = /^([lr])([A-Z].*)$/.exec(name)
46
+ if (compactPrefix) {
47
+ const sourceToken = compactPrefix[1]
48
+ const mirrorToken = sourceToken === 'r' ? 'l' : 'r'
49
+ return {
50
+ side: sourceToken === 'r' ? 'right' : 'left',
51
+ convention: 'compact-prefix',
52
+ sourceToken,
53
+ mirrorToken,
54
+ mirrorName: `${mirrorToken}${compactPrefix[2]}`
55
+ }
56
+ }
57
+
58
+ const underscorePrefix = /^(r|l|right|left)([_-].+)$/i.exec(name)
59
+ if (underscorePrefix) {
60
+ const sourceToken = underscorePrefix[1]
61
+ const side = sourceToken.toLowerCase()[0] === 'r' ? 'right' : 'left'
62
+ const mirrorToken = preserveCase(sourceToken, sourceToken.length === 1
63
+ ? (side === 'right' ? 'l' : 'r')
64
+ : (side === 'right' ? 'left' : 'right'))
65
+ return {
66
+ side,
67
+ convention: 'underscore-prefix',
68
+ sourceToken,
69
+ mirrorToken,
70
+ mirrorName: `${mirrorToken}${underscorePrefix[2]}`
71
+ }
72
+ }
73
+
74
+ const underscoreSuffix = /^(.+[_-])(r|l|right|left)$/i.exec(name)
75
+ if (underscoreSuffix) {
76
+ const sourceToken = underscoreSuffix[2]
77
+ const side = sourceToken.toLowerCase()[0] === 'r' ? 'right' : 'left'
78
+ const mirrorToken = preserveCase(sourceToken, sourceToken.length === 1
79
+ ? (side === 'right' ? 'l' : 'r')
80
+ : (side === 'right' ? 'left' : 'right'))
81
+ return {
82
+ side,
83
+ convention: 'underscore-suffix',
84
+ sourceToken,
85
+ mirrorToken,
86
+ mirrorName: `${underscoreSuffix[1]}${mirrorToken}`
87
+ }
88
+ }
89
+
90
+ const wordPrefix = /^(right|left)([A-Z].*)$/i.exec(name)
91
+ if (wordPrefix) {
92
+ const sourceToken = wordPrefix[1]
93
+ const side = sourceToken.toLowerCase() === 'right' ? 'right' : 'left'
94
+ const mirrorToken = preserveCase(sourceToken, side === 'right' ? 'left' : 'right')
95
+ return {
96
+ side,
97
+ convention: 'word-prefix',
98
+ sourceToken,
99
+ mirrorToken,
100
+ mirrorName: `${mirrorToken}${wordPrefix[2]}`
101
+ }
102
+ }
103
+
104
+ const wordSuffix = /^(.+[a-z])(Right|Left)$/i.exec(name)
105
+ if (wordSuffix) {
106
+ const sourceToken = wordSuffix[2]
107
+ const side = sourceToken.toLowerCase() === 'right' ? 'right' : 'left'
108
+ const mirrorToken = preserveCase(sourceToken, side === 'right' ? 'left' : 'right')
109
+ return {
110
+ side,
111
+ convention: 'word-suffix',
112
+ sourceToken,
113
+ mirrorToken,
114
+ mirrorName: `${wordSuffix[1]}${mirrorToken}`
115
+ }
116
+ }
117
+
118
+ return null
119
+ }
120
+
121
+ export const getMirrorNodeMatch = (figure: DzSkeleton, node: DzNode): MirrorNodeMatch => {
122
+ const name = node.getName().valueOf()
123
+ const candidate = getMirrorNameCandidate(name)
124
+
125
+ if (!candidate) {
126
+ return {
127
+ source: node,
128
+ mirror: null,
129
+ side: null,
130
+ convention: null,
131
+ sourceToken: null,
132
+ mirrorToken: null,
133
+ mirrorName: null
134
+ }
135
+ }
136
+
137
+ return {
138
+ source: node,
139
+ mirror: figure.findNodeChild(candidate.mirrorName, true),
140
+ ...candidate
141
+ }
142
+ }
143
+
144
+ export const getMirrorNodeMatches = (figure: DzSkeleton, nodes: DzNode[]): MirrorNodeMatch[] => {
145
+ return nodes.map(node => getMirrorNodeMatch(figure, node))
146
+ }
147
+
13
148
  export const getMirrorNodes = (figure: DzSkeleton, nodes: DzNode[]): DzNode[] => {
14
- let mirrorNodes: DzNode[] = []
15
-
16
- nodes.forEach((node) => {
17
- const name = node.getName().valueOf()
18
- let prefix = name[0]
19
- if (prefix !== 'r' && prefix !== 'l') return
20
- prefix = prefix === 'r' ? 'l' : 'r'
21
- const mirrorName = `${prefix}${name.substring(1)}`
22
- const mirrorNode = figure.findNodeChild(mirrorName, true)
23
- if (mirrorNode) mirrorNodes.push(mirrorNode)
24
- })
25
-
26
- return mirrorNodes
27
- }
149
+ return getMirrorNodeMatches(figure, nodes)
150
+ .map(match => match.mirror)
151
+ .filter(node => node !== null) as DzNode[]
152
+ }