Joints from a text file

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

REPLY TO THREAD
 
Thread Tools Search this Thread Display Modes
  3 Weeks Ago
Originally Posted by Scott Ayers: Hi Bret,
I'm using integers for the levels because it's more application agnostic that way.
There will be times when I do this task inside of Qt, or Maya, or some other app. Not just in C4D.
There's probably lots of other ways to skin this cat.
For example. I'll bet it can also be done using recursion rather using arrays.

-ScottA
I mean, you could still store the name of the parent then do a search for each parent
__________________
www.bretbays.com
"We do not - not wag our genitals at one another to make a point!"
 
  3 Weeks Ago
Oh yeah. I'll probably do it that way if I use it in a project. Mainly just to reduce the memory being used.
The main reason I stored joint objects in the arrays instead of their names is because I found that it was a bit quicker and less confusing to write the code that way.
I was doing a lot of experimenting, and it turned out to be less confusing and simpler to store joint objects and insert them into C4D rather than using their names.
I don't think storing joints are large enough to cause a problem. But yeah I'll most likely not do that when I actually use it in a plugin.

-ScottA
__________________
My Gallery
 
  3 Weeks Ago
Hi Scott,

dealing with the levels, you don't have to store all the joints in a list.
You can go 'up in hierarchy' from the last inserted joint to the object one level above the level (i.e. the parent) of the new joint has to be.

Here's the code for the 'main' function:

def main():
    # Create the root joint
    activeJoint = c4d.BaseObject(c4d.Ojoint)
    activeJoint.SetName(textData[0][0])
    activeLevel = textData[0][1]
    
    # Insert the root joint in the document und remove the first
    # element from the list.
    doc.InsertObject(activeJoint)   
    textData.pop(0)
    
    # Go through the elements of the (reduced) list
    for name, level in textData:
        # Go to the joint one level above the level of the new joint,
        # i.e. go to the parent
        while activeLevel >= level:
            activeJoint = activeJoint.GetUp()
            activeLevel -= 1

        # Create the new joint and insert it under the parent (activeJoint)
        newJoint = c4d.BaseObject(c4d.Ojoint)
        newJoint.SetName(name)        
        newJoint.InsertUnderLast(activeJoint)
        
        # Update informations about the active joint
        activeJoint = newJoint
        activeLevel = level
    
    c4d.EventAdd()


Peter
 
  3 Weeks Ago
Thanks Scott

take look at Kraken, old stuff of one fork before remove official repo - https://github.com/hoorayfor3d/Kraken
 
  3 Weeks Ago
Thanks for posting your code Peter. I like that version better than the one I posted. Less memory usage.
If anyone else has a different method. Please feel free share it. Especially one that uses recursion. We don't have one of those here yet.

@spirel.
Thanks for the link.. I'll check it out.

-ScottA
__________________
My Gallery
 
  3 Weeks Ago
Here's a recursive method I just came up with for creating joints from a text file without parsing them into a list first.
This way you don't need to use a list if you don't want do it that way.
#This script reads text from a .txt file and creates a joint hierarchy from it
#It uses the empty spaces in the text file to determine the parent/child branching

import c4d

def recurse_tree(parent, depth, source): 

 last_line = source.readline().rstrip()
 while last_line:
 
 #Count the number of empty spaces in each line of text
 spaces = len(last_line) - len(last_line.lstrip())
 if spaces < depth: break

 #Strip out any spaces from each line of text
 text = last_line.strip()
 
 #Create a joint for each line of text in memory only 
 jnt = c4d.BaseObject(1019362)
 jnt.SetName(text)
 
 #Add the root joint(typically the Hips) from memory
 parentName = None
 if spaces == 0:
 parentName = jnt
 doc.InsertObject(jnt)

 #Add the child joints from memory 
 if spaces >= depth:
 if parent is not None: 
 parentJnt = doc.SearchObject(parentName) 
 if parentJnt: 
 jnt.InsertUnderLast(parentJnt)
 
 if parent is not None and parent != parentName: 
 parentJnt = doc.SearchObject(parent)
 doc.InsertObject(jnt,parentJnt) 
 jnt.InsertUnderLast(parentJnt) 
 
 last_line = recurse_tree(text, spaces+1, source)
 
 return last_line

def main():
 
 inFile = open("C:\\Users\\user\\Desktop\\test.txt") #The .txt file holding the joint names
 recurse_tree(None, 0, inFile)
 inFile.close()
 
 c4d.EventAdd()

if __name__=='__main__':
 main()


-ScottA
__________________
My Gallery
 
reply share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 01:07 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.