Skip to content

Cloning of loaded OBJ model fails #313

@MrMoonshine

Description

@MrMoonshine

Hello.
I've had an issue cloning an already loaded obj file. When accessing any values of the cloned node, the program crashes.

       // Open OBJ file
    decoder, err := obj.Decode(OBJ_FILE, "") // Empty string to auto-detect matlib
    if err != nil {
        return
    }

    for _, material := range decoder.Materials {
        // Force Kd to be white. Otherwise this model will be black despite texture being loaded successfully
        material.Diffuse.R = 1.0
        material.Diffuse.G = 1.0
        material.Diffuse.B = 1.0
    }

    // Creates a new node with all the objects in the decoded file and adds it to the scene
    group, err := decoder.NewGroup()
    if err != nil {
        return
    }

    scene.Add(group)

    clone1 := group.Clone().(*core.Node)
    clone1.SetPosition(1, 2, 3)    // crashes here
    scene.Add(clone1)

Trying to analyze the returned value of the cloned function also ran into the same issue.

/// above is same as before...
    scene.Add(group)

    fmt.Printf("Type of the original is %T\n", group)
    clone1 := group.Clone()
    fmt.Printf("Type of the clone is %T\n", clone1) // crashes here

    clone1Casted := clone1.(*core.Node)
    clone1Casted.SetPosition(1, 2, 3)
    scene.Add(clone1Casted)

Results in:

Type of the original is *core.Node
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x4d2f66]

A workaround was to create a custom clone function:

func cloneCustom(original *core.Node) *core.Node {
    clone := core.NewNode()
    clone.Dispatcher.Initialize()
    clone.SetName(original.Name() + " (Clone)")

    for _, child := range original.Children() {
        clone.Add(child.Clone())
    }

    return clone
}

G3N Discord discussion:
https://discordapp.com/channels/877904175546122261/877913551853981697/1372632429025689741

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions