# Exporting a prim to a new usd file

**URL:** <https://forum.aousd.org/t/exporting-a-prim-to-a-new-usd-file/1737>\
**Category:** USD\
**Created:** [August 2, 2024, 6:09am UTC](https://forum.aousd.org/t/exporting-a-prim-to-a-new-usd-file/1737 "2024-08-02T06:09:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![droizman](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@droizman](https://forum.aousd.org/u/droizman)\
**Post date:** [August 2, 2024, 6:09am UTC](https://forum.aousd.org/t/exporting-a-prim-to-a-new-usd-file/1737/1 "2024-08-02T06:09:55Z")

</div>

In python, given a prim in a stage, how can I export that to a new usd file?

---

<div class="post-metadata">

**Author:** ![flord](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/flord/32/102_2.png) [@flord](https://forum.aousd.org/u/flord)\
**Post date:** [August 4, 2024, 3:37pm UTC](https://forum.aousd.org/t/exporting-a-prim-to-a-new-usd-file/1737/2 "2024-08-04T15:37:52Z")

</div>

That is the kind of question you could ask the new USD-code NIM from NVIDIA.

> **[NVIDIA NIM | usdcode-llama3-70b-instruct](https://build.nvidia.com/nvidia/usdcode-llama3-70b-instruct?snippet_tab=Python)**
>
> Experience the leading models to build enterprise generative AI apps now.

I just tried it and it seems to spew out something decent.  
F

---

<div class="post-metadata">

**Author:** ![pmolodo](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@pmolodo](https://forum.aousd.org/u/pmolodo)\
**Post date:** [August 4, 2024, 8:36pm UTC](https://forum.aousd.org/t/exporting-a-prim-to-a-new-usd-file/1737/3 "2024-08-04T20:36:49Z")

</div>

Offhand, I think you can try setting a population mask to only allow your prim, or set all other prims to inactive, and then export the stage.

However, with either approach, I’m not sure if you’re guaranteed that changing the population mask or setting other prims inactive won’t have knock-on effects on the prim you care about. Also, it would require altering the stage first, which may be a no-go if it’s in use by other things.

Alternatively, if the prim is only from a single layer with no opinions you care about composed on top, you could use create a new layer and use SdfCopySpec.

The surest approach might a combo - ie, export the stage first, which will also flatten it, then open your flattened export as an SdfLayer and use SdfCopySpec. That could be a potentially heavy solution for a very large scene that you only want a tiny piece of, though.

I’m curious to know what the NIM came up with… Or if there a better way to go about it, since all of my suggestions have downsides.

---

<div class="post-metadata">

**Author:** ![droizman](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@droizman](https://forum.aousd.org/u/droizman)\
**Post date:** [August 6, 2024, 6:48am UTC](https://forum.aousd.org/t/exporting-a-prim-to-a-new-usd-file/1737/4 "2024-08-06T06:48:04Z")

</div>

LLM results are full of red-herrings - functions and methods that don’t actually exist. There’s one that is close that I’ll share below but it’s a shallow export, doesn’t encompass materials or other relationships.

```auto
    export_stage = Usd.Stage.CreateInMemory()

    # Recursively export the prim and its children
    def export_prim_and_children(prim, export_prim):
        #export_materials(prim)
        for attr in prim.GetAttributes():
            if attr.IsAuthored():
                export_attr = export_prim.CreateAttribute(attr.GetName(), attr.GetTypeName())
                export_attr.Set(attr.Get())
        for child in prim.GetAllChildren():
            child_path = child.GetPath()
            child_export_prim = export_stage.DefinePrim(child_path, child.GetTypeName())
            export_prim_and_children(child, child_export_prim)

    export_prim_and_children(prim, export_prim)

    # Export the new stage to a file
    export_stage.GetRootLayer().Export(file_path)

```

---

<div class="post-metadata">

**Author:** ![spiff](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/spiff/32/22_2.png) [@spiff](https://forum.aousd.org/u/spiff)\
**Post date:** [August 18, 2024, 11:47pm UTC](https://forum.aousd.org/t/exporting-a-prim-to-a-new-usd-file/1737/5 "2024-08-18T23:47:55Z")

</div>

Opening the Stage with a population mask set to the prim and then exporting (which will flatten) is the way I’d suggest, and does not require modifying the existing stage/layers. If you care about other prims “used by” your prim and its descendants (i.e. those to which relationships or connections target), you can, after opening the masked stage, forst call `ExpandPopulationMask()` before Exporting the stage - and if you’re interested in bound Materials, as it sounds like you are, this is the thing that will pull them in for you.

Cheers,  
–spiff
