Tekitoumen opened this issue on Aug 23, 2022 ยท 8 posts
Tekitoumen posted Tue, 23 August 2022 at 3:21 AM
Hello,
Please excuse me for asking this same question.
https://www.renderosity.com/forums/threads/2972673/export-by-python
If you know a solution please let me know...
As I wrote in "export-by-python", import the obj model from python, and export the model from python in obj format.
Then the size of the obj file created will be 0 bytes.
Just an import or just an export will succeed.
I don't know what is wrong. . .
If you have any advice, could you tell me.
Thank you.
----------------------------------------------------------------------------------------
import poser
def importOBJ(pathname):
imExObject = scene.ImExporter()
imoptions = imExObject.ImportOptions("obj", None)
# Set the OBJ import options
imoptions[poser.kImOptCodeMAKEPOLYNORMSCONSISTENT] = 1
imoptions[poser.kImOptCodeOFFSETX] = 0.0
imoptions[poser.kImOptCodeOFFSETY] = 0.0
imoptions[poser.kImOptCodeOFFSETZ] = 0.0
imoptions[poser.kImOptCodeFLIPUTEXTCOORDS] = 0
imoptions[poser.kImOptCodeFLIPVTEXTCOORDS] = 0
imoptions[poser.kImOptCodeWELDIDENTICALVERTS] = 1
imoptions[poser.kImOptCodeCENTERED] = 0
imoptions[poser.kImOptCodePERCENTFIGSIZE] = -1
imoptions[poser.kImOptCodeFLIPNORMS] = 0
imoptions[poser.kImOptCodePLACEONFLOOR] = 0
try:
#imExObject.Import('obj', 'File Format Wavefront', objectPath, imoptions)
imExObject.Import('obj', 'File Format Wavefront', pathname, imoptions)
except:
print ("OBJ failed to import.")
def exportOBJ(pathname, file_name):
print(pathname)
print(file_name)
global actList
# define the file path
objectPath = os.path.join(pathname,file_name)
imExObject = scene.ImExporter()
exoptions = imExObject.ExportOptions("obj", None)
# Set the OBJ export options
exoptions[poser.kExOptCodeUSEINTERNALNAMES] = 0
exoptions[poser.kExOptCodeFIRSTFRAME] = 0
exoptions[poser.kExOptCodeLASTFRAME] = 29
exoptions[poser.kExOptCodeMULTIFRAME] = 0
exoptions[poser.kExOptCodeWELDSEAMS] = 1
exoptions[poser.kExOptCodeASMORPHTARGET] = 0
exoptions[poser.kExOptCodeBODYPARTNAMESINPOLYGROUPS] = 1
exoptions[poser.kExOptCodeFIGNAMESINGROUPS] = 0
exoptions[poser.kExOptCodeEXISTINGGROUPSINPOLYGROUPS] = 1
try:
imExObject.Export('obj', 'File Format Wavefront', objectPath, exoptions, sceneSelectionCallback)
#imExObject.Export('obj', 'File Format Wavefront', objectPath, exoptions, none)
print ("Sucessfully exported %s" % actList )
except:
print ("OBJ failed to export.")
# callback to check which actor(s) are to be exported
def sceneSelectionCallback(CB):
global actList
for act in actList:
if act.InternalName() == CB.InternalName():
include = 1
return include
else:
include = 0
return include
# define what you want exported. For this example I'll
# just include the actor currently selected in the scene
# plus the GROUND
actList = [scene.CurrentActor(),scene.ActorByInternalName("GROUND")]
# go do it
---in main-----
importOBJ("import_path")
actor = scene.Actor("target_name")
scene.SelectActor(actor)
scene.DrawAll()
exportOBJ("export_path", "obj file name")
FVerbaas posted Tue, 23 August 2022 at 3:45 PM Forum Coordinator
I am not at my system now so I cannot check but it maybe your callback does never return TRUE.
Best add print statement in it that says something when it returns a TRUE.
Also equality '==' is not the best way to compare strings. Better use 'in' : if act.InternalName() in CB.InternalName():
Tekitoumen posted Tue, 23 August 2022 at 7:12 PM
Thank you for your reply.
When I checked the return value of Callback, it was not "True".
After checking, it seems that the cause was bad timing to create actList. (I was generating the actList before the import completed)
Also, thanks for pointing out about the "==".
Thanks to you, I was able to solve the problem.
Thank you for your kindness.
adp001 posted Wed, 31 August 2022 at 7:29 PM
Tekitoumen posted at 7:12 PM Tue, 23 August 2022 - #4443226
Also, thanks for pointing out about the "==".A word of warning:
Using "in" instead of "==" can quickly become a massive problem. Why?
"" in "any string" == True
That' s why.
An empty string is contained in any valid string.
adp001 posted Wed, 31 August 2022 at 7:35 PM
Before anyone gets any ideas: This also applies to regular expressions. An empty string matches any other string.
adp001 posted Wed, 31 August 2022 at 7:59 PM
If you absolutely want to use "in" instead of "==", then this is how:
string_A = "any"
string_B = "any string"
len(string_A) > 0 and string_A in string_B == True
string_A = ""
len(string_A) > 0 and string_A in string_B == False
But.... if string_A is None then a Type Error is raised. But this also happens with a regular comparison with "==". So I leave it at that.
adp001 posted Wed, 31 August 2022 at 8:22 PM
However, what I do from time to time is to use a function:
def str_eq(sa: str, sb: str) -> bool:
return sa == sb or sa in sb if sa is not None and sb is not None and len(sa) > 0 else False
(Python 3)
The function first tries a standard comparison with "==". Only if this fails, a test with "in" is performed. In addition, the two variables passed (sa and sb) are tested for None.
Tekitoumen posted Wed, 14 September 2022 at 8:20 PM
Sorry for my late response.
I've found that you have to be careful when comparing empty strings.
I will refer to it when creating the script.
thank you very much.