Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add allowHomonymObjectName option for OBJModel.ModelSettings #9047

Open
wants to merge 1 commit into
base: 1.19.x
Choose a base branch
from

Conversation

zomb-676
Copy link

This PR adds the ability for the OBJ Loder to load .obj file with the same part name
this PR follows #8624, but for Minecraft version 1.19.2 and a small difference.
I don't know how to change an existing GitHub PR's source/target branch, so I just open a new one

Why need this?

for modeling software such as BlockBench which allows homonym cube names. When its model is exported as a .obj model file. The name subsequent to the .obj format command/keyword o will duplicate

Due to the current OBJ Loader implementation, the collection of the parts is a Map<String, ModelGroup> parts, so the latter will override the former. As a result, only the last part will remain

Implementation

add an option bool for OBJModel.Setting called allowHomonymObjectName which is false by default.
when reading from json file, its key is allow_homonym_object_name

we now will check whether the same part name occurs before, if it is and the option is true, we re-name it by plusing a char _ and a non-negative growth number after the original name until no-repeat occurs.
In a similar way to another modeling software called Blender, it can correctly load .obj file with homonym object names

re-name only works on the o .obj command/keyword now

Addition

When finding any part name that occurs before, we use a bool to record this. When load end we print an error message that indicates this and prompts this option when the option is false as this is usually not expected behavior.

rename only works on the `o` .obj command/keyword now
when allowHomonymObjectName is true and rename happens
just plus char '_' and a unique number id,
so you can just get all homonym parts by call filter and use methods like startWith
with the homonym part as parameter
@sciwhiz12 sciwhiz12 added Feature This request implements a new feature. Rendering This request deals with rendering. These must be treated with care, and tend to progress slowly. Triage This request requires the active attention of the Triage Team. Requires labelling or reviews. 1.19 labels Sep 17, 2022
Comment on lines 709 to 712
public record ModelSettings(@NotNull ResourceLocation modelLocation,
boolean automaticCulling, boolean shadeQuads, boolean flipV,
boolean emissiveAmbient, @Nullable String mtlOverride)
boolean emissiveAmbient, @Nullable String mtlOverride, boolean allowHomonymObjectName)
{ }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change because any callers to the previous constructor (without the new parameter) will break.

Please add a new constructor to this record which calls the canonical constructor with an appropriate default value for the new parameter, mark that new constructor as deprecated for removal (@Deprecated(forRemoval = true, since = "1.19.2"), and add a javadoc with a @deprecated tag directing users to the canonical constructor (which has the new parameter).

@@ -299,6 +316,10 @@ static ObjModel parse(ObjTokenizer tokenizer, ModelSettings settings, Map<String
}
}
}
if (!allowHomonymObjectName && hasHomonymObjectNameAppeared)
{
LOGGER.error("find homonym object in obj model:" + settings.modelLocation + ", use allow_homonym_object_name=true can rename them for you");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some error message cleanup, as well as using logger parameter substitution instead of manual string concatenation:

Suggested change
LOGGER.error("find homonym object in obj model:" + settings.modelLocation + ", use allow_homonym_object_name=true can rename them for you");
LOGGER.error("Found homonym object in ObjModel: {}; use the 'allow_homonym_object_name' option to automatically resolve homonyms", settings.modelLocation);

hasHomonymObjectNameAppeared = true;
if (allowHomonymObjectName)
{
int suffixNum = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should begin this at 1 instead, so the names would be: object, object_1, object_2.

@@ -57,6 +57,7 @@ public ObjModel read(JsonObject jsonObject, JsonDeserializationContext deseriali
boolean flipV = GsonHelper.getAsBoolean(jsonObject, "flip_v", false);
boolean emissiveAmbient = GsonHelper.getAsBoolean(jsonObject, "emissive_ambient", true);
String mtlOverride = GsonHelper.getAsString(jsonObject, "mtl_override", null);
boolean allowHomonymObjectName = GsonHelper.getAsBoolean(jsonObject,"allow_homonym_object_name",false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting: spaces after commas.

Suggested change
boolean allowHomonymObjectName = GsonHelper.getAsBoolean(jsonObject,"allow_homonym_object_name",false);
boolean allowHomonymObjectName = GsonHelper.getAsBoolean(jsonObject, "allow_homonym_object_name", false);

@@ -86,7 +87,7 @@ public ObjModel read(JsonObject jsonObject, JsonDeserializationContext deseriali
deprecationWarningsBuilder.put("materialLibraryOverride", "mtl_override");
}

return loadModel(new ObjModel.ModelSettings(new ResourceLocation(modelLocation), automaticCulling, shadeQuads, flipV, emissiveAmbient, mtlOverride), deprecationWarningsBuilder.build());
return loadModel(new ObjModel.ModelSettings(new ResourceLocation(modelLocation), automaticCulling, shadeQuads, flipV, emissiveAmbient, mtlOverride,allowHomonymObjectName), deprecationWarningsBuilder.build());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting: spaces after commas.

Suggested change
return loadModel(new ObjModel.ModelSettings(new ResourceLocation(modelLocation), automaticCulling, shadeQuads, flipV, emissiveAmbient, mtlOverride,allowHomonymObjectName), deprecationWarningsBuilder.build());
return loadModel(new ObjModel.ModelSettings(new ResourceLocation(modelLocation), automaticCulling, shadeQuads, flipV, emissiveAmbient, mtlOverride, allowHomonymObjectName), deprecationWarningsBuilder.build());

@sciwhiz12 sciwhiz12 removed the Triage This request requires the active attention of the Triage Team. Requires labelling or reviews. label Oct 2, 2022
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.19 Feature This request implements a new feature. Rendering This request deals with rendering. These must be treated with care, and tend to progress slowly.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants