Skip to content

GetStartedAdvP3

Amadeusz Zackiewicz edited this page Nov 14, 2022 · 5 revisions

Part 3 - Make it a button

Full code from previous part
from io_ggltf.Advanced import *
from io_ggltf.Constants import *

bucket = File.create_bucket(filePath="//", fileName="cube_export", binPath="bin/", fileType=FILE_TYPE_GLB)

cubeNode = Node.based_on_object(bucket, "Cube")
emptyNode = Node.based_on_object(bucket, "Empty")
mergedMesh = Mesh.merged_based_on_hierarchy(bucket, "Cube", uvMaps="UVMap", name="MyMergedMesh", origin="Empty", autoAttach=False)

Attach.mesh_to_node(bucket, mergedMesh, cubeNode)

File.dump_bucket(bucket)

In this part you will learn how to make a button that will run your script for you. I recommend you read through this page first before continuing.

First you need make the code you produced so far into a function, using the side panel you can paste in a snippet of code that will produce a button.

@MakeButton("My Tab/My Panel/My Button", "VIEW_3D")
def example_func(self, context):
    #Do stuff here
    return {"FINISHED"}

Simply move all your code, except the imports into #Do stuff here line and then indent it once like this:

from io_ggltf.Advanced import *
from io_ggltf.Constants import *

@MakeButton("My Tab/My Panel/My Button", "VIEW_3D")
def example_func(self, context):
    bucket = File.create_bucket(filePath="//", fileName="cube_export", binPath="bin/", fileType=FILE_TYPE_GLB)

    cubeNode = Node.based_on_object(bucket, "Cube")
    emptyNode = Node.based_on_object(bucket, "Empty")
    mergedMesh = Mesh.merged_based_on_hierarchy(bucket, "Cube", uvMaps="UVMap", name="MyMergedMesh", origin="Empty", autoAttach=False)

    Attach.mesh_to_node(bucket, mergedMesh, cubeNode)

    File.dump_bucket(bucket)
    return {"FINISHED"}

Before you run the code I recommend you change the MakeButton parameters that specify where the button should be located. I will be changing it to Exports/My shiny panel/My shiny button and leaving the second paremeter as VIEW_3D, which is exactly where I want it already.

Before you run the code you might want to format your text to fix whitespace issues, this issue can often happen when pasting code with different indent level.

Running the code now, you will notice that no file has been exported. That is because now the code will not run unless you press the new button, to see your new button simply press the N key in 3D viewport.

Pressing the button will now run your code and export the file just like it did before. You can have as many buttons as want as long as their paths do not conflict.