Skip to content

Commit

Permalink
Merge branch 'lnoel' into 'master'
Browse files Browse the repository at this point in the history
Fix object transform reset when edit mode selected

Closes #5

See merge request motion-pictures/blender/dccsync!5
  • Loading branch information
Celeborn2BeAlive committed Feb 19, 2020
2 parents edabf66 + 9e90d9a commit 4e002d5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 54 deletions.
13 changes: 13 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ def register():


def unregister():
operators.leave_current_room()

if bpy.app.timers.is_registered(refreshRoomListHack):
bpy.app.timers.unregister(refreshRoomListHack)

shareData = operators.shareData
if shareData:
if shareData.client and bpy.app.timers.is_registered(shareData.client.networkConsumer):
bpy.app.timers.unregister(shareData.client.networkConsumer)

if shareData.roomListUpdateClient and bpy.app.timers.is_registered(shareData.roomListUpdateClient.networkConsumer):
bpy.app.timers.unregister(shareData.roomListUpdateClient.networkConsumer)

operators.unregister()
ui.unregister()
data.unregister()
Expand Down
26 changes: 13 additions & 13 deletions broadcaster/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,19 @@ def decodeVector2Array(data, index):
return decodeArray(data, index, '2f', 2*4)


def readMessage(socket):
class Command:
_id = 100

def __init__(self, commandType: MessageType, data=b'', commandId=0):
self.data = data or b''
self.type = commandType
self.id = commandId
if commandId == 0:
self.id = Command._id
Command._id += 1


def readMessage(socket) -> Command:
if not socket:
return None
r, _, _ = select.select([socket], [], [], 0.0001)
Expand Down Expand Up @@ -262,18 +274,6 @@ def readMessage(socket):
return None


class Command:
_id = 100

def __init__(self, commandType: MessageType, data=b'', commandId=0):
self.data = data or b''
self.type = commandType
self.id = commandId
if commandId == 0:
self.id = Command._id
Command._id += 1


def writeMessage(socket, command: Command):
if not socket:
return
Expand Down
39 changes: 26 additions & 13 deletions clientBlender.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
import os
import platform
import ctypes
import logging
_STILL_ACTIVE = 259

logger = logging.getLogger(__package__)


class ClientBlender(Client):
def __init__(self, host=common.DEFAULT_HOST, port=common.DEFAULT_PORT):
def __init__(self, name, host=common.DEFAULT_HOST, port=common.DEFAULT_PORT):
super(ClientBlender, self).__init__(host, port)

self.name = name

self.objectNames = {} # object name / object
self.textures = set()
self.currentSceneName = ""
Expand Down Expand Up @@ -52,35 +57,40 @@ def getOrCreateCollection(self, name="Collection"):
bpy.context.scene.collection.children.link(collection)
return bpy.data.collections[name]

def getOrCreatePath(self, path, collectionName="Collection"):
def getOrCreatePath(self, path, data, collectionName="Collection"):
collection = self.getOrCreateCollection(collectionName)
pathElem = path.split('/')
parent = None
ob = None
for elem in pathElem:
# Create or get parents
for elem in pathElem[:-1]:
if elem not in bpy.data.objects:
ob = bpy.data.objects.new(elem, None)
collection.objects.link(ob)
else:
ob = bpy.data.objects[elem]
ob.parent = parent
parent = ob
# Create or get object
elem = pathElem[-1]
if elem not in bpy.data.objects:
ob = bpy.data.objects.new(elem, data)
collection.objects.link(ob)
else:
ob = bpy.data.objects[elem]
return ob

def getOrCreateObjectData(self, path, data):
ob = self.getOrCreatePath(path)
ob = self.getOrCreatePath(path, data)
if not ob:
return None
return None # todo should not happen ? assert this ?

parent = ob.parent
name = ob.name

# cannot simply replace objects data
# needs to destroy and re create it
bpy.data.objects.remove(ob, do_unlink=True)
ob = bpy.data.objects.new(name, data)
collection = self.getOrCreateCollection()
collection.objects.link(ob)
if not ob.name in collection.objects:
collection.objects.link(ob)
ob.parent = parent

def getOrCreateCamera(self, cameraName):
Expand Down Expand Up @@ -253,7 +263,7 @@ def buildTransform(self, data):
visible, start = common.decodeBool(data, start)

