# Script to convert embedded blocks to linked?

**URL:** <https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613>\
**Category:** Rhino Developer\
**Tags:** scripting\
**Created:** [February 11, 2019, 12:52am UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613 "2019-02-11T00:52:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShynnSup](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/shynnsup/32/767220_2.png) [@ShynnSup](https://discourse.mcneel.com/u/ShynnSup)\
**Post date:** [February 11, 2019, 12:52am UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613/1 "2019-02-11T00:52:46Z")

</div>

Hi guys!

I am starting a new thread for this matter but I actually found one where this was pretty much adressed except that I need to convert many embedded blocks into linked ones.

The script supplied in that thread only works for individuals

I imagine that if you have a file with all the blocks you wish to link inserted, then its just a matter of matching names:

You could select all your 200 embedded blocks on your current file, and when asked which file to obtain the linked blocks from, this file should have the same blocks from your original file, with the exact same name.

> [@Whish: quick switch from linked block to embedded block](https://discourse.mcneel.com/t/whish-quick-switch-from-linked-block-to-embedded-block/36015/26):
>
> Hey everyone, sorry for bumping this one up, but I am in the need of actually converting about 200 embedded blocks into linked ones. I imagine that if you have a file with all the blocks you wish to link inserted, then its just a matter of matching names: You could select all your 200 embedded blocks on your current file, and when asked which file to obtain the linked blocks from, this file should have the same blocks from your original file, with the exact same name.

---

<div class="post-metadata">

**Author:** ![rgr](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rgr/32/250899_2.png) [@rgr](https://discourse.mcneel.com/u/rgr)\
**Post date:** [February 11, 2019, 12:15pm UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613/2 "2019-02-11T12:15:41Z")

</div>

> import rhinoscriptsyntax as rs
> 
> def LinkBlock():
> 
> ```
> Ids = rs.GetObjects("Select blocks to link.", filter=4096, preselect =True)
> if Ids is None: return
> 
> path = "yourfilepathhere"
> 
> for Id in Ids:
> name = rs.BlockInstanceName(Id)
> filepath = path + "\\" + name + ".3dm"
> rs.Command("_-BlockManager _Properties " + chr(34) + name + chr(34) + " _UpdateType _Linked filepath _Enter _Enter", False)
> 
> ```
> 
> if **name** == “ **main** ”:  
> LinkBlock()

I can’t test it right now, but something along this should work. you obviously need to replace path with the folder for your parts and have the files named the same as your blocks.Also, double “\” so it won’t escape the sequence.

if you want to browse for each file individually, use:

> rs.Command(“\_-BlockManager \_Properties " + chr(34) + name + chr(34) + " \_UpdateType \_Linked \_Browse \_Enter \_Enter”, False)

---

<div class="post-metadata">

**Author:** ![jesterking](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jesterking/32/771114_2.png) [@jesterking](https://discourse.mcneel.com/u/jesterking)\
**Post date:** [February 11, 2019, 12:28pm UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613/3 "2019-02-11T12:28:03Z")

</div>

> [@rgr](#):
>
> Also, double “\” so it won’t escape the sequence.

You could also construct a raw string literals so you don’t have to double-escape:

> <https://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-prefixes-do-and-what-are-raw-string-literals>

---

<div class="post-metadata">

**Author:** ![ShynnSup](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/shynnsup/32/767220_2.png) [@ShynnSup](https://discourse.mcneel.com/u/ShynnSup)\
**Post date:** [February 11, 2019, 1:25pm UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613/4 "2019-02-11T13:25:02Z")

</div>

Hi, thank you!

> [@rgr](#):
>
> and have the files named the same as your blocks

I am afraid I don’t understand one thing though. Do I need multiple files, (One file for each block) ?  
Can’t all blocks be inside one single document and use that document to replace the blocks on the other one?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rgr](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rgr/32/250899_2.png) [@rgr](https://discourse.mcneel.com/u/rgr)\
**Post date:** [February 11, 2019, 3:37pm UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613/5 "2019-02-11T15:37:26Z")

</div>

You have to specify the file for each block, but you could just replace the “filepath” in the RS.Command line like so:

> rs.Command(“\_-BlockManager \_Properties " + chr(34) + name + chr(34) + " \_UpdateType \_Linked path \_Enter \_Enter”, False)

and change the path to point at the .3dm file, not just the directory.

---

<div class="post-metadata">

**Author:** ![ShynnSup](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/shynnsup/32/767220_2.png) [@ShynnSup](https://discourse.mcneel.com/u/ShynnSup)\
**Post date:** [February 11, 2019, 5:07pm UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613/6 "2019-02-11T17:07:31Z")

</div>

I can’t make it work 😓

```auto
    import rhinoscriptsyntax as rs

    def LinkBlock():

    Ids = rs.GetObjects("Select blocks to link.", filter=4096, preselect =True)
    if Ids is None: return

    path = "C:\Users\Shynn\Desktop\BlocksToLink.3dm"

    for Id in Ids:
        name = rs.BlockInstanceName(Id)
        filepath = path + "\\" + name + ".3dm"
        rs.Command("_-BlockManager _Properties " + chr(34) + name + chr(34) + " _UpdateType _Linked path _Enter _Enter", False)

    if name == “main”:
    LinkBlock()

```

I get this error:

```auto
File "C:\Users\Shynn\Desktop\ReplaceBlocksnew.py", line                
SyntaxError: Non-ASCII character '\xe2' in file C:\Users\Shynn\Desktop\ReplaceBlocksnew.py on line 17, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

```

[ReplaceBlocksnew.py](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/0/909270d25f145607b6244a90c7a6414795be64e1.py) (518 Bytes)

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [February 12, 2019, 9:33pm UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613/7 "2019-02-12T21:33:19Z")

</div>

Hi, could be a file encoding problem? I get garbled characters in place of the underscores in ` __name__ `

```auto
    if name == â€œmainâ€:
    LinkBlock()
```

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [February 12, 2019, 9:35pm UTC](https://discourse.mcneel.com/t/script-to-convert-embedded-blocks-to-linked/78613/8 "2019-02-12T21:35:22Z")

</div>

Should be:

```auto
if __name__ == ' __main__':
    LinkBlock()

```
