Skip to content

Commit

Permalink
Merge pull request #79636 from aaronfranke/gltf-file-name-path
Browse files Browse the repository at this point in the history
Set `base_path` and `filename` during GLTF export when writing to a file
  • Loading branch information
akien-mga committed Aug 4, 2023
2 parents 516cb63 + 2970839 commit e605a1d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
4 changes: 4 additions & 0 deletions modules/gltf/doc_classes/GLTFState.xml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
</methods>
<members>
<member name="base_path" type="String" setter="set_base_path" getter="get_base_path" default="&quot;&quot;">
The folder path associated with this GLTF data. This is used to find other files the GLTF file references, like images or binary buffers. This will be set during import when appending from a file, and will be set during export when writing to a file.
</member>
<member name="buffers" type="PackedByteArray[]" setter="set_buffers" getter="get_buffers" default="[]">
</member>
Expand All @@ -275,6 +276,9 @@
</member>
<member name="create_animations" type="bool" setter="set_create_animations" getter="get_create_animations" default="true">
</member>
<member name="filename" type="String" setter="set_filename" getter="get_filename" default="&quot;&quot;">
The file name associated with this GLTF data. If it ends with [code].gltf[/code], this is text-based GLTF, otherwise this is binary GLB. This will be set during import when appending from a file, and will be set during export when writing to a file. If writing to a buffer, this will be an empty string.
</member>
<member name="glb_data" type="PackedByteArray" setter="set_glb_data" getter="get_glb_data" default="PackedByteArray()">
</member>
<member name="json" type="Dictionary" setter="set_json" getter="get_json" default="{}">
Expand Down
49 changes: 26 additions & 23 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static Ref<ImporterMesh> _mesh_to_importer_mesh(Ref<Mesh> p_mesh) {
return importer_mesh;
}

Error GLTFDocument::_serialize(Ref<GLTFState> p_state, const String &p_path) {
Error GLTFDocument::_serialize(Ref<GLTFState> p_state) {
if (!p_state->buffers.size()) {
p_state->buffers.push_back(Vector<uint8_t>());
}
Expand Down Expand Up @@ -167,7 +167,7 @@ Error GLTFDocument::_serialize(Ref<GLTFState> p_state, const String &p_path) {
}

/* STEP SERIALIZE IMAGES */
err = _serialize_images(p_state, p_path);
err = _serialize_images(p_state);
if (err != OK) {
return Error::FAILED;
}
Expand Down Expand Up @@ -3001,7 +3001,7 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
return OK;
}

Error GLTFDocument::_serialize_images(Ref<GLTFState> p_state, const String &p_path) {
Error GLTFDocument::_serialize_images(Ref<GLTFState> p_state) {
Array images;
for (int i = 0; i < p_state->images.size(); i++) {
Dictionary image_dict;
Expand All @@ -3011,7 +3011,22 @@ Error GLTFDocument::_serialize_images(Ref<GLTFState> p_state, const String &p_pa
Ref<Image> image = p_state->images[i]->get_image();
ERR_CONTINUE(image.is_null());

if (p_path.to_lower().ends_with("glb") || p_path.is_empty()) {
if (p_state->filename.to_lower().ends_with("gltf")) {
String img_name = p_state->images[i]->get_name();
if (img_name.is_empty()) {
img_name = itos(i);
}
img_name = _gen_unique_name(p_state, img_name);
img_name = img_name.pad_zeros(3) + ".png";
String relative_texture_dir = "textures";
String full_texture_dir = p_state->base_path.path_join(relative_texture_dir);
Ref<DirAccess> da = DirAccess::open(p_state->base_path);
if (!da->dir_exists(full_texture_dir)) {
da->make_dir(full_texture_dir);
}
image->save_png(full_texture_dir.path_join(img_name));
image_dict["uri"] = relative_texture_dir.path_join(img_name).uri_encode();
} else {
GLTFBufferViewIndex bvi;

Ref<GLTFBufferView> bv;
Expand Down Expand Up @@ -3039,23 +3054,6 @@ Error GLTFDocument::_serialize_images(Ref<GLTFState> p_state, const String &p_pa
bvi = p_state->buffer_views.size() - 1;
image_dict["bufferView"] = bvi;
image_dict["mimeType"] = "image/png";
} else {
ERR_FAIL_COND_V(p_path.is_empty(), ERR_INVALID_PARAMETER);
String img_name = p_state->images[i]->get_name();
if (img_name.is_empty()) {
img_name = itos(i);
}
img_name = _gen_unique_name(p_state, img_name);
img_name = img_name.pad_zeros(3) + ".png";
String relative_texture_dir = "textures";
String parent_path = p_path.get_base_dir();
String full_texture_dir = parent_path + "/" + relative_texture_dir;
Ref<DirAccess> da = DirAccess::open(parent_path);
if (!da->dir_exists(full_texture_dir)) {
da->make_dir(full_texture_dir);
}
image->save_png(full_texture_dir.path_join(img_name));
image_dict["uri"] = relative_texture_dir.path_join(img_name).uri_encode();
}
images.push_back(image_dict);
}
Expand Down Expand Up @@ -7241,15 +7239,20 @@ PackedByteArray GLTFDocument::_serialize_glb_buffer(Ref<GLTFState> p_state, Erro

PackedByteArray GLTFDocument::generate_buffer(Ref<GLTFState> p_state) {
ERR_FAIL_NULL_V(p_state, PackedByteArray());
Error err = _serialize(p_state, "");
// For buffers, set the state filename to an empty string, but
// don't touch the base path, in case the user set it manually.
p_state->filename = "";
Error err = _serialize(p_state);
ERR_FAIL_COND_V(err != OK, PackedByteArray());
PackedByteArray bytes = _serialize_glb_buffer(p_state, &err);
return bytes;
}

Error GLTFDocument::write_to_filesystem(Ref<GLTFState> p_state, const String &p_path) {
ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER);
Error err = _serialize(p_state, p_path);
p_state->base_path = p_path.get_base_dir();
p_state->filename = p_path.get_file();
Error err = _serialize(p_state);
if (err != OK) {
return err;
}
Expand Down
4 changes: 2 additions & 2 deletions modules/gltf/gltf_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class GLTFDocument : public Resource {
Error _parse_meshes(Ref<GLTFState> p_state);
Error _serialize_textures(Ref<GLTFState> p_state);
Error _serialize_texture_samplers(Ref<GLTFState> p_state);
Error _serialize_images(Ref<GLTFState> p_state, const String &p_path);
Error _serialize_images(Ref<GLTFState> p_state);
Error _serialize_lights(Ref<GLTFState> p_state);
Ref<Image> _parse_image_bytes_into_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_mime_type, int p_index, String &r_file_extension);
void _parse_image_save_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_file_extension, int p_index, Ref<Image> p_image);
Expand Down Expand Up @@ -366,7 +366,7 @@ class GLTFDocument : public Resource {
GLTFMeshIndex _convert_mesh_to_gltf(Ref<GLTFState> p_state,
MeshInstance3D *p_mesh_instance);
void _convert_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player, String p_animation_track_name);
Error _serialize(Ref<GLTFState> p_state, const String &p_path);
Error _serialize(Ref<GLTFState> p_state);
Error _parse(Ref<GLTFState> p_state, String p_path, Ref<FileAccess> p_file);
};

Expand Down
15 changes: 13 additions & 2 deletions modules/gltf/gltf_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ void GLTFState::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_scene_name", "scene_name"), &GLTFState::set_scene_name);
ClassDB::bind_method(D_METHOD("get_base_path"), &GLTFState::get_base_path);
ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &GLTFState::set_base_path);
ClassDB::bind_method(D_METHOD("get_filename"), &GLTFState::get_filename);
ClassDB::bind_method(D_METHOD("set_filename", "filename"), &GLTFState::set_filename);
ClassDB::bind_method(D_METHOD("get_root_nodes"), &GLTFState::get_root_nodes);
ClassDB::bind_method(D_METHOD("set_root_nodes", "root_nodes"), &GLTFState::set_root_nodes);
ClassDB::bind_method(D_METHOD("get_textures"), &GLTFState::get_textures);
Expand Down Expand Up @@ -109,6 +111,7 @@ void GLTFState::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_materials", "get_materials"); // Vector<Ref<Material>
ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_name"), "set_scene_name", "get_scene_name"); // String
ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_path"), "set_base_path", "get_base_path"); // String
ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename"), "set_filename", "get_filename"); // String
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "root_nodes"), "set_root_nodes", "get_root_nodes"); // Vector<int>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_textures", "get_textures"); // Vector<Ref<GLTFTexture>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "texture_samplers", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_texture_samplers", "get_texture_samplers"); //Vector<Ref<GLTFTextureSampler>>
Expand Down Expand Up @@ -164,11 +167,11 @@ void GLTFState::set_minor_version(int p_minor_version) {
minor_version = p_minor_version;
}

String GLTFState::get_copyright() {
String GLTFState::get_copyright() const {
return copyright;
}

void GLTFState::set_copyright(String p_copyright) {
void GLTFState::set_copyright(const String &p_copyright) {
copyright = p_copyright;
}

Expand Down Expand Up @@ -381,6 +384,14 @@ void GLTFState::set_base_path(String p_base_path) {
base_path = p_base_path;
}

String GLTFState::get_filename() const {
return filename;
}

void GLTFState::set_filename(const String &p_filename) {
filename = p_filename;
}

Variant GLTFState::get_additional_data(const StringName &p_extension_name) {
return additional_data[p_extension_name];
}
Expand Down
9 changes: 6 additions & 3 deletions modules/gltf/gltf_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class GLTFState : public Resource {
GDCLASS(GLTFState, Resource);
friend class GLTFDocument;

String filename;
String base_path;
String filename;
Dictionary json;
int major_version = 0;
int minor_version = 0;
Expand Down Expand Up @@ -126,8 +126,8 @@ class GLTFState : public Resource {
int get_minor_version();
void set_minor_version(int p_minor_version);

String get_copyright();
void set_copyright(String p_copyright);
String get_copyright() const;
void set_copyright(const String &p_copyright);

Vector<uint8_t> get_glb_data();
void set_glb_data(Vector<uint8_t> p_glb_data);
Expand Down Expand Up @@ -171,6 +171,9 @@ class GLTFState : public Resource {
String get_base_path();
void set_base_path(String p_base_path);

String get_filename() const;
void set_filename(const String &p_filename);

PackedInt32Array get_root_nodes();
void set_root_nodes(PackedInt32Array p_root_nodes);

Expand Down

0 comments on commit e605a1d

Please sign in to comment.