annotate blender/light_control/send_to_collector.py @ 2455:2d454737a916

split blender code to new file
author drewp@bigasterisk.com
date Tue, 20 May 2025 09:24:35 -0700
parents b23afde50bc2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
1 import asyncio
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
2 import logging
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
3 from dataclasses import dataclass, field
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
4 from os import sync
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
5 from typing import cast
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
6
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
7 import bpy # type: ignore
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
8 from light9.effect.effecteval import literalColor
2455
2d454737a916 split blender code to new file
drewp@bigasterisk.com
parents: 2453
diff changeset
9 from light9_sync.time_sync.blender_time import clamp
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
10 from rdfdb.patch import Patch
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
11 from rdfdb.syncedgraph.syncedgraph import SyncedGraph
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
12 from rdflib import RDF, ConjunctiveGraph, Literal, URIRef
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
13 from rdflib.graph import _ContextType
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
14
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
15 from light9 import networking
2453
b23afde50bc2 blender addons get thier own pdm setup for now. fix time_from_graph startup race
drewp@bigasterisk.com
parents: 2436
diff changeset
16 from light9_sync.asyncio_thread import startLoopInThread
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
17 from light9.namespaces import FUNC, L9
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
18 from light9.newtypes import DeviceUri
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
19 from light9.showconfig import showUri
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
20
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
21 log = logging.getLogger('sendcoll')
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
22
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
23 UPDATE_PERIOD = 1 / 20
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
24
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
25
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
26 async def waitForConnection(graph: SyncedGraph):
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
27 # workaround for patch() quietly failing before connection. See SyncedGraph.
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
28 while not graph._isConnected():
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
29 await asyncio.sleep(.5)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
30
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
31 @dataclass
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
32 class BlenderDev:
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
33 ctx: URIRef
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
34 obj: bpy.types.Object
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
35 uri: DeviceUri
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
36 setting: URIRef
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
37 devSets: URIRef
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
38
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
39 color: tuple[float, float, float] = (0, 0, 0)
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
40
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
41 def updateColor(self):
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
42 self.color = self.obj.data.color
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
43
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
44 def effectGraphStmts(self, bset0) -> list:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
45 return [
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
46 (self.devSets, L9['setting'], self.setting, self.ctx),
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
47 (self.setting, L9['device'], self.uri, self.ctx),
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
48 (self.setting, L9['deviceAttr'], L9['color'], self.ctx),
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
49 ]
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
50
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
51 def makePatch(self, graph: SyncedGraph) -> Patch:
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
52 c = literalColor(*[clamp(x, 0, 1) for x in self.color])
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
53 return graph.getObjectPatch(self.ctx, self.setting, L9['value'], c)
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
54
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
55
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
56 @dataclass
2432
b8a408caf115 start blender sync
drewp@bigasterisk.com
parents:
diff changeset
57 class Sender:
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
58 syncedObjects: dict[str, BlenderDev] = field(default_factory=dict)
2432
b8a408caf115 start blender sync
drewp@bigasterisk.com
parents:
diff changeset
59
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
60 ctx = URIRef(showUri() + '/blender')
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
61 devSets = L9['blenderControlDevSets']
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
62
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
63 def __post_init__(self):
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
64 bpy.app.timers.register(self.findLightsInScene, first_interval=0.1) # todo: what event to use?
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
65 startLoopInThread(self.task())
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
66
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
67 async def task(self):
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
68 log.info('start task')
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
69 try:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
70 self.graph = SyncedGraph(networking.rdfdb.url, "blender_light_control")
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
71 await waitForConnection(self.graph)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
72 self.patchInitialGraph()
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
73
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
74 while True:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
75 try:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
76 await self.patchCurrentLightColors()
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
77 except Exception:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
78 log.error('skip', exc_info=True)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
79 await asyncio.sleep(1)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
80 # todo: this could be triggered by onColorChanges instead of running constantly
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
81 await asyncio.sleep(UPDATE_PERIOD)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
82 except Exception:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
83 log.error('task', exc_info=True)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
84
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
85 def patchInitialGraph(self):
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
86 g = self.effectGraph()
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
87 for d in self.syncedObjects.values():
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
88 for stmt in d.effectGraphStmts(self.devSets):
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
89 g.add(stmt)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
90 for stmt in g:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
91 log.info("adding %s", stmt)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
92 self.graph.patchSubgraph(self.ctx, g)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
93
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
94 async def patchCurrentLightColors(self):
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
95 p = Patch(addQuads=[], delQuads=[])
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
96 for d in self.syncedObjects.values():
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
97 p = p.update(d.makePatch(self.graph))
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
98 if p:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
99 await self.graph.patch(p)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
100
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
101 def effectGraph(self) -> ConjunctiveGraph:
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
102 g = ConjunctiveGraph()
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
103 ctx = cast(_ContextType, self.ctx)
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
104 g.add((L9['blenderControl'], RDF.type, L9['Effect'], ctx))
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
105 g.add((L9['blenderControl'], L9['effectFunction'], FUNC['scale'], ctx))
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
106 g.add((L9['blenderControl'], L9['publishAttr'], L9['strength'], ctx))
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
107 setting = L9['blenderControl/set0']
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
108 g.add((L9['blenderControl'], L9['setting'], setting, ctx))
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
109 g.add((setting, L9['value'], self.devSets, ctx))
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
110 g.add((setting, L9['effectAttr'], L9['deviceSettings'], ctx))
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
111 return g
2432
b8a408caf115 start blender sync
drewp@bigasterisk.com
parents:
diff changeset
112
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
113 def findLightsInScene(self):
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
114 self.syncedObjects.clear()
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
115 for obj in sorted(bpy.data.objects, key=lambda x: x.name):
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
116 if obj.get('l9dev'):
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
117 uri = DeviceUri(URIRef(obj['l9dev']))
2436
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
118 self.syncedObjects[obj.name] = BlenderDev(
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
119 self.ctx,
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
120 obj,
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
121 uri,
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
122 setting=L9['blenderControl/set1'],
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
123 devSets=self.devSets,
e683b449506b blender effect that sets lights to match blender lights
drewp@bigasterisk.com
parents: 2434
diff changeset
124 )
2434
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
125 log.info(f'found {self.syncedObjects[obj.name]}')
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
126
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
127 self.watchForUpdates()
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
128
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
129 def watchForUpdates(self):
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
130 bpy.app.handlers.depsgraph_update_post.append(self.onColorChanges)
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
131 bpy.app.handlers.frame_change_post.append(self.onColorChanges)
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
132
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
133 def onColorChanges(self, scene, deps):
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
134 for obj in deps.objects:
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
135 dev = self.syncedObjects.get(obj.name)
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
136 if dev is None:
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
137 continue
9b101d8bd7ea discover annotated lights in blender; send their color to the graph (temporary stmt)
drewp@bigasterisk.com
parents: 2432
diff changeset
138 dev.updateColor()