Exemple #1
0
    def execute(self, context):

        active = context.active_object
        if active is not None:
            # 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,
            })

            _report("authToken: ", account.authToken)

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

            sm = to_speckle_object(active, scale)

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

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

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

            placeholders = client.objects.create([sm])
            if placeholders == None: return {'CANCELLED'}

            sstream = client.streams.get(stream.streamId)
            sstream.objects.extend(placeholders)

            N = sstream.layers[-1].objectCount
            sstream.layers[-1].objectCount = N + 1
            sstream.layers[-1].topology = "0-%s" % (N + 1)

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

            res = client.streams.update(stream['streamId'], sstream)

            _report(res)

            active.speckle.enabled = True
            active.speckle.object_id = sm['_id']
            active.speckle.stream_id = stream['streamId']
            active.speckle.send_or_receive = 'send'

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

        return {'FINISHED'}
Exemple #2
0
    def execute(self, context):
        client = context.scene.speckle.client
        account = context.scene.speckle.accounts[
            context.scene.speckle.active_account]
        stream = account.streams[account.active_stream]

        client.server = account.server
        client.s.headers.update({'Authorization': account.authToken})

        active = context.active_object
        _report(active)

        if active is not None:
            if active.speckle.enabled:
                if active.speckle.send_or_receive == "send" and active.speckle.stream_id:
                    sstream = client.streams.get(active.speckle.stream_id)
                    #res = client.StreamGetAsync(active.speckle.stream_id)['resource']
                    #res = client.streams.get(active.speckle.stream_id)

                    if sstream is None:
                        _report("Getting stream failed.")
                        return {'CANCELLED'}

                    stream_units = "Meters"
                    if sstream.baseProperties:
                        stream_units = sstream.baseProperties.units

                    scale = context.scene.unit_settings.scale_length / get_scale_length(
                        stream_units)

                    sm = to_speckle_object(active, scale)

                    _report("Updating object {}".format(sm['_id']))
                    client.objects.update(active.speckle.object_id, sm)

                    return {'FINISHED'}

                    # res = client.ObjectCreateAsync([sm])
                    # new_id = res['resources'][0]['_id']

                    # for o in stream_data['objects']:
                    #     if o['_id'] == active.speckle.object_id:
                    #         o['_id'] = new_id
                    #         break

                    # res = client.StreamUpdateAsync(active.speckle.stream_id, {'objects': stream_data['objects']})
                    # res = client.ObjectDeleteAsync(active.speckle.object_id)
                    # active.speckle.object_id = new_id

                    # if res == None: return {'CANCELLED'}
            return {'FINISHED'}
        return {'CANCELLED'}
    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'}
Exemple #4
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'}
    def execute(self, context):

        active = context.active_object
        if active is not None:
            # 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,
            })

            print("authToken: ", account.authToken)

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

            sm = to_speckle_object(active, scale)

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

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

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

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

            sm['_id'] = res[0]['_id']
            pl = {'type': 'Placeholder', '_id': res[0]['_id']}

            # Get list of existing objects in stream and append new object to list
            print("Fetching stream...")
            res = client.StreamGetAsync(stream.streamId)
            #res = client.streams.get(stream.streamId)
            if res is None: return {'CANCELLED'}

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

            stream['objects'].append(pl)

            N = stream['layers'][-1]['objectCount']
            stream['layers'][-1]['objectCount'] = N + 1
            stream['layers'][-1]['topology'] = "0-%s" % (N + 1)

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

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

            active.speckle.enabled = True
            active.speckle.object_id = sm['_id']
            active.speckle.stream_id = stream['streamId']
            active.speckle.send_or_receive = 'send'

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

        return {'FINISHED'}