Jump to content

mildly OT: python class creating a class question


jonp

Recommended Posts

Sorry if this a bit of topic, mods are free to move it to the general discussion section.

I have moderate python scripting skills with just a bit of familiarity with classes, and am currently scripting some pipeline-y stuff at work. We have an established directory structure that follows, more or less, standard conventions (project/sequence/etc). Inspired by the way Houdini turns its own hierarchical structure into objects, I'd like to do something similar with our directory structure. Just as a SOP node object can only be created from within (or by) an OBJ node in Houdini's python implementation, I would like to write a module within which, for example:

- only Sequence classes can be created by, or attached to, a Project class parent

- only an Element class can be created by, or attached to, a Sequence class parent

I only ask here because of my exposure to this kind of python scripting comes directly from Houdini. I've created scripts that have classes creating other classes, but there is no enforcement of scope with each individual class.

If anyone has any pointers to a general coding style that facilitates this I'd be quite grateful.

Cheers,

Jon

Link to comment
Share on other sites

  • 3 weeks later...

Hey thanks for replying. Quick question: within my Sequence class, how would I check the type of whatever was initializing it? Like how to know the difference between:


seq = Sequence(path)
seq = proj.create_seq(path)
[/CODE]

I haven't pushed into this as much as I originally planned, since I got halfway down that road and felt like it was far enough for what we were using it for anyways.

Link to comment
Share on other sites

You can add the project as project variable to the sequence for example



class cSequence():
def __init__(self, path, project):
self.project = project
self.path = path

class cProject():
def __init__(self,path):
self.path = path
self.sequences = []

def addSequence(self):
sequence = cSequence(self.path, self)
self.sequences.append(sequence)


project = cProject("testpath")
project.addSequence()
print project.sequences[0].path
print project.sequences[0].project

[/CODE]

if You strictly stick to class functions You can not create a sequence inside a element, as element has no addSequence function, only the project class has one. so by designing You classes You can enforce consistency. Of cource one can attach a sequence to a element, but than he leaves the designed path

Edited by sanostol
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...