示例#1
0
    def execute(self, context):

        active = context.active_object
        if active is not None and active.type == 'MESH':
            # If active object is mesh

            client = context.scene.speckle.client
            client.verbose = True
            account = context.scene.speckle.accounts[
                context.scene.speckle.active_account]
            stream = account.streams[account.active_stream]

            client.server = account.server
            client.s.headers.update({
                'content-type': 'application/json',
                'Authorization': account.authToken,
            })

            scale = context.scene.unit_settings.scale_length / get_scale_length(
                stream.units)

            sp = export_ngons_as_polylines(active, scale)

            if sp is None:
                return {'CANCELLED'}

            placeholders = []
            for polyline in sp:

                #res = client.objects.create(polyline)[0]
                res = client.objects.create([polyline])
                #res = client.ObjectCreateAsync([polyline])['resources'][0]
                print(res)

                if res == None:
                    _report(client.me)
                    continue
                placeholders.extend(res)

                #polyline['_id'] = res['_id']
                #placeholders.append({'type':'Placeholder', '_id':res['_id']})

            if len(placeholders) < 1:
                return {'CANCELLED'}

                # Get list of existing objects in stream and append new object to list
            _report("Fetching stream...")
            sstream = client.streams.get(stream.streamId)

            #res = client.StreamGetAsync(stream.streamId)
            #if res is None: return {'CANCELLED'}

            #stream = res['resource']
            #if '_id' in stream.keys():
            #    del stream['_id']

            if self.clear_stream:
                _report("Clearing stream...")
                sstream.objects = placeholders
                N = 0
            else:
                sstream.objects.extend(placeholders)

            N = sstream.layers[-1].objectCount
            if self.clear_stream:
                N = 0
            sstream.layers[-1].objectCount = N + len(placeholders)
            sstream.layers[-1].topology = "0-%s" % (N + len(placeholders))

            res = client.streams.update(sstream.streamId, sstream)

            #res = client.StreamUpdateAsync(stream['streamId'], {'objects':stream['objects'], 'layers':stream['layers']})

            # Update view layer
            context.view_layer.update()
            _report("Done.")

        return {'FINISHED'}
示例#2
0
    def execute(self, context):

        selected = context.selected_objects

        if len(selected) > 0:
            # If active object is mesh

            client = context.scene.speckle.client
            client.verbose = True
            account = context.scene.speckle.accounts[context.scene.speckle.active_account]
            stream =account.streams[account.active_stream]

            client.server = account.server
            client.s.headers.update({
                'content-type': 'application/json',
                'Authorization': account.authToken,
            })            

            scale = context.scene.unit_settings.scale_length / get_scale_length(stream.units.lower())

            placeholders = []

            '''
            Get script from text editor for injection
            '''
            func = None 
            if context.scene.speckle.upload_script in bpy.data.texts:
                mod = bpy.data.texts[context.scene.speckle.upload_script].as_module()
                if hasattr(mod, "execute"):
                    func = mod.execute

            for obj in selected:

                if obj.type != 'MESH':
                    continue

                new_object = obj
                '''
                Run injected function
                '''
                if func:
                    new_object = func(context.scene, obj)

                    if new_object is None: # Make sure that the injected function returned an object
                        continue

                _report("Converting {}".format(obj.name))

                create_objects = []

                ngons = obj.get("speckle_ngons_as_polylines", False)

                if ngons:
                    create_objects = export_ngons_as_polylines(obj, scale)
                else:
                    create_objects = [to_speckle_object(obj, scale)]

                for co in create_objects:

                    placeholder = client.objects.create(co)

                    if placeholder == None or len(placeholder) < 1: return {'CANCELLED'}

                    placeholders.extend(placeholder)

                    obj.speckle.enabled = True
                    obj.speckle.object_id = placeholder[0].id
                    obj.speckle.stream_id = stream.streamId
                    obj.speckle.send_or_receive = 'send'      

            sstream = client.streams.get(stream.streamId)
            if sstream is None: return {'CANCELLED'}

            stream_patch = Stream()

            stream_patch.objects = placeholders
            stream_patch.layers = sstream.layers
            stream_patch.baseProperties.units = stream.units
            stream_patch.commitMessage = "Modified from Blender (blender.org)."

            stream_patch.layers[-1].objectCount = len(placeholders)
            stream_patch.layers[-1].topology = "0-{}".format(len(placeholders))

            _report("Updating stream %s" % stream['streamId'])
            res = client.streams.update(stream['streamId'], stream_patch)

            _report(res)

            # Update view layer
            context.view_layer.update()

        if context.area:
            context.area.tag_redraw()
        return {'FINISHED'}
示例#3
0
    def execute(self, context):

        selected = context.selected_objects

        if len(selected) > 0:
            # If active object is mesh

            client = context.scene.speckle_client
            client.verbose = True
            account = context.scene.speckle.accounts[
                context.scene.speckle.active_account]
            stream = account.streams[account.active_stream]

            client.server = account.server
            client.s.headers.update({
                'content-type': 'application/json',
                'Authorization': account.authToken,
            })

            scale = context.scene.unit_settings.scale_length / get_scale_length(
                stream.units)

            placeholders = []

            for obj in selected:

                if obj.type != 'MESH':
                    continue

                print("Converting {}".format(obj.name))

                create_objects = []

                ngons = obj.get("speckle_ngons_as_polylines", False)

                if ngons:
                    create_objects = export_ngons_as_polylines(obj, scale)
                else:
                    create_objects = [to_speckle_object(obj, scale)]

                for new_object in create_objects:

                    if '_id' in new_object.keys():
                        del new_object['_id']

                    if 'transform' in new_object.keys():
                        del new_object['transform']

                    if 'properties' in new_object.keys():
                        del new_object['properties']

                    res = client.objects.create(new_object)
                    if res == None: return {'CANCELLED'}

                    placeholders.append({
                        'type': 'Placeholder',
                        '_id': res[0]['_id']
                    })

                    obj.speckle.enabled = True
                    obj.speckle.object_id = res[0]['_id']
                    obj.speckle.stream_id = stream.streamId
                    obj.speckle.send_or_receive = 'send'

            res = client.StreamGetAsync(stream.streamId)
            if res is None: return {'CANCELLED'}

            stream = res['resource']
            if '_id' in stream.keys():
                del stream['_id']

            stream['layers'][-1]['objectCount'] = len(placeholders)
            stream['layers'][-1]['topology'] = "0-{}".format(len(placeholders))

            print("Updating stream %s" % stream['streamId'])

            res = client.StreamUpdateAsync(stream['streamId'], {
                'objects': placeholders,
                'layers': stream['layers']
            })
            print(res)

            # Update view layer
            context.view_layer.update()

        return {'FINISHED'}