try:
obj = self.getOrCreatePath(objectPath)
obj = self.getOrCreatePath(objectPath, None)
except KeyError:
# Object doesn't exist anymore
return
Expand Down Expand Up @@ -407,7 +417,7 @@ def buildSendToTrash(self, data):
collections = obj.users_collection
for collection in collections:
collection.objects.unlink(obj)
#collection = self.getOrCreateCollection()
# collection = self.getOrCreateCollection()
# collection.objects.unlink(obj)
trashCollection = self.getOrCreateCollection("__Trash__")
trashCollection.hide_viewport = True
Expand Down Expand Up @@ -488,7 +498,7 @@ def getMaterialBuffer(self, material):
if n.type == 'BSDF_PRINCIPLED':
principled = n
break
#principled = next(n for n in nodes if n.type == 'BSDF_PRINCIPLED')
# principled = next(n for n in nodes if n.type == 'BSDF_PRINCIPLED')
if principled is None:
baseColor = (0.8, 0.8, 0.8)
metallic = 0.0
Expand Down Expand Up @@ -1043,6 +1053,9 @@ def networkConsumer(self):
except queue.Empty:
return 0.01
else:
logger.info("Client %s Command %s received (queue size = %d)",
self.name, command.type, self.receivedCommands.qsize())

self.blockSignals = True
self.receivedCommandsProcessed = True

Expand Down
53 changes: 25 additions & 28 deletions operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, translate, quaternion, scale, visible):

class ShareData:
def __init__(self):
self.sessionId = 0 # For logging and debug
self.client = None
self.currentRoom = None
self.isLocal = False
Expand Down Expand Up @@ -105,10 +106,10 @@ def leave_current_room():
shareData.currentRoom = None
set_handlers(False)

if bpy.app.timers.is_registered(shareData.client.networkConsumer):
bpy.app.timers.unregister(shareData.client.networkConsumer)

if shareData.client is not None:
if bpy.app.timers.is_registered(shareData.client.networkConsumer):
bpy.app.timers.unregister(shareData.client.networkConsumer)

shareData.client.disconnect()
del(shareData.client)
shareData.client = None
Expand Down Expand Up @@ -595,7 +596,8 @@ def getRooms(force=False):
get_dcc_sync_props().remoteServerIsUp = up

if up:
shareData.roomListUpdateClient = clientBlender.ClientBlender(host, port)
shareData.roomListUpdateClient = clientBlender.ClientBlender(
f"roomListUpdateClient {shareData.sessionId}", host, port)

if not up or not shareData.roomListUpdateClient.isConnected():
rooms_cache = ["Local"]
Expand Down Expand Up @@ -669,6 +671,8 @@ def send_collections():


def send_scene_content():
logger.info("Sending scene content to server")

shareData.client.currentSceneName = bpy.context.scene.name

# First step : Send all Blender data (materials, objects, collection) existing in file
Expand Down Expand Up @@ -747,6 +751,21 @@ def server_is_up(address, port):
return False


def create_main_client(host: str, port: int, room: str):
shareData.sessionId += 1
shareData.client = clientBlender.ClientBlender(f"syncClient {shareData.sessionId}", host, port)
shareData.client.addCallback('SendContent', send_scene_content)
shareData.client.addCallback('ClearContent', clear_scene_content)
if not shareData.client.isConnected():
return {'CANCELLED'}
if not bpy.app.timers.is_registered(shareData.client.networkConsumer):
bpy.app.timers.register(shareData.client.networkConsumer)

shareData.client.joinRoom(room)
shareData.currentRoom = room
set_handlers(True)


class CreateRoomOperator(bpy.types.Operator):
"""Create a new room on DCC Sync server"""
bl_idname = "dcc_sync.create_room"
Expand All @@ -771,19 +790,8 @@ def execute(self, context):
host = get_dcc_sync_props().host
port = get_dcc_sync_props().port

shareData.client = clientBlender.ClientBlender(host, port)
shareData.client.addCallback('SendContent', send_scene_content)
shareData.client.addCallback('ClearContent', clear_scene_content)
if not shareData.client.isConnected():
return {'CANCELLED'}

if not bpy.app.timers.is_registered(shareData.client.networkConsumer):
bpy.app.timers.register(shareData.client.networkConsumer)

shareData.client.joinRoom(room)
shareData.currentRoom = room
create_main_client(host, port, room)

set_handlers(True)
UpdateRoomListOperator.rooms_cached = False
return {'FINISHED'}

Expand Down Expand Up @@ -824,18 +832,7 @@ def execute(self, context):
host = get_dcc_sync_props().host
port = get_dcc_sync_props().port

shareData.client = clientBlender.ClientBlender(host, port)
shareData.client.addCallback('SendContent', send_scene_content)
shareData.client.addCallback('ClearContent', clear_scene_content)
if not shareData.client.isConnected():
return {'CANCELLED'}
if not bpy.app.timers.is_registered(shareData.client.networkConsumer):
bpy.app.timers.register(shareData.client.networkConsumer)

shareData.client.joinRoom(room)
set_handlers(True)

shareData.currentRoom = room
create_main_client(host, port, room)

return {'FINISHED'}

Expand Down

0 comments on commit 4e002d5

Please sign in to comment.