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!

Thread Tools Search this Thread Display Modes
  11 November 2017
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.

I mean, you could still store the name of the parent then do a search for each parent
"We do not - not wag our genitals at one another to make a point!"
  11 November 2017
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.

My Gallery
  11 November 2017
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)
    activeLevel = textData[0][1]
    # Insert the root joint in the document und remove the first
    # element from the list.
    # 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)
        # Update informations about the active joint
        activeJoint = newJoint
        activeLevel = level

  11 November 2017
Thanks Scott

take look at Kraken, old stuff of one fork before remove official repo -
  11 November 2017
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.

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

My Gallery
  11 November 2017
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)
 #Add the root joint(typically the Hips) from memory
 parentName = None
 if spaces == 0:
 parentName = jnt

 #Add the child joints from memory 
 if spaces >= depth:
 if parent is not None: 
 parentJnt = doc.SearchObject(parentName) 
 if parentJnt: 
 if parent is not None and parent != parentName: 
 parentJnt = doc.SearchObject(parent)
 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)

if __name__=='__main__':

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
Society of Digital Artists

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

All times are GMT. The time now is 08:31 AM.

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