Problem with getting Mesh Hierarchy names?

Blitz3D Forums/Blitz3D Programming/Problem with getting Mesh Hierarchy names?

Guy Fawkes(Posted 2012) [#1]
Hi all, can someone please tell me why even though the mesh I bought has more than 1 bone, why this code is only returning ONE bone name, instead of all of them in the given mesh?



Any help is greatly appreciated!

Thank You! :)

Last edited 2012


Yasha(Posted 2012) [#2]
This line:

final_data$ = get_data$(i-1)


...is not adding to the string held in the final_data variable, it's just overwriting it. Each iteration of the loop, the variable is overwritten with the most recent name.

To add to a string, remember to use the 'S = S + " " + something' pattern.

(What is the get_data array even doing in this function? You put the name in the array, then immediately put it in the final_data variable? The array will still hold all of the names after the function has been called, since the array exists outside the function's local scope - this is already a better way to return many strings than packing them together into one final_data string.)


Guy Fawkes(Posted 2012) [#3]
Is this what you mean, Yasha?




Yasha(Posted 2012) [#4]
More or less. If you have a mesh node with the children "foo", "bar", and "baz", that will return "foobarbaz". Useful to you if you know how to parse it... I would have added the space myself.

This still probably isn't working as expected though. Observe that you only loop over the children of "mesh". What if those children have children of their own? Those aren't counted as children of "mesh" (each entity may only have one immediate parent), so for a model like a humanoid, this is likely ignoring most of the hierarchy (what it's actually doing is returning one level of the hierarchy).


Guy Fawkes(Posted 2012) [#5]
Crap... How do I fix it?


Yasha(Posted 2012) [#6]
With a recursive function.

If you're not familiar with recursion, this is an excellent problem for learning about it, as the nature of the mesh structure (a tree) and the nature of the desired result (a flat string, which can be concatenated with the + operator) lend themselves very naturally to the recursive style, and it's also one of the few places where you would realistically use a recursive function in B3D (which does not normally do so well with them).

There are either solutions or solutions to almost identical problems in the code archives, but I would urge you to investigate finding a recursive solution yourself, as you've set up a near-textbook learning exercise here.


Guy Fawkes(Posted 2012) [#7]
Thanks for the help, Yasha :)

Ok, better yet. I just found this code snippet that seems to work fine. Now.. how can I get it to return all of the data in get_data$(x) ?



Thanks again! :)


PowerPC603(Posted 2012) [#8]
I used something like this to list all children of a mesh:
; A global variable to separate each child's name on a new line
Global Counter
; Load an animated model with hierarchy
Global entity = LoadAnimMesh("models\Butterfly.b3d")

; Display the hierachy of the entity
DisplayChildren(entity)

Waitkey
End



; Display the entire hierarchy of a loaded entity
Function DisplayChildren(ent, loop = 0)
	Local s$

	; Loop through all children of the given entity
	For i = 0 To (CountChildren(ent) - 1)
		Counter = Counter + 1

		; Get the child
		child = GetChild(ent, i)
		; Display the name of the child (indented, based on the amount of iterations of this function)
		s = ""
		For slen = 0 To loop
			s = s + "   "
		Next
		Text 10, (Counter * 20) + 100, s + "- Name: " + EntityName(child)

		; Process this child's children
		DisplayChildren(child, loop + 1)
	Next
End Function


The global var "Counter" is only used to display each child's name on a new line.
The function is given the parent entity (which you load using LoadAnimMesh).
The function loops through all children of the entity, and while it loops through them, it also calls itself to loop through the children of each child, and so on.

Last edited 2012


Guy Fawkes(Posted 2012) [#9]
Nvm, figured it out!

Thanks, Yasha! :D




Guy Fawkes(Posted 2012) [#10]
Ok, PowerPC. let me clarify this, because my mesh hierarchy code is apparently reading the children upside down.


Why is it saying the entity does not exist, when I clearly checked to make sure the mesh was in the correct folder, and the code saved to the correct directory?




PowerPC603(Posted 2012) [#11]
I created this code in BMax using Xors3D and converted it quickly to Blitz3D's syntax.
There seems to be a difference between Blitz3D's GetChild and Xors3D's GetChild.

In Blitz3D, GetChild needs index "1" to "CountChildren(ent)", while Xors3D's GetChild needs index "0" to "CountChildren(ent) - 1".

This should work properly:

Graphics3D 800, 600, 0, 2

; A Global variable To separate Each child's name on a New Line
Global Counter
; Load an animated model with hierarchy
Global entity = LoadAnimMesh("media/magic/male/MaleNPC1.b3d")

; Display the hierachy of the entity
DisplayChildren(entity)

WaitKey
End



; Display the entire hierarchy of a loaded entity
Function DisplayChildren(ent, loop = 0)
	Local s$

	; Loop through all children of the given entity
	For i = 1 To CountChildren(ent)
		Counter = Counter + 1

		; Get the child
		child = GetChild(ent, i)
		; Display the name of the child (indented, based on the amount of iterations of this function)
		s = ""
		For slen = 0 To loop
			s = s + "   "
		Next
		Text 10, (Counter * 20) + 100, s + "- Name: " + EntityName(child)

		; Process this child's children
		DisplayChildren(child, loop + 1)
	Next
End Function


Last edited 2012


Guy Fawkes(Posted 2012) [#12]
Thanks ALOT, PowerPC! It works a treat! :D Thank you too, Yasha! :)

Last edited 2012


LineOf7s(Posted 2012) [#13]
The ever-helpful (and patient) Yasha says:
I would urge you to investigate finding a recursive solution yourself


The ever... er... Thundros says:
Ok, better yet. I just found this code snippet that seems to work fine. Now.. how can I...?


Thundros, if you're ever wondering what problem otherwise helpful people might have with you, I believe this exchange is the perfect example.

Just FYI.


Guy Fawkes(Posted 2012) [#14]
Lineof7s, I have nothing to say to u. Goodbye

Last edited 2012