create new primitive mesh for MeshInstance from code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By beetbeet

Hello,

How can i assign new primitive mesh for created MeshInstance from code?
I can do this in editor by choosing New CubeMesh, New PlaneMesh etc but with code it doesen’t works.

    var myMesh = MeshInstance.new()
    myMesh.set_mesh(CubeMesh.new())
    add_child(myMesh)
:bust_in_silhouette: Reply From: Nophlock

Could you try just this?:

var myMesh = MeshInstance.new()
myMesh.mesh = CubeMesh.new()
add_child(myMesh)

For me this is working fine (although I’m doing it with a get_node reference and not directly in the object). If it’s not working, where do you call it? In the _ready function?

it don’t works, there is no mesh data in inspector after playing scene and check this object in remote

i call that code in _ready function

beetbeet | 2019-03-26 16:24

I gave it a new try, but this time in a new project to make sure, that my existing code doesn’t interact with something and it still works as expected. The only thing that I stumbled upon, was that at first I didn’t put the camera far enough so it clipped inside the box and with blackface-culling enabled it doesn’t show the faces from the inside of the cube. Maybe the same thing applies to you? So could you quickly check that? If this isn’t the problem I will upload the small sample project that I’ve created.

Nophlock | 2019-03-26 16:46

there is no data of mesh in remote scene structure at created object, i think there is no sense thinking about visibility without that

beetbeet | 2019-03-26 16:53

ok, i need to add material from code to get it working :slight_smile:

beetbeet | 2019-03-26 17:08

The remote inspector says “[empty]” no matter if it’s really has mesh-data or not( If you create it from code):
enter image description here

This is when I click on the Cube that I’ve created from code and inspect it from the remote tab. Of course this isn’t really smart from the inspector maybe even a bug idk but I wouldn’t rely 100% on what the remote inspector is saying.

Nophlock | 2019-03-26 17:09