Making a Custom Object

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
CopernicusTheNines
Posts: 0
Joined: Fri May 20, 2011 12:17 am

Making a Custom Object

Post by CopernicusTheNines » Sun Feb 12, 2012 11:57 pm

I want to create a script that lets me add an object to the Add menu. At the most basic level, I want to create a cube, which behaves exactly like the regular Mesh cube (ie, lets me move it, scale it, etc.) The only difference is, I want to change it from being of type "MESH".

I guess that's not entirely necessary either, but basically, I want a custom object type, so that when I export my scene I can identify which cubes were my special type.

How would I do that?

SirDude
Posts: 233
Joined: Sun Oct 13, 2002 7:37 pm
Location: University of Minnesota (USA)
Contact:

Post by SirDude » Mon Feb 13, 2012 3:25 pm

If your going for simple,

You could just name all of the materials on your special cubes differently.
example:
red for normal cubes, myred for the special ones...

You could also add a custom property, however you would need to export
to a file format and or write your own exporter that will add the custom property....

CopernicusTheNines
Posts: 0
Joined: Fri May 20, 2011 12:17 am

Post by CopernicusTheNines » Tue Feb 14, 2012 2:26 am

That's what I ended up doing, I created the mesh in one script and added an ID Property (cube_object["ObjectIsCustom"] = True), then in my export script when I iterate through the scene's objects I do:

Code: Select all

try:
    isCustom = ob["ObjectIsCustom"]
except:
    isCustom = False
To set a flag indicating if it's one of my custom types.

I was wondering if there's a better way to check for the property besides the exception...?

SirDude
Posts: 233
Joined: Sun Oct 13, 2002 7:37 pm
Location: University of Minnesota (USA)
Contact:

Post by SirDude » Wed Feb 15, 2012 4:44 pm

I'm not a big python fan....

Why not just use an if statement though?

if ob["ObjectIsCustom"] == True:
isCustom = True
else:
isCustom = False

or something similar?

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv » Wed Feb 15, 2012 8:26 pm

Exceptions are somewhat expensive and should be used for exceptional (or rare) conditions.

Not to get into a "who is the biggest python weenie" contest, but another way is:

isCustom = True if ob["ObjectIsCustom"] == True else False


Having said all that, if exceptions work for you, knock yourself out! Stuff that works is good.

CopernicusTheNines
Posts: 0
Joined: Fri May 20, 2011 12:17 am

Post by CopernicusTheNines » Wed Feb 15, 2012 10:35 pm

Actually I think both of those methods would throw an error, since you're attempting to access a property ID that doesn't exist.

I found these 2 ways of doing it after my post, and I'm using the second one now:

Code: Select all

if hasattr(ob,"ObjectIsCustom"):

if "ObjectIsCustom" in ob:

ldo
Posts: 0
Joined: Sun Nov 07, 2010 11:30 pm
Location: Hamilton, New Zealand

Post by ldo » Fri Feb 17, 2012 11:11 am

When I see people write

Code: Select all

if cond == True :
instead of

Code: Select all

if cond :
where cond can only have values of True or False, I can’t help thinking “can I get you a bib?”

But when I further see

Code: Select all

if cond == True :
    var = True
else :
    var = False
instead of a simple

Code: Select all

var = cond
then I just weep. Or laugh insanely. Or both.

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv » Fri Feb 17, 2012 9:45 pm

Part of the charm of Python is that it treats anything that is remotely zero, None, null, or empty as False, and everything else as True.

Post Reply